reorganize metadata decoding in anticipation of new cuesheet block
[platform/upstream/flac.git] / src / libFLAC / stream_decoder.c
index a7a9154..347ee0d 100644 (file)
 #include "FLAC/assert.h"
 #include "protected/stream_decoder.h"
 #include "private/bitbuffer.h"
+#include "private/bitmath.h"
 #include "private/cpu.h"
 #include "private/crc.h"
 #include "private/fixed.h"
+#include "private/format.h"
 #include "private/lpc.h"
 
+#ifdef max
+#undef max
+#endif
+#define max(a,b) ((a)>(b)?(a):(b))
+
 /***********************************************************************
  *
  * Private static data
@@ -47,6 +54,9 @@ static FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size,
 static FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id);
 static FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder);
 static FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder);
+static FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length);
+static FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length);
+static FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj);
 static FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder);
 static FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder);
 static FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame);
@@ -56,7 +66,7 @@ static FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned
 static FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
 static FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
 static FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
-static FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice, FLAC__int32 *residual);
+static FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, FLAC__int32 *residual);
 static FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder);
 static FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data);
 
@@ -72,11 +82,13 @@ typedef struct FLAC__StreamDecoderPrivate {
        FLAC__StreamDecoderMetadataCallback metadata_callback;
        FLAC__StreamDecoderErrorCallback error_callback;
        void (*local_lpc_restore_signal)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
+       void (*local_lpc_restore_signal_64bit)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
        void (*local_lpc_restore_signal_16bit)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
        void *client_data;
        FLAC__BitBuffer *input;
        FLAC__int32 *output[FLAC__MAX_CHANNELS];
        FLAC__int32 *residual[FLAC__MAX_CHANNELS];
+       FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents[FLAC__MAX_CHANNELS];
        unsigned output_capacity, output_channels;
        FLAC__uint32 last_frame_number;
        FLAC__uint64 samples_decoded;
@@ -99,7 +111,7 @@ typedef struct FLAC__StreamDecoderPrivate {
  *
  ***********************************************************************/
 
-const char * const FLAC__StreamDecoderStateString[] = {
+FLAC_API const char * const FLAC__StreamDecoderStateString[] = {
        "FLAC__STREAM_DECODER_SEARCH_FOR_METADATA",
        "FLAC__STREAM_DECODER_READ_METADATA",
        "FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC",
@@ -113,18 +125,18 @@ const char * const FLAC__StreamDecoderStateString[] = {
        "FLAC__STREAM_DECODER_UNINITIALIZED"
 };
 
-const char * const FLAC__StreamDecoderReadStatusString[] = {
+FLAC_API const char * const FLAC__StreamDecoderReadStatusString[] = {
        "FLAC__STREAM_DECODER_READ_STATUS_CONTINUE",
        "FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM",
        "FLAC__STREAM_DECODER_READ_STATUS_ABORT"
 };
 
-const char * const FLAC__StreamDecoderWriteStatusString[] = {
+FLAC_API const char * const FLAC__StreamDecoderWriteStatusString[] = {
        "FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE",
        "FLAC__STREAM_DECODER_WRITE_STATUS_ABORT"
 };
 
-const char * const FLAC__StreamDecoderErrorStatusString[] = {
+FLAC_API const char * const FLAC__StreamDecoderErrorStatusString[] = {
        "FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC",
        "FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER",
        "FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH"
@@ -135,33 +147,30 @@ const char * const FLAC__StreamDecoderErrorStatusString[] = {
  * Class constructor/destructor
  *
  ***********************************************************************/
-FLAC__StreamDecoder *FLAC__stream_decoder_new()
+FLAC_API FLAC__StreamDecoder *FLAC__stream_decoder_new()
 {
        FLAC__StreamDecoder *decoder;
        unsigned i;
 
        FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
 
-       decoder = (FLAC__StreamDecoder*)malloc(sizeof(FLAC__StreamDecoder));
+       decoder = (FLAC__StreamDecoder*)calloc(1, sizeof(FLAC__StreamDecoder));
        if(decoder == 0) {
                return 0;
        }
-       memset(decoder, 0, sizeof(FLAC__StreamDecoder));
 
-       decoder->protected_ = (FLAC__StreamDecoderProtected*)malloc(sizeof(FLAC__StreamDecoderProtected));
+       decoder->protected_ = (FLAC__StreamDecoderProtected*)calloc(1, sizeof(FLAC__StreamDecoderProtected));
        if(decoder->protected_ == 0) {
                free(decoder);
                return 0;
        }
-       memset(decoder->protected_, 0, sizeof(FLAC__StreamDecoderProtected));
 
-       decoder->private_ = (FLAC__StreamDecoderPrivate*)malloc(sizeof(FLAC__StreamDecoderPrivate));
+       decoder->private_ = (FLAC__StreamDecoderPrivate*)calloc(1, sizeof(FLAC__StreamDecoderPrivate));
        if(decoder->private_ == 0) {
                free(decoder->protected_);
                free(decoder);
                return 0;
        }
-       memset(decoder->private_, 0, sizeof(FLAC__StreamDecoderPrivate));
 
        decoder->private_->input = FLAC__bitbuffer_new();
        if(decoder->private_->input == 0) {
@@ -172,7 +181,7 @@ FLAC__StreamDecoder *FLAC__stream_decoder_new()
        }
 
        decoder->private_->metadata_filter_ids_capacity = 16;
-       if(0 == (decoder->private_->metadata_filter_ids = malloc((FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) * decoder->private_->metadata_filter_ids_capacity))) {
+       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);
                free(decoder->private_);
                free(decoder->protected_);
@@ -189,6 +198,9 @@ FLAC__StreamDecoder *FLAC__stream_decoder_new()
        decoder->private_->output_channels = 0;
        decoder->private_->has_seek_table = false;
 
+       for(i = 0; i < FLAC__MAX_CHANNELS; i++)
+               FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&decoder->private_->partitioned_rice_contents[i]);
+
        set_defaults_(decoder);
 
        decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
@@ -196,8 +208,10 @@ FLAC__StreamDecoder *FLAC__stream_decoder_new()
        return decoder;
 }
 
-void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder)
+FLAC_API void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder)
 {
+       unsigned i;
+
        FLAC__ASSERT(0 != decoder);
        FLAC__ASSERT(0 != decoder->protected_);
        FLAC__ASSERT(0 != decoder->private_);
@@ -209,6 +223,10 @@ void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder)
                free(decoder->private_->metadata_filter_ids);
 
        FLAC__bitbuffer_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]);
+
        free(decoder->private_);
        free(decoder->protected_);
        free(decoder);
@@ -220,7 +238,7 @@ void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder)
  *
  ***********************************************************************/
 
-FLAC__StreamDecoderState FLAC__stream_decoder_init(FLAC__StreamDecoder *decoder)
+FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_init(FLAC__StreamDecoder *decoder)
 {
        FLAC__ASSERT(0 != decoder);
 
@@ -244,6 +262,7 @@ FLAC__StreamDecoderState FLAC__stream_decoder_init(FLAC__StreamDecoder *decoder)
        FLAC__cpu_info(&decoder->private_->cpuinfo);
        /* first default to the non-asm routines */
        decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal;
+       decoder->private_->local_lpc_restore_signal_64bit = FLAC__lpc_restore_signal_wide;
        decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal;
        /* now override with asm where appropriate */
 #ifndef FLAC__NO_ASM
@@ -270,7 +289,7 @@ FLAC__StreamDecoderState FLAC__stream_decoder_init(FLAC__StreamDecoder *decoder)
        return decoder->protected_->state;
 }
 
-void FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
+FLAC_API void FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
 {
        unsigned i;
        FLAC__ASSERT(0 != decoder);
@@ -284,7 +303,12 @@ void FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
        }
        FLAC__bitbuffer_free(decoder->private_->input);
        for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
-               /* WATCHOUT: FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the output arrays have a buffer of up to 3 zeroes in front (at negative indices) for alignment purposes; we use 4 to keep the data well-aligned. */
+               /* WATCHOUT:
+                * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the
+                * output arrays have a buffer of up to 3 zeroes in front
+                * (at negative indices) for alignment purposes; we use 4
+                * to keep the data well-aligned.
+                */
                if(0 != decoder->private_->output[i]) {
                        free(decoder->private_->output[i]-4);
                        decoder->private_->output[i] = 0;
@@ -302,7 +326,7 @@ void FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
        decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
 }
 
-FLAC__bool FLAC__stream_decoder_set_read_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderReadCallback value)
+FLAC_API FLAC__bool FLAC__stream_decoder_set_read_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderReadCallback value)
 {
        FLAC__ASSERT(0 != decoder);
        FLAC__ASSERT(0 != decoder->private_);
@@ -313,7 +337,7 @@ FLAC__bool FLAC__stream_decoder_set_read_callback(FLAC__StreamDecoder *decoder,
        return true;
 }
 
-FLAC__bool FLAC__stream_decoder_set_write_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderWriteCallback value)
+FLAC_API FLAC__bool FLAC__stream_decoder_set_write_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderWriteCallback value)
 {
        FLAC__ASSERT(0 != decoder);
        FLAC__ASSERT(0 != decoder->private_);
@@ -324,7 +348,7 @@ FLAC__bool FLAC__stream_decoder_set_write_callback(FLAC__StreamDecoder *decoder,
        return true;
 }
 
-FLAC__bool FLAC__stream_decoder_set_metadata_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderMetadataCallback value)
+FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderMetadataCallback value)
 {
        FLAC__ASSERT(0 != decoder);
        FLAC__ASSERT(0 != decoder->private_);
@@ -335,7 +359,7 @@ FLAC__bool FLAC__stream_decoder_set_metadata_callback(FLAC__StreamDecoder *decod
        return true;
 }
 
-FLAC__bool FLAC__stream_decoder_set_error_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorCallback value)
+FLAC_API FLAC__bool FLAC__stream_decoder_set_error_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorCallback value)
 {
        FLAC__ASSERT(0 != decoder);
        FLAC__ASSERT(0 != decoder->private_);
@@ -346,7 +370,7 @@ FLAC__bool FLAC__stream_decoder_set_error_callback(FLAC__StreamDecoder *decoder,
        return true;
 }
 
-FLAC__bool FLAC__stream_decoder_set_client_data(FLAC__StreamDecoder *decoder, void *value)
+FLAC_API FLAC__bool FLAC__stream_decoder_set_client_data(FLAC__StreamDecoder *decoder, void *value)
 {
        FLAC__ASSERT(0 != decoder);
        FLAC__ASSERT(0 != decoder->private_);
@@ -357,7 +381,7 @@ FLAC__bool FLAC__stream_decoder_set_client_data(FLAC__StreamDecoder *decoder, vo
        return true;
 }
 
-FLAC__bool FLAC__stream_decoder_set_metadata_respond(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
+FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
 {
        FLAC__ASSERT(0 != decoder);
        FLAC__ASSERT(0 != decoder->private_);
@@ -371,7 +395,7 @@ FLAC__bool FLAC__stream_decoder_set_metadata_respond(FLAC__StreamDecoder *decode
        return true;
 }
 
-FLAC__bool FLAC__stream_decoder_set_metadata_respond_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
+FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
 {
        FLAC__ASSERT(0 != decoder);
        FLAC__ASSERT(0 != decoder->private_);
@@ -386,7 +410,7 @@ FLAC__bool FLAC__stream_decoder_set_metadata_respond_application(FLAC__StreamDec
        FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
 
        if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
-               if(0 == (decoder->private_->metadata_filter_ids = realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2)))
+               if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2)))
                        return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
                decoder->private_->metadata_filter_ids_capacity *= 2;
        }
@@ -397,7 +421,7 @@ FLAC__bool FLAC__stream_decoder_set_metadata_respond_application(FLAC__StreamDec
        return true;
 }
 
-FLAC__bool FLAC__stream_decoder_set_metadata_respond_all(FLAC__StreamDecoder *decoder)
+FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_all(FLAC__StreamDecoder *decoder)
 {
        unsigned i;
        FLAC__ASSERT(0 != decoder);
@@ -411,7 +435,7 @@ FLAC__bool FLAC__stream_decoder_set_metadata_respond_all(FLAC__StreamDecoder *de
        return true;
 }
 
-FLAC__bool FLAC__stream_decoder_set_metadata_ignore(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
+FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
 {
        FLAC__ASSERT(0 != decoder);
        FLAC__ASSERT(0 != decoder->private_);
@@ -425,7 +449,7 @@ FLAC__bool FLAC__stream_decoder_set_metadata_ignore(FLAC__StreamDecoder *decoder
        return true;
 }
 
-FLAC__bool FLAC__stream_decoder_set_metadata_ignore_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
+FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
 {
        FLAC__ASSERT(0 != decoder);
        FLAC__ASSERT(0 != decoder->private_);
@@ -440,7 +464,7 @@ FLAC__bool FLAC__stream_decoder_set_metadata_ignore_application(FLAC__StreamDeco
        FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
 
        if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
-               if(0 == (decoder->private_->metadata_filter_ids = realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2)))
+               if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2)))
                        return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
                decoder->private_->metadata_filter_ids_capacity *= 2;
        }
@@ -451,7 +475,7 @@ FLAC__bool FLAC__stream_decoder_set_metadata_ignore_application(FLAC__StreamDeco
        return true;
 }
 
-FLAC__bool FLAC__stream_decoder_set_metadata_ignore_all(FLAC__StreamDecoder *decoder)
+FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_all(FLAC__StreamDecoder *decoder)
 {
        FLAC__ASSERT(0 != decoder);
        FLAC__ASSERT(0 != decoder->private_);
@@ -463,49 +487,49 @@ FLAC__bool FLAC__stream_decoder_set_metadata_ignore_all(FLAC__StreamDecoder *dec
        return true;
 }
 
-FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder)
+FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder)
 {
        FLAC__ASSERT(0 != decoder);
        FLAC__ASSERT(0 != decoder->protected_);
        return decoder->protected_->state;
 }
 
-unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder)
+FLAC_API unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder)
 {
        FLAC__ASSERT(0 != decoder);
        FLAC__ASSERT(0 != decoder->protected_);
        return decoder->protected_->channels;
 }
 
-FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder)
+FLAC_API FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder)
 {
        FLAC__ASSERT(0 != decoder);
        FLAC__ASSERT(0 != decoder->protected_);
        return decoder->protected_->channel_assignment;
 }
 
-unsigned FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder)
+FLAC_API unsigned FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder)
 {
        FLAC__ASSERT(0 != decoder);
        FLAC__ASSERT(0 != decoder->protected_);
        return decoder->protected_->bits_per_sample;
 }
 
-unsigned FLAC__stream_decoder_get_sample_rate(const FLAC__StreamDecoder *decoder)
+FLAC_API unsigned FLAC__stream_decoder_get_sample_rate(const FLAC__StreamDecoder *decoder)
 {
        FLAC__ASSERT(0 != decoder);
        FLAC__ASSERT(0 != decoder->protected_);
        return decoder->protected_->sample_rate;
 }
 
-unsigned FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder)
+FLAC_API unsigned FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder)
 {
        FLAC__ASSERT(0 != decoder);
        FLAC__ASSERT(0 != decoder->protected_);
        return decoder->protected_->blocksize;
 }
 
-FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
+FLAC_API FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
 {
        FLAC__ASSERT(0 != decoder);
        FLAC__ASSERT(0 != decoder->private_);
@@ -520,7 +544,7 @@ FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
        return true;
 }
 
-FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
+FLAC_API FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
 {
        FLAC__ASSERT(0 != decoder);
        FLAC__ASSERT(0 != decoder->private_);
@@ -537,7 +561,7 @@ FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
        return true;
 }
 
-FLAC__bool FLAC__stream_decoder_process_single(FLAC__StreamDecoder *decoder)
+FLAC_API FLAC__bool FLAC__stream_decoder_process_single(FLAC__StreamDecoder *decoder)
 {
        FLAC__bool got_a_frame;
        FLAC__ASSERT(0 != decoder);
@@ -574,7 +598,7 @@ FLAC__bool FLAC__stream_decoder_process_single(FLAC__StreamDecoder *decoder)
        }
 }
 
-FLAC__bool FLAC__stream_decoder_process_until_end_of_metadata(FLAC__StreamDecoder *decoder)
+FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_metadata(FLAC__StreamDecoder *decoder)
 {
        FLAC__ASSERT(0 != decoder);
        FLAC__ASSERT(0 != decoder->protected_);
@@ -601,7 +625,7 @@ FLAC__bool FLAC__stream_decoder_process_until_end_of_metadata(FLAC__StreamDecode
        }
 }
 
-FLAC__bool FLAC__stream_decoder_process_until_end_of_stream(FLAC__StreamDecoder *decoder)
+FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_stream(FLAC__StreamDecoder *decoder)
 {
        FLAC__bool dummy;
        FLAC__ASSERT(0 != decoder);
@@ -674,11 +698,11 @@ FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigne
        if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels)
                return true;
 
-       /* @@@ should change to use realloc() */
+       /* simply using realloc() is not practical because the number of channels may change mid-stream */
 
        for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
                if(0 != decoder->private_->output[i]) {
-                       free(decoder->private_->output[i]);
+                       free(decoder->private_->output[i]-4);
                        decoder->private_->output[i] = 0;
                }
                if(0 != decoder->private_->residual[i]) {
@@ -688,7 +712,12 @@ FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigne
        }
 
        for(i = 0; i < channels; i++) {
-               /* WATCHOUT: FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the output arrays have a buffer of up to 3 zeroes in front (at negative indices) for alignment purposes; we use 4 to keep the data well-aligned. */
+               /* WATCHOUT:
+                * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the
+                * output arrays have a buffer of up to 3 zeroes in front
+                * (at negative indices) for alignment purposes; we use 4
+                * to keep the data well-aligned.
+                */
                tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*(size+4));
                if(tmp == 0) {
                        decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
@@ -787,107 +816,32 @@ FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder)
 
 FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder)
 {
-       FLAC__uint32 i, x, last_block, type, length;
-       FLAC__uint64 xx;
+       FLAC__bool is_last;
+       FLAC__uint32 i, x, type, length;
 
        FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
 
-       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &last_block, FLAC__STREAM_METADATA_IS_LAST_LEN, read_callback_, decoder))
+       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_IS_LAST_LEN, read_callback_, decoder))
                return false; /* the 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))
                return false; /* the 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))
                return false; /* the read_callback_ sets the state for us */
-       if(type == FLAC__METADATA_TYPE_STREAMINFO) {
-               unsigned used_bits = 0;
-               decoder->private_->stream_info.type = type;
-               decoder->private_->stream_info.is_last = last_block;
-               decoder->private_->stream_info.length = length;
-
-               if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN, read_callback_, decoder))
-                       return false; /* the read_callback_ sets the state for us */
-               decoder->private_->stream_info.data.stream_info.min_blocksize = x;
-               used_bits += FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN;
-
-               if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN, read_callback_, decoder))
-                       return false; /* the read_callback_ sets the state for us */
-               decoder->private_->stream_info.data.stream_info.max_blocksize = x;
-               used_bits += FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN;
-
-               if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN, read_callback_, decoder))
-                       return false; /* the read_callback_ sets the state for us */
-               decoder->private_->stream_info.data.stream_info.min_framesize = x;
-               used_bits += FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN;
-
-               if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN, read_callback_, decoder))
-                       return false; /* the read_callback_ sets the state for us */
-               decoder->private_->stream_info.data.stream_info.max_framesize = x;
-               used_bits += FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN;
-
-               if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN, read_callback_, decoder))
-                       return false; /* the read_callback_ sets the state for us */
-               decoder->private_->stream_info.data.stream_info.sample_rate = x;
-               used_bits += FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN;
 
-               if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN, read_callback_, decoder))
-                       return false; /* the read_callback_ sets the state for us */
-               decoder->private_->stream_info.data.stream_info.channels = x+1;
-               used_bits += FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN;
-
-               if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN, read_callback_, decoder))
-                       return false; /* the read_callback_ sets the state for us */
-               decoder->private_->stream_info.data.stream_info.bits_per_sample = x+1;
-               used_bits += FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_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))
-                       return false; /* the read_callback_ sets the state for us */
-               used_bits += FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN;
-
-               if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, decoder->private_->stream_info.data.stream_info.md5sum, 16, read_callback_, decoder))
-                       return false; /* the 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(decoder->private_->input, 0, length, read_callback_, decoder))
-                       return false; /* the read_callback_ sets the state for us */
+       if(type == FLAC__METADATA_TYPE_STREAMINFO) {
+               if(!read_metadata_streaminfo_(decoder, is_last, length))
+                       return false;
 
                decoder->private_->has_stream_info = true;
                if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO])
                        decoder->private_->metadata_callback(decoder, &decoder->private_->stream_info, decoder->private_->client_data);
        }
        else if(type == FLAC__METADATA_TYPE_SEEKTABLE) {
-               decoder->private_->seek_table.type = type;
-               decoder->private_->seek_table.is_last = last_block;
-               decoder->private_->seek_table.length = length;
-
-               decoder->private_->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
-
-               if(0 == (decoder->private_->seek_table.data.seek_table.points = (FLAC__StreamMetadata_SeekPoint*)malloc(decoder->private_->seek_table.data.seek_table.num_points * sizeof(FLAC__StreamMetadata_SeekPoint)))) {
-                       decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
+               if(!read_metadata_seektable_(decoder, is_last, length))
                        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))
-                               return false; /* the 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))
-                               return false; /* the 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))
-                               return false; /* the read_callback_ sets the state for us */
-                       decoder->private_->seek_table.data.seek_table.points[i].frame_samples = x;
-               }
-               length -= (decoder->private_->seek_table.data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH);
-               /* if there is a partial point left, skip over it */
-               if(length > 0) {
-                       /*@@@ do an error_callback() here?  there's an argument for either way */
-                       if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, 0, length, read_callback_, decoder))
-                               return false; /* the read_callback_ sets the state for us */
-               }
 
                decoder->private_->has_seek_table = true;
                if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_SEEKTABLE])
@@ -898,8 +852,8 @@ FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder)
                unsigned real_length = length;
                FLAC__StreamMetadata block;
 
-               block.is_last = last_block;
-               block.type = type;
+               block.is_last = is_last;
+               block.type = (FLAC__MetadataType)type;
                block.length = length;
 
                if(type == FLAC__METADATA_TYPE_APPLICATION) {
@@ -926,7 +880,7 @@ FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder)
                                case FLAC__METADATA_TYPE_APPLICATION:
                                        /* remember, we read the ID already */
                                        if(real_length > 0) {
-                                               if(0 == (block.data.application.data = malloc(real_length))) {
+                                               if(0 == (block.data.application.data = (FLAC__byte*)malloc(real_length))) {
                                                        decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
                                                        return false;
                                                }
@@ -937,51 +891,8 @@ FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder)
                                                block.data.application.data = 0;
                                        break;
                                case FLAC__METADATA_TYPE_VORBIS_COMMENT:
-                                       /* 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, &block.data.vorbis_comment.vendor_string.length, read_callback_, decoder))
-                                               return false; /* the read_callback_ sets the state for us */
-                                       if(block.data.vorbis_comment.vendor_string.length > 0) {
-                                               if(0 == (block.data.vorbis_comment.vendor_string.entry = malloc(block.data.vorbis_comment.vendor_string.length))) {
-                                                       decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
-                                                       return false;
-                                               }
-                                               if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, block.data.vorbis_comment.vendor_string.entry, block.data.vorbis_comment.vendor_string.length, read_callback_, decoder))
-                                                       return false; /* the read_callback_ sets the state for us */
-                                       }
-                                       else
-                                               block.data.vorbis_comment.vendor_string.entry = 0;
-
-                                       /* 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, &block.data.vorbis_comment.num_comments, read_callback_, decoder))
-                                               return false; /* the read_callback_ sets the state for us */
-
-                                       /* read comments */
-                                       if(block.data.vorbis_comment.num_comments > 0) {
-                                               if(0 == (block.data.vorbis_comment.comments = malloc(block.data.vorbis_comment.num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry)))) {
-                                                       decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
-                                                       return false;
-                                               }
-                                               for(i = 0; i < block.data.vorbis_comment.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, &block.data.vorbis_comment.comments[i].length, read_callback_, decoder))
-                                                               return false; /* the read_callback_ sets the state for us */
-                                                       if(block.data.vorbis_comment.comments[i].length > 0) {
-                                                               if(0 == (block.data.vorbis_comment.comments[i].entry = malloc(block.data.vorbis_comment.comments[i].length))) {
-                                                                       decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
-                                                                       return false;
-                                                               }
-                                                               if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, block.data.vorbis_comment.comments[i].entry, block.data.vorbis_comment.comments[i].length, read_callback_, decoder))
-                                                                       return false; /* the read_callback_ sets the state for us */
-                                                       }
-                                                       else
-                                                               block.data.vorbis_comment.comments[i].entry = 0;
-                                               }
-                                       }
-                                       else {
-                                               block.data.vorbis_comment.comments = 0;
-                                       }
+                                       if(!read_metadata_vorbiscomment_(decoder, &block.data.vorbis_comment))
+                                               return false;
                                        break;
                                case FLAC__METADATA_TYPE_STREAMINFO:
                                case FLAC__METADATA_TYPE_SEEKTABLE:
@@ -1016,12 +927,179 @@ FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder)
                }
        }
 
-       if(last_block)
+       if(is_last)
                decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
 
        return true;
 }
 
+FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length)
+{
+       FLAC__uint32 x;
+       unsigned bits, used_bits = 0;
+
+       FLAC__ASSERT(FLAC__bitbuffer_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))
+               return false; /* the 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))
+               return false; /* the 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))
+               return false; /* the 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))
+               return false; /* the 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))
+               return false; /* the 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))
+               return false; /* the 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))
+               return false; /* the 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))
+               return false; /* the read_callback_ sets the state for us */
+       used_bits += bits;
+
+       if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, decoder->private_->stream_info.data.stream_info.md5sum, 16, read_callback_, decoder))
+               return false; /* the 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(decoder->private_->input, 0, length, read_callback_, decoder))
+               return false; /* the read_callback_ sets the state for us */
+
+       return true;
+}
+
+FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length)
+{
+       FLAC__uint32 i, x;
+       FLAC__uint64 xx;
+
+       FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
+
+       decoder->private_->seek_table.type = FLAC__METADATA_TYPE_SEEKTABLE;
+       decoder->private_->seek_table.is_last = is_last;
+       decoder->private_->seek_table.length = length;
+
+       decoder->private_->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
+
+       if(0 == (decoder->private_->seek_table.data.seek_table.points = (FLAC__StreamMetadata_SeekPoint*)malloc(decoder->private_->seek_table.data.seek_table.num_points * sizeof(FLAC__StreamMetadata_SeekPoint)))) {
+               decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
+               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))
+                       return false; /* the 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))
+                       return false; /* the 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))
+                       return false; /* the read_callback_ sets the state for us */
+               decoder->private_->seek_table.data.seek_table.points[i].frame_samples = x;
+       }
+       length -= (decoder->private_->seek_table.data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH);
+       /* if there is a partial point left, skip over it */
+       if(length > 0) {
+               /*@@@ do an error_callback() here?  there's an argument for either way */
+               if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, 0, length, read_callback_, decoder))
+                       return false; /* the read_callback_ sets the state for us */
+       }
+
+       return true;
+}
+
+FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj)
+{
+       FLAC__uint32 i;
+
+       FLAC__ASSERT(FLAC__bitbuffer_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))
+               return false; /* the 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))) {
+                       decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
+                       return false;
+               }
+               if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, obj->vendor_string.entry, obj->vendor_string.length, read_callback_, decoder))
+                       return false; /* the read_callback_ sets the state for us */
+       }
+       else
+               obj->vendor_string.entry = 0;
+
+       /* 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))
+               return false; /* the read_callback_ sets the state for us */
+
+       /* read comments */
+       if(obj->num_comments > 0) {
+               if(0 == (obj->comments = (FLAC__StreamMetadata_VorbisComment_Entry*)malloc(obj->num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry)))) {
+                       decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
+                       return false;
+               }
+               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))
+                               return false; /* the 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))) {
+                                       decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
+                                       return false;
+                               }
+                               if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, obj->comments[i].entry, obj->comments[i].length, read_callback_, decoder))
+                                       return false; /* the read_callback_ sets the state for us */
+                       }
+                       else
+                               obj->comments[i].entry = 0;
+               }
+       }
+       else {
+               obj->comments = 0;
+       }
+
+       return true;
+}
+
 FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
 {
        FLAC__uint32 x;
@@ -1614,12 +1692,13 @@ FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel,
        /* read entropy coding method info */
        if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
                return false; /* the read_callback_ sets the state for us */
-       subframe->entropy_coding_method.type = u32;
+       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))
                                return false; /* the 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];
                        break;
                default:
                        decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
@@ -1629,7 +1708,7 @@ FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel,
        /* read residual */
        switch(subframe->entropy_coding_method.type) {
                case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
-                       if(!read_residual_partitioned_rice_(decoder, order, &subframe->entropy_coding_method.data.partitioned_rice, decoder->private_->residual[channel]))
+                       if(!read_residual_partitioned_rice_(decoder, order, subframe->entropy_coding_method.data.partitioned_rice.order, &decoder->private_->partitioned_rice_contents[channel], decoder->private_->residual[channel]))
                                return false;
                        break;
                default:
@@ -1687,12 +1766,13 @@ FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, un
        /* read entropy coding method info */
        if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
                return false; /* the read_callback_ sets the state for us */
-       subframe->entropy_coding_method.type = u32;
+       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))
                                return false; /* the 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];
                        break;
                default:
                        decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
@@ -1702,7 +1782,7 @@ FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, un
        /* read residual */
        switch(subframe->entropy_coding_method.type) {
                case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
-                       if(!read_residual_partitioned_rice_(decoder, order, &subframe->entropy_coding_method.data.partitioned_rice, decoder->private_->residual[channel]))
+                       if(!read_residual_partitioned_rice_(decoder, order, subframe->entropy_coding_method.data.partitioned_rice.order, &decoder->private_->partitioned_rice_contents[channel], decoder->private_->residual[channel]))
                                return false;
                        break;
                default:
@@ -1711,10 +1791,13 @@ FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, un
 
        /* decode the subframe */
        memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
-       if(bps <= 16 && subframe->qlp_coeff_precision <= 16)
-               decoder->private_->local_lpc_restore_signal_16bit(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order);
+       if(bps + subframe->qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
+               if(bps <= 16 && subframe->qlp_coeff_precision <= 16)
+                       decoder->private_->local_lpc_restore_signal_16bit(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order);
+               else
+                       decoder->private_->local_lpc_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order);
        else
-               decoder->private_->local_lpc_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order);
+               decoder->private_->local_lpc_restore_signal_64bit(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order);
 
        return true;
 }
@@ -1741,20 +1824,24 @@ FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channe
        return true;
 }
 
-FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice, FLAC__int32 *residual)
+FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, FLAC__int32 *residual)
 {
        FLAC__uint32 rice_parameter;
        int i;
        unsigned partition, sample, u;
-       const unsigned partition_order = partitioned_rice->order;
        const unsigned partitions = 1u << partition_order;
        const unsigned partition_samples = partition_order > 0? decoder->private_->frame.header.blocksize >> partition_order : decoder->private_->frame.header.blocksize - predictor_order;
 
+       if(!FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order))) {
+               decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
+               return false;
+       }
+
        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))
                        return false; /* the read_callback_ sets the state for us */
-               partitioned_rice->parameters[partition] = rice_parameter;
+               partitioned_rice_contents->parameters[partition] = rice_parameter;
                if(rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
 #ifdef FLAC__SYMMETRIC_RICE
                        for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
@@ -1772,7 +1859,7 @@ FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigne
                else {
                        if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN, read_callback_, decoder))
                                return false; /* the read_callback_ sets the state for us */
-                       partitioned_rice->raw_bits[partition] = rice_parameter;
+                       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))
                                        return false; /* the read_callback_ sets the state for us */