several speed improvements: completely rewritten bitbuffer which uses native machine word size instead of bytes; much faster rice partition size estimation; crc16 calculation in machine word size

This commit is contained in:
Josh Coalson 2007-01-28 17:40:26 +00:00
parent 76ba93a4ee
commit 423f804d50
26 changed files with 3129 additions and 3775 deletions

View 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

View 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>
@ -106,6 +107,8 @@
<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>

View 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 \

View File

@ -61,8 +61,9 @@ endif
endif
SRCS_C = \
bitbuffer.c \
bitmath.c \
bitreader.c \
bitwriter.c \
cpu.c \
crc.c \
fixed.c \

File diff suppressed because it is too large Load Diff

1164
src/libFLAC/bitreader.c Normal file

File diff suppressed because it is too large Load Diff

845
src/libFLAC/bitwriter.c Normal file
View File

@ -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;
}

View 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;
}

View File

@ -30,8 +30,9 @@
noinst_HEADERS = \
all.h \
bitbuffer.h \
bitmath.h \
bitreader.h \
bitwriter.h \
cpu.h \
crc.h \
fixed.h \

View 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"

View File

@ -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

View File

@ -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

View File

@ -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

View 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

View File

@ -33,13 +33,13 @@
#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

View File

@ -187,11 +187,15 @@ SOURCE=.\ia32\nasm.h
# End Group
# Begin Source File
SOURCE=.\bitbuffer.c
SOURCE=.\bitmath.c
# End Source File
# Begin Source File
SOURCE=.\bitmath.c
SOURCE=.\bitreader.c
# End Source File
# Begin Source File
SOURCE=.\bitwriter.c
# End Source File
# Begin Source File
@ -275,11 +279,15 @@ SOURCE=.\include\private\all.h
# End Source File
# Begin Source File
SOURCE=.\include\private\bitbuffer.h
SOURCE=.\include\private\bitmath.h
# End Source File
# Begin Source File
SOURCE=.\include\private\bitmath.h
SOURCE=.\include\private\bitreader.h
# End Source File
# Begin Source File
SOURCE=.\include\private\bitwriter.h
# End Source File
# Begin Source File

View File

@ -180,11 +180,15 @@ SOURCE=.\ia32\nasm.h
# End Group
# Begin Source File
SOURCE=.\bitbuffer.c
SOURCE=.\bitmath.c
# End Source File
# Begin Source File
SOURCE=.\bitmath.c
SOURCE=.\bitreader.c
# End Source File
# Begin Source File
SOURCE=.\bitwriter.c
# End Source File
# Begin Source File
@ -268,11 +272,15 @@ SOURCE=.\include\private\all.h
# End Source File
# Begin Source File
SOURCE=.\include\private\bitbuffer.h
SOURCE=.\include\private\bitmath.h
# End Source File
# Begin Source File
SOURCE=.\include\private\bitmath.h
SOURCE=.\include\private\bitreader.h
# End Source File
# Begin Source File
SOURCE=.\include\private\bitwriter.h
# End Source File
# Begin Source File

View 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);

View 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.

View File

@ -44,18 +44,18 @@
#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;
if(!FLAC__bitbuffer_write_raw_uint32(bb, track->number, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN))
if(!FLAC__bitwriter_write_raw_uint32(bw, track->number, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN))
return false;
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0);
if(!FLAC__bitbuffer_write_byte_block(bb, (const FLAC__byte*)track->isrc, FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN/8))
if(!FLAC__bitwriter_write_byte_block(bw, (const FLAC__byte*)track->isrc, FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN/8))
return false;
if(!FLAC__bitbuffer_write_raw_uint32(bb, track->type, FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN))
if(!FLAC__bitwriter_write_raw_uint32(bw, track->type, FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN))
return false;
if(!FLAC__bitbuffer_write_raw_uint32(bb, track->pre_emphasis, FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN))
if(!FLAC__bitwriter_write_raw_uint32(bw, track->pre_emphasis, FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN))
return false;
if(!FLAC__bitbuffer_write_zeroes(bb, FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN))
if(!FLAC__bitwriter_write_zeroes(bw, FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN))
return false;
if(!FLAC__bitbuffer_write_raw_uint32(bb, track->num_indices, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN))
if(!FLAC__bitwriter_write_raw_uint32(bw, track->num_indices, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN))
return false;
for(j = 0; j < track->num_indices; j++) {
const FLAC__StreamMetadata_CueSheet_Index *index = track->indices + j;
if(!FLAC__bitbuffer_write_raw_uint64(bb, index->offset, FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN))
if(!FLAC__bitwriter_write_raw_uint64(bw, index->offset, FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN))
return false;
if(!FLAC__bitbuffer_write_raw_uint32(bb, index->number, FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN))
if(!FLAC__bitwriter_write_raw_uint32(bw, index->number, FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN))
return false;
if(!FLAC__bitbuffer_write_zeroes(bb, FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN))
if(!FLAC__bitwriter_write_zeroes(bw, FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN))
return false;
}
}
@ -180,52 +180,53 @@ FLAC__bool FLAC__add_metadata_block(const FLAC__StreamMetadata *metadata, FLAC__
case FLAC__METADATA_TYPE_PICTURE:
{
size_t len;
if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.picture.type, FLAC__STREAM_METADATA_PICTURE_TYPE_LEN))
if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.picture.type, FLAC__STREAM_METADATA_PICTURE_TYPE_LEN))
return false;
len = strlen(metadata->data.picture.mime_type);
if(!FLAC__bitbuffer_write_raw_uint32(bb, len, FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN))
if(!FLAC__bitwriter_write_raw_uint32(bw, len, FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN))
return false;
if(!FLAC__bitbuffer_write_byte_block(bb, (const FLAC__byte*)metadata->data.picture.mime_type, len))
if(!FLAC__bitwriter_write_byte_block(bw, (const FLAC__byte*)metadata->data.picture.mime_type, len))
return false;
len = strlen((const char *)metadata->data.picture.description);
if(!FLAC__bitbuffer_write_raw_uint32(bb, len, FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN))
if(!FLAC__bitwriter_write_raw_uint32(bw, len, FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN))
return false;
if(!FLAC__bitbuffer_write_byte_block(bb, metadata->data.picture.description, len))
if(!FLAC__bitwriter_write_byte_block(bw, metadata->data.picture.description, len))
return false;
if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.picture.width, FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN))
if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.picture.width, FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN))
return false;
if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.picture.height, FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN))
if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.picture.height, FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN))
return false;
if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.picture.depth, FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN))
if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.picture.depth, FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN))
return false;
if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.picture.colors, FLAC__STREAM_METADATA_PICTURE_COLORS_LEN))
if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.picture.colors, FLAC__STREAM_METADATA_PICTURE_COLORS_LEN))
return false;
if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.picture.data_length, FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN))
if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.picture.data_length, FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN))
return false;
if(!FLAC__bitbuffer_write_byte_block(bb, metadata->data.picture.data, metadata->data.picture.data_length))
if(!FLAC__bitwriter_write_byte_block(bw, metadata->data.picture.data, metadata->data.picture.data_length))
return false;
}
break;
default:
if(!FLAC__bitbuffer_write_byte_block(bb, metadata->data.unknown.data, metadata->length))
if(!FLAC__bitwriter_write_byte_block(bw, metadata->data.unknown.data, metadata->length))
return false;
break;
}
FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(bb));
FLAC__ASSERT(FLAC__bitwriter_is_byte_aligned(bw));
return true;
}
FLAC__bool FLAC__frame_add_header(const FLAC__FrameHeader *header, FLAC__BitBuffer *bb)
FLAC__bool FLAC__frame_add_header(const FLAC__FrameHeader *header, FLAC__BitWriter *bw)
{
unsigned u, blocksize_hint, sample_rate_hint;
FLAC__byte crc;
FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(bb));
FLAC__ASSERT(FLAC__bitwriter_is_byte_aligned(bw));
if(!FLAC__bitbuffer_write_raw_uint32(bb, FLAC__FRAME_HEADER_SYNC, FLAC__FRAME_HEADER_SYNC_LEN))
if(!FLAC__bitwriter_write_raw_uint32(bw, FLAC__FRAME_HEADER_SYNC, FLAC__FRAME_HEADER_SYNC_LEN))
return false;
if(!FLAC__bitbuffer_write_raw_uint32(bb, 0, FLAC__FRAME_HEADER_RESERVED_LEN))
if(!FLAC__bitwriter_write_raw_uint32(bw, 0, FLAC__FRAME_HEADER_RESERVED_LEN))
return false;
FLAC__ASSERT(header->blocksize > 0 && header->blocksize <= FLAC__MAX_BLOCK_SIZE);
@ -255,7 +256,7 @@ FLAC__bool FLAC__frame_add_header(const FLAC__FrameHeader *header, FLAC__BitBuff
u = 0;
break;
}
if(!FLAC__bitbuffer_write_raw_uint32(bb, u, FLAC__FRAME_HEADER_BLOCK_SIZE_LEN))
if(!FLAC__bitwriter_write_raw_uint32(bw, u, FLAC__FRAME_HEADER_BLOCK_SIZE_LEN))
return false;
FLAC__ASSERT(FLAC__format_sample_rate_is_valid(header->sample_rate));
@ -280,7 +281,7 @@ FLAC__bool FLAC__frame_add_header(const FLAC__FrameHeader *header, FLAC__BitBuff
u = 0;
break;
}
if(!FLAC__bitbuffer_write_raw_uint32(bb, u, FLAC__FRAME_HEADER_SAMPLE_RATE_LEN))
if(!FLAC__bitwriter_write_raw_uint32(bw, u, FLAC__FRAME_HEADER_SAMPLE_RATE_LEN))
return false;
FLAC__ASSERT(header->channels > 0 && header->channels <= (1u << FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN) && header->channels <= FLAC__MAX_CHANNELS);
@ -303,7 +304,7 @@ FLAC__bool FLAC__frame_add_header(const FLAC__FrameHeader *header, FLAC__BitBuff
default:
FLAC__ASSERT(0);
}
if(!FLAC__bitbuffer_write_raw_uint32(bb, u, FLAC__FRAME_HEADER_CHANNEL_ASSIGNMENT_LEN))
if(!FLAC__bitwriter_write_raw_uint32(bw, u, FLAC__FRAME_HEADER_CHANNEL_ASSIGNMENT_LEN))
return false;
FLAC__ASSERT(header->bits_per_sample > 0 && header->bits_per_sample <= (1u << FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN));
@ -315,74 +316,76 @@ FLAC__bool FLAC__frame_add_header(const FLAC__FrameHeader *header, FLAC__BitBuff
case 24: u = 6; break;
default: u = 0; break;
}
if(!FLAC__bitbuffer_write_raw_uint32(bb, u, FLAC__FRAME_HEADER_BITS_PER_SAMPLE_LEN))
if(!FLAC__bitwriter_write_raw_uint32(bw, u, FLAC__FRAME_HEADER_BITS_PER_SAMPLE_LEN))
return false;
if(!FLAC__bitbuffer_write_raw_uint32(bb, 0, FLAC__FRAME_HEADER_ZERO_PAD_LEN))
if(!FLAC__bitwriter_write_raw_uint32(bw, 0, FLAC__FRAME_HEADER_ZERO_PAD_LEN))
return false;
FLAC__ASSERT(header->number_type == FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER);
if(!FLAC__bitbuffer_write_utf8_uint32(bb, header->number.frame_number))
if(!FLAC__bitwriter_write_utf8_uint32(bw, header->number.frame_number))
return false;
if(blocksize_hint)
if(!FLAC__bitbuffer_write_raw_uint32(bb, header->blocksize-1, (blocksize_hint==6)? 8:16))
if(!FLAC__bitwriter_write_raw_uint32(bw, header->blocksize-1, (blocksize_hint==6)? 8:16))
return false;
switch(sample_rate_hint) {
case 12:
if(!FLAC__bitbuffer_write_raw_uint32(bb, header->sample_rate / 1000, 8))
if(!FLAC__bitwriter_write_raw_uint32(bw, header->sample_rate / 1000, 8))
return false;
break;
case 13:
if(!FLAC__bitbuffer_write_raw_uint32(bb, header->sample_rate, 16))
if(!FLAC__bitwriter_write_raw_uint32(bw, header->sample_rate, 16))
return false;
break;
case 14:
if(!FLAC__bitbuffer_write_raw_uint32(bb, header->sample_rate / 10, 16))
if(!FLAC__bitwriter_write_raw_uint32(bw, header->sample_rate / 10, 16))
return false;
break;
}
/* write the CRC */
if(!FLAC__bitbuffer_write_raw_uint32(bb, FLAC__bitbuffer_get_write_crc8(bb), FLAC__FRAME_HEADER_CRC_LEN))
if(!FLAC__bitwriter_get_write_crc8(bw, &crc))
return false;
if(!FLAC__bitwriter_write_raw_uint32(bw, crc, FLAC__FRAME_HEADER_CRC_LEN))
return false;
return true;
}
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_constant(const FLAC__Subframe_Constant *subframe, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitWriter *bw)
{
FLAC__bool ok;
ok =
FLAC__bitbuffer_write_raw_uint32(bb, FLAC__SUBFRAME_TYPE_CONSTANT_BYTE_ALIGNED_MASK | (wasted_bits? 1:0), FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN) &&
(wasted_bits? FLAC__bitbuffer_write_unary_unsigned(bb, wasted_bits-1) : true) &&
FLAC__bitbuffer_write_raw_int32(bb, subframe->value, subframe_bps)
FLAC__bitwriter_write_raw_uint32(bw, FLAC__SUBFRAME_TYPE_CONSTANT_BYTE_ALIGNED_MASK | (wasted_bits? 1:0), FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN) &&
(wasted_bits? FLAC__bitwriter_write_unary_unsigned(bw, wasted_bits-1) : true) &&
FLAC__bitwriter_write_raw_int32(bw, subframe->value, subframe_bps)
;
return ok;
}
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_fixed(const FLAC__Subframe_Fixed *subframe, unsigned residual_samples, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitWriter *bw)
{
unsigned i;
if(!FLAC__bitbuffer_write_raw_uint32(bb, FLAC__SUBFRAME_TYPE_FIXED_BYTE_ALIGNED_MASK | (subframe->order<<1) | (wasted_bits? 1:0), FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN))
if(!FLAC__bitwriter_write_raw_uint32(bw, FLAC__SUBFRAME_TYPE_FIXED_BYTE_ALIGNED_MASK | (subframe->order<<1) | (wasted_bits? 1:0), FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN))
return false;
if(wasted_bits)
if(!FLAC__bitbuffer_write_unary_unsigned(bb, wasted_bits-1))
if(!FLAC__bitwriter_write_unary_unsigned(bw, wasted_bits-1))
return false;
for(i = 0; i < subframe->order; i++)
if(!FLAC__bitbuffer_write_raw_int32(bb, subframe->warmup[i], subframe_bps))
if(!FLAC__bitwriter_write_raw_int32(bw, subframe->warmup[i], subframe_bps))
return false;
if(!add_entropy_coding_method_(bb, &subframe->entropy_coding_method))
if(!add_entropy_coding_method_(bw, &subframe->entropy_coding_method))
return false;
switch(subframe->entropy_coding_method.type) {
case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
if(!add_residual_partitioned_rice_(bb, subframe->residual, residual_samples, subframe->order, subframe->entropy_coding_method.data.partitioned_rice.contents->parameters, subframe->entropy_coding_method.data.partitioned_rice.contents->raw_bits, subframe->entropy_coding_method.data.partitioned_rice.order))
if(!add_residual_partitioned_rice_(bw, subframe->residual, residual_samples, subframe->order, subframe->entropy_coding_method.data.partitioned_rice.contents->parameters, subframe->entropy_coding_method.data.partitioned_rice.contents->raw_bits, subframe->entropy_coding_method.data.partitioned_rice.order))
return false;
break;
default:
@ -392,33 +395,33 @@ FLAC__bool FLAC__subframe_add_fixed(const FLAC__Subframe_Fixed *subframe, unsign
return true;
}
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_lpc(const FLAC__Subframe_LPC *subframe, unsigned residual_samples, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitWriter *bw)
{
unsigned i;
if(!FLAC__bitbuffer_write_raw_uint32(bb, FLAC__SUBFRAME_TYPE_LPC_BYTE_ALIGNED_MASK | ((subframe->order-1)<<1) | (wasted_bits? 1:0), FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN))
if(!FLAC__bitwriter_write_raw_uint32(bw, FLAC__SUBFRAME_TYPE_LPC_BYTE_ALIGNED_MASK | ((subframe->order-1)<<1) | (wasted_bits? 1:0), FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN))
return false;
if(wasted_bits)
if(!FLAC__bitbuffer_write_unary_unsigned(bb, wasted_bits-1))
if(!FLAC__bitwriter_write_unary_unsigned(bw, wasted_bits-1))
return false;
for(i = 0; i < subframe->order; i++)
if(!FLAC__bitbuffer_write_raw_int32(bb, subframe->warmup[i], subframe_bps))
if(!FLAC__bitwriter_write_raw_int32(bw, subframe->warmup[i], subframe_bps))
return false;
if(!FLAC__bitbuffer_write_raw_uint32(bb, subframe->qlp_coeff_precision-1, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN))
if(!FLAC__bitwriter_write_raw_uint32(bw, subframe->qlp_coeff_precision-1, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN))
return false;
if(!FLAC__bitbuffer_write_raw_int32(bb, subframe->quantization_level, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN))
if(!FLAC__bitwriter_write_raw_int32(bw, subframe->quantization_level, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN))
return false;
for(i = 0; i < subframe->order; i++)
if(!FLAC__bitbuffer_write_raw_int32(bb, subframe->qlp_coeff[i], subframe->qlp_coeff_precision))
if(!FLAC__bitwriter_write_raw_int32(bw, subframe->qlp_coeff[i], subframe->qlp_coeff_precision))
return false;
if(!add_entropy_coding_method_(bb, &subframe->entropy_coding_method))
if(!add_entropy_coding_method_(bw, &subframe->entropy_coding_method))
return false;
switch(subframe->entropy_coding_method.type) {
case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
if(!add_residual_partitioned_rice_(bb, subframe->residual, residual_samples, subframe->order, subframe->entropy_coding_method.data.partitioned_rice.contents->parameters, subframe->entropy_coding_method.data.partitioned_rice.contents->raw_bits, subframe->entropy_coding_method.data.partitioned_rice.order))
if(!add_residual_partitioned_rice_(bw, subframe->residual, residual_samples, subframe->order, subframe->entropy_coding_method.data.partitioned_rice.contents->parameters, subframe->entropy_coding_method.data.partitioned_rice.contents->raw_bits, subframe->entropy_coding_method.data.partitioned_rice.order))
return false;
break;
default:
@ -428,31 +431,31 @@ FLAC__bool FLAC__subframe_add_lpc(const FLAC__Subframe_LPC *subframe, unsigned r
return true;
}
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__subframe_add_verbatim(const FLAC__Subframe_Verbatim *subframe, unsigned samples, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitWriter *bw)
{
unsigned i;
const FLAC__int32 *signal = subframe->data;
if(!FLAC__bitbuffer_write_raw_uint32(bb, FLAC__SUBFRAME_TYPE_VERBATIM_BYTE_ALIGNED_MASK | (wasted_bits? 1:0), FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN))
if(!FLAC__bitwriter_write_raw_uint32(bw, FLAC__SUBFRAME_TYPE_VERBATIM_BYTE_ALIGNED_MASK | (wasted_bits? 1:0), FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN))
return false;
if(wasted_bits)
if(!FLAC__bitbuffer_write_unary_unsigned(bb, wasted_bits-1))
if(!FLAC__bitwriter_write_unary_unsigned(bw, wasted_bits-1))
return false;
for(i = 0; i < samples; i++)
if(!FLAC__bitbuffer_write_raw_int32(bb, signal[i], subframe_bps))
if(!FLAC__bitwriter_write_raw_int32(bw, signal[i], subframe_bps))
return false;
return true;
}
FLAC__bool add_entropy_coding_method_(FLAC__BitBuffer *bb, const FLAC__EntropyCodingMethod *method)
FLAC__bool add_entropy_coding_method_(FLAC__BitWriter *bw, const FLAC__EntropyCodingMethod *method)
{
if(!FLAC__bitbuffer_write_raw_uint32(bb, method->type, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN))
if(!FLAC__bitwriter_write_raw_uint32(bw, method->type, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN))
return false;
switch(method->type) {
case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
if(!FLAC__bitbuffer_write_raw_uint32(bb, method->data.partitioned_rice.order, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
if(!FLAC__bitwriter_write_raw_uint32(bw, method->data.partitioned_rice.order, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
return false;
break;
default:
@ -461,24 +464,22 @@ FLAC__bool add_entropy_coding_method_(FLAC__BitBuffer *bb, const FLAC__EntropyCo
return true;
}
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)
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)
{
if(partition_order == 0) {
unsigned i;
if(!FLAC__bitbuffer_write_raw_uint32(bb, rice_parameters[0], FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN))
if(!FLAC__bitwriter_write_raw_uint32(bw, rice_parameters[0], FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN))
return false;
if(rice_parameters[0] < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
for(i = 0; i < residual_samples; i++) {
if(!FLAC__bitbuffer_write_rice_signed(bb, residual[i], rice_parameters[0]))
return false;
}
if(!FLAC__bitwriter_write_rice_signed_block(bw, residual, residual_samples, rice_parameters[0]))
return false;
}
else {
if(!FLAC__bitbuffer_write_raw_uint32(bb, raw_bits[0], FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN))
if(!FLAC__bitwriter_write_raw_uint32(bw, raw_bits[0], FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN))
return false;
for(i = 0; i < residual_samples; i++) {
if(!FLAC__bitbuffer_write_raw_int32(bb, residual[i], raw_bits[0]))
if(!FLAC__bitwriter_write_raw_int32(bw, residual[i], raw_bits[0]))
return false;
}
}
@ -489,23 +490,21 @@ FLAC__bool add_residual_partitioned_rice_(FLAC__BitBuffer *bb, const FLAC__int32
unsigned partition_samples;
const unsigned default_partition_samples = (residual_samples+predictor_order) >> partition_order;
for(i = 0; i < (1u<<partition_order); i++) {
if(!FLAC__bitbuffer_write_raw_uint32(bb, rice_parameters[i], FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN))
if(!FLAC__bitwriter_write_raw_uint32(bw, rice_parameters[i], FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN))
return false;
partition_samples = default_partition_samples;
if(i == 0)
partition_samples -= predictor_order;
k += partition_samples;
if(rice_parameters[i] < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
for(j = k_last; j < k; j++) {
if(!FLAC__bitbuffer_write_rice_signed(bb, residual[j], rice_parameters[i]))
return false;
}
if(!FLAC__bitwriter_write_rice_signed_block(bw, residual+k_last, k-k_last, rice_parameters[i]))
return false;
}
else {
if(!FLAC__bitbuffer_write_raw_uint32(bb, raw_bits[i], FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN))
if(!FLAC__bitwriter_write_raw_uint32(bw, raw_bits[i], FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN))
return false;
for(j = k_last; j < k; j++) {
if(!FLAC__bitbuffer_write_raw_int32(bb, residual[j], raw_bits[i]))
if(!FLAC__bitwriter_write_raw_int32(bw, residual[j], raw_bits[i]))
return false;
}
}

View File

@ -30,7 +30,7 @@ test_libFLAC_LDADD = \
@OGG_LIBS@ \
-lm
test_libFLAC_SOURCES = \
bitbuffer.c \
bitwriter.c \
decoders.c \
encoders.c \
format.c \
@ -38,7 +38,7 @@ test_libFLAC_SOURCES = \
metadata.c \
metadata_manip.c \
metadata_object.c \
bitbuffer.h \
bitwriter.h \
decoders.h \
encoders.h \
format.h \

View File

@ -33,7 +33,7 @@ LIBS = -lgrabbag -lreplaygain_analysis -ltest_libs_common -lFLAC -L$(OGG_LIB_DIR
endif
SRCS_C = \
bitbuffer.c \
bitwriter.c \
decoders.c \
encoders.c \
format.c \

View File

@ -1,758 +0,0 @@
/* test_libFLAC - Unit tester for libFLAC
* Copyright (C) 2000,2001,2002,2003,2004,2005,2006 Josh Coalson
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#if HAVE_CONFIG_H
# include <config.h>
#endif
#include "FLAC/assert.h"
#include "private/bitbuffer.h" /* from the libFLAC private include area */
#include <stdio.h>
#include <string.h> /* for memcmp() */
/* 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
/*
* WATCHOUT! Since FLAC__BitBuffer is a private structure, we use a copy of
* the definition here to get at the internals. Make sure this is kept up
* to date with what is in ../libFLAC/bitbuffer.c
*/
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;
};
static FLAC__bool dummy_read_callback(FLAC__byte buffer[], size_t *bytes, void *client_data)
{
(void)buffer, (void)bytes, (void)client_data;
return true;
}
FLAC__bool test_bitbuffer()
{
FLAC__BitBuffer *bb, *bb_zero, *bb_one, *bbcopy;
FLAC__bool ok;
unsigned i, j;
static FLAC__byte test_pattern1[19] = { 0xaa, 0xf0, 0xaa, 0xbe, 0xaa, 0xaa, 0xaa, 0xa8, 0x30, 0x0a, 0xaa, 0xaa, 0xaa, 0xad, 0xea, 0xdb, 0xee, 0xfa, 0xce };
FLAC__ASSERT(FLAC__BITS_PER_BLURB == 8);
printf("\n+++ libFLAC unit test: bitbuffer\n\n");
/*
* test new -> delete
*/
printf("testing new... ");
bb = FLAC__bitbuffer_new();
if(0 == bb) {
printf("FAILED, returned NULL\n");
return false;
}
printf("OK\n");
printf("testing delete... ");
FLAC__bitbuffer_delete(bb);
printf("OK\n");
/*
* test new -> init -> delete
*/
printf("testing new... ");
bb = FLAC__bitbuffer_new();
if(0 == bb) {
printf("FAILED, returned NULL\n");
return false;
}
printf("OK\n");
printf("testing init... ");
FLAC__bitbuffer_init(bb);
if(0 == bb) {
printf("FAILED, returned NULL\n");
return false;
}
printf("OK\n");
printf("testing delete... ");
FLAC__bitbuffer_delete(bb);
printf("OK\n");
/*
* test new -> init -> clear -> delete
*/
printf("testing new... ");
bb = FLAC__bitbuffer_new();
if(0 == bb) {
printf("FAILED, returned NULL\n");
return false;
}
printf("OK\n");
printf("testing init... ");
FLAC__bitbuffer_init(bb);
if(0 == bb) {
printf("FAILED, returned NULL\n");
return false;
}
printf("OK\n");
printf("testing clear... ");
FLAC__bitbuffer_clear(bb);
if(0 == bb) {
printf("FAILED, returned NULL\n");
return false;
}
printf("OK\n");
printf("testing delete... ");
FLAC__bitbuffer_delete(bb);
printf("OK\n");
/*
* test normal usage
*/
printf("testing new... ");
bb = FLAC__bitbuffer_new();
bb_zero = FLAC__bitbuffer_new();
bb_one = FLAC__bitbuffer_new();
bbcopy = FLAC__bitbuffer_new();
if(0 == bb || 0 == bb_zero || 0 == bb_one || 0 == bbcopy) {
printf("FAILED, returned NULL\n");
return false;
}
printf("OK\n");
printf("testing init... ");
ok = FLAC__bitbuffer_init(bb) && FLAC__bitbuffer_init(bb_zero) && FLAC__bitbuffer_init(bb_one) && FLAC__bitbuffer_init(bbcopy);
printf("%s\n", ok?"OK":"FAILED");
if(!ok)
return false;
printf("testing clear... ");
ok = FLAC__bitbuffer_clear(bb) && FLAC__bitbuffer_clear(bb_zero) && FLAC__bitbuffer_clear(bb_one) && FLAC__bitbuffer_clear(bbcopy);
printf("%s\n", ok?"OK":"FAILED");
if(!ok)
return false;
printf("setting up bb_one... ");
ok = FLAC__bitbuffer_write_raw_uint32(bb_one, 1, 7) && FLAC__bitbuffer_read_raw_uint32(bb_one, &i, 6, dummy_read_callback, 0);
printf("%s\n", ok?"OK":"FAILED");
if(!ok)
return false;
FLAC__bitbuffer_dump(bb_one, stdout);
printf("capacity = %u\n", bb->capacity);
printf("testing zeroes, raw_uint32*... ");
ok =
FLAC__bitbuffer_write_raw_uint32(bb, 0x1, 1) &&
FLAC__bitbuffer_write_raw_uint32(bb, 0x1, 2) &&
FLAC__bitbuffer_write_raw_uint32(bb, 0xa, 5) &&
FLAC__bitbuffer_write_raw_uint32(bb, 0xf0, 8) &&
FLAC__bitbuffer_write_raw_uint32(bb, 0x2aa, 10) &&
FLAC__bitbuffer_write_raw_uint32(bb, 0xf, 4) &&
FLAC__bitbuffer_write_raw_uint32(bb, 0xaaaaaaaa, 32) &&
FLAC__bitbuffer_write_zeroes(bb, 4) &&
FLAC__bitbuffer_write_raw_uint32(bb, 0x3, 2) &&
FLAC__bitbuffer_write_zeroes(bb, 8) &&
FLAC__bitbuffer_write_raw_uint64(bb, FLAC__U64L(0xaaaaaaaadeadbeef), 64) &&
FLAC__bitbuffer_write_raw_uint32(bb, 0xace, 12)
;
if(!ok) {
printf("FAILED\n");
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
if(bb->blurbs != sizeof(test_pattern1)) {
printf("FAILED byte count %u != %u\n", bb->blurbs, (unsigned)sizeof(test_pattern1));
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
if(bb->bits != 0) {
printf("FAILED bit count %u != 0\n", bb->bits);
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
if(bb->total_bits != 8*bb->blurbs+bb->bits) {
printf("FAILED total_bits count %u != %u (%u:%u)\n", bb->total_bits, 8*bb->blurbs+bb->bits, bb->blurbs, bb->bits);
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
if(memcmp(bb->buffer, test_pattern1, sizeof(FLAC__byte)*sizeof(test_pattern1)) != 0) {
printf("FAILED pattern match\n");
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
printf("OK\n");
FLAC__bitbuffer_dump(bb, stdout);
printf("testing raw_uint32 some more... ");
ok = FLAC__bitbuffer_write_raw_uint32(bb, 0x3d, 6);
if(!ok) {
printf("FAILED\n");
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
if(bb->blurbs != sizeof(test_pattern1)) {
printf("FAILED byte count %u != %u\n", bb->blurbs, (unsigned)sizeof(test_pattern1));
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
if(bb->bits != 6) {
printf("FAILED bit count %u != 6\n", bb->bits);
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
if(bb->total_bits != 8*bb->blurbs+bb->bits) {
printf("FAILED total_bits count %u != %u (%u:%u)\n", bb->total_bits, 8*bb->blurbs+bb->bits, bb->blurbs, bb->bits);
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
if(memcmp(bb->buffer, test_pattern1, sizeof(FLAC__byte)*sizeof(test_pattern1)) != 0 || bb->buffer[bb->blurbs] != 0x3d) {
printf("FAILED pattern match\n");
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
printf("OK\n");
FLAC__bitbuffer_dump(bb, stdout);
printf("testing concatenate_aligned (bb_zero)... ");
ok = FLAC__bitbuffer_concatenate_aligned(bb, bb_zero);
if(!ok) {
printf("FAILED\n");
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
if(bb->blurbs != sizeof(test_pattern1)) {
printf("FAILED byte count %u != %u\n", bb->blurbs, (unsigned)sizeof(test_pattern1));
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
if(bb->bits != 6) {
printf("FAILED bit count %u != 6\n", bb->bits);
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
if(bb->total_bits != 8*bb->blurbs+bb->bits) {
printf("FAILED total_bits count %u != %u (%u:%u)\n", bb->total_bits, 8*bb->blurbs+bb->bits, bb->blurbs, bb->bits);
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
if(memcmp(bb->buffer, test_pattern1, sizeof(FLAC__byte)*sizeof(test_pattern1)) != 0 || bb->buffer[bb->blurbs] != 0x3d) {
printf("FAILED pattern match\n");
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
printf("OK\n");
FLAC__bitbuffer_dump(bb, stdout);
printf("testing concatenate_aligned (bb_one)... ");
ok = FLAC__bitbuffer_concatenate_aligned(bb, bb_one);
if(!ok) {
printf("FAILED\n");
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
if(bb->blurbs != sizeof(test_pattern1)) {
printf("FAILED byte count %u != %u\n", bb->blurbs, (unsigned)sizeof(test_pattern1));
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
if(bb->bits != 7) {
printf("FAILED bit count %u != 7\n", bb->bits);
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
if(bb->total_bits != 8*bb->blurbs+bb->bits) {
printf("FAILED total_bits count %u != %u (%u:%u)\n", bb->total_bits, 8*bb->blurbs+bb->bits, bb->blurbs, bb->bits);
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
if(memcmp(bb->buffer, test_pattern1, sizeof(FLAC__byte)*sizeof(test_pattern1)) != 0 || bb->buffer[bb->blurbs] != 0x7b) {
printf("FAILED pattern match\n");
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
printf("OK\n");
FLAC__bitbuffer_dump(bb, stdout);
printf("testing concatenate_aligned (bb_one again)... ");
(void)FLAC__bitbuffer_write_raw_uint32(bb_one, 1, 1);
(void)FLAC__bitbuffer_read_raw_uint32(bb_one, &i, 1, dummy_read_callback, 0);
ok = FLAC__bitbuffer_concatenate_aligned(bb, bb_one);
if(!ok) {
printf("FAILED\n");
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
if(bb->blurbs != sizeof(test_pattern1)+1) {
printf("FAILED byte count %u != %u\n", bb->blurbs, (unsigned)sizeof(test_pattern1)+1);
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
if(bb->bits != 0) {
printf("FAILED bit count %u != 0\n", bb->bits);
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
if(bb->total_bits != 8*bb->blurbs+bb->bits) {
printf("FAILED total_bits count %u != %u (%u:%u)\n", bb->total_bits, 8*bb->blurbs+bb->bits, bb->blurbs, bb->bits);
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
if(memcmp(bb->buffer, test_pattern1, sizeof(FLAC__byte)*sizeof(test_pattern1)) != 0 || bb->buffer[bb->blurbs-1] != 0xf7) {
printf("FAILED pattern match\n");
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
printf("OK\n");
FLAC__bitbuffer_dump(bb, stdout);
printf("testing concatenate_aligned (bb_four)... ");
(void)FLAC__bitbuffer_clear(bb_one);
(void)FLAC__bitbuffer_write_raw_uint32(bb_one, 8, 4);
ok = FLAC__bitbuffer_concatenate_aligned(bb, bb_one);
if(!ok) {
printf("FAILED\n");
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
if(bb->blurbs != sizeof(test_pattern1)+1) {
printf("FAILED byte count %u != %u\n", bb->blurbs, (unsigned)sizeof(test_pattern1)+1);
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
if(bb->bits != 4) {
printf("FAILED bit count %u != 4\n", bb->bits);
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
if(bb->total_bits != 8*bb->blurbs+bb->bits) {
printf("FAILED total_bits count %u != %u (%u:%u)\n", bb->total_bits, 8*bb->blurbs+bb->bits, bb->blurbs, bb->bits);
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
if(memcmp(bb->buffer, test_pattern1, sizeof(FLAC__byte)*sizeof(test_pattern1)) != 0 || bb->buffer[bb->blurbs] != 0x08) {
printf("FAILED pattern match\n");
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
printf("OK\n");
FLAC__bitbuffer_dump(bb, stdout);
printf("testing concatenate_aligned (bb_eight)... ");
(void)FLAC__bitbuffer_read_raw_uint32(bb_one, &i, 4, dummy_read_callback, 0);
(void)FLAC__bitbuffer_write_raw_uint32(bb_one, 0xaa, 8);
ok = FLAC__bitbuffer_concatenate_aligned(bb, bb_one);
if(!ok) {
printf("FAILED\n");
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
if(bb->blurbs != sizeof(test_pattern1)+2) {
printf("FAILED byte count %u != %u\n", bb->blurbs, (unsigned)sizeof(test_pattern1)+2);
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
if(bb->bits != 4) {
printf("FAILED bit count %u != 4\n", bb->bits);
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
if(bb->total_bits != 8*bb->blurbs+bb->bits) {
printf("FAILED total_bits count %u != %u (%u:%u)\n", bb->total_bits, 8*bb->blurbs+bb->bits, bb->blurbs, bb->bits);
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
if(memcmp(bb->buffer, test_pattern1, sizeof(FLAC__byte)*sizeof(test_pattern1)) != 0 || bb->buffer[bb->blurbs-1] != 0x8a || bb->buffer[bb->blurbs] != 0x0a) {
printf("FAILED pattern match\n");
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
printf("OK\n");
FLAC__bitbuffer_dump(bb, stdout);
printf("testing concatenate_aligned (bb_seventeen)... ");
(void)FLAC__bitbuffer_write_raw_uint32(bb_one, 0x155, 9);
ok = FLAC__bitbuffer_concatenate_aligned(bb, bb_one);
if(!ok) {
printf("FAILED\n");
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
if(bb->blurbs != sizeof(test_pattern1)+4) {
printf("FAILED byte count %u != %u\n", bb->blurbs, (unsigned)sizeof(test_pattern1)+4);
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
if(bb->bits != 5) {
printf("FAILED bit count %u != 5\n", bb->bits);
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
if(bb->total_bits != 8*bb->blurbs+bb->bits) {
printf("FAILED total_bits count %u != %u (%u:%u)\n", bb->total_bits, 8*bb->blurbs+bb->bits, bb->blurbs, bb->bits);
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
if(memcmp(bb->buffer, test_pattern1, sizeof(FLAC__byte)*sizeof(test_pattern1)) != 0 || bb->buffer[bb->blurbs-3] != 0x8a || bb->buffer[bb->blurbs-2] != 0xaa || bb->buffer[bb->blurbs-1] != 0xaa || bb->buffer[bb->blurbs] != 0x15) {
printf("FAILED pattern match\n");
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
printf("OK\n");
FLAC__bitbuffer_dump(bb, stdout);
printf("testing utf8_uint32(0x00000000)... ");
FLAC__bitbuffer_clear(bb);
FLAC__bitbuffer_write_utf8_uint32(bb, 0x00000000);
ok = bb->total_bits == 8 && bb->buffer[0] == 0;
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
printf("testing utf8_uint32(0x0000007F)... ");
FLAC__bitbuffer_clear(bb);
FLAC__bitbuffer_write_utf8_uint32(bb, 0x0000007F);
ok = bb->total_bits == 8 && bb->buffer[0] == 0x7F;
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
printf("testing utf8_uint32(0x00000080)... ");
FLAC__bitbuffer_clear(bb);
FLAC__bitbuffer_write_utf8_uint32(bb, 0x00000080);
ok = bb->total_bits == 16 && bb->buffer[0] == 0xC2 && bb->buffer[1] == 0x80;
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
printf("testing utf8_uint32(0x000007FF)... ");
FLAC__bitbuffer_clear(bb);
FLAC__bitbuffer_write_utf8_uint32(bb, 0x000007FF);
ok = bb->total_bits == 16 && bb->buffer[0] == 0xDF && bb->buffer[1] == 0xBF;
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
printf("testing utf8_uint32(0x00000800)... ");
FLAC__bitbuffer_clear(bb);
FLAC__bitbuffer_write_utf8_uint32(bb, 0x00000800);
ok = bb->total_bits == 24 && bb->buffer[0] == 0xE0 && bb->buffer[1] == 0xA0 && bb->buffer[2] == 0x80;
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
printf("testing utf8_uint32(0x0000FFFF)... ");
FLAC__bitbuffer_clear(bb);
FLAC__bitbuffer_write_utf8_uint32(bb, 0x0000FFFF);
ok = bb->total_bits == 24 && bb->buffer[0] == 0xEF && bb->buffer[1] == 0xBF && bb->buffer[2] == 0xBF;
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
printf("testing utf8_uint32(0x00010000)... ");
FLAC__bitbuffer_clear(bb);
FLAC__bitbuffer_write_utf8_uint32(bb, 0x00010000);
ok = bb->total_bits == 32 && bb->buffer[0] == 0xF0 && bb->buffer[1] == 0x90 && bb->buffer[2] == 0x80 && bb->buffer[3] == 0x80;
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
printf("testing utf8_uint32(0x001FFFFF)... ");
FLAC__bitbuffer_clear(bb);
FLAC__bitbuffer_write_utf8_uint32(bb, 0x001FFFFF);
ok = bb->total_bits == 32 && bb->buffer[0] == 0xF7 && bb->buffer[1] == 0xBF && bb->buffer[2] == 0xBF && bb->buffer[3] == 0xBF;
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
printf("testing utf8_uint32(0x00200000)... ");
FLAC__bitbuffer_clear(bb);
FLAC__bitbuffer_write_utf8_uint32(bb, 0x00200000);
ok = bb->total_bits == 40 && bb->buffer[0] == 0xF8 && bb->buffer[1] == 0x88 && bb->buffer[2] == 0x80 && bb->buffer[3] == 0x80 && bb->buffer[4] == 0x80;
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
printf("testing utf8_uint32(0x03FFFFFF)... ");
FLAC__bitbuffer_clear(bb);
FLAC__bitbuffer_write_utf8_uint32(bb, 0x03FFFFFF);
ok = bb->total_bits == 40 && bb->buffer[0] == 0xFB && bb->buffer[1] == 0xBF && bb->buffer[2] == 0xBF && bb->buffer[3] == 0xBF && bb->buffer[4] == 0xBF;
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
printf("testing utf8_uint32(0x04000000)... ");
FLAC__bitbuffer_clear(bb);
FLAC__bitbuffer_write_utf8_uint32(bb, 0x04000000);
ok = bb->total_bits == 48 && bb->buffer[0] == 0xFC && bb->buffer[1] == 0x84 && bb->buffer[2] == 0x80 && bb->buffer[3] == 0x80 && bb->buffer[4] == 0x80 && bb->buffer[5] == 0x80;
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
printf("testing utf8_uint32(0x7FFFFFFF)... ");
FLAC__bitbuffer_clear(bb);
FLAC__bitbuffer_write_utf8_uint32(bb, 0x7FFFFFFF);
ok = bb->total_bits == 48 && bb->buffer[0] == 0xFD && bb->buffer[1] == 0xBF && bb->buffer[2] == 0xBF && bb->buffer[3] == 0xBF && bb->buffer[4] == 0xBF && bb->buffer[5] == 0xBF;
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
printf("testing utf8_uint64(0x0000000000000000)... ");
FLAC__bitbuffer_clear(bb);
FLAC__bitbuffer_write_utf8_uint64(bb, 0x0000000000000000);
ok = bb->total_bits == 8 && bb->buffer[0] == 0;
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
printf("testing utf8_uint64(0x000000000000007F)... ");
FLAC__bitbuffer_clear(bb);
FLAC__bitbuffer_write_utf8_uint64(bb, 0x000000000000007F);
ok = bb->total_bits == 8 && bb->buffer[0] == 0x7F;
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
printf("testing utf8_uint64(0x0000000000000080)... ");
FLAC__bitbuffer_clear(bb);
FLAC__bitbuffer_write_utf8_uint64(bb, 0x0000000000000080);
ok = bb->total_bits == 16 && bb->buffer[0] == 0xC2 && bb->buffer[1] == 0x80;
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
printf("testing utf8_uint64(0x00000000000007FF)... ");
FLAC__bitbuffer_clear(bb);
FLAC__bitbuffer_write_utf8_uint64(bb, 0x00000000000007FF);
ok = bb->total_bits == 16 && bb->buffer[0] == 0xDF && bb->buffer[1] == 0xBF;
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
printf("testing utf8_uint64(0x0000000000000800)... ");
FLAC__bitbuffer_clear(bb);
FLAC__bitbuffer_write_utf8_uint64(bb, 0x0000000000000800);
ok = bb->total_bits == 24 && bb->buffer[0] == 0xE0 && bb->buffer[1] == 0xA0 && bb->buffer[2] == 0x80;
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
printf("testing utf8_uint64(0x000000000000FFFF)... ");
FLAC__bitbuffer_clear(bb);
FLAC__bitbuffer_write_utf8_uint64(bb, 0x000000000000FFFF);
ok = bb->total_bits == 24 && bb->buffer[0] == 0xEF && bb->buffer[1] == 0xBF && bb->buffer[2] == 0xBF;
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
printf("testing utf8_uint64(0x0000000000010000)... ");
FLAC__bitbuffer_clear(bb);
FLAC__bitbuffer_write_utf8_uint64(bb, 0x0000000000010000);
ok = bb->total_bits == 32 && bb->buffer[0] == 0xF0 && bb->buffer[1] == 0x90 && bb->buffer[2] == 0x80 && bb->buffer[3] == 0x80;
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
printf("testing utf8_uint64(0x00000000001FFFFF)... ");
FLAC__bitbuffer_clear(bb);
FLAC__bitbuffer_write_utf8_uint64(bb, 0x00000000001FFFFF);
ok = bb->total_bits == 32 && bb->buffer[0] == 0xF7 && bb->buffer[1] == 0xBF && bb->buffer[2] == 0xBF && bb->buffer[3] == 0xBF;
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
printf("testing utf8_uint64(0x0000000000200000)... ");
FLAC__bitbuffer_clear(bb);
FLAC__bitbuffer_write_utf8_uint64(bb, 0x0000000000200000);
ok = bb->total_bits == 40 && bb->buffer[0] == 0xF8 && bb->buffer[1] == 0x88 && bb->buffer[2] == 0x80 && bb->buffer[3] == 0x80 && bb->buffer[4] == 0x80;
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
printf("testing utf8_uint64(0x0000000003FFFFFF)... ");
FLAC__bitbuffer_clear(bb);
FLAC__bitbuffer_write_utf8_uint64(bb, 0x0000000003FFFFFF);
ok = bb->total_bits == 40 && bb->buffer[0] == 0xFB && bb->buffer[1] == 0xBF && bb->buffer[2] == 0xBF && bb->buffer[3] == 0xBF && bb->buffer[4] == 0xBF;
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
printf("testing utf8_uint64(0x0000000004000000)... ");
FLAC__bitbuffer_clear(bb);
FLAC__bitbuffer_write_utf8_uint64(bb, 0x0000000004000000);
ok = bb->total_bits == 48 && bb->buffer[0] == 0xFC && bb->buffer[1] == 0x84 && bb->buffer[2] == 0x80 && bb->buffer[3] == 0x80 && bb->buffer[4] == 0x80 && bb->buffer[5] == 0x80;
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
printf("testing utf8_uint64(0x000000007FFFFFFF)... ");
FLAC__bitbuffer_clear(bb);
FLAC__bitbuffer_write_utf8_uint64(bb, 0x000000007FFFFFFF);
ok = bb->total_bits == 48 && bb->buffer[0] == 0xFD && bb->buffer[1] == 0xBF && bb->buffer[2] == 0xBF && bb->buffer[3] == 0xBF && bb->buffer[4] == 0xBF && bb->buffer[5] == 0xBF;
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
printf("testing utf8_uint64(0x0000000080000000)... ");
FLAC__bitbuffer_clear(bb);
FLAC__bitbuffer_write_utf8_uint64(bb, 0x0000000080000000);
ok = bb->total_bits == 56 && bb->buffer[0] == 0xFE && bb->buffer[1] == 0x82 && bb->buffer[2] == 0x80 && bb->buffer[3] == 0x80 && bb->buffer[4] == 0x80 && bb->buffer[5] == 0x80 && bb->buffer[6] == 0x80;
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
printf("testing utf8_uint64(0x0000000FFFFFFFFF)... ");
FLAC__bitbuffer_clear(bb);
FLAC__bitbuffer_write_utf8_uint64(bb, FLAC__U64L(0x0000000FFFFFFFFF));
ok = bb->total_bits == 56 && bb->buffer[0] == 0xFE && bb->buffer[1] == 0xBF && bb->buffer[2] == 0xBF && bb->buffer[3] == 0xBF && bb->buffer[4] == 0xBF && bb->buffer[5] == 0xBF && bb->buffer[6] == 0xBF;
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
printf("testing grow... ");
FLAC__bitbuffer_clear(bb);
FLAC__bitbuffer_write_raw_uint32(bb, 0xa, 4);
j = bb->capacity;
for(i = 0; i < j; i++)
FLAC__bitbuffer_write_raw_uint32(bb, 0xaa, 8);
ok = bb->total_bits = i*8+4 && bb->buffer[0] == 0xaa && bb->buffer[i] == 0xa;
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitbuffer_dump(bb, stdout);
return false;
}
printf("capacity = %u\n", bb->capacity);
printf("testing clone... ");
ok = FLAC__bitbuffer_clone(bbcopy, bb);
if(!ok) {
printf("FAILED\n");
FLAC__bitbuffer_dump(bb, stdout);
FLAC__bitbuffer_dump(bbcopy, stdout);
return false;
}
if(bb->blurbs != bbcopy->blurbs) {
printf("FAILED byte count %u != %u\n", bb->blurbs, bbcopy->blurbs);
FLAC__bitbuffer_dump(bb, stdout);
FLAC__bitbuffer_dump(bbcopy, stdout);
return false;
}
if(bb->bits != bbcopy->bits) {
printf("FAILED bit count %u != %u\n", bb->bits, bbcopy->bits);
FLAC__bitbuffer_dump(bb, stdout);
FLAC__bitbuffer_dump(bbcopy, stdout);
return false;
}
if(bb->total_bits != bbcopy->total_bits) {
printf("FAILED total_bits count %u != %u\n", bb->total_bits, bbcopy->total_bits);
FLAC__bitbuffer_dump(bb, stdout);
FLAC__bitbuffer_dump(bbcopy, stdout);
return false;
}
if(memcmp(bb->buffer, bbcopy->buffer, sizeof(FLAC__byte)*bb->capacity) != 0) {
printf("FAILED pattern match\n");
FLAC__bitbuffer_dump(bb, stdout);
FLAC__bitbuffer_dump(bbcopy, stdout);
return false;
}
printf("OK\n");
printf("testing free... ");
FLAC__bitbuffer_free(bb);
FLAC__bitbuffer_free(bbcopy);
printf("OK\n");
printf("testing delete... ");
FLAC__bitbuffer_delete(bb);
FLAC__bitbuffer_delete(bb_zero);
FLAC__bitbuffer_delete(bb_one);
FLAC__bitbuffer_delete(bbcopy);
printf("OK\n");
printf("\nPASSED!\n");
return true;
}

View File

@ -0,0 +1,583 @@
/* test_libFLAC - Unit tester for libFLAC
* Copyright (C) 2000,2001,2002,2003,2004,2005,2006 Josh Coalson
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#if HAVE_CONFIG_H
# include <config.h>
#endif
#include "FLAC/assert.h"
#include "private/bitwriter.h" /* from the libFLAC private include area */
#include <stdio.h>
#include <string.h> /* for memcmp() */
/* 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
/*
* WATCHOUT! Since FLAC__BitWriter is a private structure, we use a copy of
* the definition here to get at the internals. Make sure this is kept up
* to date with what is in ../libFLAC/bitwriter.c
*/
typedef FLAC__uint32 bwword;
struct FLAC__BitWriter {
bwword *buffer;
bwword accum; /* accumulator; when full, accum is appended to buffer */
unsigned capacity; /* of buffer in words */
unsigned words; /* # of complete words in buffer */
unsigned bits; /* # of used bits in accum */
};
#define TOTAL_BITS(bw) ((bw)->words*sizeof(bwword)*8 + (bw)->bits)
FLAC__bool test_bitwriter()
{
FLAC__BitWriter *bw;
FLAC__bool ok;
unsigned i, j;
#if WORDS_BIGENDIAN
static bwword test_pattern1[5] = { 0xaaf0aabe, 0xaaaaaaa8, 0x300aaaaa, 0xaaadeadb, 0x00eeface };
#else
static bwword test_pattern1[5] = { 0xbeaaf0aa, 0xa8aaaaaa, 0xaaaa0a30, 0xdbeaadaa, 0x00eeface };
#endif
unsigned words, bits; /* what we think bw->words and bw->bits should be */
printf("\n+++ libFLAC unit test: bitwriter\n\n");
/*
* test new -> delete
*/
printf("testing new... ");
bw = FLAC__bitwriter_new();
if(0 == bw) {
printf("FAILED, returned NULL\n");
return false;
}
printf("OK\n");
printf("testing delete... ");
FLAC__bitwriter_delete(bw);
printf("OK\n");
/*
* test new -> init -> delete
*/
printf("testing new... ");
bw = FLAC__bitwriter_new();
if(0 == bw) {
printf("FAILED, returned NULL\n");
return false;
}
printf("OK\n");
printf("testing init... ");
FLAC__bitwriter_init(bw);
if(0 == bw) {
printf("FAILED, returned NULL\n");
return false;
}
printf("OK\n");
printf("testing delete... ");
FLAC__bitwriter_delete(bw);
printf("OK\n");
/*
* test new -> init -> clear -> delete
*/
printf("testing new... ");
bw = FLAC__bitwriter_new();
if(0 == bw) {
printf("FAILED, returned NULL\n");
return false;
}
printf("OK\n");
printf("testing init... ");
FLAC__bitwriter_init(bw);
if(0 == bw) {
printf("FAILED, returned NULL\n");
return false;
}
printf("OK\n");
printf("testing clear... ");
FLAC__bitwriter_clear(bw);
if(0 == bw) {
printf("FAILED, returned NULL\n");
return false;
}
printf("OK\n");
printf("testing delete... ");
FLAC__bitwriter_delete(bw);
printf("OK\n");
/*
* test normal usage
*/
printf("testing new... ");
bw = FLAC__bitwriter_new();
if(0 == bw) {
printf("FAILED, returned NULL\n");
return false;
}
printf("OK\n");
printf("testing init... ");
ok = FLAC__bitwriter_init(bw);
printf("%s\n", ok?"OK":"FAILED");
if(!ok)
return false;
printf("testing clear... ");
FLAC__bitwriter_clear(bw);
printf("OK\n");
words = bits = 0;
printf("capacity = %u\n", bw->capacity);
printf("testing zeroes, raw_uint32*... ");
ok =
FLAC__bitwriter_write_raw_uint32(bw, 0x1, 1) &&
FLAC__bitwriter_write_raw_uint32(bw, 0x1, 2) &&
FLAC__bitwriter_write_raw_uint32(bw, 0xa, 5) &&
FLAC__bitwriter_write_raw_uint32(bw, 0xf0, 8) &&
FLAC__bitwriter_write_raw_uint32(bw, 0x2aa, 10) &&
FLAC__bitwriter_write_raw_uint32(bw, 0xf, 4) &&
FLAC__bitwriter_write_raw_uint32(bw, 0xaaaaaaaa, 32) &&
FLAC__bitwriter_write_zeroes(bw, 4) &&
FLAC__bitwriter_write_raw_uint32(bw, 0x3, 2) &&
FLAC__bitwriter_write_zeroes(bw, 8) &&
FLAC__bitwriter_write_raw_uint64(bw, FLAC__U64L(0xaaaaaaaadeadbeef), 64) &&
FLAC__bitwriter_write_raw_uint32(bw, 0xace, 12)
;
if(!ok) {
printf("FAILED\n");
FLAC__bitwriter_dump(bw, stdout);
return false;
}
words = 4;
bits = 24;
if(bw->words != words) {
printf("FAILED byte count %u != %u\n", bw->words, words);
FLAC__bitwriter_dump(bw, stdout);
return false;
}
if(bw->bits != bits) {
printf("FAILED bit count %u != %u\n", bw->bits, bits);
FLAC__bitwriter_dump(bw, stdout);
return false;
}
if(memcmp(bw->buffer, test_pattern1, sizeof(bwword)*words) != 0) {
printf("FAILED pattern match (buffer)\n");
FLAC__bitwriter_dump(bw, stdout);
return false;
}
if((bw->accum & 0x00ffffff) != test_pattern1[words]) {
printf("FAILED pattern match (bw->accum=%08X != %08X)\n", bw->accum&0x00ffffff, test_pattern1[words]);
FLAC__bitwriter_dump(bw, stdout);
return false;
}
printf("OK\n");
FLAC__bitwriter_dump(bw, stdout);
printf("testing raw_uint32 some more... ");
ok = FLAC__bitwriter_write_raw_uint32(bw, 0x3d, 6);
if(!ok) {
printf("FAILED\n");
FLAC__bitwriter_dump(bw, stdout);
return false;
}
bits += 6;
test_pattern1[words] <<= 6;
test_pattern1[words] |= 0x3d;
if(bw->words != words) {
printf("FAILED byte count %u != %u\n", bw->words, words);
FLAC__bitwriter_dump(bw, stdout);
return false;
}
if(bw->bits != bits) {
printf("FAILED bit count %u != %u\n", bw->bits, bits);
FLAC__bitwriter_dump(bw, stdout);
return false;
}
if(memcmp(bw->buffer, test_pattern1, sizeof(bwword)*words) != 0) {
printf("FAILED pattern match (buffer)\n");
FLAC__bitwriter_dump(bw, stdout);
return false;
}
if((bw->accum & 0x3fffffff) != test_pattern1[words]) {
printf("FAILED pattern match (bw->accum=%08X != %08X)\n", bw->accum&0x3fffffff, test_pattern1[words]);
FLAC__bitwriter_dump(bw, stdout);
return false;
}
printf("OK\n");
FLAC__bitwriter_dump(bw, stdout);
printf("testing utf8_uint32(0x00000000)... ");
FLAC__bitwriter_clear(bw);
FLAC__bitwriter_write_utf8_uint32(bw, 0x00000000);
ok = TOTAL_BITS(bw) == 8 && (bw->accum & 0xff) == 0;
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitwriter_dump(bw, stdout);
return false;
}
printf("testing utf8_uint32(0x0000007F)... ");
FLAC__bitwriter_clear(bw);
FLAC__bitwriter_write_utf8_uint32(bw, 0x0000007F);
ok = TOTAL_BITS(bw) == 8 && (bw->accum & 0xff) == 0x7F;
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitwriter_dump(bw, stdout);
return false;
}
printf("testing utf8_uint32(0x00000080)... ");
FLAC__bitwriter_clear(bw);
FLAC__bitwriter_write_utf8_uint32(bw, 0x00000080);
ok = TOTAL_BITS(bw) == 16 && (bw->accum & 0xffff) == 0xC280;
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitwriter_dump(bw, stdout);
return false;
}
printf("testing utf8_uint32(0x000007FF)... ");
FLAC__bitwriter_clear(bw);
FLAC__bitwriter_write_utf8_uint32(bw, 0x000007FF);
ok = TOTAL_BITS(bw) == 16 && (bw->accum & 0xffff) == 0xDFBF;
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitwriter_dump(bw, stdout);
return false;
}
printf("testing utf8_uint32(0x00000800)... ");
FLAC__bitwriter_clear(bw);
FLAC__bitwriter_write_utf8_uint32(bw, 0x00000800);
ok = TOTAL_BITS(bw) == 24 && (bw->accum & 0xffffff) == 0xE0A080;
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitwriter_dump(bw, stdout);
return false;
}
printf("testing utf8_uint32(0x0000FFFF)... ");
FLAC__bitwriter_clear(bw);
FLAC__bitwriter_write_utf8_uint32(bw, 0x0000FFFF);
ok = TOTAL_BITS(bw) == 24 && (bw->accum & 0xffffff) == 0xEFBFBF;
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitwriter_dump(bw, stdout);
return false;
}
printf("testing utf8_uint32(0x00010000)... ");
FLAC__bitwriter_clear(bw);
FLAC__bitwriter_write_utf8_uint32(bw, 0x00010000);
#if WORDS_BIGENDIAN
ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0xF0908080;
#else
ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0x808090F0;
#endif
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitwriter_dump(bw, stdout);
return false;
}
printf("testing utf8_uint32(0x001FFFFF)... ");
FLAC__bitwriter_clear(bw);
FLAC__bitwriter_write_utf8_uint32(bw, 0x001FFFFF);
#if WORDS_BIGENDIAN
ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0xF7BFBFBF;
#else
ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0xBFBFBFF7;
#endif
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitwriter_dump(bw, stdout);
return false;
}
printf("testing utf8_uint32(0x00200000)... ");
FLAC__bitwriter_clear(bw);
FLAC__bitwriter_write_utf8_uint32(bw, 0x00200000);
#if WORDS_BIGENDIAN
ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0xF8888080 && (bw->accum & 0xff) == 0x80;
#else
ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0x808088F8 && (bw->accum & 0xff) == 0x80;
#endif
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitwriter_dump(bw, stdout);
return false;
}
printf("testing utf8_uint32(0x03FFFFFF)... ");
FLAC__bitwriter_clear(bw);
FLAC__bitwriter_write_utf8_uint32(bw, 0x03FFFFFF);
#if WORDS_BIGENDIAN
ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0xFBBFBFBF && (bw->accum & 0xff) == 0xBF;
#else
ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0xBFBFBFFB && (bw->accum & 0xff) == 0xBF;
#endif
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitwriter_dump(bw, stdout);
return false;
}
printf("testing utf8_uint32(0x04000000)... ");
FLAC__bitwriter_clear(bw);
FLAC__bitwriter_write_utf8_uint32(bw, 0x04000000);
#if WORDS_BIGENDIAN
ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0xFC848080 && (bw->accum & 0xffff) == 0x8080;
#else
ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0x808084FC && (bw->accum & 0xffff) == 0x8080;
#endif
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitwriter_dump(bw, stdout);
return false;
}
printf("testing utf8_uint32(0x7FFFFFFF)... ");
FLAC__bitwriter_clear(bw);
FLAC__bitwriter_write_utf8_uint32(bw, 0x7FFFFFFF);
#if WORDS_BIGENDIAN
ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0xFDBFBFBF && (bw->accum & 0xffff) == 0xBFBF;
#else
ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0xBFBFBFFD && (bw->accum & 0xffff) == 0xBFBF;
#endif
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitwriter_dump(bw, stdout);
return false;
}
printf("testing utf8_uint64(0x0000000000000000)... ");
FLAC__bitwriter_clear(bw);
FLAC__bitwriter_write_utf8_uint64(bw, 0x0000000000000000);
ok = TOTAL_BITS(bw) == 8 && (bw->accum & 0xff) == 0;
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitwriter_dump(bw, stdout);
return false;
}
printf("testing utf8_uint64(0x000000000000007F)... ");
FLAC__bitwriter_clear(bw);
FLAC__bitwriter_write_utf8_uint64(bw, 0x000000000000007F);
ok = TOTAL_BITS(bw) == 8 && (bw->accum & 0xff) == 0x7F;
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitwriter_dump(bw, stdout);
return false;
}
printf("testing utf8_uint64(0x0000000000000080)... ");
FLAC__bitwriter_clear(bw);
FLAC__bitwriter_write_utf8_uint64(bw, 0x0000000000000080);
ok = TOTAL_BITS(bw) == 16 && (bw->accum & 0xffff) == 0xC280;
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitwriter_dump(bw, stdout);
return false;
}
printf("testing utf8_uint64(0x00000000000007FF)... ");
FLAC__bitwriter_clear(bw);
FLAC__bitwriter_write_utf8_uint64(bw, 0x00000000000007FF);
ok = TOTAL_BITS(bw) == 16 && (bw->accum & 0xffff) == 0xDFBF;
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitwriter_dump(bw, stdout);
return false;
}
printf("testing utf8_uint64(0x0000000000000800)... ");
FLAC__bitwriter_clear(bw);
FLAC__bitwriter_write_utf8_uint64(bw, 0x0000000000000800);
ok = TOTAL_BITS(bw) == 24 && (bw->accum & 0xffffff) == 0xE0A080;
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitwriter_dump(bw, stdout);
return false;
}
printf("testing utf8_uint64(0x000000000000FFFF)... ");
FLAC__bitwriter_clear(bw);
FLAC__bitwriter_write_utf8_uint64(bw, 0x000000000000FFFF);
ok = TOTAL_BITS(bw) == 24 && (bw->accum & 0xffffff) == 0xEFBFBF;
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitwriter_dump(bw, stdout);
return false;
}
printf("testing utf8_uint64(0x0000000000010000)... ");
FLAC__bitwriter_clear(bw);
FLAC__bitwriter_write_utf8_uint64(bw, 0x0000000000010000);
#if WORDS_BIGENDIAN
ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0xF0908080;
#else
ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0x808090F0;
#endif
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitwriter_dump(bw, stdout);
return false;
}
printf("testing utf8_uint64(0x00000000001FFFFF)... ");
FLAC__bitwriter_clear(bw);
FLAC__bitwriter_write_utf8_uint64(bw, 0x00000000001FFFFF);
#if WORDS_BIGENDIAN
ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0xF7BFBFBF;
#else
ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0xBFBFBFF7;
#endif
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitwriter_dump(bw, stdout);
return false;
}
printf("testing utf8_uint64(0x0000000000200000)... ");
FLAC__bitwriter_clear(bw);
FLAC__bitwriter_write_utf8_uint64(bw, 0x0000000000200000);
#if WORDS_BIGENDIAN
ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0xF8888080 && (bw->accum & 0xff) == 0x80;
#else
ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0x808088F8 && (bw->accum & 0xff) == 0x80;
#endif
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitwriter_dump(bw, stdout);
return false;
}
printf("testing utf8_uint64(0x0000000003FFFFFF)... ");
FLAC__bitwriter_clear(bw);
FLAC__bitwriter_write_utf8_uint64(bw, 0x0000000003FFFFFF);
#if WORDS_BIGENDIAN
ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0xFBBFBFBF && (bw->accum & 0xff) == 0xBF;
#else
ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0xBFBFBFFB && (bw->accum & 0xff) == 0xBF;
#endif
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitwriter_dump(bw, stdout);
return false;
}
printf("testing utf8_uint64(0x0000000004000000)... ");
FLAC__bitwriter_clear(bw);
FLAC__bitwriter_write_utf8_uint64(bw, 0x0000000004000000);
#if WORDS_BIGENDIAN
ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0xFC848080 && (bw->accum & 0xffff) == 0x8080;
#else
ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0x808084FC && (bw->accum & 0xffff) == 0x8080;
#endif
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitwriter_dump(bw, stdout);
return false;
}
printf("testing utf8_uint64(0x000000007FFFFFFF)... ");
FLAC__bitwriter_clear(bw);
FLAC__bitwriter_write_utf8_uint64(bw, 0x000000007FFFFFFF);
#if WORDS_BIGENDIAN
ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0xFDBFBFBF && (bw->accum & 0xffff) == 0xBFBF;
#else
ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0xBFBFBFFD && (bw->accum & 0xffff) == 0xBFBF;
#endif
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitwriter_dump(bw, stdout);
return false;
}
printf("testing utf8_uint64(0x0000000080000000)... ");
FLAC__bitwriter_clear(bw);
FLAC__bitwriter_write_utf8_uint64(bw, 0x0000000080000000);
#if WORDS_BIGENDIAN
ok = TOTAL_BITS(bw) == 56 && bw->buffer[0] == 0xFE828080 && (bw->accum & 0xffffff) == 0x808080;
#else
ok = TOTAL_BITS(bw) == 56 && bw->buffer[0] == 0x808082FE && (bw->accum & 0xffffff) == 0x808080;
#endif
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitwriter_dump(bw, stdout);
return false;
}
printf("testing utf8_uint64(0x0000000FFFFFFFFF)... ");
FLAC__bitwriter_clear(bw);
FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x0000000FFFFFFFFF));
#if WORDS_BIGENDIAN
ok = TOTAL_BITS(bw) == 56 && bw->buffer[0] == 0xFEBFBFBF && (bw->accum & 0xffffff) == 0xBFBFBF;
#else
ok = TOTAL_BITS(bw) == 56 && bw->buffer[0] == 0xBFBFBFFE && (bw->accum & 0xffffff) == 0xBFBFBF;
#endif
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitwriter_dump(bw, stdout);
return false;
}
printf("testing grow... ");
FLAC__bitwriter_clear(bw);
FLAC__bitwriter_write_raw_uint32(bw, 0x5, 4);
j = bw->capacity;
for(i = 0; i < j; i++)
FLAC__bitwriter_write_raw_uint32(bw, 0xaaaaaaaa, 32);
#if WORDS_BIGENDIAN
ok = TOTAL_BITS(bw) == i*32+4 && bw->buffer[0] == 0x5aaaaaaa && (bw->accum & 0xf) == 0xa;
#else
ok = TOTAL_BITS(bw) == i*32+4 && bw->buffer[0] == 0xaaaaaa5a && (bw->accum & 0xf) == 0xa;
#endif
printf("%s\n", ok?"OK":"FAILED");
if(!ok) {
FLAC__bitwriter_dump(bw, stdout);
return false;
}
printf("capacity = %u\n", bw->capacity);
printf("testing free... ");
FLAC__bitwriter_free(bw);
printf("OK\n");
printf("testing delete... ");
FLAC__bitwriter_delete(bw);
printf("OK\n");
printf("\nPASSED!\n");
return true;
}

View File

@ -21,6 +21,6 @@
#include "FLAC/ordinals.h"
FLAC__bool test_bitbuffer();
FLAC__bool test_bitwriter();
#endif

View File

@ -20,7 +20,7 @@
# include <config.h>
#endif
#include "bitbuffer.h"
#include "bitwriter.h"
#include "decoders.h"
#include "encoders.h"
#include "format.h"
@ -30,7 +30,7 @@ int main(int argc, char *argv[])
{
(void)argc, (void)argv;
if(!test_bitbuffer())
if(!test_bitwriter())
return 1;
if(!test_format())