several speed improvements: completely rewritten bitbuffer which uses native machine...
authorJosh Coalson <jcoalson@users.sourceforce.net>
Sun, 28 Jan 2007 17:40:26 +0000 (17:40 +0000)
committerJosh Coalson <jcoalson@users.sourceforce.net>
Sun, 28 Jan 2007 17:40:26 +0000 (17:40 +0000)
26 files changed:
build/config.mk
doc/html/changelog.html
src/libFLAC/Makefile.am
src/libFLAC/Makefile.lite
src/libFLAC/bitbuffer.c [deleted file]
src/libFLAC/bitreader.c [new file with mode: 0644]
src/libFLAC/bitwriter.c [new file with mode: 0644]
src/libFLAC/crc.c
src/libFLAC/include/private/Makefile.am
src/libFLAC/include/private/all.h
src/libFLAC/include/private/bitbuffer.h [deleted file]
src/libFLAC/include/private/bitreader.h [new file with mode: 0644]
src/libFLAC/include/private/bitwriter.h [new file with mode: 0644]
src/libFLAC/include/private/crc.h
src/libFLAC/include/private/stream_encoder_framing.h
src/libFLAC/libFLAC_dynamic.dsp
src/libFLAC/libFLAC_static.dsp
src/libFLAC/stream_decoder.c
src/libFLAC/stream_encoder.c
src/libFLAC/stream_encoder_framing.c
src/test_libFLAC/Makefile.am
src/test_libFLAC/Makefile.lite
src/test_libFLAC/bitbuffer.c [deleted file]
src/test_libFLAC/bitwriter.c [new file with mode: 0644]
src/test_libFLAC/bitwriter.h [moved from src/test_libFLAC/bitbuffer.h with 97% similarity]
src/test_libFLAC/main.c

index b87433f..0e9df21 100644 (file)
@@ -43,8 +43,7 @@ all default: $(DEFAULT_BUILD)
 VERSION=\"1.1.3\"
 
 ifeq ($(DARWIN_BUILD),yes)
-#CONFIG_CFLAGS=-DHAVE_INTTYPES_H -DHAVE_ICONV -DHAVE_LANGINFO_CODESET -DHAVE_SOCKLEN_T -DFLAC__HAS_OGG -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -DFLAC__SYS_DARWIN
-CONFIG_CFLAGS=-DHAVE_INTTYPES_H -DHAVE_ICONV -DHAVE_LANGINFO_CODESET -DFLAC__HAS_OGG -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -DFLAC__SYS_DARWIN
+CONFIG_CFLAGS=-DHAVE_INTTYPES_H -DHAVE_ICONV -DHAVE_LANGINFO_CODESET -DFLAC__HAS_OGG -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -DFLAC__SYS_DARWIN -DWORDS_BIGENDIAN
 ICONV_INCLUDE_DIR=$(HOME)/local.i18n/include
 ICONV_LIB_DIR=$(HOME)/local.i18n/lib
 else
index 5d24763..4a0b7f7 100644 (file)
@@ -62,7 +62,7 @@
                        <li>
                                General:
                                <ul>
-                                       <li>(none)</li>
+                                       <li>Encoding and decoding speedups across the board.  Encoding at -8 is twice as fast.</li>
                                </ul>
                        </li>
                        <li>
@@ -80,6 +80,7 @@
                        <li>
                                flac:
                                <ul>
+                                       <li>Encoding and decoding speedups across the board.  Encoding at -8 is twice as fast.</li>
                                        <li>Fixed a bug that caused suboptimal default compression settings in some locales (<a href="https://sourceforge.net/tracker/index.php?func=detail&amp;aid=1608883&amp;group_id=13478&amp;atid=113478">SF #1608883</a>).</li>
                                        <li>Fixed a bug where FLAC-to-FLAC transcoding of a corrupted FLAC file would truncate the transcoded file at the first error (<a href="http://sourceforge.net/tracker/index.php?func=detail&amp;aid=1615019&amp;group_id=13478&amp;atid=113478">SF #1615019</a>).</li>
                                        <li>Fixed a bug where using <span class="argument"><a href="documentation_tools_flac.html#flac_options_decode_through_errors">-F</a></span> with FLAC-to-FLAC transcoding of a corrupted FLAC would have no effect (<a href="http://sourceforge.net/tracker/index.php?func=detail&amp;aid=1615391&amp;group_id=13478&amp;atid=113478">SF #1615391</a>).</li>
                        <li>
                                libraries:
                                <ul>
+                                       <li>Completely rewritten bitbuffer which uses native machine word size instead of bytes for dramatic speed improvements.</li>
+                                       <li>Much faster rice partition size estimation which greatly speeds encoding in higher modes.</li>
                                        <li>Fixed a bug with default apodization settings that were erroneous in some locales (<a href="https://sourceforge.net/tracker/index.php?func=detail&amp;aid=1608883&amp;group_id=13478&amp;atid=113478">SF #1608883</a>).</li>
                                </ul>
                        </li>
index 4bdc418..0d28fee 100644 (file)
@@ -92,8 +92,9 @@ endif
 # see 'http://www.gnu.org/software/libtool/manual.html#Libtool-versioning' for numbering convention
 libFLAC_la_LDFLAGS = -version-info 8:0:0 -lm $(LOCAL_EXTRA_LDFLAGS)
 libFLAC_la_SOURCES = \
-       bitbuffer.c \
        bitmath.c \
+       bitreader.c \
+       bitwriter.c \
        cpu.c \
        crc.c \
        fixed.c \
index 780c94b..5e0f7fb 100644 (file)
@@ -61,8 +61,9 @@ endif
 endif
 
 SRCS_C = \
-       bitbuffer.c \
        bitmath.c \
+       bitreader.c \
+       bitwriter.c \
        cpu.c \
        crc.c \
        fixed.c \
diff --git a/src/libFLAC/bitbuffer.c b/src/libFLAC/bitbuffer.c
deleted file mode 100644 (file)
index e70bcda..0000000
+++ /dev/null
@@ -1,2548 +0,0 @@
-/* libFLAC - Free Lossless Audio Codec library
- * Copyright (C) 2000,2001,2002,2003,2004,2005,2006  Josh Coalson
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * - Neither the name of the Xiph.org Foundation nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#if HAVE_CONFIG_H
-#  include <config.h>
-#endif
-
-#include <stdlib.h> /* for malloc() */
-#include <string.h> /* for memcpy(), memset() */
-#include "private/bitbuffer.h"
-#include "private/bitmath.h"
-#include "private/crc.h"
-#include "FLAC/assert.h"
-
-/*
- * Along the way you will see two versions of some functions, selected
- * by a FLAC__NO_MANUAL_INLINING macro.  One is the simplified, more
- * readable, and slow version, and the other is the same function
- * where crucial parts have been manually inlined and are much faster.
- *
- */
-
-/*
- * Some optimization strategies are slower with older versions of MSVC
- */
-#if defined _MSC_VER && _MSC_VER <= 1200
-#define FLAC__OLD_MSVC_FLAVOR
-#endif
-
-/*
- * This should be at least twice as large as the largest number of blurbs
- * required to represent any 'number' (in any encoding) you are going to
- * read.  With FLAC this is on the order of maybe a few hundred bits.
- * If the buffer is smaller than that, the decoder won't be able to read
- * in a whole number that is in a variable length encoding (e.g. Rice).
- *
- * The number we are actually using here is based on what would be the
- * approximate maximum size of a verbatim frame at the default block size,
- * for CD audio (4096 sample * 4 bytes per sample), plus some wiggle room.
- * 32kbytes sounds reasonable.  For kicks we subtract out 64 bytes for any
- * alignment or malloc overhead.
- *
- * Increase this number to decrease the number of read callbacks, at the
- * expense of using more memory.  Or decrease for the reverse effect,
- * keeping in mind the limit from the first paragraph.
- */
-static const unsigned FLAC__BITBUFFER_DEFAULT_CAPACITY = ((65536 - 64) * 8) / FLAC__BITS_PER_BLURB; /* blurbs */
-
-#ifndef FLAC__OLD_MSVC_FLAVOR
-static const unsigned char byte_to_unary_table[] = {
-       8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
-       3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
-       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
-       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
-       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
-       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
-       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
-       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
-       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
-};
-#endif
-
-#if FLAC__BITS_PER_BLURB == 8
-#define FLAC__BITS_PER_BLURB_LOG2 3
-#define FLAC__BYTES_PER_BLURB 1
-#define FLAC__BLURB_ALL_ONES ((FLAC__byte)0xff)
-#define FLAC__BLURB_TOP_BIT_ONE ((FLAC__byte)0x80)
-#define BLURB_BIT_TO_MASK(b) (((FLAC__blurb)'\x80') >> (b))
-#define CRC16_UPDATE_BLURB(bb, blurb, crc) FLAC__CRC16_UPDATE((blurb), (crc));
-#ifndef FLAC__OLD_MSVC_FLAVOR
-#define FLAC__ALIGNED_BLURB_UNARY(blurb) (byte_to_unary_table[blurb])
-#endif
-#elif FLAC__BITS_PER_BLURB == 32
-#define FLAC__BITS_PER_BLURB_LOG2 5
-#define FLAC__BYTES_PER_BLURB 4
-#define FLAC__BLURB_ALL_ONES ((FLAC__uint32)0xffffffff)
-#define FLAC__BLURB_TOP_BIT_ONE ((FLAC__uint32)0x80000000)
-#define BLURB_BIT_TO_MASK(b) (((FLAC__blurb)0x80000000) >> (b))
-#define CRC16_UPDATE_BLURB(bb, blurb, crc) crc16_update_blurb((bb), (blurb));
-#ifndef FLAC__OLD_MSVC_FLAVOR
-#define FLAC__ALIGNED_BLURB_UNARY(blurb) ((blurb) <= 0xff ? byte_to_unary_table[blurb] + 24 : ((blurb) <= 0xffff ? byte_to_unary_table[(blurb) >> 8] + 16 : ((blurb) <= 0xffffff ? byte_to_unary_table[(blurb) >> 16] + 8 : byte_to_unary_table[(blurb) >> 24])))
-#endif
-#else
-/* ERROR, only sizes of 8 and 32 are supported */
-#endif
-
-#define FLAC__BLURBS_TO_BITS(blurbs) ((blurbs) << FLAC__BITS_PER_BLURB_LOG2)
-
-#ifdef min
-#undef min
-#endif
-#define min(x,y) ((x)<(y)?(x):(y))
-#ifdef max
-#undef max
-#endif
-#define max(x,y) ((x)>(y)?(x):(y))
-
-/* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
-#ifdef _MSC_VER
-#define FLAC__U64L(x) x
-#else
-#define FLAC__U64L(x) x##LLU
-#endif
-
-#ifndef FLaC__INLINE
-#define FLaC__INLINE
-#endif
-
-struct FLAC__BitBuffer {
-       FLAC__blurb *buffer;
-       unsigned capacity; /* in blurbs */
-       unsigned blurbs, bits;
-       unsigned total_bits; /* must always == FLAC__BITS_PER_BLURB*blurbs+bits */
-       unsigned consumed_blurbs, consumed_bits;
-       unsigned total_consumed_bits; /* must always == FLAC__BITS_PER_BLURB*consumed_blurbs+consumed_bits */
-       FLAC__uint16 read_crc16;
-#if FLAC__BITS_PER_BLURB == 32
-       unsigned crc16_align;
-#endif
-       FLAC__blurb save_head, save_tail;
-};
-
-#if FLAC__BITS_PER_BLURB == 32
-static void crc16_update_blurb(FLAC__BitBuffer *bb, FLAC__blurb blurb)
-{
-       if(bb->crc16_align == 0) {
-               FLAC__CRC16_UPDATE(blurb >> 24, bb->read_crc16);
-               FLAC__CRC16_UPDATE((blurb >> 16) & 0xff, bb->read_crc16);
-               FLAC__CRC16_UPDATE((blurb >> 8) & 0xff, bb->read_crc16);
-               FLAC__CRC16_UPDATE(blurb & 0xff, bb->read_crc16);
-       }
-       else if(bb->crc16_align == 8) {
-               FLAC__CRC16_UPDATE((blurb >> 16) & 0xff, bb->read_crc16);
-               FLAC__CRC16_UPDATE((blurb >> 8) & 0xff, bb->read_crc16);
-               FLAC__CRC16_UPDATE(blurb & 0xff, bb->read_crc16);
-       }
-       else if(bb->crc16_align == 16) {
-               FLAC__CRC16_UPDATE((blurb >> 8) & 0xff, bb->read_crc16);
-               FLAC__CRC16_UPDATE(blurb & 0xff, bb->read_crc16);
-       }
-       else if(bb->crc16_align == 24) {
-               FLAC__CRC16_UPDATE(blurb & 0xff, bb->read_crc16);
-       }
-       bb->crc16_align = 0;
-}
-#endif
-
-/*
- * WATCHOUT: The current implentation is not friendly to shrinking, i.e. it
- * does not shift out what is consumed, it just chops off the end, whether
- * there is unconsumed data there or not.  This is OK because currently we
- * never shrink the buffer, but if this ever changes, we'll have to do some
- * fixups here.
- */
-static FLAC__bool bitbuffer_resize_(FLAC__BitBuffer *bb, unsigned new_capacity)
-{
-       FLAC__blurb *new_buffer;
-
-       FLAC__ASSERT(0 != bb);
-       FLAC__ASSERT(0 != bb->buffer);
-
-       if(bb->capacity == new_capacity)
-               return true;
-
-       new_buffer = (FLAC__blurb*)calloc(new_capacity, sizeof(FLAC__blurb));
-       if(new_buffer == 0)
-               return false;
-       memcpy(new_buffer, bb->buffer, sizeof(FLAC__blurb)*min(bb->blurbs+(bb->bits?1:0), new_capacity));
-       if(new_capacity < bb->blurbs+(bb->bits?1:0)) {
-               bb->blurbs = new_capacity;
-               bb->bits = 0;
-               bb->total_bits = FLAC__BLURBS_TO_BITS(new_capacity);
-       }
-       if(new_capacity < bb->consumed_blurbs+(bb->consumed_bits?1:0)) {
-               bb->consumed_blurbs = new_capacity;
-               bb->consumed_bits = 0;
-               bb->total_consumed_bits = FLAC__BLURBS_TO_BITS(new_capacity);
-       }
-       free(bb->buffer); /* we've already asserted above that (0 != bb->buffer) */
-       bb->buffer = new_buffer;
-       bb->capacity = new_capacity;
-       return true;
-}
-
-static FLAC__bool bitbuffer_grow_(FLAC__BitBuffer *bb, unsigned min_blurbs_to_add)
-{
-       unsigned new_capacity;
-
-       FLAC__ASSERT(min_blurbs_to_add > 0);
-
-       new_capacity = max(bb->capacity * 2, bb->capacity + min_blurbs_to_add);
-       return bitbuffer_resize_(bb, new_capacity);
-}
-
-static FLAC__bool bitbuffer_ensure_size_(FLAC__BitBuffer *bb, unsigned bits_to_add)
-{
-       FLAC__ASSERT(0 != bb);
-       FLAC__ASSERT(0 != bb->buffer);
-
-       if(FLAC__BLURBS_TO_BITS(bb->capacity) < bb->total_bits + bits_to_add)
-               return bitbuffer_grow_(bb, (bits_to_add >> FLAC__BITS_PER_BLURB_LOG2) + 2);
-       else
-               return true;
-}
-
-static FLAC__bool bitbuffer_read_from_client_(FLAC__BitBuffer *bb, FLAC__BitbufferReadCallback read_callback, void *client_data)
-{
-       size_t bytes;
-       FLAC__byte *target;
-
-       /* first shift the unconsumed buffer data toward the front as much as possible */
-       if(bb->total_consumed_bits >= FLAC__BITS_PER_BLURB) {
-#if FLAC__BITS_PER_BLURB == 8
-               /*
-                * memset and memcpy are usually implemented in assembly language
-                * by the system libc, and they can be much faster
-                */
-               const unsigned r_end = bb->blurbs + (bb->bits? 1:0);
-               const unsigned r = bb->consumed_blurbs, l = r_end - r;
-               memmove(&bb->buffer[0], &bb->buffer[r], l);
-               memset(&bb->buffer[l], 0, r);
-#elif FLAC__BITS_PER_BLURB == 32
-               /* still needs optimization */
-               const unsigned r_end = bb->blurbs + (bb->bits? 1:0);
-               unsigned l = 0, r = bb->consumed_blurbs;
-               for( ; r < r_end; l++, r++)
-                       bb->buffer[l] = bb->buffer[r];
-               for( ; l < r_end; l++)
-                       bb->buffer[l] = 0;
-#else
-               FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
-#endif /* FLAC__BITS_PER_BLURB == 32 or 8 */
-
-               bb->blurbs -= bb->consumed_blurbs;
-               bb->total_bits -= FLAC__BLURBS_TO_BITS(bb->consumed_blurbs);
-               bb->consumed_blurbs = 0;
-               bb->total_consumed_bits = bb->consumed_bits;
-       }
-
-       /* grow if we need to */
-       if(bb->capacity <= 1) {
-               if(!bitbuffer_resize_(bb, 16))
-                       return false;
-       }
-
-       /* set the target for reading, taking into account blurb alignment */
-#if FLAC__BITS_PER_BLURB == 8
-       /* blurb == byte, so no gyrations necessary: */
-       target = bb->buffer + bb->blurbs;
-       bytes = bb->capacity - bb->blurbs;
-#elif FLAC__BITS_PER_BLURB == 32
-       /* @@@ WATCHOUT: code currently only works for big-endian: */
-       FLAC__ASSERT((bb->bits & 7) == 0);
-       target = (FLAC__byte*)(bb->buffer + bb->blurbs) + (bb->bits >> 3);
-       bytes = ((bb->capacity - bb->blurbs) << 2) - (bb->bits >> 3); /* i.e. (bb->capacity - bb->blurbs) * FLAC__BYTES_PER_BLURB - (bb->bits / 8) */
-#else
-       FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
-#endif
-
-       /* finally, read in some data */
-       if(!read_callback(target, &bytes, client_data))
-               return false;
-
-       /* now we have to handle partial blurb cases: */
-#if FLAC__BITS_PER_BLURB == 8
-       /* blurb == byte, so no gyrations necessary: */
-       bb->blurbs += bytes;
-       bb->total_bits += FLAC__BLURBS_TO_BITS(bytes);
-#elif FLAC__BITS_PER_BLURB == 32
-       /* @@@ WATCHOUT: code currently only works for big-endian: */
-       {
-               const unsigned aligned_bytes = (bb->bits >> 3) + bytes;
-               bb->blurbs += (aligned_bytes >> 2); /* i.e. aligned_bytes / FLAC__BYTES_PER_BLURB */
-               bb->bits = (aligned_bytes & 3u) << 3; /* i.e. (aligned_bytes % FLAC__BYTES_PER_BLURB) * 8 */
-               bb->total_bits += (bytes << 3);
-       }
-#else
-       FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
-#endif
-       return true;
-}
-
-/***********************************************************************
- *
- * Class constructor/destructor
- *
- ***********************************************************************/
-
-FLAC__BitBuffer *FLAC__bitbuffer_new()
-{
-       FLAC__BitBuffer *bb = (FLAC__BitBuffer*)calloc(1, sizeof(FLAC__BitBuffer));
-
-       /* calloc() implies:
-               memset(bb, 0, sizeof(FLAC__BitBuffer));
-               bb->buffer = 0;
-               bb->capacity = 0;
-               bb->blurbs = bb->bits = bb->total_bits = 0;
-               bb->consumed_blurbs = bb->consumed_bits = bb->total_consumed_bits = 0;
-       */
-       return bb;
-}
-
-void FLAC__bitbuffer_delete(FLAC__BitBuffer *bb)
-{
-       FLAC__ASSERT(0 != bb);
-
-       FLAC__bitbuffer_free(bb);
-       free(bb);
-}
-
-/***********************************************************************
- *
- * Public class methods
- *
- ***********************************************************************/
-
-FLAC__bool FLAC__bitbuffer_init(FLAC__BitBuffer *bb)
-{
-       FLAC__ASSERT(0 != bb);
-
-       bb->buffer = 0;
-       bb->capacity = 0;
-       bb->blurbs = bb->bits = bb->total_bits = 0;
-       bb->consumed_blurbs = bb->consumed_bits = bb->total_consumed_bits = 0;
-
-       return FLAC__bitbuffer_clear(bb);
-}
-
-FLAC__bool FLAC__bitbuffer_init_from(FLAC__BitBuffer *bb, const FLAC__byte buffer[], unsigned bytes)
-{
-       FLAC__ASSERT(0 != bb);
-       FLAC__ASSERT(bytes > 0);
-
-       if(!FLAC__bitbuffer_init(bb))
-               return false;
-
-       if(!bitbuffer_ensure_size_(bb, bytes << 3))
-               return false;
-
-       FLAC__ASSERT(0 != buffer);
-       /* @@@ WATCHOUT: code currently only works for 8-bits-per-blurb inclusive-or big-endian: */
-       memcpy((FLAC__byte*)bb->buffer, buffer, sizeof(FLAC__byte)*bytes);
-       bb->blurbs = bytes / FLAC__BYTES_PER_BLURB;
-       bb->bits = (bytes % FLAC__BYTES_PER_BLURB) << 3;
-       bb->total_bits = bytes << 3;
-       return true;
-}
-
-FLAC__bool FLAC__bitbuffer_concatenate_aligned(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
-{
-       unsigned bits_to_add = src->total_bits - src->total_consumed_bits;
-
-       FLAC__ASSERT(0 != dest);
-       FLAC__ASSERT(0 != src);
-
-       if(bits_to_add == 0)
-               return true;
-       if(dest->bits != src->consumed_bits)
-               return false;
-       if(!bitbuffer_ensure_size_(dest, bits_to_add))
-               return false;
-       if(dest->bits == 0) {
-               memcpy(dest->buffer+dest->blurbs, src->buffer+src->consumed_blurbs, sizeof(FLAC__blurb)*(src->blurbs-src->consumed_blurbs + ((src->bits)? 1:0)));
-       }
-       else if(dest->bits + bits_to_add > FLAC__BITS_PER_BLURB) {
-               dest->buffer[dest->blurbs] <<= (FLAC__BITS_PER_BLURB - dest->bits);
-               dest->buffer[dest->blurbs] |= (src->buffer[src->consumed_blurbs] & ((1u << (FLAC__BITS_PER_BLURB-dest->bits)) - 1));
-               memcpy(dest->buffer+dest->blurbs+1, src->buffer+src->consumed_blurbs+1, sizeof(FLAC__blurb)*(src->blurbs-src->consumed_blurbs-1 + ((src->bits)? 1:0)));
-       }
-       else {
-               dest->buffer[dest->blurbs] <<= bits_to_add;
-               dest->buffer[dest->blurbs] |= (src->buffer[src->consumed_blurbs] & ((1u << bits_to_add) - 1));
-       }
-       dest->bits = src->bits;
-       dest->total_bits += bits_to_add;
-       dest->blurbs = dest->total_bits / FLAC__BITS_PER_BLURB;
-
-       return true;
-}
-
-void FLAC__bitbuffer_free(FLAC__BitBuffer *bb)
-{
-       FLAC__ASSERT(0 != bb);
-
-       if(0 != bb->buffer)
-               free(bb->buffer);
-       bb->buffer = 0;
-       bb->capacity = 0;
-       bb->blurbs = bb->bits = bb->total_bits = 0;
-       bb->consumed_blurbs = bb->consumed_bits = bb->total_consumed_bits = 0;
-}
-
-FLAC__bool FLAC__bitbuffer_clear(FLAC__BitBuffer *bb)
-{
-       if(bb->buffer == 0) {
-               bb->capacity = FLAC__BITBUFFER_DEFAULT_CAPACITY;
-               bb->buffer = (FLAC__blurb*)calloc(bb->capacity, sizeof(FLAC__blurb));
-               if(bb->buffer == 0)
-                       return false;
-       }
-       else {
-               memset(bb->buffer, 0, bb->blurbs + (bb->bits?1:0));
-       }
-       bb->blurbs = bb->bits = bb->total_bits = 0;
-       bb->consumed_blurbs = bb->consumed_bits = bb->total_consumed_bits = 0;
-       return true;
-}
-
-FLAC__bool FLAC__bitbuffer_clone(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
-{
-       FLAC__ASSERT(0 != dest);
-       FLAC__ASSERT(0 != dest->buffer);
-       FLAC__ASSERT(0 != src);
-       FLAC__ASSERT(0 != src->buffer);
-
-       if(dest->capacity < src->capacity)
-               if(!bitbuffer_resize_(dest, src->capacity))
-                       return false;
-       memcpy(dest->buffer, src->buffer, sizeof(FLAC__blurb)*min(src->capacity, src->blurbs+1));
-       dest->blurbs = src->blurbs;
-       dest->bits = src->bits;
-       dest->total_bits = src->total_bits;
-       dest->consumed_blurbs = src->consumed_blurbs;
-       dest->consumed_bits = src->consumed_bits;
-       dest->total_consumed_bits = src->total_consumed_bits;
-       dest->read_crc16 = src->read_crc16;
-       return true;
-}
-
-void FLAC__bitbuffer_reset_read_crc16(FLAC__BitBuffer *bb, FLAC__uint16 seed)
-{
-       FLAC__ASSERT(0 != bb);
-       FLAC__ASSERT(0 != bb->buffer);
-       FLAC__ASSERT((bb->consumed_bits & 7) == 0);
-
-       bb->read_crc16 = seed;
-#if FLAC__BITS_PER_BLURB == 8
-       /* no need to do anything */
-#elif FLAC__BITS_PER_BLURB == 32
-       bb->crc16_align = bb->consumed_bits;
-#else
-       FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
-#endif
-}
-
-FLAC__uint16 FLAC__bitbuffer_get_read_crc16(FLAC__BitBuffer *bb)
-{
-       FLAC__ASSERT(0 != bb);
-       FLAC__ASSERT(0 != bb->buffer);
-       FLAC__ASSERT((bb->bits & 7) == 0);
-       FLAC__ASSERT((bb->consumed_bits & 7) == 0);
-
-#if FLAC__BITS_PER_BLURB == 8
-       /* no need to do anything */
-#elif FLAC__BITS_PER_BLURB == 32
-       /*@@@ BUG: even though this probably can't happen with FLAC, need to fix the case where we are called here for the very first blurb and crc16_align is > 0 */
-       if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) {
-               if(bb->consumed_bits == 8) {
-                       const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
-                       FLAC__CRC16_UPDATE(blurb >> 24, bb->read_crc16);
-               }
-               else if(bb->consumed_bits == 16) {
-                       const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
-                       FLAC__CRC16_UPDATE(blurb >> 24, bb->read_crc16);
-                       FLAC__CRC16_UPDATE((blurb >> 16) & 0xff, bb->read_crc16);
-               }
-               else if(bb->consumed_bits == 24) {
-                       const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
-                       FLAC__CRC16_UPDATE(blurb >> 24, bb->read_crc16);
-                       FLAC__CRC16_UPDATE((blurb >> 16) & 0xff, bb->read_crc16);
-                       FLAC__CRC16_UPDATE((blurb >> 8) & 0xff, bb->read_crc16);
-               }
-       }
-       else {
-               if(bb->consumed_bits == 8) {
-                       const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
-                       FLAC__CRC16_UPDATE(blurb >> (bb->bits-8), bb->read_crc16);
-               }
-               else if(bb->consumed_bits == 16) {
-                       const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
-                       FLAC__CRC16_UPDATE(blurb >> (bb->bits-8), bb->read_crc16);
-                       FLAC__CRC16_UPDATE((blurb >> (bb->bits-16)) & 0xff, bb->read_crc16);
-               }
-               else if(bb->consumed_bits == 24) {
-                       const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
-                       FLAC__CRC16_UPDATE(blurb >> (bb->bits-8), bb->read_crc16);
-                       FLAC__CRC16_UPDATE((blurb >> (bb->bits-16)) & 0xff, bb->read_crc16);
-                       FLAC__CRC16_UPDATE((blurb >> (bb->bits-24)) & 0xff, bb->read_crc16);
-               }
-       }
-       bb->crc16_align = bb->consumed_bits;
-#else
-       FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
-#endif
-       return bb->read_crc16;
-}
-
-FLAC__uint16 FLAC__bitbuffer_get_write_crc16(const FLAC__BitBuffer *bb)
-{
-       FLAC__ASSERT((bb->bits & 7) == 0); /* assert that we're byte-aligned */
-
-#if FLAC__BITS_PER_BLURB == 8
-       return FLAC__crc16(bb->buffer, bb->blurbs);
-#elif FLAC__BITS_PER_BLURB == 32
-       /* @@@ WATCHOUT: code currently only works for big-endian: */
-       return FLAC__crc16((FLAC__byte*)(bb->buffer), (bb->blurbs * FLAC__BYTES_PER_BLURB) + (bb->bits >> 3));
-#else
-       FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
-#endif
-}
-
-FLAC__byte FLAC__bitbuffer_get_write_crc8(const FLAC__BitBuffer *bb)
-{
-       FLAC__ASSERT(0 != bb);
-       FLAC__ASSERT((bb->bits & 7) == 0); /* assert that we're byte-aligned */
-       FLAC__ASSERT(bb->buffer[0] == 0xff); /* MAGIC NUMBER for the first byte of the sync code */
-#if FLAC__BITS_PER_BLURB == 8
-       return FLAC__crc8(bb->buffer, bb->blurbs);
-#elif FLAC__BITS_PER_BLURB == 32
-       /* @@@ WATCHOUT: code currently only works for big-endian: */
-       return FLAC__crc8((FLAC__byte*)(bb->buffer), (bb->blurbs * FLAC__BYTES_PER_BLURB) + (bb->bits >> 3));
-#else
-       FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
-#endif
-}
-
-FLAC__bool FLAC__bitbuffer_is_byte_aligned(const FLAC__BitBuffer *bb)
-{
-       return ((bb->bits & 7) == 0);
-}
-
-FLAC__bool FLAC__bitbuffer_is_consumed_byte_aligned(const FLAC__BitBuffer *bb)
-{
-       return ((bb->consumed_bits & 7) == 0);
-}
-
-unsigned FLAC__bitbuffer_bits_left_for_byte_alignment(const FLAC__BitBuffer *bb)
-{
-       return 8 - (bb->consumed_bits & 7);
-}
-
-unsigned FLAC__bitbuffer_get_input_bytes_unconsumed(const FLAC__BitBuffer *bb)
-{
-       FLAC__ASSERT((bb->consumed_bits & 7) == 0 && (bb->bits & 7) == 0);
-       return (bb->total_bits - bb->total_consumed_bits) >> 3;
-}
-
-unsigned FLAC__bitbuffer_get_input_bits_unconsumed(const FLAC__BitBuffer *bb)
-{
-       return bb->total_bits - bb->total_consumed_bits;
-}
-
-void FLAC__bitbuffer_get_buffer(FLAC__BitBuffer *bb, const FLAC__byte **buffer, size_t *bytes)
-{
-       FLAC__ASSERT((bb->consumed_bits & 7) == 0 && (bb->bits & 7) == 0);
-#if FLAC__BITS_PER_BLURB == 8
-       *buffer = bb->buffer + bb->consumed_blurbs;
-       *bytes = bb->blurbs - bb->consumed_blurbs;
-#elif FLAC__BITS_PER_BLURB == 32
-       /* @@@ WATCHOUT: code currently only works for big-endian: */
-       *buffer = (FLAC__byte*)(bb->buffer + bb->consumed_blurbs) + (bb->consumed_bits >> 3);
-       *bytes = (bb->total_bits - bb->total_consumed_bits) >> 3;
-#else
-       FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
-#endif
-}
-
-void FLAC__bitbuffer_release_buffer(FLAC__BitBuffer *bb)
-{
-#if FLAC__BITS_PER_BLURB == 8
-       (void)bb;
-#elif FLAC__BITS_PER_BLURB == 32
-       /* @@@ WATCHOUT: code currently only works for big-endian: */
-       (void)bb;
-#else
-       FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
-#endif
-}
-
-FLAC__bool FLAC__bitbuffer_write_zeroes(FLAC__BitBuffer *bb, unsigned bits)
-{
-       unsigned n;
-
-       FLAC__ASSERT(0 != bb);
-       FLAC__ASSERT(0 != bb->buffer);
-
-       if(bits == 0)
-               return true;
-       if(!bitbuffer_ensure_size_(bb, bits))
-               return false;
-       bb->total_bits += bits;
-       while(bits > 0) {
-               n = min(FLAC__BITS_PER_BLURB - bb->bits, bits);
-               bb->buffer[bb->blurbs] <<= n;
-               bits -= n;
-               bb->bits += n;
-               if(bb->bits == FLAC__BITS_PER_BLURB) {
-                       bb->blurbs++;
-                       bb->bits = 0;
-               }
-       }
-       return true;
-}
-
-FLAC__bool FLAC__bitbuffer_write_raw_uint32(FLAC__BitBuffer *bb, FLAC__uint32 val, unsigned bits)
-{
-       unsigned n, k;
-
-       FLAC__ASSERT(0 != bb);
-       FLAC__ASSERT(0 != bb->buffer);
-
-       FLAC__ASSERT(bits <= 32);
-       if(bits == 0)
-               return true;
-       /* inline the size check so we don't incur a function call unnecessarily */
-       if(FLAC__BLURBS_TO_BITS(bb->capacity) < bb->total_bits + bits) {
-               if(!bitbuffer_ensure_size_(bb, bits))
-                       return false;
-       }
-
-       /* zero-out unused bits; WATCHOUT: other code relies on this, so this needs to stay */
-       if(bits < 32) /* @@@ gcc seems to require this because the following line causes incorrect results when bits==32; investigate */
-               val &= (~(0xffffffff << bits)); /* zero-out unused bits */
-
-       bb->total_bits += bits;
-       while(bits > 0) {
-               n = FLAC__BITS_PER_BLURB - bb->bits;
-               if(n == FLAC__BITS_PER_BLURB) { /* i.e. bb->bits == 0 */
-                       if(bits < FLAC__BITS_PER_BLURB) {
-                               bb->buffer[bb->blurbs] = (FLAC__blurb)val;
-                               bb->bits = bits;
-                               break;
-                       }
-                       else if(bits == FLAC__BITS_PER_BLURB) {
-                               bb->buffer[bb->blurbs++] = (FLAC__blurb)val;
-                               break;
-                       }
-                       else {
-                               k = bits - FLAC__BITS_PER_BLURB;
-                               bb->buffer[bb->blurbs++] = (FLAC__blurb)(val >> k);
-                               /* we know k < 32 so no need to protect against the gcc bug mentioned above */
-                               val &= (~(0xffffffff << k));
-                               bits -= FLAC__BITS_PER_BLURB;
-                       }
-               }
-               else if(bits <= n) {
-                       bb->buffer[bb->blurbs] <<= bits;
-                       bb->buffer[bb->blurbs] |= val;
-                       if(bits == n) {
-                               bb->blurbs++;
-                               bb->bits = 0;
-                       }
-                       else
-                               bb->bits += bits;
-                       break;
-               }
-               else {
-                       k = bits - n;
-                       bb->buffer[bb->blurbs] <<= n;
-                       bb->buffer[bb->blurbs] |= (val >> k);
-                       /* we know n > 0 so k < 32 so no need to protect against the gcc bug mentioned above */
-                       val &= (~(0xffffffff << k));
-                       bits -= n;
-                       bb->blurbs++;
-                       bb->bits = 0;
-               }
-       }
-
-       return true;
-}
-
-FLAC__bool FLAC__bitbuffer_write_raw_int32(FLAC__BitBuffer *bb, FLAC__int32 val, unsigned bits)
-{
-       return FLAC__bitbuffer_write_raw_uint32(bb, (FLAC__uint32)val, bits);
-}
-
-FLAC__bool FLAC__bitbuffer_write_raw_uint64(FLAC__BitBuffer *bb, FLAC__uint64 val, unsigned bits)
-{
-       static const FLAC__uint64 mask[] = {
-               0,
-               FLAC__U64L(0x0000000000000001), FLAC__U64L(0x0000000000000003), FLAC__U64L(0x0000000000000007), FLAC__U64L(0x000000000000000F),
-               FLAC__U64L(0x000000000000001F), FLAC__U64L(0x000000000000003F), FLAC__U64L(0x000000000000007F), FLAC__U64L(0x00000000000000FF),
-               FLAC__U64L(0x00000000000001FF), FLAC__U64L(0x00000000000003FF), FLAC__U64L(0x00000000000007FF), FLAC__U64L(0x0000000000000FFF),
-               FLAC__U64L(0x0000000000001FFF), FLAC__U64L(0x0000000000003FFF), FLAC__U64L(0x0000000000007FFF), FLAC__U64L(0x000000000000FFFF),
-               FLAC__U64L(0x000000000001FFFF), FLAC__U64L(0x000000000003FFFF), FLAC__U64L(0x000000000007FFFF), FLAC__U64L(0x00000000000FFFFF),
-               FLAC__U64L(0x00000000001FFFFF), FLAC__U64L(0x00000000003FFFFF), FLAC__U64L(0x00000000007FFFFF), FLAC__U64L(0x0000000000FFFFFF),
-               FLAC__U64L(0x0000000001FFFFFF), FLAC__U64L(0x0000000003FFFFFF), FLAC__U64L(0x0000000007FFFFFF), FLAC__U64L(0x000000000FFFFFFF),
-               FLAC__U64L(0x000000001FFFFFFF), FLAC__U64L(0x000000003FFFFFFF), FLAC__U64L(0x000000007FFFFFFF), FLAC__U64L(0x00000000FFFFFFFF),
-               FLAC__U64L(0x00000001FFFFFFFF), FLAC__U64L(0x00000003FFFFFFFF), FLAC__U64L(0x00000007FFFFFFFF), FLAC__U64L(0x0000000FFFFFFFFF),
-               FLAC__U64L(0x0000001FFFFFFFFF), FLAC__U64L(0x0000003FFFFFFFFF), FLAC__U64L(0x0000007FFFFFFFFF), FLAC__U64L(0x000000FFFFFFFFFF),
-               FLAC__U64L(0x000001FFFFFFFFFF), FLAC__U64L(0x000003FFFFFFFFFF), FLAC__U64L(0x000007FFFFFFFFFF), FLAC__U64L(0x00000FFFFFFFFFFF),
-               FLAC__U64L(0x00001FFFFFFFFFFF), FLAC__U64L(0x00003FFFFFFFFFFF), FLAC__U64L(0x00007FFFFFFFFFFF), FLAC__U64L(0x0000FFFFFFFFFFFF),
-               FLAC__U64L(0x0001FFFFFFFFFFFF), FLAC__U64L(0x0003FFFFFFFFFFFF), FLAC__U64L(0x0007FFFFFFFFFFFF), FLAC__U64L(0x000FFFFFFFFFFFFF),
-               FLAC__U64L(0x001FFFFFFFFFFFFF), FLAC__U64L(0x003FFFFFFFFFFFFF), FLAC__U64L(0x007FFFFFFFFFFFFF), FLAC__U64L(0x00FFFFFFFFFFFFFF),
-               FLAC__U64L(0x01FFFFFFFFFFFFFF), FLAC__U64L(0x03FFFFFFFFFFFFFF), FLAC__U64L(0x07FFFFFFFFFFFFFF), FLAC__U64L(0x0FFFFFFFFFFFFFFF),
-               FLAC__U64L(0x1FFFFFFFFFFFFFFF), FLAC__U64L(0x3FFFFFFFFFFFFFFF), FLAC__U64L(0x7FFFFFFFFFFFFFFF), FLAC__U64L(0xFFFFFFFFFFFFFFFF)
-       };
-       unsigned n, k;
-
-       FLAC__ASSERT(0 != bb);
-       FLAC__ASSERT(0 != bb->buffer);
-
-       FLAC__ASSERT(bits <= 64);
-       if(bits == 0)
-               return true;
-       if(!bitbuffer_ensure_size_(bb, bits))
-               return false;
-       val &= mask[bits];
-       bb->total_bits += bits;
-       while(bits > 0) {
-               if(bb->bits == 0) {
-                       if(bits < FLAC__BITS_PER_BLURB) {
-                               bb->buffer[bb->blurbs] = (FLAC__blurb)val;
-                               bb->bits = bits;
-                               break;
-                       }
-                       else if(bits == FLAC__BITS_PER_BLURB) {
-                               bb->buffer[bb->blurbs++] = (FLAC__blurb)val;
-                               break;
-                       }
-                       else {
-                               k = bits - FLAC__BITS_PER_BLURB;
-                               bb->buffer[bb->blurbs++] = (FLAC__blurb)(val >> k);
-                               /* we know k < 64 so no need to protect against the gcc bug mentioned above */
-                               val &= (~(FLAC__U64L(0xffffffffffffffff) << k));
-                               bits -= FLAC__BITS_PER_BLURB;
-                       }
-               }
-               else {
-                       n = min(FLAC__BITS_PER_BLURB - bb->bits, bits);
-                       k = bits - n;
-                       bb->buffer[bb->blurbs] <<= n;
-                       bb->buffer[bb->blurbs] |= (val >> k);
-                       /* we know n > 0 so k < 64 so no need to protect against the gcc bug mentioned above */
-                       val &= (~(FLAC__U64L(0xffffffffffffffff) << k));
-                       bits -= n;
-                       bb->bits += n;
-                       if(bb->bits == FLAC__BITS_PER_BLURB) {
-                               bb->blurbs++;
-                               bb->bits = 0;
-                       }
-               }
-       }
-
-       return true;
-}
-
-#if 0 /* UNUSED */
-FLAC__bool FLAC__bitbuffer_write_raw_int64(FLAC__BitBuffer *bb, FLAC__int64 val, unsigned bits)
-{
-       return FLAC__bitbuffer_write_raw_uint64(bb, (FLAC__uint64)val, bits);
-}
-#endif
-
-FLaC__INLINE FLAC__bool FLAC__bitbuffer_write_raw_uint32_little_endian(FLAC__BitBuffer *bb, FLAC__uint32 val)
-{
-       /* this doesn't need to be that fast as currently it is only used for vorbis comments */
-
-       /* NOTE: we rely on the fact that FLAC__bitbuffer_write_raw_uint32() masks out the unused bits */
-       if(!FLAC__bitbuffer_write_raw_uint32(bb, val, 8))
-               return false;
-       if(!FLAC__bitbuffer_write_raw_uint32(bb, val>>8, 8))
-               return false;
-       if(!FLAC__bitbuffer_write_raw_uint32(bb, val>>16, 8))
-               return false;
-       if(!FLAC__bitbuffer_write_raw_uint32(bb, val>>24, 8))
-               return false;
-
-       return true;
-}
-
-FLaC__INLINE FLAC__bool FLAC__bitbuffer_write_byte_block(FLAC__BitBuffer *bb, const FLAC__byte vals[], unsigned nvals)
-{
-       unsigned i;
-
-       /* this could be faster but currently we don't need it to be */
-       for(i = 0; i < nvals; i++) {
-               if(!FLAC__bitbuffer_write_raw_uint32(bb, (FLAC__uint32)(vals[i]), 8))
-                       return false;
-       }
-
-       return true;
-}
-
-FLAC__bool FLAC__bitbuffer_write_unary_unsigned(FLAC__BitBuffer *bb, unsigned val)
-{
-       if(val < 32)
-               return FLAC__bitbuffer_write_raw_uint32(bb, 1, ++val);
-       else if(val < 64)
-               return FLAC__bitbuffer_write_raw_uint64(bb, 1, ++val);
-       else {
-               if(!FLAC__bitbuffer_write_zeroes(bb, val))
-                       return false;
-               return FLAC__bitbuffer_write_raw_uint32(bb, 1, 1);
-       }
-}
-
-unsigned FLAC__bitbuffer_rice_bits(int val, unsigned parameter)
-{
-       unsigned msbs, uval;
-
-       /* fold signed to unsigned */
-       if(val < 0)
-               /* equivalent to
-                *     (unsigned)(((--val) << 1) - 1);
-                * but without the overflow problem at MININT
-                */
-               uval = (unsigned)(((-(++val)) << 1) + 1);
-       else
-               uval = (unsigned)(val << 1);
-
-       msbs = uval >> parameter;
-
-       return 1 + parameter + msbs;
-}
-
-#if 0 /* UNUSED */
-unsigned FLAC__bitbuffer_golomb_bits_signed(int val, unsigned parameter)
-{
-       unsigned bits, msbs, uval;
-       unsigned k;
-
-       FLAC__ASSERT(parameter > 0);
-
-       /* fold signed to unsigned */
-       if(val < 0)
-               /* equivalent to
-                *     (unsigned)(((--val) << 1) - 1);
-                * but without the overflow problem at MININT
-                */
-               uval = (unsigned)(((-(++val)) << 1) + 1);
-       else
-               uval = (unsigned)(val << 1);
-
-       k = FLAC__bitmath_ilog2(parameter);
-       if(parameter == 1u<<k) {
-               FLAC__ASSERT(k <= 30);
-
-               msbs = uval >> k;
-               bits = 1 + k + msbs;
-       }
-       else {
-               unsigned q, r, d;
-
-               d = (1 << (k+1)) - parameter;
-               q = uval / parameter;
-               r = uval - (q * parameter);
-
-               bits = 1 + q + k;
-               if(r >= d)
-                       bits++;
-       }
-       return bits;
-}
-
-unsigned FLAC__bitbuffer_golomb_bits_unsigned(unsigned uval, unsigned parameter)
-{
-       unsigned bits, msbs;
-       unsigned k;
-
-       FLAC__ASSERT(parameter > 0);
-
-       k = FLAC__bitmath_ilog2(parameter);
-       if(parameter == 1u<<k) {
-               FLAC__ASSERT(k <= 30);
-
-               msbs = uval >> k;
-               bits = 1 + k + msbs;
-       }
-       else {
-               unsigned q, r, d;
-
-               d = (1 << (k+1)) - parameter;
-               q = uval / parameter;
-               r = uval - (q * parameter);
-
-               bits = 1 + q + k;
-               if(r >= d)
-                       bits++;
-       }
-       return bits;
-}
-#endif /* UNUSED */
-
-FLAC__bool FLAC__bitbuffer_write_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
-{
-       unsigned total_bits, interesting_bits, msbs, uval;
-       FLAC__uint32 pattern;
-
-       FLAC__ASSERT(0 != bb);
-       FLAC__ASSERT(0 != bb->buffer);
-       FLAC__ASSERT(parameter <= 30);
-
-       /* fold signed to unsigned */
-       if(val < 0)
-               /* equivalent to
-                *     (unsigned)(((--val) << 1) - 1);
-                * but without the overflow problem at MININT
-                */
-               uval = (unsigned)(((-(++val)) << 1) + 1);
-       else
-               uval = (unsigned)(val << 1);
-
-       msbs = uval >> parameter;
-       interesting_bits = 1 + parameter;
-       total_bits = interesting_bits + msbs;
-       pattern = 1 << parameter; /* the unary end bit */
-       pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
-
-       if(total_bits <= 32) {
-               if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
-                       return false;
-       }
-       else {
-               /* write the unary MSBs */
-               if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
-                       return false;
-               /* write the unary end bit and binary LSBs */
-               if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
-                       return false;
-       }
-       return true;
-}
-
-#if 0 /* UNUSED */
-FLAC__bool FLAC__bitbuffer_write_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, FLAC__bool *overflow)
-{
-       unsigned total_bits, interesting_bits, msbs, uval;
-       FLAC__uint32 pattern;
-
-       FLAC__ASSERT(0 != bb);
-       FLAC__ASSERT(0 != bb->buffer);
-       FLAC__ASSERT(parameter <= 30);
-
-       *overflow = false;
-
-       /* fold signed to unsigned */
-       if(val < 0)
-               /* equivalent to
-                *     (unsigned)(((--val) << 1) - 1);
-                * but without the overflow problem at MININT
-                */
-               uval = (unsigned)(((-(++val)) << 1) + 1);
-       else
-               uval = (unsigned)(val << 1);
-
-       msbs = uval >> parameter;
-       interesting_bits = 1 + parameter;
-       total_bits = interesting_bits + msbs;
-       pattern = 1 << parameter; /* the unary end bit */
-       pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
-
-       if(total_bits <= 32) {
-               if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
-                       return false;
-       }
-       else if(total_bits > max_bits) {
-               *overflow = true;
-               return true;
-       }
-       else {
-               /* write the unary MSBs */
-               if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
-                       return false;
-               /* write the unary end bit and binary LSBs */
-               if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
-                       return false;
-       }
-       return true;
-}
-#endif /* UNUSED */
-
-#if 0 /* UNUSED */
-FLAC__bool FLAC__bitbuffer_write_golomb_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
-{
-       unsigned total_bits, msbs, uval;
-       unsigned k;
-
-       FLAC__ASSERT(0 != bb);
-       FLAC__ASSERT(0 != bb->buffer);
-       FLAC__ASSERT(parameter > 0);
-
-       /* fold signed to unsigned */
-       if(val < 0)
-               /* equivalent to
-                *     (unsigned)(((--val) << 1) - 1);
-                * but without the overflow problem at MININT
-                */
-               uval = (unsigned)(((-(++val)) << 1) + 1);
-       else
-               uval = (unsigned)(val << 1);
-
-       k = FLAC__bitmath_ilog2(parameter);
-       if(parameter == 1u<<k) {
-               unsigned pattern;
-
-               FLAC__ASSERT(k <= 30);
-
-               msbs = uval >> k;
-               total_bits = 1 + k + msbs;
-               pattern = 1 << k; /* the unary end bit */
-               pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
-
-               if(total_bits <= 32) {
-                       if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
-                               return false;
-               }
-               else {
-                       /* write the unary MSBs */
-                       if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
-                               return false;
-                       /* write the unary end bit and binary LSBs */
-                       if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1))
-                               return false;
-               }
-       }
-       else {
-               unsigned q, r, d;
-
-               d = (1 << (k+1)) - parameter;
-               q = uval / parameter;
-               r = uval - (q * parameter);
-               /* write the unary MSBs */
-               if(!FLAC__bitbuffer_write_zeroes(bb, q))
-                       return false;
-               /* write the unary end bit */
-               if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
-                       return false;
-               /* write the binary LSBs */
-               if(r >= d) {
-                       if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1))
-                               return false;
-               }
-               else {
-                       if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k))
-                               return false;
-               }
-       }
-       return true;
-}
-
-FLAC__bool FLAC__bitbuffer_write_golomb_unsigned(FLAC__BitBuffer *bb, unsigned uval, unsigned parameter)
-{
-       unsigned total_bits, msbs;
-       unsigned k;
-
-       FLAC__ASSERT(0 != bb);
-       FLAC__ASSERT(0 != bb->buffer);
-       FLAC__ASSERT(parameter > 0);
-
-       k = FLAC__bitmath_ilog2(parameter);
-       if(parameter == 1u<<k) {
-               unsigned pattern;
-
-               FLAC__ASSERT(k <= 30);
-
-               msbs = uval >> k;
-               total_bits = 1 + k + msbs;
-               pattern = 1 << k; /* the unary end bit */
-               pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
-
-               if(total_bits <= 32) {
-                       if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
-                               return false;
-               }
-               else {
-                       /* write the unary MSBs */
-                       if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
-                               return false;
-                       /* write the unary end bit and binary LSBs */
-                       if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1))
-                               return false;
-               }
-       }
-       else {
-               unsigned q, r, d;
-
-               d = (1 << (k+1)) - parameter;
-               q = uval / parameter;
-               r = uval - (q * parameter);
-               /* write the unary MSBs */
-               if(!FLAC__bitbuffer_write_zeroes(bb, q))
-                       return false;
-               /* write the unary end bit */
-               if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
-                       return false;
-               /* write the binary LSBs */
-               if(r >= d) {
-                       if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1))
-                               return false;
-               }
-               else {
-                       if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k))
-                               return false;
-               }
-       }
-       return true;
-}
-#endif /* UNUSED */
-
-FLAC__bool FLAC__bitbuffer_write_utf8_uint32(FLAC__BitBuffer *bb, FLAC__uint32 val)
-{
-       FLAC__bool ok = 1;
-
-       FLAC__ASSERT(0 != bb);
-       FLAC__ASSERT(0 != bb->buffer);
-
-       FLAC__ASSERT(!(val & 0x80000000)); /* this version only handles 31 bits */
-
-       if(val < 0x80) {
-               return FLAC__bitbuffer_write_raw_uint32(bb, val, 8);
-       }
-       else if(val < 0x800) {
-               ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (val>>6), 8);
-               ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
-       }
-       else if(val < 0x10000) {
-               ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (val>>12), 8);
-               ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
-               ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
-       }
-       else if(val < 0x200000) {
-               ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (val>>18), 8);
-               ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
-               ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
-               ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
-       }
-       else if(val < 0x4000000) {
-               ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (val>>24), 8);
-               ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
-               ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
-               ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
-               ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
-       }
-       else {
-               ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (val>>30), 8);
-               ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>24)&0x3F), 8);
-               ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
-               ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
-               ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
-               ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
-       }
-
-       return ok;
-}
-
-FLAC__bool FLAC__bitbuffer_write_utf8_uint64(FLAC__BitBuffer *bb, FLAC__uint64 val)
-{
-       FLAC__bool ok = 1;
-
-       FLAC__ASSERT(0 != bb);
-       FLAC__ASSERT(0 != bb->buffer);
-
-       FLAC__ASSERT(!(val & FLAC__U64L(0xFFFFFFF000000000))); /* this version only handles 36 bits */
-
-       if(val < 0x80) {
-               return FLAC__bitbuffer_write_raw_uint32(bb, (FLAC__uint32)val, 8);
-       }
-       else if(val < 0x800) {
-               ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (FLAC__uint32)(val>>6), 8);
-               ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
-       }
-       else if(val < 0x10000) {
-               ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (FLAC__uint32)(val>>12), 8);
-               ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
-               ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
-       }
-       else if(val < 0x200000) {
-               ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (FLAC__uint32)(val>>18), 8);
-               ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
-               ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
-               ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
-       }
-       else if(val < 0x4000000) {
-               ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (FLAC__uint32)(val>>24), 8);
-               ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
-               ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
-               ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
-               ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
-       }
-       else if(val < 0x80000000) {
-               ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (FLAC__uint32)(val>>30), 8);
-               ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
-               ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
-               ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
-               ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
-               ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
-       }
-       else {
-               ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFE, 8);
-               ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>30)&0x3F), 8);
-               ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
-               ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
-               ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
-               ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
-               ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
-       }
-
-       return ok;
-}
-
-FLAC__bool FLAC__bitbuffer_zero_pad_to_byte_boundary(FLAC__BitBuffer *bb)
-{
-       /* 0-pad to byte boundary */
-       if(bb->bits & 7u)
-               return FLAC__bitbuffer_write_zeroes(bb, 8 - (bb->bits & 7u));
-       else
-               return true;
-}
-
-FLAC__bool FLAC__bitbuffer_peek_bit(FLAC__BitBuffer *bb, unsigned *val, FLAC__BitbufferReadCallback read_callback, void *client_data)
-{
-       /* to avoid a drastic speed penalty we don't:
-       FLAC__ASSERT(0 != bb);
-       FLAC__ASSERT(0 != bb->buffer);
-       FLAC__ASSERT(bb->bits == 0);
-       */
-
-       while(1) {
-               if(bb->total_consumed_bits < bb->total_bits) {
-                       *val = (bb->buffer[bb->consumed_blurbs] & BLURB_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
-                       return true;
-               }
-               else {
-                       if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
-                               return false;
-               }
-       }
-}
-
-FLAC__bool FLAC__bitbuffer_read_bit(FLAC__BitBuffer *bb, unsigned *val, FLAC__BitbufferReadCallback read_callback, void *client_data)
-{
-       /* to avoid a drastic speed penalty we don't:
-       FLAC__ASSERT(0 != bb);
-       FLAC__ASSERT(0 != bb->buffer);
-       FLAC__ASSERT(bb->bits == 0);
-       */
-
-       while(1) {
-               if(bb->total_consumed_bits < bb->total_bits) {
-                       *val = (bb->buffer[bb->consumed_blurbs] & BLURB_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
-                       bb->consumed_bits++;
-                       if(bb->consumed_bits == FLAC__BITS_PER_BLURB) {
-                               CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
-                               bb->consumed_blurbs++;
-                               bb->consumed_bits = 0;
-                       }
-                       bb->total_consumed_bits++;
-                       return true;
-               }
-               else {
-                       if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
-                               return false;
-               }
-       }
-}
-
-FLAC__bool FLAC__bitbuffer_read_bit_to_uint32(FLAC__BitBuffer *bb, FLAC__uint32 *val, FLAC__BitbufferReadCallback read_callback, void *client_data)
-{
-       /* to avoid a drastic speed penalty we don't:
-       FLAC__ASSERT(0 != bb);
-       FLAC__ASSERT(0 != bb->buffer);
-       FLAC__ASSERT(bb->bits == 0);
-       */
-
-       while(1) {
-               if(bb->total_consumed_bits < bb->total_bits) {
-                       *val <<= 1;
-                       *val |= (bb->buffer[bb->consumed_blurbs] & BLURB_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
-                       bb->consumed_bits++;
-                       if(bb->consumed_bits == FLAC__BITS_PER_BLURB) {
-                               CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
-                               bb->consumed_blurbs++;
-                               bb->consumed_bits = 0;
-                       }
-                       bb->total_consumed_bits++;
-                       return true;
-               }
-               else {
-                       if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
-                               return false;
-               }
-       }
-}
-
-FLAC__bool FLAC__bitbuffer_read_bit_to_uint64(FLAC__BitBuffer *bb, FLAC__uint64 *val, FLAC__BitbufferReadCallback read_callback, void *client_data)
-{
-       /* to avoid a drastic speed penalty we don't:
-       FLAC__ASSERT(0 != bb);
-       FLAC__ASSERT(0 != bb->buffer);
-       FLAC__ASSERT(bb->bits == 0);
-       */
-
-       while(1) {
-               if(bb->total_consumed_bits < bb->total_bits) {
-                       *val <<= 1;
-                       *val |= (bb->buffer[bb->consumed_blurbs] & BLURB_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
-                       bb->consumed_bits++;
-                       if(bb->consumed_bits == FLAC__BITS_PER_BLURB) {
-                               CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
-                               bb->consumed_blurbs++;
-                               bb->consumed_bits = 0;
-                       }
-                       bb->total_consumed_bits++;
-                       return true;
-               }
-               else {
-                       if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
-                               return false;
-               }
-       }
-}
-
-FLaC__INLINE FLAC__bool FLAC__bitbuffer_read_raw_uint32(FLAC__BitBuffer *bb, FLAC__uint32 *val, const unsigned bits, FLAC__BitbufferReadCallback read_callback, void *client_data)
-#ifdef FLAC__NO_MANUAL_INLINING
-{
-       unsigned i;
-
-       FLAC__ASSERT(0 != bb);
-       FLAC__ASSERT(0 != bb->buffer);
-
-       FLAC__ASSERT(bits <= 32);
-
-       *val = 0;
-       for(i = 0; i < bits; i++) {
-               if(!FLAC__bitbuffer_read_bit_to_uint32(bb, val, read_callback, client_data))
-                       return false;
-       }
-       return true;
-}
-#else
-{
-       unsigned i, bits_ = bits;
-       FLAC__uint32 v = 0;
-
-       FLAC__ASSERT(0 != bb);
-       FLAC__ASSERT(0 != bb->buffer);
-
-       FLAC__ASSERT(bits <= 32);
-       FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
-
-       if(bits == 0) {
-               *val = 0;
-               return true;
-       }
-
-       while(bb->total_consumed_bits + bits > bb->total_bits) {
-               if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
-                       return false;
-       }
-#if FLAC__BITS_PER_BLURB > 8
-       if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/
-#endif
-               if(bb->consumed_bits) {
-                       i = FLAC__BITS_PER_BLURB - bb->consumed_bits;
-                       if(i <= bits_) {
-                               v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits);
-                               bits_ -= i;
-                               CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
-                               bb->consumed_blurbs++;
-                               bb->consumed_bits = 0;
-                               /* we hold off updating bb->total_consumed_bits until the end */
-                       }
-                       else {
-                               *val = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits)) >> (i-bits_);
-                               bb->consumed_bits += bits_;
-                               bb->total_consumed_bits += bits_;
-                               return true;
-                       }
-               }
-#if FLAC__BITS_PER_BLURB == 32
-               /* note that we know bits_ cannot be > 32 because of previous assertions */
-               if(bits_ == FLAC__BITS_PER_BLURB) {
-                       v = bb->buffer[bb->consumed_blurbs];
-                       CRC16_UPDATE_BLURB(bb, v, bb->read_crc16);
-                       bb->consumed_blurbs++;
-                       /* bb->consumed_bits is already 0 */
-                       bb->total_consumed_bits += bits;
-                       *val = v;
-                       return true;
-               }
-#else
-               while(bits_ >= FLAC__BITS_PER_BLURB) {
-                       v <<= FLAC__BITS_PER_BLURB;
-                       v |= bb->buffer[bb->consumed_blurbs];
-                       bits_ -= FLAC__BITS_PER_BLURB;
-                       CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
-                       bb->consumed_blurbs++;
-                       /* bb->consumed_bits is already 0 */
-                       /* we hold off updating bb->total_consumed_bits until the end */
-               }
-#endif
-               if(bits_ > 0) {
-                       v <<= bits_;
-                       v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_));
-                       bb->consumed_bits = bits_;
-                       /* we hold off updating bb->total_consumed_bits until the end */
-               }
-               bb->total_consumed_bits += bits;
-               *val = v;
-#if FLAC__BITS_PER_BLURB > 8
-       }
-       else {
-               *val = 0;
-               for(i = 0; i < bits; i++) {
-                       if(!FLAC__bitbuffer_read_bit_to_uint32(bb, val, read_callback, client_data))
-                               return false;
-               }
-       }
-#endif
-       return true;
-}
-#endif
-
-FLAC__bool FLAC__bitbuffer_read_raw_int32(FLAC__BitBuffer *bb, FLAC__int32 *val, const unsigned bits, FLAC__BitbufferReadCallback read_callback, void *client_data)
-#ifdef FLAC__NO_MANUAL_INLINING
-{
-       unsigned i;
-       FLAC__uint32 v;
-
-       FLAC__ASSERT(0 != bb);
-       FLAC__ASSERT(0 != bb->buffer);
-
-       FLAC__ASSERT(bits <= 32);
-
-       if(bits == 0) {
-               *val = 0;
-               return true;
-       }
-
-       v = 0;
-       for(i = 0; i < bits; i++) {
-               if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &v, read_callback, client_data))
-                       return false;
-       }
-
-       /* fix the sign */
-       i = 32 - bits;
-       if(i) {
-               v <<= i;
-               *val = (FLAC__int32)v;
-               *val >>= i;
-       }
-       else
-               *val = (FLAC__int32)v;
-
-       return true;
-}
-#else
-{
-       unsigned i, bits_ = bits;
-       FLAC__uint32 v = 0;
-
-       FLAC__ASSERT(0 != bb);
-       FLAC__ASSERT(0 != bb->buffer);
-
-       FLAC__ASSERT(bits <= 32);
-       FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
-
-       if(bits == 0) {
-               *val = 0;
-               return true;
-       }
-
-       while(bb->total_consumed_bits + bits > bb->total_bits) {
-               if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
-                       return false;
-       }
-#if FLAC__BITS_PER_BLURB > 8
-       if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/
-#endif
-               if(bb->consumed_bits) {
-                       i = FLAC__BITS_PER_BLURB - bb->consumed_bits;
-                       if(i <= bits_) {
-                               v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits);
-                               bits_ -= i;
-                               CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
-                               bb->consumed_blurbs++;
-                               bb->consumed_bits = 0;
-                               /* we hold off updating bb->total_consumed_bits until the end */
-                       }
-                       else {
-                               /* bits_ must be < FLAC__BITS_PER_BLURB-1 if we get to here */
-                               v = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits));
-                               v <<= (32-i);
-                               *val = (FLAC__int32)v;
-                               *val >>= (32-bits_);
-                               bb->consumed_bits += bits_;
-                               bb->total_consumed_bits += bits_;
-                               return true;
-                       }
-               }
-#if FLAC__BITS_PER_BLURB == 32
-               /* note that we know bits_ cannot be > 32 because of previous assertions */
-               if(bits_ == FLAC__BITS_PER_BLURB) {
-                       v = bb->buffer[bb->consumed_blurbs];
-                       bits_ = 0;
-                       CRC16_UPDATE_BLURB(bb, v, bb->read_crc16);
-                       bb->consumed_blurbs++;
-                       /* bb->consumed_bits is already 0 */
-                       /* we hold off updating bb->total_consumed_bits until the end */
-               }
-#else
-               while(bits_ >= FLAC__BITS_PER_BLURB) {
-                       v <<= FLAC__BITS_PER_BLURB;
-                       v |= bb->buffer[bb->consumed_blurbs];
-                       bits_ -= FLAC__BITS_PER_BLURB;
-                       CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
-                       bb->consumed_blurbs++;
-                       /* bb->consumed_bits is already 0 */
-                       /* we hold off updating bb->total_consumed_bits until the end */
-               }
-#endif
-               if(bits_ > 0) {
-                       v <<= bits_;
-                       v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_));
-                       bb->consumed_bits = bits_;
-                       /* we hold off updating bb->total_consumed_bits until the end */
-               }
-               bb->total_consumed_bits += bits;
-#if FLAC__BITS_PER_BLURB > 8
-       }
-       else {
-               for(i = 0; i < bits; i++) {
-                       if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &v, read_callback, client_data))
-                               return false;
-               }
-       }
-#endif
-
-       /* fix the sign */
-       i = 32 - bits;
-       if(i) {
-               v <<= i;
-               *val = (FLAC__int32)v;
-               *val >>= i;
-       }
-       else
-               *val = (FLAC__int32)v;
-
-       return true;
-}
-#endif
-
-FLAC__bool FLAC__bitbuffer_read_raw_uint64(FLAC__BitBuffer *bb, FLAC__uint64 *val, const unsigned bits, FLAC__BitbufferReadCallback read_callback, void *client_data)
-#ifdef FLAC__NO_MANUAL_INLINING
-{
-       unsigned i;
-
-       FLAC__ASSERT(0 != bb);
-       FLAC__ASSERT(0 != bb->buffer);
-
-       FLAC__ASSERT(bits <= 64);
-
-       *val = 0;
-       for(i = 0; i < bits; i++) {
-               if(!FLAC__bitbuffer_read_bit_to_uint64(bb, val, read_callback, client_data))
-                       return false;
-       }
-       return true;
-}
-#else
-{
-       unsigned i, bits_ = bits;
-       FLAC__uint64 v = 0;
-
-       FLAC__ASSERT(0 != bb);
-       FLAC__ASSERT(0 != bb->buffer);
-
-       FLAC__ASSERT(bits <= 64);
-       FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
-
-       if(bits == 0) {
-               *val = 0;
-               return true;
-       }
-
-       while(bb->total_consumed_bits + bits > bb->total_bits) {
-               if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
-                       return false;
-       }
-#if FLAC__BITS_PER_BLURB > 8
-       if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/
-#endif
-               if(bb->consumed_bits) {
-                       i = FLAC__BITS_PER_BLURB - bb->consumed_bits;
-                       if(i <= bits_) {
-                               v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits);
-                               bits_ -= i;
-                               CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
-                               bb->consumed_blurbs++;
-                               bb->consumed_bits = 0;
-                               /* we hold off updating bb->total_consumed_bits until the end */
-                       }
-                       else {
-                               *val = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits)) >> (i-bits_);
-                               bb->consumed_bits += bits_;
-                               bb->total_consumed_bits += bits_;
-                               return true;
-                       }
-               }
-               while(bits_ >= FLAC__BITS_PER_BLURB) {
-                       v <<= FLAC__BITS_PER_BLURB;
-                       v |= bb->buffer[bb->consumed_blurbs];
-                       bits_ -= FLAC__BITS_PER_BLURB;
-                       CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
-                       bb->consumed_blurbs++;
-                       /* bb->consumed_bits is already 0 */
-                       /* we hold off updating bb->total_consumed_bits until the end */
-               }
-               if(bits_ > 0) {
-                       v <<= bits_;
-                       v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_));
-                       bb->consumed_bits = bits_;
-                       /* we hold off updating bb->total_consumed_bits until the end */
-               }
-               bb->total_consumed_bits += bits;
-               *val = v;
-#if FLAC__BITS_PER_BLURB > 8
-       }
-       else {
-               *val = 0;
-               for(i = 0; i < bits; i++) {
-                       if(!FLAC__bitbuffer_read_bit_to_uint64(bb, val, read_callback, client_data))
-                               return false;
-               }
-       }
-#endif
-       return true;
-}
-#endif
-
-#if 0 /* UNUSED */
-FLAC__bool FLAC__bitbuffer_read_raw_int64(FLAC__BitBuffer *bb, FLAC__int64 *val, const unsigned bits, FLAC__BitbufferReadCallback read_callback, void *client_data)
-#ifdef FLAC__NO_MANUAL_INLINING
-{
-       unsigned i;
-       FLAC__uint64 v;
-
-       FLAC__ASSERT(0 != bb);
-       FLAC__ASSERT(0 != bb->buffer);
-
-       FLAC__ASSERT(bits <= 64);
-
-       v = 0;
-       for(i = 0; i < bits; i++) {
-               if(!FLAC__bitbuffer_read_bit_to_uint64(bb, &v, read_callback, client_data))
-                       return false;
-       }
-       /* fix the sign */
-       i = 64 - bits;
-       if(i) {
-               v <<= i;
-               *val = (FLAC__int64)v;
-               *val >>= i;
-       }
-       else
-               *val = (FLAC__int64)v;
-
-       return true;
-}
-#else
-{
-       unsigned i, bits_ = bits;
-       FLAC__uint64 v = 0;
-
-       FLAC__ASSERT(0 != bb);
-       FLAC__ASSERT(0 != bb->buffer);
-
-       FLAC__ASSERT(bits <= 64);
-       FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
-
-       if(bits == 0) {
-               *val = 0;
-               return true;
-       }
-
-       while(bb->total_consumed_bits + bits > bb->total_bits) {
-               if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
-                       return false;
-       }
-#if FLAC__BITS_PER_BLURB > 8
-       if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/
-#endif
-               if(bb->consumed_bits) {
-                       i = FLAC__BITS_PER_BLURB - bb->consumed_bits;
-                       if(i <= bits_) {
-                               v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits);
-                               bits_ -= i;
-                               CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
-                               bb->consumed_blurbs++;
-                               bb->consumed_bits = 0;
-                               /* we hold off updating bb->total_consumed_bits until the end */
-                       }
-                       else {
-                               /* bits_ must be < FLAC__BITS_PER_BLURB-1 if we get to here */
-                               v = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits));
-                               v <<= (64-i);
-                               *val = (FLAC__int64)v;
-                               *val >>= (64-bits_);
-                               bb->consumed_bits += bits_;
-                               bb->total_consumed_bits += bits_;
-                               return true;
-                       }
-               }
-               while(bits_ >= FLAC__BITS_PER_BLURB) {
-                       v <<= FLAC__BITS_PER_BLURB;
-                       v |= bb->buffer[bb->consumed_blurbs];
-                       bits_ -= FLAC__BITS_PER_BLURB;
-                       CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
-                       bb->consumed_blurbs++;
-                       /* bb->consumed_bits is already 0 */
-                       /* we hold off updating bb->total_consumed_bits until the end */
-               }
-               if(bits_ > 0) {
-                       v <<= bits_;
-                       v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_));
-                       bb->consumed_bits = bits_;
-                       /* we hold off updating bb->total_consumed_bits until the end */
-               }
-               bb->total_consumed_bits += bits;
-#if FLAC__BITS_PER_BLURB > 8
-       }
-       else {
-               for(i = 0; i < bits; i++) {
-                       if(!FLAC__bitbuffer_read_bit_to_uint64(bb, &v, read_callback, client_data))
-                               return false;
-               }
-       }
-#endif
-
-       /* fix the sign */
-       i = 64 - bits;
-       if(i) {
-               v <<= i;
-               *val = (FLAC__int64)v;
-               *val >>= i;
-       }
-       else
-               *val = (FLAC__int64)v;
-
-       return true;
-}
-#endif
-#endif
-
-FLaC__INLINE FLAC__bool FLAC__bitbuffer_read_raw_uint32_little_endian(FLAC__BitBuffer *bb, FLAC__uint32 *val, FLAC__BitbufferReadCallback read_callback, void *client_data)
-{
-       FLAC__uint32 x8, x32 = 0;
-
-       /* this doesn't need to be that fast as currently it is only used for vorbis comments */
-
-       if(!FLAC__bitbuffer_read_raw_uint32(bb, &x32, 8, read_callback, client_data))
-               return false;
-
-       if(!FLAC__bitbuffer_read_raw_uint32(bb, &x8, 8, read_callback, client_data))
-               return false;
-       x32 |= (x8 << 8);
-
-       if(!FLAC__bitbuffer_read_raw_uint32(bb, &x8, 8, read_callback, client_data))
-               return false;
-       x32 |= (x8 << 16);
-
-       if(!FLAC__bitbuffer_read_raw_uint32(bb, &x8, 8, read_callback, client_data))
-               return false;
-       x32 |= (x8 << 24);
-
-       *val = x32;
-       return true;
-}
-
-FLAC__bool FLAC__bitbuffer_skip_bits_no_crc(FLAC__BitBuffer *bb, unsigned bits, FLAC__BitbufferReadCallback read_callback, void *client_data)
-{
-       /*
-        * @@@ a slightly faster implementation is possible but
-        * probably not that useful since this is only called a
-        * couple of times in the metadata readers.
-        */
-       FLAC__ASSERT(0 != bb);
-       FLAC__ASSERT(0 != bb->buffer);
-
-       if(bits > 0) {
-               const unsigned n = bb->consumed_bits & 7;
-               unsigned m;
-               FLAC__uint32 x;
-
-               if(n != 0) {
-                       m = min(8-n, bits);
-                       if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, m, read_callback, client_data))
-                               return false;
-                       bits -= m;
-               }
-               m = bits / 8;
-               if(m > 0) {
-                       if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(bb, 0, m, read_callback, client_data))
-                               return false;
-                       bits %= 8;
-               }
-               if(bits > 0) {
-                       if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, bits, read_callback, client_data))
-                               return false;
-               }
-       }
-
-       return true;
-}
-
-FLAC__bool FLAC__bitbuffer_read_byte_block_aligned_no_crc(FLAC__BitBuffer *bb, FLAC__byte *val, unsigned nvals, FLAC__BitbufferReadCallback read_callback, void *client_data)
-{
-       FLAC__ASSERT(0 != bb);
-       FLAC__ASSERT(0 != bb->buffer);
-       FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(bb));
-       FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(bb));
-#if FLAC__BITS_PER_BLURB == 8
-       while(nvals > 0) {
-               unsigned chunk = min(nvals, bb->blurbs - bb->consumed_blurbs);
-               if(chunk == 0) {
-                       if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
-                               return false;
-               }
-               else {
-                       if(0 != val) {
-                               memcpy(val, bb->buffer + bb->consumed_blurbs, FLAC__BYTES_PER_BLURB * chunk);
-                               val += FLAC__BYTES_PER_BLURB * chunk;
-                       }
-                       nvals -= chunk;
-                       bb->consumed_blurbs += chunk;
-                       bb->total_consumed_bits = (bb->consumed_blurbs << FLAC__BITS_PER_BLURB_LOG2);
-               }
-       }
-#else
-       @@@ need to write this still
-       FLAC__ASSERT(0);
-#endif
-
-       return true;
-}
-
-FLaC__INLINE FLAC__bool FLAC__bitbuffer_read_unary_unsigned(FLAC__BitBuffer *bb, unsigned *val, FLAC__BitbufferReadCallback read_callback, void *client_data)
-#ifdef FLAC__NO_MANUAL_INLINING
-{
-       unsigned bit, val_ = 0;
-
-       FLAC__ASSERT(0 != bb);
-       FLAC__ASSERT(0 != bb->buffer);
-
-       while(1) {
-               if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
-                       return false;
-               if(bit)
-                       break;
-               else
-                       val_++;
-       }
-       *val = val_;
-       return true;
-}
-#else
-{
-       unsigned i, val_ = 0;
-       unsigned total_blurbs_ = (bb->total_bits + (FLAC__BITS_PER_BLURB-1)) / FLAC__BITS_PER_BLURB;
-       FLAC__blurb b;
-
-       FLAC__ASSERT(0 != bb);
-       FLAC__ASSERT(0 != bb->buffer);
-
-#if FLAC__BITS_PER_BLURB > 8
-       if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/
-#endif
-               if(bb->consumed_bits) {
-                       b = bb->buffer[bb->consumed_blurbs] << bb->consumed_bits;
-                       if(b) {
-                               for(i = 0; !(b & FLAC__BLURB_TOP_BIT_ONE); i++)
-                                       b <<= 1;
-                               *val = i;
-                               i++;
-                               bb->consumed_bits += i;
-                               bb->total_consumed_bits += i;
-                               if(bb->consumed_bits == FLAC__BITS_PER_BLURB) {
-                                       CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
-                                       bb->consumed_blurbs++;
-                                       bb->consumed_bits = 0;
-                               }
-                               return true;
-                       }
-                       else {
-                               val_ = FLAC__BITS_PER_BLURB - bb->consumed_bits;
-                               CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
-                               bb->consumed_blurbs++;
-                               bb->consumed_bits = 0;
-                               bb->total_consumed_bits += val_;
-                       }
-               }
-               while(1) {
-                       if(bb->consumed_blurbs >= total_blurbs_) {
-                               if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
-                                       return false;
-                               total_blurbs_ = (bb->total_bits + (FLAC__BITS_PER_BLURB-1)) / FLAC__BITS_PER_BLURB;
-                       }
-                       b = bb->buffer[bb->consumed_blurbs];
-                       if(b) {
-                               for(i = 0; !(b & FLAC__BLURB_TOP_BIT_ONE); i++)
-                                       b <<= 1;
-                               val_ += i;
-                               i++;
-                               bb->consumed_bits = i;
-                               *val = val_;
-                               if(i == FLAC__BITS_PER_BLURB) {
-                                       CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
-                                       bb->consumed_blurbs++;
-                                       bb->consumed_bits = 0;
-                               }
-                               bb->total_consumed_bits += i;
-                               return true;
-                       }
-                       else {
-                               val_ += FLAC__BITS_PER_BLURB;
-                               CRC16_UPDATE_BLURB(bb, 0, bb->read_crc16);
-                               bb->consumed_blurbs++;
-                               /* bb->consumed_bits is already 0 */
-                               bb->total_consumed_bits += FLAC__BITS_PER_BLURB;
-                       }
-               }
-#if FLAC__BITS_PER_BLURB > 8
-       }
-       else {
-               while(1) {
-                       if(!FLAC__bitbuffer_read_bit(bb, &i, read_callback, client_data))
-                               return false;
-                       if(i)
-                               break;
-                       else
-                               val_++;
-               }
-               *val = val_;
-               return true;
-       }
-#endif
-}
-#endif
-
-FLAC__bool FLAC__bitbuffer_read_rice_signed(FLAC__BitBuffer *bb, int *val, unsigned parameter, FLAC__BitbufferReadCallback read_callback, void *client_data)
-{
-       FLAC__uint32 lsbs = 0, msbs = 0;
-       unsigned uval;
-
-       FLAC__ASSERT(0 != bb);
-       FLAC__ASSERT(0 != bb->buffer);
-       FLAC__ASSERT(parameter <= 31);
-
-       /* read the unary MSBs and end bit */
-       if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
-               return false;
-
-       /* read the binary LSBs */
-       if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
-               return false;
-
-       /* compose the value */
-       uval = (msbs << parameter) | lsbs;
-       if(uval & 1)
-               *val = -((int)(uval >> 1)) - 1;
-       else
-               *val = (int)(uval >> 1);
-
-       return true;
-}
-
-FLAC__bool FLAC__bitbuffer_read_rice_signed_block(FLAC__BitBuffer *bb, int vals[], unsigned nvals, unsigned parameter, FLAC__BitbufferReadCallback read_callback, void *client_data)
-#ifdef FLAC__OLD_MSVC_FLAVOR
-{
-       const FLAC__blurb *buffer = bb->buffer;
-
-       unsigned i, j, val_i = 0;
-       unsigned cbits = 0, uval = 0, msbs = 0, lsbs_left = 0;
-       FLAC__blurb blurb, save_blurb;
-       unsigned state = 0; /* 0 = getting unary MSBs, 1 = getting binary LSBs */
-
-       FLAC__ASSERT(0 != bb);
-       FLAC__ASSERT(0 != bb->buffer);
-       FLAC__ASSERT(parameter <= 31);
-
-       if(nvals == 0)
-               return true;
-
-       i = bb->consumed_blurbs;
-       /*
-        * We unroll the main loop to take care of partially consumed blurbs here.
-        */
-       if(bb->consumed_bits > 0) {
-               save_blurb = blurb = buffer[i];
-               cbits = bb->consumed_bits;
-               blurb <<= cbits;
-
-               while(1) {
-                       if(state == 0) {
-                               if(blurb) {
-                                       for(j = 0; !(blurb & FLAC__BLURB_TOP_BIT_ONE); j++)
-                                               blurb <<= 1;
-                                       msbs += j;
-
-                                       /* dispose of the unary end bit */
-                                       blurb <<= 1;
-                                       j++;
-                                       cbits += j;
-
-                                       uval = 0;
-                                       lsbs_left = parameter;
-                                       state++;
-                                       if(cbits == FLAC__BITS_PER_BLURB) {
-                                               cbits = 0;
-                                               CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
-                                               break;
-                                       }
-                               }
-                               else {
-                                       msbs += FLAC__BITS_PER_BLURB - cbits;
-                                       cbits = 0;
-                                       CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
-                                       break;
-                               }
-                       }
-                       else {
-                               const unsigned available_bits = FLAC__BITS_PER_BLURB - cbits;
-                               if(lsbs_left >= available_bits) {
-                                       uval <<= available_bits;
-                                       uval |= (blurb >> cbits);
-                                       cbits = 0;
-                                       CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
-
-                                       if(lsbs_left == available_bits) {
-                                               /* compose the value */
-                                               uval |= (msbs << parameter);
-                                               if(uval & 1)
-                                                       vals[val_i++] = -((int)(uval >> 1)) - 1;
-                                               else
-                                                       vals[val_i++] = (int)(uval >> 1);
-                                               if(val_i == nvals)
-                                                       break;
-
-                                               msbs = 0;
-                                               state = 0;
-                                       }
-
-                                       lsbs_left -= available_bits;
-                                       break;
-                               }
-                               else {
-                                       uval <<= lsbs_left;
-                                       uval |= (blurb >> (FLAC__BITS_PER_BLURB - lsbs_left));
-                                       blurb <<= lsbs_left;
-                                       cbits += lsbs_left;
-
-                                       /* compose the value */
-                                       uval |= (msbs << parameter);
-                                       if(uval & 1)
-                                               vals[val_i++] = -((int)(uval >> 1)) - 1;
-                                       else
-                                               vals[val_i++] = (int)(uval >> 1);
-                                       if(val_i == nvals) {
-                                               /* back up one if we exited the for loop because we read all nvals but the end came in the middle of a blurb */
-                                               i--;
-                                               break;
-                                       }
-
-                                       msbs = 0;
-                                       state = 0;
-                               }
-                       }
-               }
-               i++;
-
-               bb->consumed_blurbs = i;
-               bb->consumed_bits = cbits;
-               bb->total_consumed_bits = (i << FLAC__BITS_PER_BLURB_LOG2) | cbits;
-       }
-
-       /*
-        * Now that we are blurb-aligned the logic is slightly simpler
-        */
-       while(val_i < nvals) {
-               for( ; i < bb->blurbs && val_i < nvals; i++) {
-                       save_blurb = blurb = buffer[i];
-                       cbits = 0;
-                       while(1) {
-                               if(state == 0) {
-                                       if(blurb) {
-                                               for(j = 0; !(blurb & FLAC__BLURB_TOP_BIT_ONE); j++)
-                                                       blurb <<= 1;
-                                               msbs += j;
-
-                                               /* dispose of the unary end bit */
-                                               blurb <<= 1;
-                                               j++;
-                                               cbits += j;
-
-                                               uval = 0;
-                                               lsbs_left = parameter;
-                                               state++;
-                                               if(cbits == FLAC__BITS_PER_BLURB) {
-                                                       cbits = 0;
-                                                       CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
-                                                       break;
-                                               }
-                                       }
-                                       else {
-                                               msbs += FLAC__BITS_PER_BLURB - cbits;
-                                               cbits = 0;
-                                               CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
-                                               break;
-                                       }
-                               }
-                               else {
-                                       const unsigned available_bits = FLAC__BITS_PER_BLURB - cbits;
-                                       if(lsbs_left >= available_bits) {
-                                               uval <<= available_bits;
-                                               uval |= (blurb >> cbits);
-                                               cbits = 0;
-                                               CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
-
-                                               if(lsbs_left == available_bits) {
-                                                       /* compose the value */
-                                                       uval |= (msbs << parameter);
-                                                       if(uval & 1)
-                                                               vals[val_i++] = -((int)(uval >> 1)) - 1;
-                                                       else
-                                                               vals[val_i++] = (int)(uval >> 1);
-                                                       if(val_i == nvals)
-                                                               break;
-
-                                                       msbs = 0;
-                                                       state = 0;
-                                               }
-
-                                               lsbs_left -= available_bits;
-                                               break;
-                                       }
-                                       else {
-                                               uval <<= lsbs_left;
-                                               uval |= (blurb >> (FLAC__BITS_PER_BLURB - lsbs_left));
-                                               blurb <<= lsbs_left;
-                                               cbits += lsbs_left;
-
-                                               /* compose the value */
-                                               uval |= (msbs << parameter);
-                                               if(uval & 1)
-                                                       vals[val_i++] = -((int)(uval >> 1)) - 1;
-                                               else
-                                                       vals[val_i++] = (int)(uval >> 1);
-                                               if(val_i == nvals) {
-                                                       /* back up one if we exited the for loop because we read all nvals but the end came in the middle of a blurb */
-                                                       i--;
-                                                       break;
-                                               }
-
-                                               msbs = 0;
-                                               state = 0;
-                                       }
-                               }
-                       }
-               }
-               bb->consumed_blurbs = i;
-               bb->consumed_bits = cbits;
-               bb->total_consumed_bits = (i << FLAC__BITS_PER_BLURB_LOG2) | cbits;
-               if(val_i < nvals) {
-                       if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
-                               return false;
-                       /* these must be zero because we can only get here if we got to the end of the buffer */
-                       FLAC__ASSERT(bb->consumed_blurbs == 0);
-                       FLAC__ASSERT(bb->consumed_bits == 0);
-                       i = 0;
-               }
-       }
-
-       return true;
-}
-#else
-{
-       const FLAC__blurb *buffer = bb->buffer;
-
-       unsigned i, j, val_i = nvals;
-       unsigned cbits = 0, uval = 0, msbs = 0, lsbs_left = 0;
-       FLAC__blurb blurb, save_blurb;
-       unsigned state = 0; /* 0 = getting unary MSBs, 1 = getting binary LSBs */
-
-       FLAC__ASSERT(0 != bb);
-       FLAC__ASSERT(0 != bb->buffer);
-       FLAC__ASSERT(parameter <= 31);
-
-       if(nvals == 0)
-               return true;
-
-       cbits = bb->consumed_bits;
-       i = bb->consumed_blurbs;
-       while(val_i != 0) {
-               for( ; i < bb->blurbs; i++) {
-                       blurb = (save_blurb = buffer[i]) << cbits;
-                       while(1) {
-                               if(state == 0) {
-                                       if(blurb) {
-                                               j = FLAC__ALIGNED_BLURB_UNARY(blurb);
-                                               msbs += j;
-                                               j++;
-                                               cbits += j;
-
-                                               uval = 0;
-                                               lsbs_left = parameter;
-                                               state++;
-                                               if(cbits == FLAC__BITS_PER_BLURB) {
-                                                       cbits = 0;
-                                                       CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
-                                                       break;
-                                               }
-                                               blurb <<= j;
-                                       }
-                                       else {
-                                               msbs += FLAC__BITS_PER_BLURB - cbits;
-                                               cbits = 0;
-                                               CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
-                                               break;
-                                       }
-                               }
-                               else {
-                                       const unsigned available_bits = FLAC__BITS_PER_BLURB - cbits;
-                                       if(lsbs_left >= available_bits) {
-                                               uval <<= available_bits;
-                                               uval |= (blurb >> cbits);
-                                               cbits = 0;
-                                               CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
-
-                                               if(lsbs_left == available_bits) {
-                                                       /* compose the value */
-                                                       uval |= (msbs << parameter);
-                                                       *vals = (int)(uval >> 1 ^ -(int)(uval & 1));
-                                                       --val_i;
-                                                       if(val_i == 0) {
-                                                               i++;
-                                                               goto break2;
-                                                       }
-                                                       ++vals;
-
-                                                       msbs = 0;
-                                                       state = 0;
-                                               }
-
-                                               lsbs_left -= available_bits;
-                                               break;
-                                       }
-                                       else {
-                                               cbits += lsbs_left;
-                                               uval <<= lsbs_left;
-                                               uval |= (blurb >> (FLAC__BITS_PER_BLURB - lsbs_left));
-                                               blurb <<= lsbs_left;
-
-                                               /* compose the value */
-                                               uval |= (msbs << parameter);
-                                               *vals = (int)(uval >> 1 ^ -(int)(uval & 1));
-                                               --val_i;
-                                               if(val_i == 0)
-                                                       goto break2;
-                                               ++vals;
-
-                                               msbs = 0;
-                                               state = 0;
-                                       }
-                               }
-                       }
-               }
-break2:
-               bb->consumed_blurbs = i;
-               bb->consumed_bits = cbits;
-               bb->total_consumed_bits = (i << FLAC__BITS_PER_BLURB_LOG2) | cbits;
-               if(val_i != 0) {
-                       if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
-                               return false;
-                       /* these must be zero because we can only get here if we got to the end of the buffer */
-                       FLAC__ASSERT(bb->consumed_blurbs == 0);
-                       FLAC__ASSERT(bb->consumed_bits == 0);
-                       i = 0;
-               }
-       }
-
-       return true;
-}
-#endif
-
-#if 0 /* UNUSED */
-FLAC__bool FLAC__bitbuffer_read_golomb_signed(FLAC__BitBuffer *bb, int *val, unsigned parameter, FLAC__BitbufferReadCallback read_callback, void *client_data)
-{
-       FLAC__uint32 lsbs = 0, msbs = 0;
-       unsigned bit, uval, k;
-
-       FLAC__ASSERT(0 != bb);
-       FLAC__ASSERT(0 != bb->buffer);
-
-       k = FLAC__bitmath_ilog2(parameter);
-
-       /* read the unary MSBs and end bit */
-       if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
-               return false;
-
-       /* read the binary LSBs */
-       if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
-               return false;
-
-       if(parameter == 1u<<k) {
-               /* compose the value */
-               uval = (msbs << k) | lsbs;
-       }
-       else {
-               unsigned d = (1 << (k+1)) - parameter;
-               if(lsbs >= d) {
-                       if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
-                               return false;
-                       lsbs <<= 1;
-                       lsbs |= bit;
-                       lsbs -= d;
-               }
-               /* compose the value */
-               uval = msbs * parameter + lsbs;
-       }
-
-       /* unfold unsigned to signed */
-       if(uval & 1)
-               *val = -((int)(uval >> 1)) - 1;
-       else
-               *val = (int)(uval >> 1);
-
-       return true;
-}
-
-FLAC__bool FLAC__bitbuffer_read_golomb_unsigned(FLAC__BitBuffer *bb, unsigned *val, unsigned parameter, FLAC__BitbufferReadCallback read_callback, void *client_data)
-{
-       FLAC__uint32 lsbs, msbs = 0;
-       unsigned bit, k;
-
-       FLAC__ASSERT(0 != bb);
-       FLAC__ASSERT(0 != bb->buffer);
-
-       k = FLAC__bitmath_ilog2(parameter);
-
-       /* read the unary MSBs and end bit */
-       if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
-               return false;
-
-       /* read the binary LSBs */
-       if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
-               return false;
-
-       if(parameter == 1u<<k) {
-               /* compose the value */
-               *val = (msbs << k) | lsbs;
-       }
-       else {
-               unsigned d = (1 << (k+1)) - parameter;
-               if(lsbs >= d) {
-                       if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
-                               return false;
-                       lsbs <<= 1;
-                       lsbs |= bit;
-                       lsbs -= d;
-               }
-               /* compose the value */
-               *val = msbs * parameter + lsbs;
-       }
-
-       return true;
-}
-#endif /* UNUSED */
-
-/* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */
-FLAC__bool FLAC__bitbuffer_read_utf8_uint32(FLAC__BitBuffer *bb, FLAC__uint32 *val, FLAC__BitbufferReadCallback read_callback, void *client_data, FLAC__byte *raw, unsigned *rawlen)
-{
-       FLAC__uint32 v = 0;
-       FLAC__uint32 x;
-       unsigned i;
-
-       if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
-               return false;
-       if(raw)
-               raw[(*rawlen)++] = (FLAC__byte)x;
-       if(!(x & 0x80)) { /* 0xxxxxxx */
-               v = x;
-               i = 0;
-       }
-       else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
-               v = x & 0x1F;
-               i = 1;
-       }
-       else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
-               v = x & 0x0F;
-               i = 2;
-       }
-       else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
-               v = x & 0x07;
-               i = 3;
-       }
-       else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
-               v = x & 0x03;
-               i = 4;
-       }
-       else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
-               v = x & 0x01;
-               i = 5;
-       }
-       else {
-               *val = 0xffffffff;
-               return true;
-       }
-       for( ; i; i--) {
-               if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
-                       return false;
-               if(raw)
-                       raw[(*rawlen)++] = (FLAC__byte)x;
-               if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
-                       *val = 0xffffffff;
-                       return true;
-               }
-               v <<= 6;
-               v |= (x & 0x3F);
-       }
-       *val = v;
-       return true;
-}
-
-/* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */
-FLAC__bool FLAC__bitbuffer_read_utf8_uint64(FLAC__BitBuffer *bb, FLAC__uint64 *val, FLAC__BitbufferReadCallback read_callback, void *client_data, FLAC__byte *raw, unsigned *rawlen)
-{
-       FLAC__uint64 v = 0;
-       FLAC__uint32 x;
-       unsigned i;
-
-       if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
-               return false;
-       if(raw)
-               raw[(*rawlen)++] = (FLAC__byte)x;
-       if(!(x & 0x80)) { /* 0xxxxxxx */
-               v = x;
-               i = 0;
-       }
-       else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
-               v = x & 0x1F;
-               i = 1;
-       }
-       else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
-               v = x & 0x0F;
-               i = 2;
-       }
-       else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
-               v = x & 0x07;
-               i = 3;
-       }
-       else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
-               v = x & 0x03;
-               i = 4;
-       }
-       else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
-               v = x & 0x01;
-               i = 5;
-       }
-       else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
-               v = 0;
-               i = 6;
-       }
-       else {
-               *val = FLAC__U64L(0xffffffffffffffff);
-               return true;
-       }
-       for( ; i; i--) {
-               if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
-                       return false;
-               if(raw)
-                       raw[(*rawlen)++] = (FLAC__byte)x;
-               if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
-                       *val = FLAC__U64L(0xffffffffffffffff);
-                       return true;
-               }
-               v <<= 6;
-               v |= (x & 0x3F);
-       }
-       *val = v;
-       return true;
-}
-
-void FLAC__bitbuffer_dump(const FLAC__BitBuffer *bb, FILE *out)
-{
-       unsigned i, j;
-       if(bb == 0) {
-               fprintf(out, "bitbuffer is NULL\n");
-       }
-       else {
-               fprintf(out, "bitbuffer: capacity=%u blurbs=%u bits=%u total_bits=%u consumed: blurbs=%u, bits=%u, total_bits=%u\n", bb->capacity, bb->blurbs, bb->bits, bb->total_bits, bb->consumed_blurbs, bb->consumed_bits, bb->total_consumed_bits);
-
-               for(i = 0; i < bb->blurbs; i++) {
-                       fprintf(out, "%08X: ", i);
-                       for(j = 0; j < FLAC__BITS_PER_BLURB; j++)
-                               if(i*FLAC__BITS_PER_BLURB+j < bb->total_consumed_bits)
-                                       fprintf(out, ".");
-                               else
-                                       fprintf(out, "%01u", bb->buffer[i] & (1 << (FLAC__BITS_PER_BLURB-j-1)) ? 1:0);
-                       fprintf(out, "\n");
-               }
-               if(bb->bits > 0) {
-                       fprintf(out, "%08X: ", i);
-                       for(j = 0; j < bb->bits; j++)
-                               if(i*FLAC__BITS_PER_BLURB+j < bb->total_consumed_bits)
-                                       fprintf(out, ".");
-                               else
-                                       fprintf(out, "%01u", bb->buffer[i] & (1 << (bb->bits-j-1)) ? 1:0);
-                       fprintf(out, "\n");
-               }
-       }
-}
diff --git a/src/libFLAC/bitreader.c b/src/libFLAC/bitreader.c
new file mode 100644 (file)
index 0000000..b7e3a32
--- /dev/null
@@ -0,0 +1,1164 @@
+/* libFLAC - Free Lossless Audio Codec library
+ * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007  Josh Coalson
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * - Neither the name of the Xiph.org Foundation nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#if HAVE_CONFIG_H
+#  include <config.h>
+#endif
+
+#include <stdlib.h> /* for malloc() */
+#include <string.h> /* for memcpy(), memset() */
+#if defined(_MSC_VER) && _MSC_VER <= 1200
+#include <winsock.h> /* for ntohl() */
+#else
+#include <netinet/in.h> /* for ntohl() */
+#endif
+#include "private/bitmath.h"
+#include "private/bitreader.h"
+#include "private/crc.h"
+#include "FLAC/assert.h"
+
+/*
+ * Along the way you will see two versions of some functions, selected
+ * by a FLAC__NO_MANUAL_INLINING macro.  One is the simplified, more
+ * readable, and slow version, and the other is the same function
+ * where crucial parts have been manually inlined and are much faster.
+ *
+ */
+
+/* Things should be fastest when this matches the machine word size */
+/* WATCHOUT: if you change this you must also change the following #defines down to ALIGNED_UNARY_BITS below to match */
+/* WATCHOUT: there are a few places where the code will not work unless brword is >= 32 bits wide */
+/*           also, some sections currently only have fast versions for 4 or 8 bytes per word */
+typedef FLAC__uint32 brword;
+#define FLAC__BYTES_PER_WORD 4
+#define FLAC__BITS_PER_WORD 32
+#define FLAC__WORD_ALL_ONES ((FLAC__uint32)0xffffffff)
+#define FLAC__WORD_TOP_BIT_ONE ((FLAC__uint32)0x80000000)
+/* SWAP_BE_WORD_TO_HOST swaps bytes in a brword (which is always big-endian) if necessary to match host byte order */
+#if WORDS_BIGENDIAN
+#define SWAP_BE_WORD_TO_HOST(x) (x)
+#else
+#define SWAP_BE_WORD_TO_HOST(x) ntohl(x)
+#endif
+/* counts the # of zero MSBs in a word */
+#define ALIGNED_UNARY_BITS(word) ( \
+       (word) <= 0xffff ? \
+               ( (word) <= 0xff? byte_to_unary_table[word] + 24 : byte_to_unary_table[(word) >> 8] + 16 ) : \
+               ( (word) <= 0xffffff? byte_to_unary_table[word >> 16] + 8 : byte_to_unary_table[(word) >> 24] ) \
+)
+/* this alternate might be slightly faster on some systems/compilers: */
+#define ALIGNED_UNARY_BITS2(word) ( (word) <= 0xff ? byte_to_unary_table[word] + 24 : ((word) <= 0xffff ? byte_to_unary_table[(word) >> 8] + 16 : ((word) <= 0xffffff ? byte_to_unary_table[(word) >> 16] + 8 : byte_to_unary_table[(word) >> 24])) )
+
+
+/*
+ * This should be at least twice as large as the largest number of words
+ * required to represent any 'number' (in any encoding) you are going to
+ * read.  With FLAC this is on the order of maybe a few hundred bits.
+ * If the buffer is smaller than that, the decoder won't be able to read
+ * in a whole number that is in a variable length encoding (e.g. Rice).
+ * But to be practical it should be at least 1K bytes.
+ *
+ * Increase this number to decrease the number of read callbacks, at the
+ * expense of using more memory.  Or decrease for the reverse effect,
+ * keeping in mind the limit from the first paragraph.  The optimal size
+ * also depends on the CPU cache size and other factors; some twiddling
+ * may be necessary to squeeze out the best performance.
+ */
+static const unsigned FLAC__BITREADER_DEFAULT_CAPACITY = 65536u / FLAC__BITS_PER_WORD; /* in words */
+
+static const unsigned char byte_to_unary_table[] = {
+       8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
+       3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
+       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
+};
+
+#ifdef min
+#undef min
+#endif
+#define min(x,y) ((x)<(y)?(x):(y))
+#ifdef max
+#undef max
+#endif
+#define max(x,y) ((x)>(y)?(x):(y))
+
+/* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
+#ifdef _MSC_VER
+#define FLAC__U64L(x) x
+#else
+#define FLAC__U64L(x) x##LLU
+#endif
+
+#ifndef FLaC__INLINE
+#define FLaC__INLINE
+#endif
+
+struct FLAC__BitReader {
+       /* any partially-consumed word at the head will stay right-justified as bits are consumed from the left */
+       /* any incomplete word at the tail will be left-justified, and bytes from the read callback are added on the right */
+       brword *buffer;
+       unsigned capacity; /* in words */
+       unsigned words; /* # of completed words in buffer */
+       unsigned bytes; /* # of bytes in incomplete word at buffer[words] */
+       unsigned consumed_words, consumed_bits; /* #words+(#bits of head word) already consumed from the front of buffer */
+       unsigned read_crc16; /* the running frame CRC */
+       unsigned crc16_align; /* the number of bits in the current consumed word that should not be CRC'd */
+       FLAC__BitReaderReadCallback read_callback;
+       void *client_data;
+};
+
+static FLaC__INLINE void crc16_update_word_(FLAC__BitReader *br, brword word)
+{
+       register unsigned crc = br->read_crc16;
+#if FLAC__BYTES_PER_WORD == 4
+       switch(br->crc16_align) {
+               case  0: crc = FLAC__CRC16_UPDATE((unsigned)(word >> 24), crc);
+               case  8: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 16) & 0xff), crc);
+               case 16: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 8) & 0xff), crc);
+               case 24: br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)(word & 0xff), crc);
+       }
+#elif FLAC__BYTES_PER_WORD == 8
+       switch(br->crc16_align) {
+               case  0: crc = FLAC__CRC16_UPDATE((unsigned)(word >> 56), crc);
+               case  8: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 48) & 0xff), crc);
+               case 16: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 40) & 0xff), crc);
+               case 24: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 32) & 0xff), crc);
+               case 32: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 24) & 0xff), crc);
+               case 40: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 16) & 0xff), crc);
+               case 48: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 8) & 0xff), crc);
+               case 56: br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)(word & 0xff), crc);
+       }
+#else
+       for( ; br->crc16_align < FLAC__BITS_PER_WORD; br->crc16_align += 8)
+               crc = FLAC__CRC16_UPDATE((unsigned)((word >> (FLAC__BITS_PER_WORD-8-br->crc16_align)) & 0xff), crc);
+       br->read_crc16 = crc;
+#endif
+       br->crc16_align = 0;
+}
+
+static FLAC__bool bitreader_read_from_client_(FLAC__BitReader *br)
+{
+       unsigned start, end;
+       size_t bytes;
+       FLAC__byte *target;
+
+       /* first shift the unconsumed buffer data toward the front as much as possible */
+       if(br->consumed_words > 0) {
+               start = br->consumed_words;
+               end = br->words + (br->bytes? 1:0);
+               memmove(br->buffer, br->buffer+start, FLAC__BYTES_PER_WORD * (end - start));
+
+               br->words -= start;
+               br->consumed_words = 0;
+       }
+
+       /*
+        * set the target for reading, taking into account word alignment and endianness
+        */
+       bytes = (br->capacity - br->words) * FLAC__BYTES_PER_WORD - br->bytes;
+       if(bytes == 0)
+               return false; /* no space left, buffer is too small; see note for FLAC__BITREADER_DEFAULT_CAPACITY  */
+       target = ((FLAC__byte*)(br->buffer+br->words)) + br->bytes;
+
+       /* before reading, if the existing reader looks like this (say brword is 32 bits wide)
+        *   bitstream :  11 22 33 44 55            br->words=1 br->bytes=1 (partial tail word is left-justified)
+        *   buffer[BE]:  11 22 33 44 55 ?? ?? ??   (shown layed out as bytes sequentially in memory)
+        *   buffer[LE]:  44 33 22 11 ?? ?? ?? 55   (?? being don't-care)
+        *                               ^^-------target, bytes=3
+        * on LE machines, have to byteswap the odd tail word so nothing is
+        * overwritten:
+        */
+#if WORDS_BIGENDIAN
+#else
+       if(br->bytes)
+               br->buffer[br->words] = SWAP_BE_WORD_TO_HOST(br->buffer[br->words]);
+#endif
+
+       /* now it looks like:
+        *   bitstream :  11 22 33 44 55            br->words=1 br->bytes=1
+        *   buffer[BE]:  11 22 33 44 55 ?? ?? ??
+        *   buffer[LE]:  44 33 22 11 55 ?? ?? ??
+        *                               ^^-------target, bytes=3
+        */
+
+       /* read in the data; note that the callback may return a smaller number of bytes */
+       if(!br->read_callback(target, &bytes, br->client_data))
+               return false;
+
+       /* after reading bytes 66 77 88 99 AA BB CC DD EE FF from the client:
+        *   bitstream :  11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF
+        *   buffer[BE]:  11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF ??
+        *   buffer[LE]:  44 33 22 11 55 66 77 88 99 AA BB CC DD EE FF ??
+        * now have to byteswap on LE machines:
+        */
+#if WORDS_BIGENDIAN
+#else
+       end = (br->words*FLAC__BYTES_PER_WORD + br->bytes + bytes + (FLAC__BYTES_PER_WORD-1)) / FLAC__BYTES_PER_WORD;
+       for(start = br->words; start < end; start++)
+               br->buffer[start] = SWAP_BE_WORD_TO_HOST(br->buffer[start]);
+#endif
+
+       /* now it looks like:
+        *   bitstream :  11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF
+        *   buffer[BE]:  11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF ??
+        *   buffer[LE]:  44 33 22 11 88 77 66 55 CC BB AA 99 ?? FF EE DD
+        * finally we'll update the reader values:
+        */
+       end = br->words*FLAC__BYTES_PER_WORD + br->bytes + bytes;
+       br->words = end / FLAC__BYTES_PER_WORD;
+       br->bytes = end % FLAC__BYTES_PER_WORD;
+
+       return true;
+}
+
+/***********************************************************************
+ *
+ * Class constructor/destructor
+ *
+ ***********************************************************************/
+
+FLAC__BitReader *FLAC__bitreader_new()
+{
+       FLAC__BitReader *br = (FLAC__BitReader*)calloc(1, sizeof(FLAC__BitReader));
+
+       /* calloc() implies:
+               memset(br, 0, sizeof(FLAC__BitReader));
+               br->buffer = 0;
+               br->capacity = 0;
+               br->words = br->bytes = 0;
+               br->consumed_words = br->consumed_bits = 0;
+               br->read_callback = 0;
+               br->client_data = 0;
+       */
+       return br;
+}
+
+void FLAC__bitreader_delete(FLAC__BitReader *br)
+{
+       FLAC__ASSERT(0 != br);
+
+       FLAC__bitreader_free(br);
+       free(br);
+}
+
+/***********************************************************************
+ *
+ * Public class methods
+ *
+ ***********************************************************************/
+
+FLAC__bool FLAC__bitreader_init(FLAC__BitReader *br, FLAC__BitReaderReadCallback rcb, void *cd)
+{
+       FLAC__ASSERT(0 != br);
+
+       br->words = br->bytes = 0;
+       br->consumed_words = br->consumed_bits = 0;
+       br->capacity = FLAC__BITREADER_DEFAULT_CAPACITY;
+       br->buffer = (brword*)malloc(sizeof(brword) * br->capacity);
+       if(br->buffer == 0)
+               return false;
+       br->read_callback = rcb;
+       br->client_data = cd;
+
+       return true;
+}
+
+void FLAC__bitreader_free(FLAC__BitReader *br)
+{
+       FLAC__ASSERT(0 != br);
+
+       if(0 != br->buffer)
+               free(br->buffer);
+       br->buffer = 0;
+       br->capacity = 0;
+       br->words = br->bytes = 0;
+       br->consumed_words = br->consumed_bits = 0;
+       br->read_callback = 0;
+       br->client_data = 0;
+}
+
+FLAC__bool FLAC__bitreader_clear(FLAC__BitReader *br)
+{
+       br->words = br->bytes = 0;
+       br->consumed_words = br->consumed_bits = 0;
+       return true;
+}
+
+void FLAC__bitreader_dump(const FLAC__BitReader *br, FILE *out)
+{
+       unsigned i, j;
+       if(br == 0) {
+               fprintf(out, "bitreader is NULL\n");
+       }
+       else {
+               fprintf(out, "bitreader: capacity=%u words=%u bytes=%u consumed: words=%u, bits=%u\n", br->capacity, br->words, br->bytes, br->consumed_words, br->consumed_bits);
+
+               for(i = 0; i < br->words; i++) {
+                       fprintf(out, "%08X: ", i);
+                       for(j = 0; j < FLAC__BITS_PER_WORD; j++)
+                               if(i < br->consumed_words || (i == br->consumed_words && j < br->consumed_bits))
+                                       fprintf(out, ".");
+                               else
+                                       fprintf(out, "%01u", br->buffer[i] & (1 << (FLAC__BITS_PER_WORD-j-1)) ? 1:0);
+                       fprintf(out, "\n");
+               }
+               if(br->bytes > 0) {
+                       fprintf(out, "%08X: ", i);
+                       for(j = 0; j < br->bytes*8; j++)
+                               if(i < br->consumed_words || (i == br->consumed_words && j < br->consumed_bits))
+                                       fprintf(out, ".");
+                               else
+                                       fprintf(out, "%01u", br->buffer[i] & (1 << (br->bytes*8-j-1)) ? 1:0);
+                       fprintf(out, "\n");
+               }
+       }
+}
+
+void FLAC__bitreader_reset_read_crc16(FLAC__BitReader *br, FLAC__uint16 seed)
+{
+       FLAC__ASSERT(0 != br);
+       FLAC__ASSERT(0 != br->buffer);
+       FLAC__ASSERT((br->consumed_bits & 7) == 0);
+
+       br->read_crc16 = (unsigned)seed;
+       br->crc16_align = br->consumed_bits;
+}
+
+FLAC__uint16 FLAC__bitreader_get_read_crc16(FLAC__BitReader *br)
+{
+       FLAC__ASSERT(0 != br);
+       FLAC__ASSERT(0 != br->buffer);
+       FLAC__ASSERT((br->consumed_bits & 7) == 0);
+       FLAC__ASSERT(br->crc16_align <= br->consumed_bits);
+
+       /* CRC any tail bytes in a partially-consumed word */
+       if(br->consumed_bits) {
+               const brword tail = br->buffer[br->consumed_words];
+#ifdef DEBUG
+if(br->crc16_align)fprintf(stderr,"@@@@@@ FLAC__bitreader_get_read_crc16() got nonzero crc align = %u\n",br->crc16_align);
+#endif
+               /* non-zero crc align here can probably never happen with FLAC but check for consistency */
+               for( ; br->crc16_align < br->consumed_bits; br->crc16_align += 8)
+                       br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)((tail >> (FLAC__BITS_PER_WORD-8-br->crc16_align)) & 0xff), br->read_crc16);
+       }
+       return br->read_crc16;
+}
+
+FLaC__INLINE FLAC__bool FLAC__bitreader_is_consumed_byte_aligned(const FLAC__BitReader *br)
+{
+       return ((br->consumed_bits & 7) == 0);
+}
+
+FLaC__INLINE unsigned FLAC__bitreader_bits_left_for_byte_alignment(const FLAC__BitReader *br)
+{
+       return 8 - (br->consumed_bits & 7);
+}
+
+FLaC__INLINE unsigned FLAC__bitreader_get_input_bits_unconsumed(const FLAC__BitReader *br)
+{
+       return (br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8 - br->consumed_bits;
+}
+
+FLaC__INLINE FLAC__bool FLAC__bitreader_read_raw_uint32(FLAC__BitReader *br, FLAC__uint32 *val, unsigned bits)
+{
+       FLAC__ASSERT(0 != br);
+       FLAC__ASSERT(0 != br->buffer);
+
+       FLAC__ASSERT(bits <= 32);
+       FLAC__ASSERT((br->capacity*FLAC__BITS_PER_WORD) * 2 >= bits);
+       FLAC__ASSERT(br->consumed_words <= br->words);
+
+       /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
+       FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
+
+       if(bits == 0) { /* OPT: investigate if this can ever happen, maybe change to assertion */
+               *val = 0;
+               return true;
+       }
+
+       while((br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8 - br->consumed_bits < bits) {
+               if(!bitreader_read_from_client_(br))
+                       return false;
+       }
+       if(br->consumed_words < br->words) { /* if we've not consumed up to a partial tail word... */
+               /* OPT: taking out the consumed_bits==0 "else" case below might make things faster if less code allows the compiler to inline this function */
+               if(br->consumed_bits) {
+                       /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
+                       const unsigned n = FLAC__BITS_PER_WORD - br->consumed_bits;
+                       const brword word = br->buffer[br->consumed_words];
+                       if(bits < n) {
+                               *val = (word & (FLAC__WORD_ALL_ONES >> br->consumed_bits)) >> (n-bits);
+                               br->consumed_bits += bits;
+                               return true;
+                       }
+                       *val = word & (FLAC__WORD_ALL_ONES >> br->consumed_bits);
+                       bits -= n;
+                       crc16_update_word_(br, word);
+                       br->consumed_words++;
+                       br->consumed_bits = 0;
+                       if(bits) { /* if there are still bits left to read, there have to be less than 32 so they will all be in the next word */
+                               *val <<= bits;
+                               *val |= (br->buffer[br->consumed_words] >> (FLAC__BITS_PER_WORD-bits));
+                               br->consumed_bits = bits;
+                       }
+                       return true;
+               }
+               else {
+                       const brword word = br->buffer[br->consumed_words];
+                       if(bits < FLAC__BITS_PER_WORD) {
+                               *val = word >> (FLAC__BITS_PER_WORD-bits);
+                               br->consumed_bits = bits;
+                               return true;
+                       }
+                       /* at this point 'bits' must be == FLAC__BITS_PER_WORD; because of previous assertions, it can't be larger */
+                       *val = word;
+                       crc16_update_word_(br, word);
+                       br->consumed_words++;
+                       return true;
+               }
+       }
+       else {
+               /* in this case we're starting our read at a partial tail word;
+                * the reader has guaranteed that we have at least 'bits' bits
+                * available to read, which makes this case simpler.
+                */
+               /* OPT: taking out the consumed_bits==0 "else" case below might make things faster if less code allows the compiler to inline this function */
+               if(br->consumed_bits) {
+                       /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
+                       FLAC__ASSERT(br->consumed_bits + bits <= br->bytes*8);
+                       *val = (br->buffer[br->consumed_words] & (FLAC__WORD_ALL_ONES >> br->consumed_bits)) >> (FLAC__BITS_PER_WORD-br->consumed_bits-bits);
+                       br->consumed_bits += bits;
+                       return true;
+               }
+               else {
+                       *val = br->buffer[br->consumed_words] >> (FLAC__BITS_PER_WORD-bits);
+                       br->consumed_bits += bits;
+                       return true;
+               }
+       }
+}
+
+FLAC__bool FLAC__bitreader_read_raw_int32(FLAC__BitReader *br, FLAC__int32 *val, unsigned bits)
+{
+       /* OPT: inline raw uint32 code here, or make into a macro if possible in the .h file */
+       if(!FLAC__bitreader_read_raw_uint32(br, (FLAC__uint32*)val, bits))
+               return false;
+       /* sign-extend: */
+       *val <<= (32-bits);
+       *val >>= (32-bits);
+       return true;
+}
+
+FLAC__bool FLAC__bitreader_read_raw_uint64(FLAC__BitReader *br, FLAC__uint64 *val, unsigned bits)
+{
+       FLAC__uint32 hi, lo;
+
+       if(bits > 32) {
+               if(!FLAC__bitreader_read_raw_uint32(br, &hi, bits-32))
+                       return false;
+               if(!FLAC__bitreader_read_raw_uint32(br, &lo, 32))
+                       return false;
+               *val = hi;
+               *val <<= 32;
+               *val |= lo;
+       }
+       else {
+               if(!FLAC__bitreader_read_raw_uint32(br, &lo, bits))
+                       return false;
+               *val = lo;
+       }
+       return true;
+}
+
+FLaC__INLINE FLAC__bool FLAC__bitreader_read_uint32_little_endian(FLAC__BitReader *br, FLAC__uint32 *val)
+{
+       FLAC__uint32 x8, x32 = 0;
+
+       /* this doesn't need to be that fast as currently it is only used for vorbis comments */
+
+       if(!FLAC__bitreader_read_raw_uint32(br, &x32, 8))
+               return false;
+
+       if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
+               return false;
+       x32 |= (x8 << 8);
+
+       if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
+               return false;
+       x32 |= (x8 << 16);
+
+       if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
+               return false;
+       x32 |= (x8 << 24);
+
+       *val = x32;
+       return true;
+}
+
+FLAC__bool FLAC__bitreader_skip_bits_no_crc(FLAC__BitReader *br, unsigned bits)
+{
+       /*
+        * OPT: a faster implementation is possible but probably not that useful
+        * since this is only called a couple of times in the metadata readers.
+        */
+       FLAC__ASSERT(0 != br);
+       FLAC__ASSERT(0 != br->buffer);
+
+       if(bits > 0) {
+               const unsigned n = br->consumed_bits & 7;
+               unsigned m;
+               FLAC__uint32 x;
+
+               if(n != 0) {
+                       m = min(8-n, bits);
+                       if(!FLAC__bitreader_read_raw_uint32(br, &x, m))
+                               return false;
+                       bits -= m;
+               }
+               m = bits / 8;
+               if(m > 0) {
+                       if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(br, m))
+                               return false;
+                       bits %= 8;
+               }
+               if(bits > 0) {
+                       if(!FLAC__bitreader_read_raw_uint32(br, &x, bits))
+                               return false;
+               }
+       }
+
+       return true;
+}
+
+FLAC__bool FLAC__bitreader_skip_byte_block_aligned_no_crc(FLAC__BitReader *br, unsigned nvals)
+{
+       FLAC__uint32 x;
+
+       FLAC__ASSERT(0 != br);
+       FLAC__ASSERT(0 != br->buffer);
+       FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(br));
+
+       /* step 1: skip over partial head word to get word aligned */
+       while(nvals && br->consumed_bits) { /* i.e. run until we read 'nvals' bytes or we hit the end of the head word */
+               if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
+                       return false;
+               nvals--;
+       }
+       if(0 == nvals)
+               return true;
+       /* step 2: skip whole words in chunks */
+       while(nvals >= FLAC__BYTES_PER_WORD) {
+               if(br->consumed_words < br->words) {
+                       br->consumed_words++;
+                       nvals -= FLAC__BYTES_PER_WORD;
+               }
+               else if(!bitreader_read_from_client_(br))
+                       return false;
+       }
+       /* step 3: skip any remainder from partial tail bytes */
+       while(nvals) {
+               if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
+                       return false;
+               nvals--;
+       }
+
+       return true;
+}
+
+FLAC__bool FLAC__bitreader_read_byte_block_aligned_no_crc(FLAC__BitReader *br, FLAC__byte *val, unsigned nvals)
+{
+       FLAC__uint32 x;
+
+       FLAC__ASSERT(0 != br);
+       FLAC__ASSERT(0 != br->buffer);
+       FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(br));
+
+       /* step 1: read from partial head word to get word aligned */
+       while(nvals && br->consumed_bits) { /* i.e. run until we read 'nvals' bytes or we hit the end of the head word */
+               if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
+                       return false;
+               *val++ = (FLAC__byte)x;
+               nvals--;
+       }
+       if(0 == nvals)
+               return true;
+       /* step 2: read whole words in chunks */
+       while(nvals >= FLAC__BYTES_PER_WORD) {
+               if(br->consumed_words < br->words) {
+                       const brword word = br->buffer[br->consumed_words++];
+#if FLAC__BYTES_PER_WORD == 4
+                       val[0] = (FLAC__byte)(word >> 24);
+                       val[1] = (FLAC__byte)(word >> 16);
+                       val[2] = (FLAC__byte)(word >> 8);
+                       val[3] = (FLAC__byte)word;
+#elif FLAC__BYTES_PER_WORD == 8
+                       val[0] = (FLAC__byte)(word >> 56);
+                       val[1] = (FLAC__byte)(word >> 48);
+                       val[2] = (FLAC__byte)(word >> 40);
+                       val[3] = (FLAC__byte)(word >> 32);
+                       val[4] = (FLAC__byte)(word >> 24);
+                       val[5] = (FLAC__byte)(word >> 16);
+                       val[6] = (FLAC__byte)(word >> 8);
+                       val[7] = (FLAC__byte)word;
+#else
+                       for(x = 0; x < FLAC__BYTES_PER_WORD; x++)
+                               val[x] = (FLAC__byte)(word >> (8*(FLAC__BYTES_PER_WORD-x-1)));
+#endif
+                       val += FLAC__BYTES_PER_WORD;
+                       nvals -= FLAC__BYTES_PER_WORD;
+               }
+               else if(!bitreader_read_from_client_(br))
+                       return false;
+       }
+       /* step 3: read any remainder from partial tail bytes */
+       while(nvals) {
+               if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
+                       return false;
+               *val++ = (FLAC__byte)x;
+               nvals--;
+       }
+
+       return true;
+}
+
+FLaC__INLINE FLAC__bool FLAC__bitreader_read_unary_unsigned(FLAC__BitReader *br, unsigned *val)
+#ifdef FLAC__NO_MANUAL_INLINING
+{
+       unsigned bit;
+
+       FLAC__ASSERT(0 != br);
+       FLAC__ASSERT(0 != br->buffer);
+
+       *val = 0;
+       while(1) {
+               if(!FLAC__bitreader_read_bit(br, &bit))
+                       return false;
+               if(bit)
+                       break;
+               else
+                       *val++;
+       }
+       return true;
+}
+#else
+{
+       unsigned i;
+
+       FLAC__ASSERT(0 != br);
+       FLAC__ASSERT(0 != br->buffer);
+
+       *val = 0;
+       while(1) {
+               while(br->consumed_words < br->words) { /* if we've not consumed up to a partial tail word... */
+                       brword b = br->buffer[br->consumed_words] << br->consumed_bits;
+                       if(b) {
+#if 0 /* too slow, but this is the idea: */
+                               for(i = 0; !(b & FLAC__WORD_TOP_BIT_ONE); i++)
+                                       b <<= 1;
+#else
+                               i = ALIGNED_UNARY_BITS(b);
+#endif
+                               *val += i;
+                               i++;
+                               br->consumed_bits += i;
+                               if(br->consumed_bits == FLAC__BITS_PER_WORD) {
+                                       crc16_update_word_(br, br->buffer[br->consumed_words]);
+                                       br->consumed_words++;
+                                       br->consumed_bits = 0;
+                               }
+                               return true;
+                       }
+                       else {
+                               *val += FLAC__BITS_PER_WORD - br->consumed_bits;
+                               crc16_update_word_(br, br->buffer[br->consumed_words]);
+                               br->consumed_words++;
+                               br->consumed_bits = 0;
+                               /* didn't find stop bit yet, have to keep going... */
+                       }
+               }
+               /* at this point we've eaten up all the whole words; have to try
+                * reading through any tail bytes before calling the read callback.
+                * this is a repeat of the above logic adjusted for the fact we
+                * don't have a whole word.  note though if the client is feeding
+                * us data a byte at a time (unlikely), br->consumed_bits may not
+                * be zero.
+                */
+               if(br->bytes) {
+                       const unsigned end = br->bytes * 8;
+                       brword b = (br->buffer[br->consumed_words] & (FLAC__WORD_ALL_ONES << (FLAC__BITS_PER_WORD-end))) << br->consumed_bits;
+                       if(b) {
+#if 0 /* too slow, but this is the idea: */
+                               for(i = 0; !(b & FLAC__WORD_TOP_BIT_ONE); i++)
+                                       b <<= 1;
+#else
+                               i = ALIGNED_UNARY_BITS(b);
+#endif
+                               *val += i;
+                               i++;
+                               br->consumed_bits += i;
+                               FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_WORD);
+                               return true;
+                       }
+                       else {
+                               *val += end - br->consumed_bits;
+                               br->consumed_bits += end;
+                               FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_WORD);
+                               /* didn't find stop bit yet, have to keep going... */
+                       }
+               }
+               if(!bitreader_read_from_client_(br))
+                       return false;
+       }
+}
+#endif
+
+FLAC__bool FLAC__bitreader_read_rice_signed(FLAC__BitReader *br, int *val, unsigned parameter)
+{
+       FLAC__uint32 lsbs = 0, msbs = 0;
+       unsigned uval;
+
+       FLAC__ASSERT(0 != br);
+       FLAC__ASSERT(0 != br->buffer);
+       FLAC__ASSERT(parameter <= 31);
+
+       /* read the unary MSBs and end bit */
+       if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
+               return false;
+
+       /* read the binary LSBs */
+       if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, parameter))
+               return false;
+
+       /* compose the value */
+       uval = (msbs << parameter) | lsbs;
+       if(uval & 1)
+               *val = -((int)(uval >> 1)) - 1;
+       else
+               *val = (int)(uval >> 1);
+
+       return true;
+}
+
+/* this is by far the most heavily used reader call.  it ain't pretty but it's fast */
+/* a lot of the logic is copied, then adapted, from FLAC__bitreader_read_unary_unsigned() and FLAC__bitreader_read_raw_uint32() */
+FLAC__bool FLAC__bitreader_read_rice_signed_block(FLAC__BitReader *br, int vals[], unsigned nvals, unsigned parameter)
+{
+       unsigned i;
+       unsigned uval = 0;
+       unsigned bits; /* the # of binary LSBs left to read to finish a rice codeword */
+
+       /* try and get br->consumed_words and br->consumed_bits into register;
+        * must remember to flush them back to *br before calling other
+        * bitwriter functions that use them, and before returning */
+       register unsigned cwords;
+       register unsigned cbits;
+
+       FLAC__ASSERT(0 != br);
+       FLAC__ASSERT(0 != br->buffer);
+       /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
+       FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
+       FLAC__ASSERT(parameter < 32);
+       /* the above two asserts also guarantee that the binary part never straddles more that 2 words, so we don't have to loop to read it */
+
+       if(nvals == 0)
+               return true;
+
+       cbits = br->consumed_bits;
+       cwords = br->consumed_words;
+
+       while(1) {
+
+               /* read unary part */
+               while(1) {
+                       while(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
+                               brword b = br->buffer[cwords] << cbits;
+                               if(b) {
+#if 0 /* too slow, but this is the idea: */
+                                       for(i = 0; !(b & FLAC__WORD_TOP_BIT_ONE); i++)
+                                               b <<= 1;
+#else
+                                       i = ALIGNED_UNARY_BITS(b);
+#endif
+                                       uval += i;
+                                       bits = parameter;
+                                       i++;
+                                       cbits += i;
+                                       if(cbits == FLAC__BITS_PER_WORD) {
+                                               crc16_update_word_(br, br->buffer[cwords]);
+                                               cwords++;
+                                               cbits = 0;
+                                       }
+                                       goto break1;
+                               }
+                               else {
+                                       uval += FLAC__BITS_PER_WORD - cbits;
+                                       crc16_update_word_(br, br->buffer[cwords]);
+                                       cwords++;
+                                       cbits = 0;
+                                       /* didn't find stop bit yet, have to keep going... */
+                               }
+                       }
+                       /* at this point we've eaten up all the whole words; have to try
+                        * reading through any tail bytes before calling the read callback.
+                        * this is a repeat of the above logic adjusted for the fact we
+                        * don't have a whole word.  note though if the client is feeding
+                        * us data a byte at a time (unlikely), br->consumed_bits may not
+                        * be zero.
+                        */
+                       if(br->bytes) {
+                               const unsigned end = br->bytes * 8;
+                               brword b = (br->buffer[cwords] & (FLAC__WORD_ALL_ONES << (FLAC__BITS_PER_WORD-end))) << cbits;
+                               if(b) {
+#if 0 /* too slow, but this is the idea: */
+                                       for(i = 0; !(b & FLAC__WORD_TOP_BIT_ONE); i++)
+                                               b <<= 1;
+#else
+                                       i = ALIGNED_UNARY_BITS(b);
+#endif
+                                       uval += i;
+                                       bits = parameter;
+                                       i++;
+                                       cbits += i;
+                                       FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
+                                       goto break1;
+                               }
+                               else {
+                                       uval += end - cbits;
+                                       cbits += end;
+                                       FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
+                                       /* didn't find stop bit yet, have to keep going... */
+                               }
+                       }
+                       /* flush registers and read; bitreader_read_from_client_() does
+                        * not touch br->consumed_bits at all but we still need to set
+                        * it in case it fails and we have to return false.
+                        */
+                       br->consumed_bits = cbits;
+                       br->consumed_words = cwords;
+                       if(!bitreader_read_from_client_(br))
+                               return false;
+                       cwords = br->consumed_words;
+               }
+break1:
+               /* read binary part */
+               FLAC__ASSERT(cwords <= br->words);
+
+               if(bits) {
+                       while((br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits < bits) {
+                               /* flush registers and read; bitreader_read_from_client_() does
+                                * not touch br->consumed_bits at all but we still need to set
+                                * it in case it fails and we have to return false.
+                                */
+                               br->consumed_bits = cbits;
+                               br->consumed_words = cwords;
+                               if(!bitreader_read_from_client_(br))
+                                       return false;
+                               cwords = br->consumed_words;
+                       }
+                       if(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
+                               if(cbits) {
+                                       /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
+                                       const unsigned n = FLAC__BITS_PER_WORD - cbits;
+                                       const brword word = br->buffer[cwords];
+                                       if(bits < n) {
+                                               uval <<= bits;
+                                               uval |= (word & (FLAC__WORD_ALL_ONES >> cbits)) >> (n-bits);
+                                               cbits += bits;
+                                               goto break2;
+                                       }
+                                       uval <<= n;
+                                       uval |= word & (FLAC__WORD_ALL_ONES >> cbits);
+                                       bits -= n;
+                                       crc16_update_word_(br, word);
+                                       cwords++;
+                                       cbits = 0;
+                                       if(bits) { /* if there are still bits left to read, there have to be less than 32 so they will all be in the next word */
+                                               uval <<= bits;
+                                               uval |= (br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits));
+                                               cbits = bits;
+                                       }
+                                       goto break2;
+                               }
+                               else {
+                                       FLAC__ASSERT(bits < FLAC__BITS_PER_WORD);
+                                       uval <<= bits;
+                                       uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits);
+                                       cbits = bits;
+                                       goto break2;
+                               }
+                       }
+                       else {
+                               /* in this case we're starting our read at a partial tail word;
+                                * the reader has guaranteed that we have at least 'bits' bits
+                                * available to read, which makes this case simpler.
+                                */
+                               uval <<= bits;
+                               if(cbits) {
+                                       /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
+                                       FLAC__ASSERT(cbits + bits <= br->bytes*8);
+                                       uval |= (br->buffer[cwords] & (FLAC__WORD_ALL_ONES >> cbits)) >> (FLAC__BITS_PER_WORD-cbits-bits);
+                                       cbits += bits;
+                                       goto break2;
+                               }
+                               else {
+                                       uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits);
+                                       cbits += bits;
+                                       goto break2;
+                               }
+                       }
+               }
+break2:
+               /* compose the value */
+               *vals = (int)(uval >> 1 ^ -(int)(uval & 1));
+
+               /* are we done? */
+               --nvals;
+               if(nvals == 0) {
+                       br->consumed_bits = cbits;
+                       br->consumed_words = cwords;
+                       return true;
+               }
+
+               uval = 0;
+               ++vals;
+
+       }
+}
+
+#if 0 /* UNUSED */
+FLAC__bool FLAC__bitreader_read_golomb_signed(FLAC__BitReader *br, int *val, unsigned parameter)
+{
+       FLAC__uint32 lsbs = 0, msbs = 0;
+       unsigned bit, uval, k;
+
+       FLAC__ASSERT(0 != br);
+       FLAC__ASSERT(0 != br->buffer);
+
+       k = FLAC__bitmath_ilog2(parameter);
+
+       /* read the unary MSBs and end bit */
+       if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
+               return false;
+
+       /* read the binary LSBs */
+       if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, k))
+               return false;
+
+       if(parameter == 1u<<k) {
+               /* compose the value */
+               uval = (msbs << k) | lsbs;
+       }
+       else {
+               unsigned d = (1 << (k+1)) - parameter;
+               if(lsbs >= d) {
+                       if(!FLAC__bitreader_read_bit(br, &bit))
+                               return false;
+                       lsbs <<= 1;
+                       lsbs |= bit;
+                       lsbs -= d;
+               }
+               /* compose the value */
+               uval = msbs * parameter + lsbs;
+       }
+
+       /* unfold unsigned to signed */
+       if(uval & 1)
+               *val = -((int)(uval >> 1)) - 1;
+       else
+               *val = (int)(uval >> 1);
+
+       return true;
+}
+
+FLAC__bool FLAC__bitreader_read_golomb_unsigned(FLAC__BitReader *br, unsigned *val, unsigned parameter)
+{
+       FLAC__uint32 lsbs, msbs = 0;
+       unsigned bit, k;
+
+       FLAC__ASSERT(0 != br);
+       FLAC__ASSERT(0 != br->buffer);
+
+       k = FLAC__bitmath_ilog2(parameter);
+
+       /* read the unary MSBs and end bit */
+       if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
+               return false;
+
+       /* read the binary LSBs */
+       if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, k))
+               return false;
+
+       if(parameter == 1u<<k) {
+               /* compose the value */
+               *val = (msbs << k) | lsbs;
+       }
+       else {
+               unsigned d = (1 << (k+1)) - parameter;
+               if(lsbs >= d) {
+                       if(!FLAC__bitreader_read_bit(br, &bit))
+                               return false;
+                       lsbs <<= 1;
+                       lsbs |= bit;
+                       lsbs -= d;
+               }
+               /* compose the value */
+               *val = msbs * parameter + lsbs;
+       }
+
+       return true;
+}
+#endif /* UNUSED */
+
+/* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */
+FLAC__bool FLAC__bitreader_read_utf8_uint32(FLAC__BitReader *br, FLAC__uint32 *val, FLAC__byte *raw, unsigned *rawlen)
+{
+       FLAC__uint32 v = 0;
+       FLAC__uint32 x;
+       unsigned i;
+
+       if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
+               return false;
+       if(raw)
+               raw[(*rawlen)++] = (FLAC__byte)x;
+       if(!(x & 0x80)) { /* 0xxxxxxx */
+               v = x;
+               i = 0;
+       }
+       else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
+               v = x & 0x1F;
+               i = 1;
+       }
+       else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
+               v = x & 0x0F;
+               i = 2;
+       }
+       else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
+               v = x & 0x07;
+               i = 3;
+       }
+       else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
+               v = x & 0x03;
+               i = 4;
+       }
+       else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
+               v = x & 0x01;
+               i = 5;
+       }
+       else {
+               *val = 0xffffffff;
+               return true;
+       }
+       for( ; i; i--) {
+               if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
+                       return false;
+               if(raw)
+                       raw[(*rawlen)++] = (FLAC__byte)x;
+               if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
+                       *val = 0xffffffff;
+                       return true;
+               }
+               v <<= 6;
+               v |= (x & 0x3F);
+       }
+       *val = v;
+       return true;
+}
+
+/* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */
+FLAC__bool FLAC__bitreader_read_utf8_uint64(FLAC__BitReader *br, FLAC__uint64 *val, FLAC__byte *raw, unsigned *rawlen)
+{
+       FLAC__uint64 v = 0;
+       FLAC__uint32 x;
+       unsigned i;
+
+       if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
+               return false;
+       if(raw)
+               raw[(*rawlen)++] = (FLAC__byte)x;
+       if(!(x & 0x80)) { /* 0xxxxxxx */
+               v = x;
+               i = 0;
+       }
+       else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
+               v = x & 0x1F;
+               i = 1;
+       }
+       else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
+               v = x & 0x0F;
+               i = 2;
+       }
+       else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
+               v = x & 0x07;
+               i = 3;
+       }
+       else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
+               v = x & 0x03;
+               i = 4;
+       }
+       else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
+               v = x & 0x01;
+               i = 5;
+       }
+       else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
+               v = 0;
+               i = 6;
+       }
+       else {
+               *val = FLAC__U64L(0xffffffffffffffff);
+               return true;
+       }
+       for( ; i; i--) {
+               if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
+                       return false;
+               if(raw)
+                       raw[(*rawlen)++] = (FLAC__byte)x;
+               if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
+                       *val = FLAC__U64L(0xffffffffffffffff);
+                       return true;
+               }
+               v <<= 6;
+               v |= (x & 0x3F);
+       }
+       *val = v;
+       return true;
+}
diff --git a/src/libFLAC/bitwriter.c b/src/libFLAC/bitwriter.c
new file mode 100644 (file)
index 0000000..953217d
--- /dev/null
@@ -0,0 +1,845 @@
+/* libFLAC - Free Lossless Audio Codec library
+ * Copyright (C) 2000,2001,2002,2003,2004,2005,2006  Josh Coalson
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * - Neither the name of the Xiph.org Foundation nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#if HAVE_CONFIG_H
+#  include <config.h>
+#endif
+
+#include <stdlib.h> /* for malloc() */
+#include <string.h> /* for memcpy(), memset() */
+#if defined(_MSC_VER) && _MSC_VER <= 1200
+#include <winsock.h> /* for ntohl() */
+#else
+#include <netinet/in.h> /* for ntohl() */
+#endif
+#if 0 /* UNUSED */
+#include "private/bitmath.h"
+#endif
+#include "private/bitwriter.h"
+#include "private/crc.h"
+#include "FLAC/assert.h"
+
+/* Things should be fastest when this matches the machine word size */
+/* WATCHOUT: if you change this you must also change the following #defines down to SWAP_BE_WORD_TO_HOST below to match */
+/* WATCHOUT: there are a few places where the code will not work unless bwword is >= 32 bits wide */
+typedef FLAC__uint32 bwword;
+#define FLAC__BYTES_PER_WORD 4
+#define FLAC__BITS_PER_WORD 32
+#define FLAC__WORD_ALL_ONES ((FLAC__uint32)0xffffffff)
+/* SWAP_BE_WORD_TO_HOST swaps bytes in a bwword (which is always big-endian) if necessary to match host byte order */
+#if WORDS_BIGENDIAN
+#define SWAP_BE_WORD_TO_HOST(x) (x)
+#else
+#define SWAP_BE_WORD_TO_HOST(x) ntohl(x)
+#endif
+
+/*
+ * The default capacity here doesn't matter too much.  The buffer always grows
+ * to hold whatever is written to it.  Usually the encoder will stop adding at
+ * a frame or metadata block, then write that out and clear the buffer for the
+ * next one.
+ */
+static const unsigned FLAC__BITWRITER_DEFAULT_CAPACITY = 32768u / sizeof(bwword); /* size in words */
+/* When growing, increment 4K at a time */
+static const unsigned FLAC__BITWRITER_DEFAULT_INCREMENT = 4096u / sizeof(bwword); /* size in words */
+
+#define FLAC__WORDS_TO_BITS(words) ((words) * FLAC__BITS_PER_WORD)
+#define FLAC__TOTAL_BITS(bw) (FLAC__WORDS_TO_BITS((bw)->words) + (bw)->bits)
+
+#ifdef min
+#undef min
+#endif
+#define min(x,y) ((x)<(y)?(x):(y))
+
+/* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
+#ifdef _MSC_VER
+#define FLAC__U64L(x) x
+#else
+#define FLAC__U64L(x) x##LLU
+#endif
+
+#ifndef FLaC__INLINE
+#define FLaC__INLINE
+#endif
+
+struct FLAC__BitWriter {
+       bwword *buffer;
+       bwword accum; /* accumulator; bits are right-justified; when full, accum is appended to buffer */
+       unsigned capacity; /* capacity of buffer in words */
+       unsigned words; /* # of complete words in buffer */
+       unsigned bits; /* # of used bits in accum */
+};
+
+/* * WATCHOUT: The current implementation only grows the buffer. */
+static FLAC__bool bitwriter_grow_(FLAC__BitWriter *bw, unsigned bits_to_add)
+{
+       unsigned new_capacity;
+       bwword *new_buffer;
+
+       FLAC__ASSERT(0 != bw);
+       FLAC__ASSERT(0 != bw->buffer);
+
+       /* calculate total words needed to store 'bits_to_add' additional bits */
+       new_capacity = bw->words + ((bw->bits + bits_to_add + FLAC__BITS_PER_WORD - 1) / FLAC__BITS_PER_WORD);
+
+       /* it's possible (due to pessimism in the growth estimation that
+        * leads to this call) that we don't actually need to grow
+        */
+       if(bw->capacity >= new_capacity)
+               return true;
+
+       /* round up capacity increase to the nearest FLAC__BITWRITER_DEFAULT_INCREMENT */
+       if((new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT)
+               new_capacity += FLAC__BITWRITER_DEFAULT_INCREMENT - ((new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT);
+       /* make sure we got everything right */
+       FLAC__ASSERT(0 == (new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT);
+       FLAC__ASSERT(new_capacity > bw->capacity);
+       FLAC__ASSERT(new_capacity >= bw->words + ((bw->bits + bits_to_add + FLAC__BITS_PER_WORD - 1) / FLAC__BITS_PER_WORD));
+
+       new_buffer = (bwword*)realloc(bw->buffer, sizeof(bwword)*new_capacity);
+       if(new_buffer == 0)
+               return false;
+       bw->buffer = new_buffer;
+       bw->capacity = new_capacity;
+       return true;
+}
+
+
+/***********************************************************************
+ *
+ * Class constructor/destructor
+ *
+ ***********************************************************************/
+
+FLAC__BitWriter *FLAC__bitwriter_new()
+{
+       FLAC__BitWriter *bw = (FLAC__BitWriter*)calloc(1, sizeof(FLAC__BitWriter));
+       /* note that calloc() sets all members to 0 for us */
+       return bw;
+}
+
+void FLAC__bitwriter_delete(FLAC__BitWriter *bw)
+{
+       FLAC__ASSERT(0 != bw);
+
+       FLAC__bitwriter_free(bw);
+       free(bw);
+}
+
+/***********************************************************************
+ *
+ * Public class methods
+ *
+ ***********************************************************************/
+
+FLAC__bool FLAC__bitwriter_init(FLAC__BitWriter *bw)
+{
+       FLAC__ASSERT(0 != bw);
+
+       bw->words = bw->bits = 0;
+       bw->capacity = FLAC__BITWRITER_DEFAULT_CAPACITY;
+       bw->buffer = (bwword*)malloc(sizeof(bwword) * bw->capacity);
+       if(bw->buffer == 0)
+               return false;
+
+       return true;
+}
+
+void FLAC__bitwriter_free(FLAC__BitWriter *bw)
+{
+       FLAC__ASSERT(0 != bw);
+
+       if(0 != bw->buffer)
+               free(bw->buffer);
+       bw->buffer = 0;
+       bw->capacity = 0;
+       bw->words = bw->bits = 0;
+}
+
+void FLAC__bitwriter_clear(FLAC__BitWriter *bw)
+{
+       bw->words = bw->bits = 0;
+}
+
+void FLAC__bitwriter_dump(const FLAC__BitWriter *bw, FILE *out)
+{
+       unsigned i, j;
+       if(bw == 0) {
+               fprintf(out, "bitwriter is NULL\n");
+       }
+       else {
+               fprintf(out, "bitwriter: capacity=%u words=%u bits=%u total_bits=%u\n", bw->capacity, bw->words, bw->bits, FLAC__TOTAL_BITS(bw));
+
+               for(i = 0; i < bw->words; i++) {
+                       fprintf(out, "%08X: ", i);
+                       for(j = 0; j < FLAC__BITS_PER_WORD; j++)
+                               fprintf(out, "%01u", bw->buffer[i] & (1 << (FLAC__BITS_PER_WORD-j-1)) ? 1:0);
+                       fprintf(out, "\n");
+               }
+               if(bw->bits > 0) {
+                       fprintf(out, "%08X: ", i);
+                       for(j = 0; j < bw->bits; j++)
+                               fprintf(out, "%01u", bw->accum & (1 << (bw->bits-j-1)) ? 1:0);
+                       fprintf(out, "\n");
+               }
+       }
+}
+
+FLAC__bool FLAC__bitwriter_get_write_crc16(FLAC__BitWriter *bw, FLAC__uint16 *crc)
+{
+       const FLAC__byte *buffer;
+       size_t bytes;
+
+       FLAC__ASSERT((bw->bits & 7) == 0); /* assert that we're byte-aligned */
+
+       if(!FLAC__bitwriter_get_buffer(bw, &buffer, &bytes))
+               return false;
+
+       *crc = (FLAC__uint16)FLAC__crc16(buffer, bytes);
+       FLAC__bitwriter_release_buffer(bw);
+       return true;
+}
+
+FLAC__bool FLAC__bitwriter_get_write_crc8(FLAC__BitWriter *bw, FLAC__byte *crc)
+{
+       const FLAC__byte *buffer;
+       size_t bytes;
+
+       FLAC__ASSERT((bw->bits & 7) == 0); /* assert that we're byte-aligned */
+
+       if(!FLAC__bitwriter_get_buffer(bw, &buffer, &bytes))
+               return false;
+
+       *crc = FLAC__crc8(buffer, bytes);
+       FLAC__bitwriter_release_buffer(bw);
+       return true;
+}
+
+FLAC__bool FLAC__bitwriter_is_byte_aligned(const FLAC__BitWriter *bw)
+{
+       return ((bw->bits & 7) == 0);
+}
+
+unsigned FLAC__bitwriter_get_input_bits_unconsumed(const FLAC__BitWriter *bw)
+{
+       return FLAC__TOTAL_BITS(bw);
+}
+
+FLAC__bool FLAC__bitwriter_get_buffer(FLAC__BitWriter *bw, const FLAC__byte **buffer, size_t *bytes)
+{
+       FLAC__ASSERT((bw->bits & 7) == 0);
+       /* double protection */
+       if(bw->bits & 7)
+               return false;
+       /* if we have bits in the accumulator we have to flush those to the buffer first */
+       if(bw->bits) {
+#ifdef DEBUG
+if(bw->words == bw->capacity)fprintf(stderr,"@@@@@@ DEBUG:resizing in FLAC__bitwriter_get_buffer()\n");
+#endif
+               FLAC__ASSERT(bw->words <= bw->capacity);
+               if(bw->words == bw->capacity && !bitwriter_grow_(bw, FLAC__BITS_PER_WORD))
+                       return false;
+               /* append bits as complete word to buffer, but don't change bw->accum or bw->bits */
+               bw->buffer[bw->words] = SWAP_BE_WORD_TO_HOST(bw->accum << (FLAC__BITS_PER_WORD-bw->bits));
+       }
+       /* now we can just return what we have */
+       *buffer = (FLAC__byte*)bw->buffer;
+       *bytes = (FLAC__BYTES_PER_WORD * bw->words) + (bw->bits >> 3);
+       return true;
+}
+
+void FLAC__bitwriter_release_buffer(FLAC__BitWriter *bw)
+{
+       /* nothing to do.  in the future, strict checking of a 'writer-is-in-
+        * get-mode' flag could be added everywhere and then cleared here
+        */
+       (void)bw;
+}
+
+FLaC__INLINE FLAC__bool FLAC__bitwriter_write_zeroes(FLAC__BitWriter *bw, unsigned bits)
+{
+       unsigned n;
+
+       FLAC__ASSERT(0 != bw);
+       FLAC__ASSERT(0 != bw->buffer);
+
+       if(bits == 0)
+               return true;
+       /* slightly pessimistic size check but faster than "<= bw->words + (bw->bits+bits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */
+       if(bw->capacity <= bw->words + bits && !bitwriter_grow_(bw, bits))
+               return false;
+       /* first part gets to word alignment */
+       if(bw->bits) {
+               n = min(FLAC__BITS_PER_WORD - bw->bits, bits);
+               bw->accum <<= n;
+               bits -= n;
+               bw->bits += n;
+               if(bw->bits == FLAC__BITS_PER_WORD) {
+                       bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
+                       bw->bits = 0;
+               }
+               else
+                       return true;
+       }
+       /* do whole words */
+       while(bits >= FLAC__BITS_PER_WORD) {
+               bw->buffer[bw->words++] = 0;
+               bits -= FLAC__BITS_PER_WORD;
+       }
+       /* do any leftovers */
+       if(bits > 0) {
+               bw->accum = 0;
+               bw->bits = bits;
+       }
+       return true;
+}
+
+FLaC__INLINE FLAC__bool FLAC__bitwriter_write_raw_uint32(FLAC__BitWriter *bw, FLAC__uint32 val, unsigned bits)
+{
+       register unsigned left;
+
+       /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
+       FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
+
+       FLAC__ASSERT(0 != bw);
+       FLAC__ASSERT(0 != bw->buffer);
+
+       FLAC__ASSERT(bits <= 32);
+       if(bits == 0)
+               return true;
+
+       /* slightly pessimistic size check but faster than "<= bw->words + (bw->bits+bits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */
+       if(bw->capacity <= bw->words + bits && !bitwriter_grow_(bw, bits))
+               return false;
+
+       left = FLAC__BITS_PER_WORD - bw->bits;
+       if(bits < left) {
+               bw->accum <<= bits;
+               bw->accum |= val;
+               bw->bits += bits;
+       }
+       else if(bw->bits) { /* WATCHOUT: if bw->bits == 0, left==FLAC__BITS_PER_WORD and bw->accum<<=left is a NOP instead of setting to 0 */
+#ifdef DEBUG
+if(left>=FLAC__BITS_PER_WORD)fprintf(stderr,"@@@@@@ bitwriter shift error @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
+#endif
+               bw->accum <<= left;
+               bw->accum |= val >> (bw->bits = bits - left);
+               bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
+               bw->accum = val;
+       }
+       else {
+#ifdef DEBUG
+if(bits!=left)fprintf(stderr,"@@@@@@ bitwriter shift error2 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
+#endif
+               bw->accum = val;
+               bw->bits = 0;
+               bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(val);
+       }
+
+       return true;
+}
+
+FLaC__INLINE FLAC__bool FLAC__bitwriter_write_raw_int32(FLAC__BitWriter *bw, FLAC__int32 val, unsigned bits)
+{
+       /* zero-out unused bits */
+       if(bits < 32)
+               val &= (~(0xffffffff << bits));
+
+       return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, bits);
+}
+
+FLaC__INLINE FLAC__bool FLAC__bitwriter_write_raw_uint64(FLAC__BitWriter *bw, FLAC__uint64 val, unsigned bits)
+{
+       /* this could be a little faster but it's not used for much */
+       if(bits > 32) {
+               return
+                       FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)(val>>32), bits-32) &&
+                       FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, 32);
+       }
+       else
+               return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, bits);
+}
+
+FLaC__INLINE FLAC__bool FLAC__bitwriter_write_raw_uint32_little_endian(FLAC__BitWriter *bw, FLAC__uint32 val)
+{
+       /* this doesn't need to be that fast as currently it is only used for vorbis comments */
+
+       if(!FLAC__bitwriter_write_raw_uint32(bw, val & 0xff, 8))
+               return false;
+       if(!FLAC__bitwriter_write_raw_uint32(bw, (val>>8) & 0xff, 8))
+               return false;
+       if(!FLAC__bitwriter_write_raw_uint32(bw, (val>>16) & 0xff, 8))
+               return false;
+       if(!FLAC__bitwriter_write_raw_uint32(bw, val>>24, 8))
+               return false;
+
+       return true;
+}
+
+FLaC__INLINE FLAC__bool FLAC__bitwriter_write_byte_block(FLAC__BitWriter *bw, const FLAC__byte vals[], unsigned nvals)
+{
+       unsigned i;
+
+       /* this could be faster but currently we don't need it to be since it's only used for writing metadata */
+       for(i = 0; i < nvals; i++) {
+               if(!FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)(vals[i]), 8))
+                       return false;
+       }
+
+       return true;
+}
+
+FLAC__bool FLAC__bitwriter_write_unary_unsigned(FLAC__BitWriter *bw, unsigned val)
+{
+       if(val < 32)
+               return FLAC__bitwriter_write_raw_uint32(bw, 1, ++val);
+       else
+               return
+                       FLAC__bitwriter_write_zeroes(bw, val) &&
+                       FLAC__bitwriter_write_raw_uint32(bw, 1, 1);
+}
+
+unsigned FLAC__bitwriter_rice_bits(FLAC__int32 val, unsigned parameter)
+{
+       FLAC__uint32 uval;
+
+       FLAC__ASSERT(parameter < sizeof(unsigned)*8);
+
+       /* fold signed to unsigned; actual formula is: negative(v)? -2v-1 : 2v */
+       uval = (val<<1) ^ (val>>31);
+
+       return 1 + parameter + (uval >> parameter);
+}
+
+#if 0 /* UNUSED */
+unsigned FLAC__bitwriter_golomb_bits_signed(int val, unsigned parameter)
+{
+       unsigned bits, msbs, uval;
+       unsigned k;
+
+       FLAC__ASSERT(parameter > 0);
+
+       /* fold signed to unsigned */
+       if(val < 0)
+               uval = (unsigned)(((-(++val)) << 1) + 1);
+       else
+               uval = (unsigned)(val << 1);
+
+       k = FLAC__bitmath_ilog2(parameter);
+       if(parameter == 1u<<k) {
+               FLAC__ASSERT(k <= 30);
+
+               msbs = uval >> k;
+               bits = 1 + k + msbs;
+       }
+       else {
+               unsigned q, r, d;
+
+               d = (1 << (k+1)) - parameter;
+               q = uval / parameter;
+               r = uval - (q * parameter);
+
+               bits = 1 + q + k;
+               if(r >= d)
+                       bits++;
+       }
+       return bits;
+}
+
+unsigned FLAC__bitwriter_golomb_bits_unsigned(unsigned uval, unsigned parameter)
+{
+       unsigned bits, msbs;
+       unsigned k;
+
+       FLAC__ASSERT(parameter > 0);
+
+       k = FLAC__bitmath_ilog2(parameter);
+       if(parameter == 1u<<k) {
+               FLAC__ASSERT(k <= 30);
+
+               msbs = uval >> k;
+               bits = 1 + k + msbs;
+       }
+       else {
+               unsigned q, r, d;
+
+               d = (1 << (k+1)) - parameter;
+               q = uval / parameter;
+               r = uval - (q * parameter);
+
+               bits = 1 + q + k;
+               if(r >= d)
+                       bits++;
+       }
+       return bits;
+}
+#endif /* UNUSED */
+
+FLAC__bool FLAC__bitwriter_write_rice_signed(FLAC__BitWriter *bw, FLAC__int32 val, unsigned parameter)
+{
+       unsigned total_bits, interesting_bits, msbs;
+       FLAC__uint32 uval, pattern;
+
+       FLAC__ASSERT(0 != bw);
+       FLAC__ASSERT(0 != bw->buffer);
+       FLAC__ASSERT(parameter < 8*sizeof(uval));
+
+       /* fold signed to unsigned; actual formula is: negative(v)? -2v-1 : 2v */
+       uval = (val<<1) ^ (val>>31);
+
+       msbs = uval >> parameter;
+       interesting_bits = 1 + parameter;
+       total_bits = interesting_bits + msbs;
+       pattern = 1 << parameter; /* the unary end bit */
+       pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
+
+       if(total_bits <= 32)
+               return FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits);
+       else
+               return
+                       FLAC__bitwriter_write_zeroes(bw, msbs) && /* write the unary MSBs */
+                       FLAC__bitwriter_write_raw_uint32(bw, pattern, interesting_bits); /* write the unary end bit and binary LSBs */
+}
+
+FLAC__bool FLAC__bitwriter_write_rice_signed_block(FLAC__BitWriter *bw, const FLAC__int32 *vals, unsigned nvals, unsigned parameter)
+{
+       const FLAC__uint32 mask1 = FLAC__WORD_ALL_ONES << parameter; /* we val|=mask1 to set the stop bit above it... */
+       const FLAC__uint32 mask2 = FLAC__WORD_ALL_ONES >> (31-parameter); /* ...then mask off the bits above the stop bit with val&=mask2*/
+       FLAC__uint32 uval;
+       register unsigned left;
+       const unsigned lsbits = 1 + parameter;
+       unsigned msbits;
+
+       FLAC__ASSERT(0 != bw);
+       FLAC__ASSERT(0 != bw->buffer);
+       FLAC__ASSERT(parameter < 8*sizeof(bwword)-1);
+       /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
+       FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
+
+       while(nvals) {
+               /* fold signed to unsigned; actual formula is: negative(v)? -2v-1 : 2v */
+        uval = (*vals<<1) ^ (*vals>>31);
+
+               msbits = uval >> parameter;
+
+               /* slightly pessimistic size check but faster than "<= bw->words + (bw->bits+msbits+lsbits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */
+               /* OPT: pessimism may cause flurry of false calls to grow_ which eat up all savings before it */
+               if(bw->capacity <= bw->words + bw->bits + msbits + lsbits && !bitwriter_grow_(bw, msbits+lsbits))
+                       return false;
+
+               if(msbits) {
+                       /* first part gets to word alignment */
+                       if(bw->bits) {
+                               left = min(FLAC__BITS_PER_WORD - bw->bits, msbits);
+                               bw->accum <<= left;
+                               msbits -= left;
+                               bw->bits += left;
+                               if(bw->bits == FLAC__BITS_PER_WORD) {
+                                       bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
+                                       bw->bits = 0;
+                               }
+                               else
+                                       goto break1;
+                       }
+                       /* do whole words */
+                       while(msbits >= FLAC__BITS_PER_WORD) {
+                               bw->buffer[bw->words++] = 0;
+                               msbits -= FLAC__BITS_PER_WORD;
+                       }
+                       /* do any leftovers */
+                       if(msbits > 0) {
+                               bw->accum = 0;
+                               bw->bits = msbits;
+                       }
+               }
+break1:
+               uval |= mask1; /* set stop bit */
+               uval &= mask2; /* mask off unused top bits */
+
+               left = FLAC__BITS_PER_WORD - bw->bits;
+               if(lsbits < left) {
+                       bw->accum <<= lsbits;
+                       bw->accum |= uval;
+                       bw->bits += lsbits;
+               }
+               else {
+                       /* if bw->bits == 0, left==FLAC__BITS_PER_WORD which will always
+                        * be > lsbits (because of previous assertions) so it would have
+                        * triggered the (lsbits<left) case above.
+                        */
+                       FLAC__ASSERT(bw->bits);
+#ifdef DEBUG
+if(left>=FLAC__BITS_PER_WORD)fprintf(stderr,"@@@@@@ bitwriter shift error @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
+#endif
+                       bw->accum <<= left;
+                       bw->accum |= uval >> (bw->bits = lsbits - left);
+                       bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
+                       bw->accum = uval;
+               }
+               vals++;
+               nvals--;
+       }
+       return true;
+}
+
+#if 0 /* UNUSED */
+FLAC__bool FLAC__bitwriter_write_golomb_signed(FLAC__BitWriter *bw, int val, unsigned parameter)
+{
+       unsigned total_bits, msbs, uval;
+       unsigned k;
+
+       FLAC__ASSERT(0 != bw);
+       FLAC__ASSERT(0 != bw->buffer);
+       FLAC__ASSERT(parameter > 0);
+
+       /* fold signed to unsigned */
+       if(val < 0)
+               uval = (unsigned)(((-(++val)) << 1) + 1);
+       else
+               uval = (unsigned)(val << 1);
+
+       k = FLAC__bitmath_ilog2(parameter);
+       if(parameter == 1u<<k) {
+               unsigned pattern;
+
+               FLAC__ASSERT(k <= 30);
+
+               msbs = uval >> k;
+               total_bits = 1 + k + msbs;
+               pattern = 1 << k; /* the unary end bit */
+               pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
+
+               if(total_bits <= 32) {
+                       if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits))
+                               return false;
+               }
+               else {
+                       /* write the unary MSBs */
+                       if(!FLAC__bitwriter_write_zeroes(bw, msbs))
+                               return false;
+                       /* write the unary end bit and binary LSBs */
+                       if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, k+1))
+                               return false;
+               }
+       }
+       else {
+               unsigned q, r, d;
+
+               d = (1 << (k+1)) - parameter;
+               q = uval / parameter;
+               r = uval - (q * parameter);
+               /* write the unary MSBs */
+               if(!FLAC__bitwriter_write_zeroes(bw, q))
+                       return false;
+               /* write the unary end bit */
+               if(!FLAC__bitwriter_write_raw_uint32(bw, 1, 1))
+                       return false;
+               /* write the binary LSBs */
+               if(r >= d) {
+                       if(!FLAC__bitwriter_write_raw_uint32(bw, r+d, k+1))
+                               return false;
+               }
+               else {
+                       if(!FLAC__bitwriter_write_raw_uint32(bw, r, k))
+                               return false;
+               }
+       }
+       return true;
+}
+
+FLAC__bool FLAC__bitwriter_write_golomb_unsigned(FLAC__BitWriter *bw, unsigned uval, unsigned parameter)
+{
+       unsigned total_bits, msbs;
+       unsigned k;
+
+       FLAC__ASSERT(0 != bw);
+       FLAC__ASSERT(0 != bw->buffer);
+       FLAC__ASSERT(parameter > 0);
+
+       k = FLAC__bitmath_ilog2(parameter);
+       if(parameter == 1u<<k) {
+               unsigned pattern;
+
+               FLAC__ASSERT(k <= 30);
+
+               msbs = uval >> k;
+               total_bits = 1 + k + msbs;
+               pattern = 1 << k; /* the unary end bit */
+               pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
+
+               if(total_bits <= 32) {
+                       if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits))
+                               return false;
+               }
+               else {
+                       /* write the unary MSBs */
+                       if(!FLAC__bitwriter_write_zeroes(bw, msbs))
+                               return false;
+                       /* write the unary end bit and binary LSBs */
+                       if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, k+1))
+                               return false;
+               }
+       }
+       else {
+               unsigned q, r, d;
+
+               d = (1 << (k+1)) - parameter;
+               q = uval / parameter;
+               r = uval - (q * parameter);
+               /* write the unary MSBs */
+               if(!FLAC__bitwriter_write_zeroes(bw, q))
+                       return false;
+               /* write the unary end bit */
+               if(!FLAC__bitwriter_write_raw_uint32(bw, 1, 1))
+                       return false;
+               /* write the binary LSBs */
+               if(r >= d) {
+                       if(!FLAC__bitwriter_write_raw_uint32(bw, r+d, k+1))
+                               return false;
+               }
+               else {
+                       if(!FLAC__bitwriter_write_raw_uint32(bw, r, k))
+                               return false;
+               }
+       }
+       return true;
+}
+#endif /* UNUSED */
+
+FLAC__bool FLAC__bitwriter_write_utf8_uint32(FLAC__BitWriter *bw, FLAC__uint32 val)
+{
+       FLAC__bool ok = 1;
+
+       FLAC__ASSERT(0 != bw);
+       FLAC__ASSERT(0 != bw->buffer);
+
+       FLAC__ASSERT(!(val & 0x80000000)); /* this version only handles 31 bits */
+
+       if(val < 0x80) {
+               return FLAC__bitwriter_write_raw_uint32(bw, val, 8);
+       }
+       else if(val < 0x800) {
+               ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xC0 | (val>>6), 8);
+               ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
+       }
+       else if(val < 0x10000) {
+               ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xE0 | (val>>12), 8);
+               ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
+               ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
+       }
+       else if(val < 0x200000) {
+               ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF0 | (val>>18), 8);
+               ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>12)&0x3F), 8);
+               ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
+               ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
+       }
+       else if(val < 0x4000000) {
+               ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF8 | (val>>24), 8);
+               ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>18)&0x3F), 8);
+               ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>12)&0x3F), 8);
+               ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
+               ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
+       }
+       else {
+               ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xFC | (val>>30), 8);
+               ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>24)&0x3F), 8);
+               ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>18)&0x3F), 8);
+               ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>12)&0x3F), 8);
+               ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
+               ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
+       }
+
+       return ok;
+}
+
+FLAC__bool FLAC__bitwriter_write_utf8_uint64(FLAC__BitWriter *bw, FLAC__uint64 val)
+{
+       FLAC__bool ok = 1;
+
+       FLAC__ASSERT(0 != bw);
+       FLAC__ASSERT(0 != bw->buffer);
+
+       FLAC__ASSERT(!(val & FLAC__U64L(0xFFFFFFF000000000))); /* this version only handles 36 bits */
+
+       if(val < 0x80) {
+               return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, 8);
+       }
+       else if(val < 0x800) {
+               ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xC0 | (FLAC__uint32)(val>>6), 8);
+               ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
+       }
+       else if(val < 0x10000) {
+               ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xE0 | (FLAC__uint32)(val>>12), 8);
+               ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
+               ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
+       }
+       else if(val < 0x200000) {
+               ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF0 | (FLAC__uint32)(val>>18), 8);
+               ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
+               ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
+               ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
+       }
+       else if(val < 0x4000000) {
+               ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF8 | (FLAC__uint32)(val>>24), 8);
+               ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
+               ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
+               ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
+               ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
+       }
+       else if(val < 0x80000000) {
+               ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xFC | (FLAC__uint32)(val>>30), 8);
+               ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
+               ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
+               ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
+               ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
+               ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
+       }
+       else {
+               ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xFE, 8);
+               ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>30)&0x3F), 8);
+               ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
+               ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
+               ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
+               ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
+               ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
+       }
+
+       return ok;
+}
+
+FLAC__bool FLAC__bitwriter_zero_pad_to_byte_boundary(FLAC__BitWriter *bw)
+{
+       /* 0-pad to byte boundary */
+       if(bw->bits & 7u)
+               return FLAC__bitwriter_write_zeroes(bw, 8 - (bw->bits & 7u));
+       else
+               return true;
+}
index 1d31ca3..de4e80e 100644 (file)
@@ -74,7 +74,7 @@ FLAC__byte const FLAC__crc8_table[256] = {
 
 /* CRC-16, poly = x^16 + x^15 + x^2 + x^0, init = 0 */
 
-FLAC__uint16 FLAC__crc16_table[256] = {
+unsigned FLAC__crc16_table[256] = {
        0x0000,  0x8005,  0x800f,  0x000a,  0x801b,  0x001e,  0x0014,  0x8011,
        0x8033,  0x0036,  0x003c,  0x8039,  0x0028,  0x802d,  0x8027,  0x0022,
        0x8063,  0x0066,  0x006c,  0x8069,  0x0078,  0x807d,  0x8077,  0x0072,
@@ -131,23 +131,12 @@ FLAC__uint8 FLAC__crc8(const FLAC__byte *data, unsigned len)
        return crc;
 }
 
-void FLAC__crc16_update(const FLAC__byte data, FLAC__uint16 *crc)
+unsigned FLAC__crc16(const FLAC__byte *data, unsigned len)
 {
-       *crc = (*crc<<8) ^ FLAC__crc16_table[(*crc>>8) ^ data];
-}
-
-void FLAC__crc16_update_block(const FLAC__byte *data, unsigned len, FLAC__uint16 *crc)
-{
-       while(len--)
-               *crc = (*crc<<8) ^ FLAC__crc16_table[(*crc>>8) ^ *data++];
-}
-
-FLAC__uint16 FLAC__crc16(const FLAC__byte *data, unsigned len)
-{
-       FLAC__uint16 crc = 0;
+       unsigned crc = 0;
 
        while(len--)
-               crc = (crc<<8) ^ FLAC__crc16_table[(crc>>8) ^ *data++];
+               crc = ((crc<<8) ^ FLAC__crc16_table[(crc>>8) ^ *data++]) & 0xffff;
 
        return crc;
 }
index 023cc50..6ee7df6 100644 (file)
@@ -30,8 +30,9 @@
 
 noinst_HEADERS = \
        all.h \
-       bitbuffer.h \
        bitmath.h \
+       bitreader.h \
+       bitwriter.h \
        cpu.h \
        crc.h \
        fixed.h \
index 531e5b0..ab937a9 100644 (file)
@@ -32,8 +32,9 @@
 #ifndef FLAC__PRIVATE__ALL_H
 #define FLAC__PRIVATE__ALL_H
 
-#include "bitbuffer.h"
 #include "bitmath.h"
+#include "bitreader.h"
+#include "bitwriter.h"
 #include "cpu.h"
 #include "crc.h"
 #include "fixed.h"
diff --git a/src/libFLAC/include/private/bitbuffer.h b/src/libFLAC/include/private/bitbuffer.h
deleted file mode 100644 (file)
index eea7738..0000000
+++ /dev/null
@@ -1,152 +0,0 @@
-/* libFLAC - Free Lossless Audio Codec library
- * Copyright (C) 2000,2001,2002,2003,2004,2005,2006  Josh Coalson
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * - Neither the name of the Xiph.org Foundation nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#ifndef FLAC__PRIVATE__BITBUFFER_H
-#define FLAC__PRIVATE__BITBUFFER_H
-
-#include <stdio.h> /* for FILE */
-#include "FLAC/ordinals.h"
-
-/* @@@ This should be configurable. Valid values are currently 8 and 32. */
-/* @@@ WATCHOUT!  do not use 32 with a little endian system yet. */
-#define FLAC__BITS_PER_BLURB 8
-
-#if FLAC__BITS_PER_BLURB == 8
-typedef FLAC__byte FLAC__blurb;
-#elif FLAC__BITS_PER_BLURB == 32
-typedef FLAC__uint32 FLAC__blurb;
-#else
-/* ERROR, only sizes of 8 and 32 are supported */
-#endif
-
-/*
- * opaque structure definition
- */
-struct FLAC__BitBuffer;
-typedef struct FLAC__BitBuffer FLAC__BitBuffer;
-
-/*
- * construction, deletion, initialization, cloning functions
- */
-FLAC__BitBuffer *FLAC__bitbuffer_new();
-void FLAC__bitbuffer_delete(FLAC__BitBuffer *bb);
-FLAC__bool FLAC__bitbuffer_init(FLAC__BitBuffer *bb);
-FLAC__bool FLAC__bitbuffer_init_from(FLAC__BitBuffer *bb, const FLAC__byte buffer[], unsigned bytes);
-FLAC__bool FLAC__bitbuffer_concatenate_aligned(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src);
-void FLAC__bitbuffer_free(FLAC__BitBuffer *bb); /* does not 'free(buffer)' */
-FLAC__bool FLAC__bitbuffer_clear(FLAC__BitBuffer *bb);
-FLAC__bool FLAC__bitbuffer_clone(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src);
-
-/*
- * CRC functions
- */
-void FLAC__bitbuffer_reset_read_crc16(FLAC__BitBuffer *bb, FLAC__uint16 seed);
-FLAC__uint16 FLAC__bitbuffer_get_read_crc16(FLAC__BitBuffer *bb);
-FLAC__uint16 FLAC__bitbuffer_get_write_crc16(const FLAC__BitBuffer *bb);
-FLAC__byte FLAC__bitbuffer_get_write_crc8(const FLAC__BitBuffer *bb);
-
-/*
- * info functions
- */
-FLAC__bool FLAC__bitbuffer_is_byte_aligned(const FLAC__BitBuffer *bb);
-FLAC__bool FLAC__bitbuffer_is_consumed_byte_aligned(const FLAC__BitBuffer *bb);
-unsigned FLAC__bitbuffer_bits_left_for_byte_alignment(const FLAC__BitBuffer *bb);
-unsigned FLAC__bitbuffer_get_input_bytes_unconsumed(const FLAC__BitBuffer *bb); /* do not call unless byte-aligned */
-unsigned FLAC__bitbuffer_get_input_bits_unconsumed(const FLAC__BitBuffer *bb); /* can be called anytime, returns total # of bits unconsumed */
-
-/*
- * direct buffer access
- */
-void FLAC__bitbuffer_get_buffer(FLAC__BitBuffer *bb, const FLAC__byte **buffer, size_t *bytes);
-void FLAC__bitbuffer_release_buffer(FLAC__BitBuffer *bb);
-
-/*
- * write functions
- */
-FLAC__bool FLAC__bitbuffer_write_zeroes(FLAC__BitBuffer *bb, unsigned bits);
-FLAC__bool FLAC__bitbuffer_write_raw_uint32(FLAC__BitBuffer *bb, FLAC__uint32 val, unsigned bits);
-FLAC__bool FLAC__bitbuffer_write_raw_int32(FLAC__BitBuffer *bb, FLAC__int32 val, unsigned bits);
-FLAC__bool FLAC__bitbuffer_write_raw_uint64(FLAC__BitBuffer *bb, FLAC__uint64 val, unsigned bits);
-#if 0 /* UNUSED */
-FLAC__bool FLAC__bitbuffer_write_raw_int64(FLAC__BitBuffer *bb, FLAC__int64 val, unsigned bits);
-#endif
-FLAC__bool FLAC__bitbuffer_write_raw_uint32_little_endian(FLAC__BitBuffer *bb, FLAC__uint32 val); /*only for bits=32*/
-FLAC__bool FLAC__bitbuffer_write_byte_block(FLAC__BitBuffer *bb, const FLAC__byte vals[], unsigned nvals);
-FLAC__bool FLAC__bitbuffer_write_unary_unsigned(FLAC__BitBuffer *bb, unsigned val);
-unsigned FLAC__bitbuffer_rice_bits(int val, unsigned parameter);
-#if 0 /* UNUSED */
-unsigned FLAC__bitbuffer_golomb_bits_signed(int val, unsigned parameter);
-unsigned FLAC__bitbuffer_golomb_bits_unsigned(unsigned val, unsigned parameter);
-#endif
-FLAC__bool FLAC__bitbuffer_write_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter);
-#if 0 /* UNUSED */
-FLAC__bool FLAC__bitbuffer_write_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, FLAC__bool *overflow);
-#endif
-#if 0 /* UNUSED */
-FLAC__bool FLAC__bitbuffer_write_golomb_signed(FLAC__BitBuffer *bb, int val, unsigned parameter);
-FLAC__bool FLAC__bitbuffer_write_golomb_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, FLAC__bool *overflow);
-FLAC__bool FLAC__bitbuffer_write_golomb_unsigned(FLAC__BitBuffer *bb, unsigned val, unsigned parameter);
-FLAC__bool FLAC__bitbuffer_write_golomb_unsigned_guarded(FLAC__BitBuffer *bb, unsigned val, unsigned parameter, unsigned max_bits, FLAC__bool *overflow);
-#endif
-FLAC__bool FLAC__bitbuffer_write_utf8_uint32(FLAC__BitBuffer *bb, FLAC__uint32 val);
-FLAC__bool FLAC__bitbuffer_write_utf8_uint64(FLAC__BitBuffer *bb, FLAC__uint64 val);
-FLAC__bool FLAC__bitbuffer_zero_pad_to_byte_boundary(FLAC__BitBuffer *bb);
-
-/*
- * read functions
- */
-typedef FLAC__bool (*FLAC__BitbufferReadCallback)(FLAC__byte buffer[], size_t *bytes, void *client_data);
-
-FLAC__bool FLAC__bitbuffer_peek_bit(FLAC__BitBuffer *bb, unsigned *val, FLAC__BitbufferReadCallback read_callback, void *client_data);
-FLAC__bool FLAC__bitbuffer_read_bit(FLAC__BitBuffer *bb, unsigned *val, FLAC__BitbufferReadCallback read_callback, void *client_data);
-FLAC__bool FLAC__bitbuffer_read_bit_to_uint32(FLAC__BitBuffer *bb, FLAC__uint32 *val, FLAC__BitbufferReadCallback read_callback, void *client_data);
-FLAC__bool FLAC__bitbuffer_read_bit_to_uint64(FLAC__BitBuffer *bb, FLAC__uint64 *val, FLAC__BitbufferReadCallback read_callback, void *client_data);
-FLAC__bool FLAC__bitbuffer_read_raw_uint32(FLAC__BitBuffer *bb, FLAC__uint32 *val, const unsigned bits, FLAC__BitbufferReadCallback read_callback, void *client_data);
-FLAC__bool FLAC__bitbuffer_read_raw_int32(FLAC__BitBuffer *bb, FLAC__int32 *val, const unsigned bits, FLAC__BitbufferReadCallback read_callback, void *client_data);
-FLAC__bool FLAC__bitbuffer_read_raw_uint64(FLAC__BitBuffer *bb, FLAC__uint64 *val, const unsigned bits, FLAC__BitbufferReadCallback read_callback, void *client_data);
-#if 0 /* UNUSED */
-FLAC__bool FLAC__bitbuffer_read_raw_int64(FLAC__BitBuffer *bb, FLAC__int64 *val, const unsigned bits, FLAC__BitbufferReadCallback read_callback, void *client_data);
-#endif
-FLAC__bool FLAC__bitbuffer_read_raw_uint32_little_endian(FLAC__BitBuffer *bb, FLAC__uint32 *val, FLAC__BitbufferReadCallback read_callback, void *client_data); /*only for bits=32*/
-FLAC__bool FLAC__bitbuffer_skip_bits_no_crc(FLAC__BitBuffer *bb, unsigned bits, FLAC__BitbufferReadCallback read_callback, void *client_data); /* WATCHOUT: does not CRC the skipped data! */ /*@@@@ add to unit tests */
-FLAC__bool FLAC__bitbuffer_read_byte_block_aligned_no_crc(FLAC__BitBuffer *bb, FLAC__byte *val, unsigned nvals, FLAC__BitbufferReadCallback read_callback, void *client_data); /* val may be 0 to skip bytes instead of reading them */ /* WATCHOUT: does not CRC the read data! */
-FLAC__bool FLAC__bitbuffer_read_unary_unsigned(FLAC__BitBuffer *bb, unsigned *val, FLAC__BitbufferReadCallback read_callback, void *client_data);
-FLAC__bool FLAC__bitbuffer_read_rice_signed(FLAC__BitBuffer *bb, int *val, unsigned parameter, FLAC__BitbufferReadCallback read_callback, void *client_data);
-FLAC__bool FLAC__bitbuffer_read_rice_signed_block(FLAC__BitBuffer *bb, int vals[], unsigned nvals, unsigned parameter, FLAC__BitbufferReadCallback read_callback, void *client_data);
-#if 0 /* UNUSED */
-FLAC__bool FLAC__bitbuffer_read_golomb_signed(FLAC__BitBuffer *bb, int *val, unsigned parameter, FLAC__BitbufferReadCallback read_callback, void *client_data);
-FLAC__bool FLAC__bitbuffer_read_golomb_unsigned(FLAC__BitBuffer *bb, unsigned *val, unsigned parameter, FLAC__BitbufferReadCallback read_callback, void *client_data);
-#endif
-FLAC__bool FLAC__bitbuffer_read_utf8_uint32(FLAC__BitBuffer *bb, FLAC__uint32 *val, FLAC__BitbufferReadCallback read_callback, void *client_data, FLAC__byte *raw, unsigned *rawlen);
-FLAC__bool FLAC__bitbuffer_read_utf8_uint64(FLAC__BitBuffer *bb, FLAC__uint64 *val, FLAC__BitbufferReadCallback read_callback, void *client_data, FLAC__byte *raw, unsigned *rawlen);
-void FLAC__bitbuffer_dump(const FLAC__BitBuffer *bb, FILE *out);
-
-#endif
diff --git a/src/libFLAC/include/private/bitreader.h b/src/libFLAC/include/private/bitreader.h
new file mode 100644 (file)
index 0000000..35a3d7d
--- /dev/null
@@ -0,0 +1,90 @@
+/* libFLAC - Free Lossless Audio Codec library
+ * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007  Josh Coalson
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * - Neither the name of the Xiph.org Foundation nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef FLAC__PRIVATE__BITREADER_H
+#define FLAC__PRIVATE__BITREADER_H
+
+#include <stdio.h> /* for FILE */
+#include "FLAC/ordinals.h"
+
+/*
+ * opaque structure definition
+ */
+struct FLAC__BitReader;
+typedef struct FLAC__BitReader FLAC__BitReader;
+
+typedef FLAC__bool (*FLAC__BitReaderReadCallback)(FLAC__byte buffer[], size_t *bytes, void *client_data);
+
+/*
+ * construction, deletion, initialization, etc functions
+ */
+FLAC__BitReader *FLAC__bitreader_new();
+void FLAC__bitreader_delete(FLAC__BitReader *br);
+FLAC__bool FLAC__bitreader_init(FLAC__BitReader *br, FLAC__BitReaderReadCallback rcb, void *cd);
+void FLAC__bitreader_free(FLAC__BitReader *br); /* does not 'free(br)' */
+FLAC__bool FLAC__bitreader_clear(FLAC__BitReader *br);
+void FLAC__bitreader_dump(const FLAC__BitReader *br, FILE *out);
+
+/*
+ * CRC functions
+ */
+void FLAC__bitreader_reset_read_crc16(FLAC__BitReader *br, FLAC__uint16 seed);
+FLAC__uint16 FLAC__bitreader_get_read_crc16(FLAC__BitReader *br);
+
+/*
+ * info functions
+ */
+FLAC__bool FLAC__bitreader_is_consumed_byte_aligned(const FLAC__BitReader *br);
+unsigned FLAC__bitreader_bits_left_for_byte_alignment(const FLAC__BitReader *br);
+unsigned FLAC__bitreader_get_input_bits_unconsumed(const FLAC__BitReader *br);
+
+/*
+ * read functions
+ */
+
+FLAC__bool FLAC__bitreader_read_raw_uint32(FLAC__BitReader *br, FLAC__uint32 *val, unsigned bits);
+FLAC__bool FLAC__bitreader_read_raw_int32(FLAC__BitReader *br, FLAC__int32 *val, unsigned bits);
+FLAC__bool FLAC__bitreader_read_raw_uint64(FLAC__BitReader *br, FLAC__uint64 *val, unsigned bits);
+FLAC__bool FLAC__bitreader_read_uint32_little_endian(FLAC__BitReader *br, FLAC__uint32 *val); /*only for bits=32*/
+FLAC__bool FLAC__bitreader_skip_bits_no_crc(FLAC__BitReader *br, unsigned bits); /* WATCHOUT: does not CRC the skipped data! */ /*@@@@ add to unit tests */
+FLAC__bool FLAC__bitreader_skip_byte_block_aligned_no_crc(FLAC__BitReader *br, unsigned nvals); /* WATCHOUT: does not CRC the read data! */
+FLAC__bool FLAC__bitreader_read_byte_block_aligned_no_crc(FLAC__BitReader *br, FLAC__byte *val, unsigned nvals); /* WATCHOUT: does not CRC the read data! */
+FLAC__bool FLAC__bitreader_read_unary_unsigned(FLAC__BitReader *br, unsigned *val);
+FLAC__bool FLAC__bitreader_read_rice_signed(FLAC__BitReader *br, int *val, unsigned parameter);
+FLAC__bool FLAC__bitreader_read_rice_signed_block(FLAC__BitReader *br, int vals[], unsigned nvals, unsigned parameter);
+#if 0 /* UNUSED */
+FLAC__bool FLAC__bitreader_read_golomb_signed(FLAC__BitReader *br, int *val, unsigned parameter);
+FLAC__bool FLAC__bitreader_read_golomb_unsigned(FLAC__BitReader *br, unsigned *val, unsigned parameter);
+#endif
+FLAC__bool FLAC__bitreader_read_utf8_uint32(FLAC__BitReader *br, FLAC__uint32 *val, FLAC__byte *raw, unsigned *rawlen);
+FLAC__bool FLAC__bitreader_read_utf8_uint64(FLAC__BitReader *br, FLAC__uint64 *val, FLAC__byte *raw, unsigned *rawlen);
+
+#endif
diff --git a/src/libFLAC/include/private/bitwriter.h b/src/libFLAC/include/private/bitwriter.h
new file mode 100644 (file)
index 0000000..1ada096
--- /dev/null
@@ -0,0 +1,103 @@
+/* libFLAC - Free Lossless Audio Codec library
+ * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007  Josh Coalson
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * - Neither the name of the Xiph.org Foundation nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef FLAC__PRIVATE__BITWRITER_H
+#define FLAC__PRIVATE__BITWRITER_H
+
+#include <stdio.h> /* for FILE */
+#include "FLAC/ordinals.h"
+
+/*
+ * opaque structure definition
+ */
+struct FLAC__BitWriter;
+typedef struct FLAC__BitWriter FLAC__BitWriter;
+
+/*
+ * construction, deletion, initialization, etc functions
+ */
+FLAC__BitWriter *FLAC__bitwriter_new();
+void FLAC__bitwriter_delete(FLAC__BitWriter *bw);
+FLAC__bool FLAC__bitwriter_init(FLAC__BitWriter *bw);
+void FLAC__bitwriter_free(FLAC__BitWriter *bw); /* does not 'free(buffer)' */
+void FLAC__bitwriter_clear(FLAC__BitWriter *bw);
+void FLAC__bitwriter_dump(const FLAC__BitWriter *bw, FILE *out);
+
+/*
+ * CRC functions
+ *
+ * non-const *bw because they have to cal FLAC__bitwriter_get_buffer()
+ */
+FLAC__bool FLAC__bitwriter_get_write_crc16(FLAC__BitWriter *bw, FLAC__uint16 *crc);
+FLAC__bool FLAC__bitwriter_get_write_crc8(FLAC__BitWriter *bw, FLAC__byte *crc);
+
+/*
+ * info functions
+ */
+FLAC__bool FLAC__bitwriter_is_byte_aligned(const FLAC__BitWriter *bw);
+unsigned FLAC__bitwriter_get_input_bits_unconsumed(const FLAC__BitWriter *bw); /* can be called anytime, returns total # of bits unconsumed */
+
+/*
+ * direct buffer access
+ *
+ * there may be no calls on the bitwriter between get and release.
+ * the bitwriter continues to own the returned buffer.
+ * before get, bitwriter MUST be byte aligned: check with FLAC__bitwriter_is_byte_aligned()
+ */
+FLAC__bool FLAC__bitwriter_get_buffer(FLAC__BitWriter *bw, const FLAC__byte **buffer, size_t *bytes);
+void FLAC__bitwriter_release_buffer(FLAC__BitWriter *bw);
+
+/*
+ * write functions
+ */
+FLAC__bool FLAC__bitwriter_write_zeroes(FLAC__BitWriter *bw, unsigned bits);
+FLAC__bool FLAC__bitwriter_write_raw_uint32(FLAC__BitWriter *bw, FLAC__uint32 val, unsigned bits);
+FLAC__bool FLAC__bitwriter_write_raw_int32(FLAC__BitWriter *bw, FLAC__int32 val, unsigned bits);
+FLAC__bool FLAC__bitwriter_write_raw_uint64(FLAC__BitWriter *bw, FLAC__uint64 val, unsigned bits);
+FLAC__bool FLAC__bitwriter_write_raw_uint32_little_endian(FLAC__BitWriter *bw, FLAC__uint32 val); /*only for bits=32*/
+FLAC__bool FLAC__bitwriter_write_byte_block(FLAC__BitWriter *bw, const FLAC__byte vals[], unsigned nvals);
+FLAC__bool FLAC__bitwriter_write_unary_unsigned(FLAC__BitWriter *bw, unsigned val);
+unsigned FLAC__bitwriter_rice_bits(FLAC__int32 val, unsigned parameter);
+#if 0 /* UNUSED */
+unsigned FLAC__bitwriter_golomb_bits_signed(int val, unsigned parameter);
+unsigned FLAC__bitwriter_golomb_bits_unsigned(unsigned val, unsigned parameter);
+#endif
+FLAC__bool FLAC__bitwriter_write_rice_signed(FLAC__BitWriter *bw, FLAC__int32 val, unsigned parameter);
+FLAC__bool FLAC__bitwriter_write_rice_signed_block(FLAC__BitWriter *bw, const FLAC__int32 *vals, unsigned nvals, unsigned parameter);
+#if 0 /* UNUSED */
+FLAC__bool FLAC__bitwriter_write_golomb_signed(FLAC__BitWriter *bw, int val, unsigned parameter);
+FLAC__bool FLAC__bitwriter_write_golomb_unsigned(FLAC__BitWriter *bw, unsigned val, unsigned parameter);
+#endif
+FLAC__bool FLAC__bitwriter_write_utf8_uint32(FLAC__BitWriter *bw, FLAC__uint32 val);
+FLAC__bool FLAC__bitwriter_write_utf8_uint64(FLAC__BitWriter *bw, FLAC__uint64 val);
+FLAC__bool FLAC__bitwriter_zero_pad_to_byte_boundary(FLAC__BitWriter *bw);
+
+#endif
index 28f1916..94f1ba8 100644 (file)
@@ -48,10 +48,14 @@ FLAC__uint8 FLAC__crc8(const FLAC__byte *data, unsigned len);
 ** polynomial = x^16 + x^15 + x^2 + x^0
 ** init = 0
 */
-extern FLAC__uint16 FLAC__crc16_table[256];
-#define FLAC__CRC16_UPDATE(data, crc) (crc) = ((crc)<<8) ^ FLAC__crc16_table[((crc)>>8) ^ (data)];
-void FLAC__crc16_update(const FLAC__byte data, FLAC__uint16 *crc);
-void FLAC__crc16_update_block(const FLAC__byte *data, unsigned len, FLAC__uint16 *crc);
-FLAC__uint16 FLAC__crc16(const FLAC__byte *data, unsigned len);
+extern unsigned FLAC__crc16_table[256];
+
+#define FLAC__CRC16_UPDATE(data, crc) (((((crc)<<8) & 0xffff) ^ FLAC__crc16_table[((crc)>>8) ^ (data)]))
+/* this alternate may be faster on some systems/compilers */
+#if 0
+#define FLAC__CRC16_UPDATE(data, crc) ((((crc)<<8) ^ FLAC__crc16_table[((crc)>>8) ^ (data)]) & 0xffff)
+#endif
+
+unsigned FLAC__crc16(const FLAC__byte *data, unsigned len);
 
 #endif
index b394953..58b13d1 100644 (file)
 #define FLAC__PRIVATE__STREAM_ENCODER_FRAMING_H
 
 #include "FLAC/format.h"
-#include "bitbuffer.h"
+#include "bitwriter.h"
 
-FLAC__bool FLAC__add_metadata_block(const FLAC__StreamMetadata *metadata, FLAC__BitBuffer *bb);
-FLAC__bool FLAC__frame_add_header(const FLAC__FrameHeader *header, FLAC__BitBuffer *bb);
-FLAC__bool FLAC__subframe_add_constant(const FLAC__Subframe_Constant *subframe, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitBuffer *bb);
-FLAC__bool FLAC__subframe_add_fixed(const FLAC__Subframe_Fixed *subframe, unsigned residual_samples, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitBuffer *bb);
-FLAC__bool FLAC__subframe_add_lpc(const FLAC__Subframe_LPC *subframe, unsigned residual_samples, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitBuffer *bb);
-FLAC__bool FLAC__subframe_add_verbatim(const FLAC__Subframe_Verbatim *subframe, unsigned samples, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitBuffer *bb);
+FLAC__bool FLAC__add_metadata_block(const FLAC__StreamMetadata *metadata, FLAC__BitWriter *bw);
+FLAC__bool FLAC__frame_add_header(const FLAC__FrameHeader *header, FLAC__BitWriter *bw);
+FLAC__bool FLAC__subframe_add_constant(const FLAC__Subframe_Constant *subframe, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitWriter *bw);
+FLAC__bool FLAC__subframe_add_fixed(const FLAC__Subframe_Fixed *subframe, unsigned residual_samples, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitWriter *bw);
+FLAC__bool FLAC__subframe_add_lpc(const FLAC__Subframe_LPC *subframe, unsigned residual_samples, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitWriter *bw);
+FLAC__bool FLAC__subframe_add_verbatim(const FLAC__Subframe_Verbatim *subframe, unsigned samples, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitWriter *bw);
 
 #endif
index dc4c352..d2c6df8 100644 (file)
@@ -187,11 +187,15 @@ SOURCE=.\ia32\nasm.h
 # End Group\r
 # Begin Source File\r
 \r
-SOURCE=.\bitbuffer.c\r
+SOURCE=.\bitmath.c\r
 # End Source File\r
 # Begin Source File\r
 \r
-SOURCE=.\bitmath.c\r
+SOURCE=.\bitreader.c\r
+# End Source File\r
+# Begin Source File\r
+\r
+SOURCE=.\bitwriter.c\r
 # End Source File\r
 # Begin Source File\r
 \r
@@ -275,11 +279,15 @@ SOURCE=.\include\private\all.h
 # End Source File\r
 # Begin Source File\r
 \r
-SOURCE=.\include\private\bitbuffer.h\r
+SOURCE=.\include\private\bitmath.h\r
 # End Source File\r
 # Begin Source File\r
 \r
-SOURCE=.\include\private\bitmath.h\r
+SOURCE=.\include\private\bitreader.h\r
+# End Source File\r
+# Begin Source File\r
+\r
+SOURCE=.\include\private\bitwriter.h\r
 # End Source File\r
 # Begin Source File\r
 \r
index a3d90e2..9e73f2c 100644 (file)
@@ -180,11 +180,15 @@ SOURCE=.\ia32\nasm.h
 # End Group\r
 # Begin Source File\r
 \r
-SOURCE=.\bitbuffer.c\r
+SOURCE=.\bitmath.c\r
 # End Source File\r
 # Begin Source File\r
 \r
-SOURCE=.\bitmath.c\r
+SOURCE=.\bitreader.c\r
+# End Source File\r
+# Begin Source File\r
+\r
+SOURCE=.\bitwriter.c\r
 # End Source File\r
 # Begin Source File\r
 \r
@@ -268,11 +272,15 @@ SOURCE=.\include\private\all.h
 # End Source File\r
 # Begin Source File\r
 \r
-SOURCE=.\include\private\bitbuffer.h\r
+SOURCE=.\include\private\bitmath.h\r
 # End Source File\r
 # Begin Source File\r
 \r
-SOURCE=.\include\private\bitmath.h\r
+SOURCE=.\include\private\bitreader.h\r
+# End Source File\r
+# Begin Source File\r
+\r
+SOURCE=.\include\private\bitwriter.h\r
 # End Source File\r
 # Begin Source File\r
 \r
index bcf63f6..8e29353 100644 (file)
@@ -54,7 +54,7 @@
 #endif
 #include "FLAC/assert.h"
 #include "protected/stream_decoder.h"
-#include "private/bitbuffer.h"
+#include "private/bitreader.h"
 #include "private/bitmath.h"
 #include "private/cpu.h"
 #include "private/crc.h"
@@ -168,7 +168,7 @@ typedef struct FLAC__StreamDecoderPrivate {
        void (*local_lpc_restore_signal_16bit_order8)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
        void *client_data;
        FILE *file; /* only used if FLAC__stream_decoder_init_file()/FLAC__stream_decoder_init_file() called, else NULL */
-       FLAC__BitBuffer *input;
+       FLAC__BitReader *input;
        FLAC__int32 *output[FLAC__MAX_CHANNELS];
        FLAC__int32 *residual[FLAC__MAX_CHANNELS]; /* WATCHOUT: these are the aligned pointers; the real pointers that should be free()'d are residual_unaligned[] below */
        FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents[FLAC__MAX_CHANNELS];
@@ -298,7 +298,7 @@ FLAC_API FLAC__StreamDecoder *FLAC__stream_decoder_new()
                return 0;
        }
 
-       decoder->private_->input = FLAC__bitbuffer_new();
+       decoder->private_->input = FLAC__bitreader_new();
        if(decoder->private_->input == 0) {
                free(decoder->private_);
                free(decoder->protected_);
@@ -308,7 +308,7 @@ FLAC_API FLAC__StreamDecoder *FLAC__stream_decoder_new()
 
        decoder->private_->metadata_filter_ids_capacity = 16;
        if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)malloc((FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) * decoder->private_->metadata_filter_ids_capacity))) {
-               FLAC__bitbuffer_delete(decoder->private_->input);
+               FLAC__bitreader_delete(decoder->private_->input);
                free(decoder->private_);
                free(decoder->protected_);
                free(decoder);
@@ -350,7 +350,7 @@ FLAC_API void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder)
        if(0 != decoder->private_->metadata_filter_ids)
                free(decoder->private_->metadata_filter_ids);
 
-       FLAC__bitbuffer_delete(decoder->private_->input);
+       FLAC__bitreader_delete(decoder->private_->input);
 
        for(i = 0; i < FLAC__MAX_CHANNELS; i++)
                FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&decoder->private_->partitioned_rice_contents[i]);
@@ -442,7 +442,7 @@ static FLAC__StreamDecoderInitStatus init_stream_internal_(
 
        /* from here on, errors are fatal */
 
-       if(!FLAC__bitbuffer_init(decoder->private_->input)) {
+       if(!FLAC__bitreader_init(decoder->private_->input, read_callback_, decoder)) {
                decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
                return FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR;
        }
@@ -677,7 +677,7 @@ FLAC_API FLAC__bool FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
                decoder->private_->seek_table.data.seek_table.points = 0;
                decoder->private_->has_seek_table = false;
        }
-       FLAC__bitbuffer_free(decoder->private_->input);
+       FLAC__bitreader_free(decoder->private_->input);
        for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
                /* WATCHOUT:
                 * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the
@@ -939,6 +939,9 @@ FLAC_API FLAC__bool FLAC__stream_decoder_get_decode_position(const FLAC__StreamD
                return false;
        if(decoder->private_->tell_callback(decoder, position, decoder->private_->client_data) != FLAC__STREAM_DECODER_TELL_STATUS_OK)
                return false;
+       /* should never happen since all FLAC frames and metadata blocks are byte aligned, but check just in case */
+       if(!FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input))
+               return false;
        FLAC__ASSERT(*position >= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder));
        *position -= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder);
        return true;
@@ -958,7 +961,7 @@ FLAC_API FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
                FLAC__ogg_decoder_aspect_flush(&decoder->protected_->ogg_decoder_aspect);
 #endif
 
-       if(!FLAC__bitbuffer_clear(decoder->private_->input)) {
+       if(!FLAC__bitreader_clear(decoder->private_->input)) {
                decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
                return false;
        }
@@ -1229,7 +1232,9 @@ FLAC_API FLAC__bool FLAC__stream_decoder_seek_absolute(FLAC__StreamDecoder *deco
 unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder)
 {
        FLAC__ASSERT(0 != decoder);
-       return FLAC__bitbuffer_get_input_bytes_unconsumed(decoder->private_->input);
+       FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
+       FLAC__ASSERT(!(FLAC__bitreader_get_input_bits_unconsumed(decoder->private_->input) & 7));
+       return FLAC__bitreader_get_input_bits_unconsumed(decoder->private_->input) / 8;
 }
 
 /***********************************************************************
@@ -1354,7 +1359,7 @@ FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder)
        unsigned i, id;
        FLAC__bool first = true;
 
-       FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
+       FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
 
        for(i = id = 0; i < 4; ) {
                if(decoder->private_->cached) {
@@ -1362,7 +1367,7 @@ FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder)
                        decoder->private_->cached = false;
                }
                else {
-                       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
+                       if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
                                return false; /* read_callback_ sets the state for us */
                }
                if(x == FLAC__STREAM_SYNC_STRING[i]) {
@@ -1383,7 +1388,7 @@ FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder)
                id = 0;
                if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
                        decoder->private_->header_warmup[0] = (FLAC__byte)x;
-                       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
+                       if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
                                return false; /* read_callback_ sets the state for us */
 
                        /* we have to check if we just read two 0xff's in a row; the second may actually be the beginning of the sync code */
@@ -1414,16 +1419,16 @@ FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder)
        FLAC__bool is_last;
        FLAC__uint32 i, x, type, length;
 
-       FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
+       FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
 
-       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_IS_LAST_LEN, read_callback_, decoder))
+       if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_IS_LAST_LEN))
                return false; /* read_callback_ sets the state for us */
        is_last = x? true : false;
 
-       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &type, FLAC__STREAM_METADATA_TYPE_LEN, read_callback_, decoder))
+       if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &type, FLAC__STREAM_METADATA_TYPE_LEN))
                return false; /* read_callback_ sets the state for us */
 
-       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN, read_callback_, decoder))
+       if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN))
                return false; /* read_callback_ sets the state for us */
 
        if(type == FLAC__METADATA_TYPE_STREAMINFO) {
@@ -1454,7 +1459,7 @@ FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder)
                block.length = length;
 
                if(type == FLAC__METADATA_TYPE_APPLICATION) {
-                       if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8, read_callback_, decoder))
+                       if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8))
                                return false; /* read_callback_ sets the state for us */
 
                        real_length -= FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8;
@@ -1464,14 +1469,14 @@ FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder)
                }
 
                if(skip_it) {
-                       if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, real_length, read_callback_, decoder))
+                       if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, real_length))
                                return false; /* read_callback_ sets the state for us */
                }
                else {
                        switch(type) {
                                case FLAC__METADATA_TYPE_PADDING:
                                        /* skip the padding bytes */
-                                       if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, real_length, read_callback_, decoder))
+                                       if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, real_length))
                                                return false; /* read_callback_ sets the state for us */
                                        break;
                                case FLAC__METADATA_TYPE_APPLICATION:
@@ -1481,7 +1486,7 @@ FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder)
                                                        decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
                                                        return false;
                                                }
-                                               if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.data, real_length, read_callback_, decoder))
+                                               if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.data, real_length))
                                                        return false; /* read_callback_ sets the state for us */
                                        }
                                        else
@@ -1509,7 +1514,7 @@ FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder)
                                                        decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
                                                        return false;
                                                }
-                                               if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.unknown.data, real_length, read_callback_, decoder))
+                                               if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.unknown.data, real_length))
                                                        return false; /* read_callback_ sets the state for us */
                                        }
                                        else
@@ -1579,67 +1584,67 @@ FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is
        FLAC__uint32 x;
        unsigned bits, used_bits = 0;
 
-       FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
+       FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
 
        decoder->private_->stream_info.type = FLAC__METADATA_TYPE_STREAMINFO;
        decoder->private_->stream_info.is_last = is_last;
        decoder->private_->stream_info.length = length;
 
        bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN;
-       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, bits, read_callback_, decoder))
+       if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, bits))
                return false; /* read_callback_ sets the state for us */
        decoder->private_->stream_info.data.stream_info.min_blocksize = x;
        used_bits += bits;
 
        bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN;
-       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN, read_callback_, decoder))
+       if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN))
                return false; /* read_callback_ sets the state for us */
        decoder->private_->stream_info.data.stream_info.max_blocksize = x;
        used_bits += bits;
 
        bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN;
-       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN, read_callback_, decoder))
+       if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN))
                return false; /* read_callback_ sets the state for us */
        decoder->private_->stream_info.data.stream_info.min_framesize = x;
        used_bits += bits;
 
        bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN;
-       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN, read_callback_, decoder))
+       if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN))
                return false; /* read_callback_ sets the state for us */
        decoder->private_->stream_info.data.stream_info.max_framesize = x;
        used_bits += bits;
 
        bits = FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN;
-       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN, read_callback_, decoder))
+       if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN))
                return false; /* read_callback_ sets the state for us */
        decoder->private_->stream_info.data.stream_info.sample_rate = x;
        used_bits += bits;
 
        bits = FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN;
-       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN, read_callback_, decoder))
+       if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN))
                return false; /* read_callback_ sets the state for us */
        decoder->private_->stream_info.data.stream_info.channels = x+1;
        used_bits += bits;
 
        bits = FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN;
-       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN, read_callback_, decoder))
+       if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN))
                return false; /* read_callback_ sets the state for us */
        decoder->private_->stream_info.data.stream_info.bits_per_sample = x+1;
        used_bits += bits;
 
        bits = FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN;
-       if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &decoder->private_->stream_info.data.stream_info.total_samples, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN, read_callback_, decoder))
+       if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &decoder->private_->stream_info.data.stream_info.total_samples, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN))
                return false; /* read_callback_ sets the state for us */
        used_bits += bits;
 
-       if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, decoder->private_->stream_info.data.stream_info.md5sum, 16, read_callback_, decoder))
+       if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, decoder->private_->stream_info.data.stream_info.md5sum, 16))
                return false; /* read_callback_ sets the state for us */
        used_bits += 16*8;
 
        /* skip the rest of the block */
        FLAC__ASSERT(used_bits % 8 == 0);
        length -= (used_bits / 8);
-       if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, length, read_callback_, decoder))
+       if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, length))
                return false; /* read_callback_ sets the state for us */
 
        return true;
@@ -1650,7 +1655,7 @@ FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_
        FLAC__uint32 i, x;
        FLAC__uint64 xx;
 
-       FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
+       FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
 
        decoder->private_->seek_table.type = FLAC__METADATA_TYPE_SEEKTABLE;
        decoder->private_->seek_table.is_last = is_last;
@@ -1664,15 +1669,15 @@ FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_
                return false;
        }
        for(i = 0; i < decoder->private_->seek_table.data.seek_table.num_points; i++) {
-               if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN, read_callback_, decoder))
+               if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN))
                        return false; /* read_callback_ sets the state for us */
                decoder->private_->seek_table.data.seek_table.points[i].sample_number = xx;
 
-               if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN, read_callback_, decoder))
+               if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN))
                        return false; /* read_callback_ sets the state for us */
                decoder->private_->seek_table.data.seek_table.points[i].stream_offset = xx;
 
-               if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN, read_callback_, decoder))
+               if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN))
                        return false; /* read_callback_ sets the state for us */
                decoder->private_->seek_table.data.seek_table.points[i].frame_samples = x;
        }
@@ -1680,7 +1685,7 @@ FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_
        /* if there is a partial point left, skip over it */
        if(length > 0) {
                /*@@@ do a send_error_to_client_() here?  there's an argument for either way */
-               if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, length, read_callback_, decoder))
+               if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, length))
                        return false; /* read_callback_ sets the state for us */
        }
 
@@ -1691,18 +1696,18 @@ FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__Stre
 {
        FLAC__uint32 i;
 
-       FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
+       FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
 
        /* read vendor string */
        FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
-       if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &obj->vendor_string.length, read_callback_, decoder))
+       if(!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->vendor_string.length))
                return false; /* read_callback_ sets the state for us */
        if(obj->vendor_string.length > 0) {
                if(0 == (obj->vendor_string.entry = (FLAC__byte*)malloc(obj->vendor_string.length+1))) {
                        decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
                        return false;
                }
-               if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, obj->vendor_string.entry, obj->vendor_string.length, read_callback_, decoder))
+               if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->vendor_string.entry, obj->vendor_string.length))
                        return false; /* read_callback_ sets the state for us */
                obj->vendor_string.entry[obj->vendor_string.length] = '\0';
        }
@@ -1711,7 +1716,7 @@ FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__Stre
 
        /* read num comments */
        FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN == 32);
-       if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &obj->num_comments, read_callback_, decoder))
+       if(!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->num_comments))
                return false; /* read_callback_ sets the state for us */
 
        /* read comments */
@@ -1722,14 +1727,14 @@ FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__Stre
                }
                for(i = 0; i < obj->num_comments; i++) {
                        FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
-                       if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &obj->comments[i].length, read_callback_, decoder))
+                       if(!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->comments[i].length))
                                return false; /* read_callback_ sets the state for us */
                        if(obj->comments[i].length > 0) {
                                if(0 == (obj->comments[i].entry = (FLAC__byte*)malloc(obj->comments[i].length+1))) {
                                        decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
                                        return false;
                                }
-                               if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, obj->comments[i].entry, obj->comments[i].length, read_callback_, decoder))
+                               if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->comments[i].entry, obj->comments[i].length))
                                        return false; /* read_callback_ sets the state for us */
                                obj->comments[i].entry[obj->comments[i].length] = '\0';
                        }
@@ -1748,25 +1753,25 @@ FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMet
 {
        FLAC__uint32 i, j, x;
 
-       FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
+       FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
 
        memset(obj, 0, sizeof(FLAC__StreamMetadata_CueSheet));
 
        FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0);
-       if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)obj->media_catalog_number, FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN/8, read_callback_, decoder))
+       if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)obj->media_catalog_number, FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN/8))
                return false; /* read_callback_ sets the state for us */
 
-       if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &obj->lead_in, FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN, read_callback_, decoder))
+       if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &obj->lead_in, FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN))
                return false; /* read_callback_ sets the state for us */
 
-       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN, read_callback_, decoder))
+       if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN))
                return false; /* read_callback_ sets the state for us */
        obj->is_cd = x? true : false;
 
-       if(!FLAC__bitbuffer_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN, read_callback_, decoder))
+       if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN))
                return false; /* read_callback_ sets the state for us */
 
-       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN, read_callback_, decoder))
+       if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN))
                return false; /* read_callback_ sets the state for us */
        obj->num_tracks = x;
 
@@ -1777,29 +1782,29 @@ FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMet
                }
                for(i = 0; i < obj->num_tracks; i++) {
                        FLAC__StreamMetadata_CueSheet_Track *track = &obj->tracks[i];
-                       if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &track->offset, FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN, read_callback_, decoder))
+                       if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &track->offset, FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN))
                                return false; /* read_callback_ sets the state for us */
 
-                       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN, read_callback_, decoder))
+                       if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN))
                                return false; /* read_callback_ sets the state for us */
                        track->number = (FLAC__byte)x;
 
                        FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0);
-                       if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)track->isrc, FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN/8, read_callback_, decoder))
+                       if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)track->isrc, FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN/8))
                                return false; /* read_callback_ sets the state for us */
 
-                       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN, read_callback_, decoder))
+                       if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN))
                                return false; /* read_callback_ sets the state for us */
                        track->type = x;
 
-                       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN, read_callback_, decoder))
+                       if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN))
                                return false; /* read_callback_ sets the state for us */
                        track->pre_emphasis = x;
 
-                       if(!FLAC__bitbuffer_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN, read_callback_, decoder))
+                       if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN))
                                return false; /* read_callback_ sets the state for us */
 
-                       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN, read_callback_, decoder))
+                       if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN))
                                return false; /* read_callback_ sets the state for us */
                        track->num_indices = (FLAC__byte)x;
 
@@ -1810,14 +1815,14 @@ FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMet
                                }
                                for(j = 0; j < track->num_indices; j++) {
                                        FLAC__StreamMetadata_CueSheet_Index *index = &track->indices[j];
-                                       if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &index->offset, FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN, read_callback_, decoder))
+                                       if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &index->offset, FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN))
                                                return false; /* read_callback_ sets the state for us */
 
-                                       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN, read_callback_, decoder))
+                                       if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN))
                                                return false; /* read_callback_ sets the state for us */
                                        index->number = (FLAC__byte)x;
 
-                                       if(!FLAC__bitbuffer_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN, read_callback_, decoder))
+                                       if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN))
                                                return false; /* read_callback_ sets the state for us */
                                }
                        }
@@ -1831,63 +1836,63 @@ FLAC__bool read_metadata_picture_(FLAC__StreamDecoder *decoder, FLAC__StreamMeta
 {
        FLAC__uint32 len;
 
-       FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
+       FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
 
        /* read type */
-       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &obj->type, FLAC__STREAM_METADATA_PICTURE_TYPE_LEN, read_callback_, decoder))
+       if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->type, FLAC__STREAM_METADATA_PICTURE_TYPE_LEN))
                return false; /* read_callback_ sets the state for us */
 
        /* read MIME type */
-       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &len, FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN, read_callback_, decoder))
+       if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &len, FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN))
                return false; /* read_callback_ sets the state for us */
        if(0 == (obj->mime_type = (char*)malloc(len+1))) {
                decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
                return false;
        }
        if(len > 0) {
-               if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)obj->mime_type, len, read_callback_, decoder))
+               if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)obj->mime_type, len))
                        return false; /* read_callback_ sets the state for us */
        }
        obj->mime_type[len] = '\0';
 
        /* read description */
-       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &len, FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN, read_callback_, decoder))
+       if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &len, FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN))
                return false; /* read_callback_ sets the state for us */
        if(0 == (obj->description = (FLAC__byte*)malloc(len+1))) {
                decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
                return false;
        }
        if(len > 0) {
-               if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, obj->description, len, read_callback_, decoder))
+               if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->description, len))
                        return false; /* read_callback_ sets the state for us */
        }
        obj->description[len] = '\0';
 
        /* read width */
-       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &obj->width, FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN, read_callback_, decoder))
+       if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->width, FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN))
                return false; /* read_callback_ sets the state for us */
 
        /* read height */
-       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &obj->height, FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN, read_callback_, decoder))
+       if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->height, FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN))
                return false; /* read_callback_ sets the state for us */
 
        /* read depth */
-       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &obj->depth, FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN, read_callback_, decoder))
+       if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->depth, FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN))
                return false; /* read_callback_ sets the state for us */
 
        /* read colors */
-       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &obj->colors, FLAC__STREAM_METADATA_PICTURE_COLORS_LEN, read_callback_, decoder))
+       if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->colors, FLAC__STREAM_METADATA_PICTURE_COLORS_LEN))
                return false; /* read_callback_ sets the state for us */
 
        /* read data */
-       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &(obj->data_length), FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN, read_callback_, decoder))
+       if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &(obj->data_length), FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN))
                return false; /* read_callback_ sets the state for us */
        if(0 == (obj->data = (FLAC__byte*)malloc(obj->data_length))) {
                decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
                return false;
        }
        if(obj->data_length > 0) {
-               if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, obj->data, obj->data_length, read_callback_, decoder))
+               if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->data, obj->data_length))
                        return false; /* read_callback_ sets the state for us */
        }
 
@@ -1900,18 +1905,18 @@ FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
        unsigned i, skip;
 
        /* skip the version and flags bytes */
-       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 24, read_callback_, decoder))
+       if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 24))
                return false; /* read_callback_ sets the state for us */
        /* get the size (in bytes) to skip */
        skip = 0;
        for(i = 0; i < 4; i++) {
-               if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
+               if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
                        return false; /* read_callback_ sets the state for us */
                skip <<= 7;
                skip |= (x & 0x7f);
        }
        /* skip the rest of the tag */
-       if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, skip, read_callback_, decoder))
+       if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, skip))
                return false; /* read_callback_ sets the state for us */
        return true;
 }
@@ -1931,8 +1936,8 @@ FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder)
        }
 
        /* make sure we're byte aligned */
-       if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) {
-               if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder))
+       if(!FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)) {
+               if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__bitreader_bits_left_for_byte_alignment(decoder->private_->input)))
                        return false; /* read_callback_ sets the state for us */
        }
 
@@ -1942,12 +1947,12 @@ FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder)
                        decoder->private_->cached = false;
                }
                else {
-                       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
+                       if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
                                return false; /* read_callback_ sets the state for us */
                }
                if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
                        decoder->private_->header_warmup[0] = (FLAC__byte)x;
-                       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
+                       if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
                                return false; /* read_callback_ sets the state for us */
 
                        /* we have to check if we just read two 0xff's in a row; the second may actually be the beginning of the sync code */
@@ -1976,16 +1981,16 @@ FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FL
        unsigned channel;
        unsigned i;
        FLAC__int32 mid, side, left, right;
-       FLAC__uint16 frame_crc; /* the one we calculate from the input stream */
+       unsigned frame_crc; /* the one we calculate from the input stream */
        FLAC__uint32 x;
 
        *got_a_frame = false;
 
        /* init the CRC */
        frame_crc = 0;
-       FLAC__CRC16_UPDATE(decoder->private_->header_warmup[0], frame_crc);
-       FLAC__CRC16_UPDATE(decoder->private_->header_warmup[1], frame_crc);
-       FLAC__bitbuffer_reset_read_crc16(decoder->private_->input, frame_crc);
+       frame_crc = FLAC__CRC16_UPDATE(decoder->private_->header_warmup[0], frame_crc);
+       frame_crc = FLAC__CRC16_UPDATE(decoder->private_->header_warmup[1], frame_crc);
+       FLAC__bitreader_reset_read_crc16(decoder->private_->input, (FLAC__uint16)frame_crc);
 
        if(!read_frame_header_(decoder))
                return false;
@@ -2034,10 +2039,10 @@ FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FL
        /*
         * Read the frame CRC-16 from the footer and check
         */
-       frame_crc = FLAC__bitbuffer_get_read_crc16(decoder->private_->input);
-       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__FRAME_FOOTER_CRC_LEN, read_callback_, decoder))
+       frame_crc = FLAC__bitreader_get_read_crc16(decoder->private_->input);
+       if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__FRAME_FOOTER_CRC_LEN))
                return false; /* read_callback_ sets the state for us */
-       if(frame_crc == (FLAC__uint16)x) {
+       if(frame_crc == x) {
                if(do_full_decode) {
                        /* Undo any special channel coding */
                        switch(decoder->private_->frame.header.channel_assignment) {
@@ -2117,7 +2122,7 @@ FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder)
        const FLAC__bool is_known_variable_blocksize_stream = (decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.min_blocksize != decoder->private_->stream_info.data.stream_info.max_blocksize);
        const FLAC__bool is_known_fixed_blocksize_stream = (decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.min_blocksize == decoder->private_->stream_info.data.stream_info.max_blocksize);
 
-       FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
+       FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
 
        /* init the raw header with the saved bits from synchronization */
        raw_header[0] = decoder->private_->header_warmup[0];
@@ -2141,7 +2146,7 @@ FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder)
         * read in the raw header as bytes so we can CRC it, and parse it on the way
         */
        for(i = 0; i < 2; i++) {
-               if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
+               if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
                        return false; /* read_callback_ sets the state for us */
                if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
                        /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */
@@ -2312,7 +2317,7 @@ FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder)
         */
        if(is_known_variable_blocksize_stream) {
                if(blocksize_hint) {
-                       if(!FLAC__bitbuffer_read_utf8_uint64(decoder->private_->input, &xx, read_callback_, decoder, raw_header, &raw_header_len))
+                       if(!FLAC__bitreader_read_utf8_uint64(decoder->private_->input, &xx, raw_header, &raw_header_len))
                                return false; /* read_callback_ sets the state for us */
                        if(xx == FLAC__U64L(0xffffffffffffffff)) { /* i.e. non-UTF8 code... */
                                decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
@@ -2328,7 +2333,7 @@ FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder)
                        is_unparseable = true;
        }
        else {
-               if(!FLAC__bitbuffer_read_utf8_uint32(decoder->private_->input, &x, read_callback_, decoder, raw_header, &raw_header_len))
+               if(!FLAC__bitreader_read_utf8_uint32(decoder->private_->input, &x, raw_header, &raw_header_len))
                        return false; /* read_callback_ sets the state for us */
                if(x == 0xffffffff) { /* i.e. non-UTF8 code... */
                        decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
@@ -2357,12 +2362,12 @@ FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder)
        }
 
        if(blocksize_hint) {
-               if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
+               if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
                        return false; /* read_callback_ sets the state for us */
                raw_header[raw_header_len++] = (FLAC__byte)x;
                if(blocksize_hint == 7) {
                        FLAC__uint32 _x;
-                       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &_x, 8, read_callback_, decoder))
+                       if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &_x, 8))
                                return false; /* read_callback_ sets the state for us */
                        raw_header[raw_header_len++] = (FLAC__byte)_x;
                        x = (x << 8) | _x;
@@ -2371,12 +2376,12 @@ FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder)
        }
 
        if(sample_rate_hint) {
-               if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
+               if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
                        return false; /* read_callback_ sets the state for us */
                raw_header[raw_header_len++] = (FLAC__byte)x;
                if(sample_rate_hint != 12) {
                        FLAC__uint32 _x;
-                       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &_x, 8, read_callback_, decoder))
+                       if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &_x, 8))
                                return false; /* read_callback_ sets the state for us */
                        raw_header[raw_header_len++] = (FLAC__byte)_x;
                        x = (x << 8) | _x;
@@ -2390,7 +2395,7 @@ FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder)
        }
 
        /* read the CRC-8 byte */
-       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
+       if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
                return false; /* read_callback_ sets the state for us */
        crc8 = (FLAC__byte)x;
 
@@ -2415,7 +2420,7 @@ FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsign
        FLAC__bool wasted_bits;
        unsigned i;
 
-       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder)) /* MAGIC NUMBER */
+       if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) /* MAGIC NUMBER */
                return false; /* read_callback_ sets the state for us */
 
        wasted_bits = (x & 1);
@@ -2423,7 +2428,7 @@ FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsign
 
        if(wasted_bits) {
                unsigned u;
-               if(!FLAC__bitbuffer_read_unary_unsigned(decoder->private_->input, &u, read_callback_, decoder))
+               if(!FLAC__bitreader_read_unary_unsigned(decoder->private_->input, &u))
                        return false; /* read_callback_ sets the state for us */
                decoder->private_->frame.subframes[channel].wasted_bits = u+1;
                bps -= decoder->private_->frame.subframes[channel].wasted_bits;
@@ -2488,7 +2493,7 @@ FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channe
 
        decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;
 
-       if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder))
+       if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &x, bps))
                return false; /* read_callback_ sets the state for us */
 
        subframe->value = x;
@@ -2516,18 +2521,18 @@ FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel,
 
        /* read warm-up samples */
        for(u = 0; u < order; u++) {
-               if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder))
+               if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, bps))
                        return false; /* read_callback_ sets the state for us */
                subframe->warmup[u] = i32;
        }
 
        /* read entropy coding method info */
-       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
+       if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN))
                return false; /* read_callback_ sets the state for us */
        subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32;
        switch(subframe->entropy_coding_method.type) {
                case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
-                       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
+                       if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
                                return false; /* read_callback_ sets the state for us */
                        subframe->entropy_coding_method.data.partitioned_rice.order = u32;
                        subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
@@ -2571,13 +2576,13 @@ FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, un
 
        /* read warm-up samples */
        for(u = 0; u < order; u++) {
-               if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder))
+               if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, bps))
                        return false; /* read_callback_ sets the state for us */
                subframe->warmup[u] = i32;
        }
 
        /* read qlp coeff precision */
-       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN, read_callback_, decoder))
+       if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN))
                return false; /* read_callback_ sets the state for us */
        if(u32 == (1u << FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN) - 1) {
                send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
@@ -2587,24 +2592,24 @@ FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, un
        subframe->qlp_coeff_precision = u32+1;
 
        /* read qlp shift */
-       if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN, read_callback_, decoder))
+       if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN))
                return false; /* read_callback_ sets the state for us */
        subframe->quantization_level = i32;
 
        /* read quantized lp coefficiencts */
        for(u = 0; u < order; u++) {
-               if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, subframe->qlp_coeff_precision, read_callback_, decoder))
+               if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, subframe->qlp_coeff_precision))
                        return false; /* read_callback_ sets the state for us */
                subframe->qlp_coeff[u] = i32;
        }
 
        /* read entropy coding method info */
-       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
+       if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN))
                return false; /* read_callback_ sets the state for us */
        subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32;
        switch(subframe->entropy_coding_method.type) {
                case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
-                       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
+                       if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
                                return false; /* read_callback_ sets the state for us */
                        subframe->entropy_coding_method.data.partitioned_rice.order = u32;
                        subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
@@ -2655,7 +2660,7 @@ FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channe
        subframe->data = residual;
 
        for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
-               if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder))
+               if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &x, bps))
                        return false; /* read_callback_ sets the state for us */
                residual[i] = x;
        }
@@ -2698,21 +2703,21 @@ FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigne
 
        sample = 0;
        for(partition = 0; partition < partitions; partition++) {
-               if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN, read_callback_, decoder))
+               if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN))
                        return false; /* read_callback_ sets the state for us */
                partitioned_rice_contents->parameters[partition] = rice_parameter;
                if(rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
                        u = (partition_order == 0 || partition > 0)? partition_samples : partition_samples - predictor_order;
-                       if(!FLAC__bitbuffer_read_rice_signed_block(decoder->private_->input, residual + sample, u, rice_parameter, read_callback_, decoder))
+                       if(!FLAC__bitreader_read_rice_signed_block(decoder->private_->input, residual + sample, u, rice_parameter))
                                return false; /* read_callback_ sets the state for us */
                        sample += u;
                }
                else {
-                       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN, read_callback_, decoder))
+                       if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN))
                                return false; /* read_callback_ sets the state for us */
                        partitioned_rice_contents->raw_bits[partition] = rice_parameter;
                        for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
-                               if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i, rice_parameter, read_callback_, decoder))
+                               if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i, rice_parameter))
                                        return false; /* read_callback_ sets the state for us */
                                residual[sample] = i;
                        }
@@ -2724,9 +2729,9 @@ FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigne
 
 FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder)
 {
-       if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) {
+       if(!FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)) {
                FLAC__uint32 zero = 0;
-               if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &zero, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder))
+               if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &zero, FLAC__bitreader_bits_left_for_byte_alignment(decoder->private_->input)))
                        return false; /* read_callback_ sets the state for us */
                if(zero != 0) {
                        send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
index f6d2944..d9e1612 100644 (file)
@@ -55,7 +55,7 @@
 #include "FLAC/assert.h"
 #include "FLAC/stream_decoder.h"
 #include "protected/stream_encoder.h"
-#include "private/bitbuffer.h"
+#include "private/bitwriter.h"
 #include "private/bitmath.h"
 #include "private/crc.h"
 #include "private/cpu.h"
@@ -175,7 +175,7 @@ static FLAC__bool add_subframe_(
        unsigned blocksize,
        unsigned subframe_bps,
        const FLAC__Subframe *subframe,
-       FLAC__BitBuffer *frame
+       FLAC__BitWriter *frame
 );
 
 static unsigned evaluate_constant_subframe_(
@@ -348,7 +348,7 @@ typedef struct FLAC__StreamEncoderPrivate {
        unsigned best_subframe_bits_mid_side[2];
        FLAC__uint64 *abs_residual_partition_sums;        /* workspace where the sum of abs(candidate residual) for each partition is stored */
        unsigned *raw_bits_per_partition;                 /* workspace where the sum of silog2(candidate residual) for each partition is stored */
-       FLAC__BitBuffer *frame;                           /* the current frame being worked on */
+       FLAC__BitWriter *frame;                           /* the current frame being worked on */
        unsigned loose_mid_side_stereo_frames;            /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
        unsigned loose_mid_side_stereo_frame_count;       /* number of frames using the current channel assignment */
        FLAC__ChannelAssignment last_channel_assignment;
@@ -535,7 +535,7 @@ FLAC_API FLAC__StreamEncoder *FLAC__stream_encoder_new()
                return 0;
        }
 
-       encoder->private_->frame = FLAC__bitbuffer_new();
+       encoder->private_->frame = FLAC__bitwriter_new();
        if(encoder->private_->frame == 0) {
                free(encoder->private_);
                free(encoder->protected_);
@@ -609,7 +609,7 @@ FLAC_API void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
        for(i = 0; i < 2; i++)
                FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_extra[i]);
 
-       FLAC__bitbuffer_delete(encoder->private_->frame);
+       FLAC__bitwriter_delete(encoder->private_->frame);
        free(encoder->private_);
        free(encoder->protected_);
        free(encoder);
@@ -982,7 +982,7 @@ static FLAC__StreamEncoderInitStatus init_stream_internal_(
                return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
        }
 
-       if(!FLAC__bitbuffer_init(encoder->private_->frame)) {
+       if(!FLAC__bitwriter_init(encoder->private_->frame)) {
                encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
                return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
        }
@@ -1040,7 +1040,7 @@ static FLAC__StreamEncoderInitStatus init_stream_internal_(
         */
        if(encoder->protected_->verify)
                encoder->private_->verify.state_hint = ENCODER_IN_MAGIC;
-       if(!FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN)) {
+       if(!FLAC__bitwriter_write_raw_uint32(encoder->private_->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN)) {
                encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
                return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
        }
@@ -1067,10 +1067,6 @@ static FLAC__StreamEncoderInitStatus init_stream_internal_(
        encoder->private_->streaminfo.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */
        memset(encoder->private_->streaminfo.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */
        FLAC__MD5Init(&encoder->private_->md5context);
-       if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
-               encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
-               return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
-       }
        if(!FLAC__add_metadata_block(&encoder->private_->streaminfo, encoder->private_->frame)) {
                encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
                return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
@@ -1107,10 +1103,6 @@ static FLAC__StreamEncoderInitStatus init_stream_internal_(
                vorbis_comment.data.vorbis_comment.vendor_string.entry = 0;
                vorbis_comment.data.vorbis_comment.num_comments = 0;
                vorbis_comment.data.vorbis_comment.comments = 0;
-               if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
-                       encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
-                       return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
-               }
                if(!FLAC__add_metadata_block(&vorbis_comment, encoder->private_->frame)) {
                        encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
                        return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
@@ -1126,10 +1118,6 @@ static FLAC__StreamEncoderInitStatus init_stream_internal_(
         */
        for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
                encoder->protected_->metadata[i]->is_last = (i == encoder->protected_->num_metadata_blocks - 1);
-               if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
-                       encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
-                       return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
-               }
                if(!FLAC__add_metadata_block(encoder->protected_->metadata[i], encoder->private_->frame)) {
                        encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
                        return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
@@ -2447,7 +2435,7 @@ void free_(FLAC__StreamEncoder *encoder)
                        }
                }
        }
-       FLAC__bitbuffer_free(encoder->private_->frame);
+       FLAC__bitwriter_free(encoder->private_->frame);
 }
 
 FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_blocksize)
@@ -2584,9 +2572,12 @@ FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples, FLAC
        const FLAC__byte *buffer;
        size_t bytes;
 
-       FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
+       FLAC__ASSERT(FLAC__bitwriter_is_byte_aligned(encoder->private_->frame));
 
-       FLAC__bitbuffer_get_buffer(encoder->private_->frame, &buffer, &bytes);
+       if(!FLAC__bitwriter_get_buffer(encoder->private_->frame, &buffer, &bytes)) {
+               encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
+               return false;
+       }
 
        if(encoder->protected_->verify) {
                encoder->private_->verify.output.data = buffer;
@@ -2596,7 +2587,8 @@ FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples, FLAC
                }
                else {
                        if(!FLAC__stream_decoder_process_single(encoder->private_->verify.decoder)) {
-                               FLAC__bitbuffer_release_buffer(encoder->private_->frame);
+                               FLAC__bitwriter_release_buffer(encoder->private_->frame);
+                               FLAC__bitwriter_clear(encoder->private_->frame);
                                if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA)
                                        encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
                                return false;
@@ -2605,12 +2597,14 @@ FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples, FLAC
        }
 
        if(write_frame_(encoder, buffer, bytes, samples, is_last_block) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
-               FLAC__bitbuffer_release_buffer(encoder->private_->frame);
+               FLAC__bitwriter_release_buffer(encoder->private_->frame);
+               FLAC__bitwriter_clear(encoder->private_->frame);
                encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
                return false;
        }
 
-       FLAC__bitbuffer_release_buffer(encoder->private_->frame);
+       FLAC__bitwriter_release_buffer(encoder->private_->frame);
+       FLAC__bitwriter_clear(encoder->private_->frame);
 
        if(samples > 0) {
                encoder->private_->streaminfo.data.stream_info.min_framesize = min(bytes, encoder->private_->streaminfo.data.stream_info.min_framesize);
@@ -3057,6 +3051,7 @@ void update_ogg_metadata_(FLAC__StreamEncoder *encoder)
 
 FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block, FLAC__bool is_last_block)
 {
+       FLAC__uint16 crc;
        FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
 
        /*
@@ -3078,7 +3073,7 @@ FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional
        /*
         * Zero-pad the frame to a byte_boundary
         */
-       if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(encoder->private_->frame)) {
+       if(!FLAC__bitwriter_zero_pad_to_byte_boundary(encoder->private_->frame)) {
                encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
                return false;
        }
@@ -3086,8 +3081,14 @@ FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional
        /*
         * CRC-16 the whole thing
         */
-       FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
-       FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__bitbuffer_get_write_crc16(encoder->private_->frame), FLAC__FRAME_FOOTER_CRC_LEN);
+       FLAC__ASSERT(FLAC__bitwriter_is_byte_aligned(encoder->private_->frame));
+       if(
+               !FLAC__bitwriter_get_write_crc16(encoder->private_->frame, &crc) ||
+               !FLAC__bitwriter_write_raw_uint32(encoder->private_->frame, crc, FLAC__FRAME_FOOTER_CRC_LEN)
+       ) {
+               encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
+               return false;
+       }
 
        /*
         * Write it
@@ -3128,10 +3129,6 @@ FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_fracti
        /*
         * Setup the frame
         */
-       if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
-               encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
-               return false;
-       }
        frame_header.blocksize = encoder->protected_->blocksize;
        frame_header.sample_rate = encoder->protected_->sample_rate;
        frame_header.channels = encoder->protected_->channels;
@@ -3395,6 +3392,8 @@ FLAC__bool process_subframe_(
        unsigned _candidate_bits, _best_bits;
        unsigned _best_subframe;
 
+       FLAC__ASSERT(frame_header->blocksize > 0);
+
        /* verbatim subframe is the baseline against which we measure other compressed subframes */
        _best_subframe = 0;
        if(encoder->private_->disable_verbatim_subframes && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER)
@@ -3441,6 +3440,8 @@ FLAC__bool process_subframe_(
                                else {
                                        min_fixed_order = max_fixed_order = guess_fixed_order;
                                }
+                               if(max_fixed_order >= frame_header->blocksize)
+                                       max_fixed_order = frame_header->blocksize - 1;
                                for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
 #ifndef FLAC__INTEGER_ONLY_LIBRARY
                                        if(fixed_residual_bits_per_sample[fixed_order] >= (FLAC__float)subframe_bps)
@@ -3515,6 +3516,8 @@ FLAC__bool process_subframe_(
                                                                        );
                                                                min_lpc_order = max_lpc_order = guess_lpc_order;
                                                        }
+                                                       if(max_lpc_order >= frame_header->blocksize)
+                                                               max_lpc_order = frame_header->blocksize - 1;
                                                        for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
                                                                lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
                                                                if(lpc_residual_bits_per_sample >= (FLAC__double)subframe_bps)
@@ -3594,7 +3597,7 @@ FLAC__bool add_subframe_(
        unsigned blocksize,
        unsigned subframe_bps,
        const FLAC__Subframe *subframe,
-       FLAC__BitBuffer *frame
+       FLAC__BitWriter *frame
 )
 {
        switch(subframe->type) {
@@ -3640,23 +3643,23 @@ static void spotcheck_subframe_estimate_(
 )
 {
        FLAC__bool ret;
-       FLAC__BitBuffer *frame = FLAC__bitbuffer_new();
+       FLAC__BitWriter *frame = FLAC__bitwriter_new();
        if(frame == 0) {
                fprintf(stderr, "EST: can't allocate frame\n");
                return;
        }
-       if(!FLAC__bitbuffer_init(frame)) {
+       if(!FLAC__bitwriter_init(frame)) {
                fprintf(stderr, "EST: can't init frame\n");
                return;
        }
        ret = add_subframe_(encoder, blocksize, subframe_bps, subframe, frame);
        FLAC__ASSERT(ret);
        {
-               const unsigned actual = FLAC__bitbuffer_get_input_bits_unconsumed(frame);
+               const unsigned actual = FLAC__bitwriter_get_input_bits_unconsumed(frame);
                if(estimate != actual)
                        fprintf(stderr, "EST: bad, frame#%u sub#%%d type=%8s est=%u, actual=%u, delta=%d\n", encoder->private_->current_frame_number, FLAC__SubframeTypeString[subframe->type], estimate, actual, (int)actual-(int)estimate);
        }
-       FLAC__bitbuffer_delete(frame);
+       FLAC__bitwriter_delete(frame);
 }
 #endif
 
@@ -3672,7 +3675,7 @@ unsigned evaluate_constant_subframe_(
        subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
        subframe->data.constant.value = signal;
 
-       estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe_bps;
+       estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe->wasted_bits + subframe_bps;
 
 #if SPOTCHECK_ESTIMATE
        spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate);
@@ -3732,7 +3735,7 @@ unsigned evaluate_fixed_subframe_(
        for(i = 0; i < order; i++)
                subframe->data.fixed.warmup[i] = signal[i];
 
-       estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (order * subframe_bps) + residual_bits;
+       estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe->wasted_bits + (order * subframe_bps) + residual_bits;
 
 #if SPOTCHECK_ESTIMATE
        spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate);
@@ -3815,7 +3818,7 @@ unsigned evaluate_lpc_subframe_(
        for(i = 0; i < order; i++)
                subframe->data.lpc.warmup[i] = signal[i];
 
-       estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN + FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN + (order * (qlp_coeff_precision + subframe_bps)) + residual_bits;
+       estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe->wasted_bits + FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN + FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN + (order * (qlp_coeff_precision + subframe_bps)) + residual_bits;
 
 #if SPOTCHECK_ESTIMATE
        spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate);
@@ -3839,7 +3842,7 @@ unsigned evaluate_verbatim_subframe_(
 
        subframe->data.verbatim.data = signal;
 
-       estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (blocksize * subframe_bps);
+       estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe->wasted_bits + (blocksize * subframe_bps);
 
 #if SPOTCHECK_ESTIMATE
        spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate);
@@ -3954,7 +3957,8 @@ void precompute_partition_info_sums_(
                                partition_samples -= predictor_order;
                        abs_residual_partition_sum = 0;
                        for(partition_sample = 0; partition_sample < partition_samples; partition_sample++, residual_sample++) {
-#if 0 /* OPT: abs() may be faster for some compilers */
+#if defined _MSC_VER && _MSC_VER <= 1200
+                               /* OPT: abs() may be faster for some compilers */
                                abs_residual_partition_sum += abs(residual[residual_sample]); /* abs(INT_MIN) is undefined, but if the residual is INT_MIN we have bigger problems */
 #else
                                const FLAC__int32 r = residual[residual_sample];
@@ -4044,6 +4048,7 @@ void precompute_partition_info_escapes_(
        }
 }
 
+/*@@@@@@ overflow is a possible problem here for hi-res samples */
 #ifdef EXACT_RICE_BITS_CALCULATION
 static __inline unsigned count_rice_bits_in_partition_(
        const unsigned rice_parameter,
@@ -4051,9 +4056,12 @@ static __inline unsigned count_rice_bits_in_partition_(
        const FLAC__int32 *residual
 )
 {
-       unsigned i, partition_bits = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
+       unsigned i, partition_bits =
+               FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN +
+               (1+rice_parameter) * partition_samples /* 1 for unary stop bit + rice_parameter for the binary portion */
+       ;
        for(i = 0; i < partition_samples; i++)
-               partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter);
+               partition_bits += ( (FLAC__uint32)((residual[i]<<1)^(residual[i]>>31)) >> rice_parameter );
        return partition_bits;
 }
 #else
@@ -4068,8 +4076,8 @@ static __inline unsigned count_rice_bits_in_partition_(
                (1+rice_parameter) * partition_samples + /* 1 for unary stop bit + rice_parameter for the binary portion */
                (
                        rice_parameter?
-                               (abs_residual_partition_sum >> (rice_parameter-1)) /* rice_parameter-1 because the real coder sign-folds instead of using a sign bit */
-                               : (abs_residual_partition_sum << 1) /* can't shift by negative number, so reverse */
+                               (unsigned)(abs_residual_partition_sum >> (rice_parameter-1)) /* rice_parameter-1 because the real coder sign-folds instead of using a sign bit */
+                               : (unsigned)(abs_residual_partition_sum << 1) /* can't shift by negative number, so reverse */
                )
                - (partition_samples >> 1)
                /* -(partition_samples>>1) to subtract out extra contributions to the abs_residual_partition_sum.
index 0738c92..5554028 100644 (file)
 #endif
 #define max(x,y) ((x)>(y)?(x):(y))
 
-static FLAC__bool add_entropy_coding_method_(FLAC__BitBuffer *bb, const FLAC__EntropyCodingMethod *method);
-static FLAC__bool add_residual_partitioned_rice_(FLAC__BitBuffer *bb, const FLAC__int32 residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned rice_parameters[], const unsigned raw_bits[], const unsigned partition_order);
+static FLAC__bool add_entropy_coding_method_(FLAC__BitWriter *bw, const FLAC__EntropyCodingMethod *method);
+static FLAC__bool add_residual_partitioned_rice_(FLAC__BitWriter *bw, const FLAC__int32 residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned rice_parameters[], const unsigned raw_bits[], const unsigned partition_order);
 
-FLAC__bool FLAC__add_metadata_block(const FLAC__StreamMetadata *metadata, FLAC__BitBuffer *bb)
+FLAC__bool FLAC__add_metadata_block(const FLAC__StreamMetadata *metadata, FLAC__BitWriter *bw)
 {
        unsigned i, j;
        const unsigned vendor_string_length = (unsigned)strlen(FLAC__VENDOR_STRING);
 
-       if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->is_last, FLAC__STREAM_METADATA_IS_LAST_LEN))
+       if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->is_last, FLAC__STREAM_METADATA_IS_LAST_LEN))
                return false;
 
-       if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->type, FLAC__STREAM_METADATA_TYPE_LEN))
+       if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->type, FLAC__STREAM_METADATA_TYPE_LEN))
                return false;
 
        /*
@@ -68,111 +68,111 @@ FLAC__bool FLAC__add_metadata_block(const FLAC__StreamMetadata *metadata, FLAC__
                i += vendor_string_length;
        }
        FLAC__ASSERT(i < (1u << FLAC__STREAM_METADATA_LENGTH_LEN));
-       if(!FLAC__bitbuffer_write_raw_uint32(bb, i, FLAC__STREAM_METADATA_LENGTH_LEN))
+       if(!FLAC__bitwriter_write_raw_uint32(bw, i, FLAC__STREAM_METADATA_LENGTH_LEN))
                return false;
 
        switch(metadata->type) {
                case FLAC__METADATA_TYPE_STREAMINFO:
                        FLAC__ASSERT(metadata->data.stream_info.min_blocksize < (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN));
-                       if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.stream_info.min_blocksize, FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN))
+                       if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.stream_info.min_blocksize, FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN))
                                return false;
                        FLAC__ASSERT(metadata->data.stream_info.max_blocksize < (1u << FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN));
-                       if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.stream_info.max_blocksize, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN))
+                       if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.stream_info.max_blocksize, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN))
                                return false;
                        FLAC__ASSERT(metadata->data.stream_info.min_framesize < (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN));
-                       if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.stream_info.min_framesize, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN))
+                       if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.stream_info.min_framesize, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN))
                                return false;
                        FLAC__ASSERT(metadata->data.stream_info.max_framesize < (1u << FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN));
-                       if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.stream_info.max_framesize, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN))
+                       if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.stream_info.max_framesize, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN))
                                return false;
                        FLAC__ASSERT(FLAC__format_sample_rate_is_valid(metadata->data.stream_info.sample_rate));
-                       if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.stream_info.sample_rate, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN))
+                       if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.stream_info.sample_rate, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN))
                                return false;
                        FLAC__ASSERT(metadata->data.stream_info.channels > 0);
                        FLAC__ASSERT(metadata->data.stream_info.channels <= (1u << FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN));
-                       if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.stream_info.channels-1, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN))
+                       if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.stream_info.channels-1, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN))
                                return false;
                        FLAC__ASSERT(metadata->data.stream_info.bits_per_sample > 0);
                        FLAC__ASSERT(metadata->data.stream_info.bits_per_sample <= (1u << FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN));
-                       if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.stream_info.bits_per_sample-1, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN))
+                       if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.stream_info.bits_per_sample-1, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN))
                                return false;
-                       if(!FLAC__bitbuffer_write_raw_uint64(bb, metadata->data.stream_info.total_samples, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN))
+                       if(!FLAC__bitwriter_write_raw_uint64(bw, metadata->data.stream_info.total_samples, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN))
                                return false;
-                       if(!FLAC__bitbuffer_write_byte_block(bb, metadata->data.stream_info.md5sum, 16))
+                       if(!FLAC__bitwriter_write_byte_block(bw, metadata->data.stream_info.md5sum, 16))
                                return false;
                        break;
                case FLAC__METADATA_TYPE_PADDING:
-                       if(!FLAC__bitbuffer_write_zeroes(bb, metadata->length * 8))
+                       if(!FLAC__bitwriter_write_zeroes(bw, metadata->length * 8))
                                return false;
                        break;
                case FLAC__METADATA_TYPE_APPLICATION:
-                       if(!FLAC__bitbuffer_write_byte_block(bb, metadata->data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8))
+                       if(!FLAC__bitwriter_write_byte_block(bw, metadata->data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8))
                                return false;
-                       if(!FLAC__bitbuffer_write_byte_block(bb, metadata->data.application.data, metadata->length - (FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8)))
+                       if(!FLAC__bitwriter_write_byte_block(bw, metadata->data.application.data, metadata->length - (FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8)))
                                return false;
                        break;
                case FLAC__METADATA_TYPE_SEEKTABLE:
                        for(i = 0; i < metadata->data.seek_table.num_points; i++) {
-                               if(!FLAC__bitbuffer_write_raw_uint64(bb, metadata->data.seek_table.points[i].sample_number, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN))
+                               if(!FLAC__bitwriter_write_raw_uint64(bw, metadata->data.seek_table.points[i].sample_number, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN))
                                        return false;
-                               if(!FLAC__bitbuffer_write_raw_uint64(bb, metadata->data.seek_table.points[i].stream_offset, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN))
+                               if(!FLAC__bitwriter_write_raw_uint64(bw, metadata->data.seek_table.points[i].stream_offset, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN))
                                        return false;
-                               if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.seek_table.points[i].frame_samples, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN))
+                               if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.seek_table.points[i].frame_samples, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN))
                                        return false;
                        }
                        break;
                case FLAC__METADATA_TYPE_VORBIS_COMMENT:
-                       if(!FLAC__bitbuffer_write_raw_uint32_little_endian(bb, vendor_string_length))
+                       if(!FLAC__bitwriter_write_raw_uint32_little_endian(bw, vendor_string_length))
                                return false;
-                       if(!FLAC__bitbuffer_write_byte_block(bb, (const FLAC__byte*)FLAC__VENDOR_STRING, vendor_string_length))
+                       if(!FLAC__bitwriter_write_byte_block(bw, (const FLAC__byte*)FLAC__VENDOR_STRING, vendor_string_length))
                                return false;
-                       if(!FLAC__bitbuffer_write_raw_uint32_little_endian(bb, metadata->data.vorbis_comment.num_comments))
+                       if(!FLAC__bitwriter_write_raw_uint32_little_endian(bw, metadata->data.vorbis_comment.num_comments))
                                return false;
                        for(i = 0; i < metadata->data.vorbis_comment.num_comments; i++) {
-                               if(!FLAC__bitbuffer_write_raw_uint32_little_endian(bb, metadata->data.vorbis_comment.comments[i].length))
+                               if(!FLAC__bitwriter_write_raw_uint32_little_endian(bw, metadata->data.vorbis_comment.comments[i].length))
                                        return false;
-                               if(!FLAC__bitbuffer_write_byte_block(bb, metadata->data.vorbis_comment.comments[i].entry, metadata->data.vorbis_comment.comments[i].length))
+                               if(!FLAC__bitwriter_write_byte_block(bw, metadata->data.vorbis_comment.comments[i].entry, metadata->data.vorbis_comment.comments[i].length))
                                        return false;
                        }
                        break;
                case FLAC__METADATA_TYPE_CUESHEET:
                        FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0);
-                       if(!FLAC__bitbuffer_write_byte_block(bb, (const FLAC__byte*)metadata->data.cue_sheet.media_catalog_number, FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN/8))
+                       if(!FLAC__bitwriter_write_byte_block(bw, (const FLAC__byte*)metadata->data.cue_sheet.media_catalog_number, FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN/8))
                                return false;
-                       if(!FLAC__bitbuffer_write_raw_uint64(bb, metadata->data.cue_sheet.lead_in, FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN))
+                       if(!FLAC__bitwriter_write_raw_uint64(bw, metadata->data.cue_sheet.lead_in, FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN))
                                return false;
-                       if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.cue_sheet.is_cd? 1 : 0, FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN))
+                       if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.cue_sheet.is_cd? 1 : 0, FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN))
                                return false;
-                       if(!FLAC__bitbuffer_write_zeroes(bb, FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN))
+                       if(!FLAC__bitwriter_write_zeroes(bw, FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN))
                                return false;
-                       if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.cue_sheet.num_tracks, FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN))
+                       if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.cue_sheet.num_tracks, FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN))
                                return false;
                        for(i = 0; i < metadata->data.cue_sheet.num_tracks; i++) {
                                const FLAC__StreamMetadata_CueSheet_Track *track = metadata->data.cue_sheet.tracks + i;
 
-                               if(!FLAC__bitbuffer_write_raw_uint64(bb, track->offset, FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN))
+                               if(!FLAC__bitwriter_write_raw_uint64(bw, track->offset, FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN))
                                        return false;
-                            &nb