Fix a couple of Windows 2Gig file size issues.
[platform/upstream/flac.git] / src / flac / decode.c
index e241dc2..2f2c311 100644 (file)
@@ -1,5 +1,5 @@
 /* flac - Command-line FLAC encoder/decoder
- * Copyright (C) 2000,2001,2002,2003,2004  Josh Coalson
+ * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007,2008,2009  Josh Coalson
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
  * 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.
+ * 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.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#ifdef HAVE_CONFIG_H
-#include <config.h>
+#if HAVE_CONFIG_H
+#  include <config.h>
 #endif
 
-#if defined _WIN32 && !defined __CYGWIN__
-/* where MSVC puts unlink() */
-# include <io.h>
-#else
-# include <unistd.h>
-#endif
 #include <errno.h>
 #include <math.h> /* for floor() */
-#include <stdio.h> /* for FILE et al. */
-#include <string.h> /* for strcmp() */
+#include <stdio.h> /* for FILE etc. */
+#include <string.h> /* for strcmp(), strerror() */
 #include "FLAC/all.h"
 #include "share/grabbag.h"
 #include "share/replaygain_synthesis.h"
+#include "share/compat.h"
 #include "decode.h"
 
-#ifdef FLAC__HAS_OGG
-#include "OggFLAC/file_decoder.h"
-#endif
-
 typedef struct {
-#ifdef FLAC__HAS_OGG
+#if FLAC__HAS_OGG
        FLAC__bool is_ogg;
+       FLAC__bool use_first_serial_number;
+       long serial_number;
 #endif
 
-       FLAC__bool is_aiff_out;
-       FLAC__bool is_wave_out;
+       FileFormat format;
+       FLAC__bool treat_warnings_as_errors;
        FLAC__bool continue_through_decode_errors;
+       FLAC__bool channel_map_none;
 
        struct {
                replaygain_synthesis_spec_t spec;
@@ -63,39 +57,37 @@ typedef struct {
        utils__CueSpecification *cue_specification;
 
        const char *inbasefilename;
+       const char *infilename;
        const char *outfilename;
 
        FLAC__uint64 samples_processed;
        unsigned frame_counter;
        FLAC__bool abort_flag;
        FLAC__bool aborting_due_to_until; /* true if we intentionally abort decoding prematurely because we hit the --until point */
+       FLAC__bool aborting_due_to_unparseable; /* true if we abort decoding because we hit an unparseable frame */
+       FLAC__bool error_callback_suppress_messages; /* turn on to prevent repeating messages from the error callback */
 
-       struct {
-               FLAC__bool needs_fixup;
-               unsigned riff_offset; /* or FORM offset for AIFF */
-               unsigned data_offset; /* or SSND offset for AIFF */
-               unsigned frames_offset; /* AIFF only */
-       } wave_chunk_size_fixup;
+       FLAC__bool iff_headers_need_fixup;
 
        FLAC__bool is_big_endian;
        FLAC__bool is_unsigned_samples;
+       FLAC__bool got_stream_info;
+       FLAC__bool has_md5sum;
        FLAC__uint64 total_samples;
        unsigned bps;
        unsigned channels;
        unsigned sample_rate;
+       FLAC__uint32 channel_mask;
 
-       union {
-               union {
-                       FLAC__FileDecoder *file;
-               } flac;
-#ifdef FLAC__HAS_OGG
-               union {
-                       OggFLAC__FileDecoder *file;
-               } ogg;
-#endif
-       } decoder;
+       /* these are used only in analyze mode */
+       FLAC__uint64 decode_position;
+
+       FLAC__StreamDecoder *decoder;
 
        FILE *fout;
+
+       foreign_metadata_t *foreign_metadata; /* NULL unless --keep-foreign-metadata requested */
+       FLAC__off_t fm_offset1, fm_offset2, fm_offset3;
 } DecoderSession;
 
 
@@ -105,28 +97,27 @@ static FLAC__bool is_big_endian_host_;
 /*
  * local routines
  */
-static FLAC__bool DecoderSession_construct(DecoderSession *d, FLAC__bool is_ogg, FLAC__bool is_aiff_out, FLAC__bool is_wave_out, FLAC__bool continue_through_decode_errors, replaygain_synthesis_spec_t replaygain_synthesis_spec, FLAC__bool analysis_mode, analysis_options aopts, utils__SkipUntilSpecification *skip_specification, utils__SkipUntilSpecification *until_specification, utils__CueSpecification *cue_specification, const char *infilename, const char *outfilename);
+static FLAC__bool DecoderSession_construct(DecoderSession *d, FLAC__bool is_ogg, FLAC__bool use_first_serial_number, long serial_number, FileFormat format, FLAC__bool treat_warnings_as_errors, FLAC__bool continue_through_decode_errors, FLAC__bool channel_map_none, replaygain_synthesis_spec_t replaygain_synthesis_spec, FLAC__bool analysis_mode, analysis_options aopts, utils__SkipUntilSpecification *skip_specification, utils__SkipUntilSpecification *until_specification, utils__CueSpecification *cue_specification, foreign_metadata_t *foreign_metadata, const char *infilename, const char *outfilename);
 static void DecoderSession_destroy(DecoderSession *d, FLAC__bool error_occurred);
-static FLAC__bool DecoderSession_init_decoder(DecoderSession *d, decode_options_t decode_options, const char *infilename);
+static FLAC__bool DecoderSession_init_decoder(DecoderSession *d, const char *infilename);
 static FLAC__bool DecoderSession_process(DecoderSession *d);
 static int DecoderSession_finish_ok(DecoderSession *d);
 static int DecoderSession_finish_error(DecoderSession *d);
 static FLAC__bool canonicalize_until_specification(utils__SkipUntilSpecification *spec, const char *inbasefilename, unsigned sample_rate, FLAC__uint64 skip, FLAC__uint64 total_samples_in_input);
-static FLAC__bool write_necessary_headers(DecoderSession *decoder_session);
+static FLAC__bool write_iff_headers(FILE *f, DecoderSession *decoder_session, FLAC__uint64 samples);
+static FLAC__bool write_riff_wave_fmt_chunk_body(FILE *f, FLAC__bool is_waveformatextensible, unsigned bps, unsigned channels, unsigned sample_rate, FLAC__uint32 channel_mask);
+static FLAC__bool write_aiff_form_comm_chunk(FILE *f, FLAC__uint64 samples, unsigned bps, unsigned channels, unsigned sample_rate);
 static FLAC__bool write_little_endian_uint16(FILE *f, FLAC__uint16 val);
 static FLAC__bool write_little_endian_uint32(FILE *f, FLAC__uint32 val);
+static FLAC__bool write_little_endian_uint64(FILE *f, FLAC__uint64 val);
 static FLAC__bool write_big_endian_uint16(FILE *f, FLAC__uint16 val);
 static FLAC__bool write_big_endian_uint32(FILE *f, FLAC__uint32 val);
 static FLAC__bool write_sane_extended(FILE *f, unsigned val);
-static FLAC__bool fixup_wave_chunk_size(const char *outfilename, FLAC__bool is_wave_out, unsigned riff_offset, unsigned data_offset, unsigned frames_offset, FLAC__uint32 total_samples, unsigned channels, unsigned bps);
-/*
- * We use 'void *' so that we can use the same callbacks for the
- * FLAC__StreamDecoder and FLAC__FileDecoder.  The 'decoder' argument is
- * actually never used in the callbacks.
- */
-static FLAC__StreamDecoderWriteStatus write_callback(const void *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
-static void metadata_callback(const void *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
-static void error_callback(const void *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
+static FLAC__bool fixup_iff_headers(DecoderSession *d);
+static FLAC__StreamDecoderWriteStatus write_callback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
+static void metadata_callback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
+static void error_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
+static void print_error_with_init_status(const DecoderSession *d, const char *message, FLAC__StreamDecoderInitStatus init_status);
 static void print_error_with_state(const DecoderSession *d, const char *message);
 static void print_stats(const DecoderSession *decoder_session);
 
@@ -134,109 +125,54 @@ static void print_stats(const DecoderSession *decoder_session);
 /*
  * public routines
  */
-int flac__decode_aiff(const char *infilename, const char *outfilename, FLAC__bool analysis_mode, analysis_options aopts, wav_decode_options_t options)
-{
-       DecoderSession decoder_session;
-
-       if(!
-               DecoderSession_construct(
-                       &decoder_session,
-#ifdef FLAC__HAS_OGG
-                       options.common.is_ogg,
-#else
-                       /*is_ogg=*/false,
-#endif
-                       /*is_aiff_out=*/true,
-                       /*is_wave_out=*/false,
-                       options.common.continue_through_decode_errors,
-                       options.common.replaygain_synthesis_spec,
-                       analysis_mode,
-                       aopts,
-                       &options.common.skip_specification,
-                       &options.common.until_specification,
-                       options.common.has_cue_specification? &options.common.cue_specification : 0,
-                       infilename,
-                       outfilename
-               )
-       )
-               return 1;
-
-       if(!DecoderSession_init_decoder(&decoder_session, options.common, infilename))
-               return DecoderSession_finish_error(&decoder_session);
-
-       if(!DecoderSession_process(&decoder_session))
-               return DecoderSession_finish_error(&decoder_session);
-
-       return DecoderSession_finish_ok(&decoder_session);
-}
-
-int flac__decode_wav(const char *infilename, const char *outfilename, FLAC__bool analysis_mode, analysis_options aopts, wav_decode_options_t options)
-{
-       DecoderSession decoder_session;
-
-       if(!
-               DecoderSession_construct(
-                       &decoder_session,
-#ifdef FLAC__HAS_OGG
-                       options.common.is_ogg,
-#else
-                       /*is_ogg=*/false,
-#endif
-                       /*is_aiff_out=*/false,
-                       /*is_wave_out=*/true,
-                       options.common.continue_through_decode_errors,
-                       options.common.replaygain_synthesis_spec,
-                       analysis_mode,
-                       aopts,
-                       &options.common.skip_specification,
-                       &options.common.until_specification,
-                       options.common.has_cue_specification? &options.common.cue_specification : 0,
-                       infilename,
-                       outfilename
-               )
-       )
-               return 1;
-
-       if(!DecoderSession_init_decoder(&decoder_session, options.common, infilename))
-               return DecoderSession_finish_error(&decoder_session);
-
-       if(!DecoderSession_process(&decoder_session))
-               return DecoderSession_finish_error(&decoder_session);
-
-       return DecoderSession_finish_ok(&decoder_session);
-}
-
-int flac__decode_raw(const char *infilename, const char *outfilename, FLAC__bool analysis_mode, analysis_options aopts, raw_decode_options_t options)
+int flac__decode_file(const char *infilename, const char *outfilename, FLAC__bool analysis_mode, analysis_options aopts, decode_options_t options)
 {
        DecoderSession decoder_session;
 
-       decoder_session.is_big_endian = options.is_big_endian;
-       decoder_session.is_unsigned_samples = options.is_unsigned_samples;
+       FLAC__ASSERT(
+               options.format == FORMAT_WAVE ||
+               options.format == FORMAT_WAVE64 ||
+               options.format == FORMAT_RF64 ||
+               options.format == FORMAT_AIFF ||
+               options.format == FORMAT_AIFF_C ||
+               options.format == FORMAT_RAW
+       );
+
+       if(options.format == FORMAT_RAW) {
+               decoder_session.is_big_endian = options.format_options.raw.is_big_endian;
+               decoder_session.is_unsigned_samples = options.format_options.raw.is_unsigned_samples;
+       }
 
        if(!
                DecoderSession_construct(
                        &decoder_session,
-#ifdef FLAC__HAS_OGG
-                       options.common.is_ogg,
+#if FLAC__HAS_OGG
+                       options.is_ogg,
+                       options.use_first_serial_number,
+                       options.serial_number,
 #else
                        /*is_ogg=*/false,
+                       /*use_first_serial_number=*/false,
+                       /*serial_number=*/0,
 #endif
-                       /*is_aiff_out=*/false,
-                       /*is_wave_out=*/false,
-                       options.common.continue_through_decode_errors,
-                       options.common.replaygain_synthesis_spec,
+                       options.format,
+                       options.treat_warnings_as_errors,
+                       options.continue_through_decode_errors,
+                       options.channel_map_none,
+                       options.replaygain_synthesis_spec,
                        analysis_mode,
                        aopts,
-                       &options.common.skip_specification,
-                       &options.common.until_specification,
-                       options.common.has_cue_specification? &options.common.cue_specification : 0,
+                       &options.skip_specification,
+                       &options.until_specification,
+                       options.has_cue_specification? &options.cue_specification : 0,
+                       options.format == FORMAT_RAW? NULL : options.format_options.iff.foreign_metadata,
                        infilename,
                        outfilename
                )
        )
                return 1;
 
-       if(!DecoderSession_init_decoder(&decoder_session, options.common, infilename))
+       if(!DecoderSession_init_decoder(&decoder_session, infilename))
                return DecoderSession_finish_error(&decoder_session);
 
        if(!DecoderSession_process(&decoder_session))
@@ -245,17 +181,22 @@ int flac__decode_raw(const char *infilename, const char *outfilename, FLAC__bool
        return DecoderSession_finish_ok(&decoder_session);
 }
 
-FLAC__bool DecoderSession_construct(DecoderSession *d, FLAC__bool is_ogg, FLAC__bool is_aiff_out, FLAC__bool is_wave_out, FLAC__bool continue_through_decode_errors, replaygain_synthesis_spec_t replaygain_synthesis_spec, FLAC__bool analysis_mode, analysis_options aopts, utils__SkipUntilSpecification *skip_specification, utils__SkipUntilSpecification *until_specification, utils__CueSpecification *cue_specification, const char *infilename, const char *outfilename)
+FLAC__bool DecoderSession_construct(DecoderSession *d, FLAC__bool is_ogg, FLAC__bool use_first_serial_number, long serial_number, FileFormat format, FLAC__bool treat_warnings_as_errors, FLAC__bool continue_through_decode_errors, FLAC__bool channel_map_none, replaygain_synthesis_spec_t replaygain_synthesis_spec, FLAC__bool analysis_mode, analysis_options aopts, utils__SkipUntilSpecification *skip_specification, utils__SkipUntilSpecification *until_specification, utils__CueSpecification *cue_specification, foreign_metadata_t *foreign_metadata, const char *infilename, const char *outfilename)
 {
-#ifdef FLAC__HAS_OGG
+#if FLAC__HAS_OGG
        d->is_ogg = is_ogg;
+       d->use_first_serial_number = use_first_serial_number;
+       d->serial_number = serial_number;
 #else
        (void)is_ogg;
+       (void)use_first_serial_number;
+       (void)serial_number;
 #endif
 
-       d->is_aiff_out = is_aiff_out;
-       d->is_wave_out = is_wave_out;
+       d->format = format;
+       d->treat_warnings_as_errors = treat_warnings_as_errors;
        d->continue_through_decode_errors = continue_through_decode_errors;
+       d->channel_map_none = channel_map_none;
        d->replaygain.spec = replaygain_synthesis_spec;
        d->replaygain.apply = false;
        d->replaygain.scale = 0.0;
@@ -268,22 +209,34 @@ FLAC__bool DecoderSession_construct(DecoderSession *d, FLAC__bool is_ogg, FLAC__
        d->cue_specification = cue_specification;
 
        d->inbasefilename = grabbag__file_get_basename(infilename);
+       d->infilename = infilename;
        d->outfilename = outfilename;
 
        d->samples_processed = 0;
        d->frame_counter = 0;
        d->abort_flag = false;
        d->aborting_due_to_until = false;
+       d->aborting_due_to_unparseable = false;
+       d->error_callback_suppress_messages = false;
 
-       d->wave_chunk_size_fixup.needs_fixup = false;
+       d->iff_headers_need_fixup = false;
 
-       d->decoder.flac.file = 0;
-#ifdef FLAC__HAS_OGG
-       d->decoder.ogg.file = 0;
-#endif
+       d->total_samples = 0;
+       d->got_stream_info = false;
+       d->has_md5sum = false;
+       d->bps = 0;
+       d->channels = 0;
+       d->sample_rate = 0;
+       d->channel_mask = 0;
+
+       d->decode_position = 0;
+
+       d->decoder = 0;
 
        d->fout = 0; /* initialized with an open file later if necessary */
 
+       d->foreign_metadata = foreign_metadata;
+
        FLAC__ASSERT(!(d->test_only && d->analysis_mode));
 
        if(!d->test_only) {
@@ -292,7 +245,7 @@ FLAC__bool DecoderSession_construct(DecoderSession *d, FLAC__bool is_ogg, FLAC__
                }
                else {
                        if(0 == (d->fout = fopen(outfilename, "wb"))) {
-                               flac__utils_printf(stderr, 1, "%s: ERROR: can't open output file %s\n", d->inbasefilename, outfilename);
+                               flac__utils_printf(stderr, 1, "%s: ERROR: can't open output file %s: %s\n", d->inbasefilename, outfilename, strerror(errno));
                                DecoderSession_destroy(d, /*error_occurred=*/true);
                                return false;
                        }
@@ -314,76 +267,49 @@ void DecoderSession_destroy(DecoderSession *d, FLAC__bool error_occurred)
        }
 }
 
-FLAC__bool DecoderSession_init_decoder(DecoderSession *decoder_session, decode_options_t decode_options, const char *infilename)
+FLAC__bool DecoderSession_init_decoder(DecoderSession *decoder_session, const char *infilename)
 {
+       FLAC__StreamDecoderInitStatus init_status;
        FLAC__uint32 test = 1;
 
        is_big_endian_host_ = (*((FLAC__byte*)(&test)))? false : true;
 
-#ifdef FLAC__HAS_OGG
-       if(decoder_session->is_ogg) {
-               decoder_session->decoder.ogg.file = OggFLAC__file_decoder_new();
-
-               if(0 == decoder_session->decoder.ogg.file) {
-                       flac__utils_printf(stderr, 1, "%s: ERROR creating the decoder instance\n", decoder_session->inbasefilename);
+       if(!decoder_session->analysis_mode && !decoder_session->test_only && decoder_session->foreign_metadata) {
+               const char *error;
+               if(!flac__foreign_metadata_read_from_flac(decoder_session->foreign_metadata, infilename, &error)) {
+                       flac__utils_printf(stderr, 1, "%s: ERROR reading foreign metadata: %s\n", decoder_session->inbasefilename, error);
                        return false;
                }
+       }
 
-               OggFLAC__file_decoder_set_md5_checking(decoder_session->decoder.ogg.file, true);
-               OggFLAC__file_decoder_set_filename(decoder_session->decoder.ogg.file, infilename);
-               if(!decode_options.use_first_serial_number)
-                       OggFLAC__file_decoder_set_serial_number(decoder_session->decoder.ogg.file, decode_options.serial_number);
-               if (0 != decoder_session->cue_specification)
-                       OggFLAC__file_decoder_set_metadata_respond(decoder_session->decoder.ogg.file, FLAC__METADATA_TYPE_CUESHEET);
-               if (decoder_session->replaygain.spec.apply)
-                       OggFLAC__file_decoder_set_metadata_respond(decoder_session->decoder.ogg.file, FLAC__METADATA_TYPE_VORBIS_COMMENT);
-
-               /*
-                * The three ugly casts here are to 'downcast' the 'void *' argument of
-                * the callback down to 'OggFLAC__FileDecoder *'.  In C++ this would be
-                * unnecessary but here the cast makes the C compiler happy.
-                */
-               OggFLAC__file_decoder_set_write_callback(decoder_session->decoder.ogg.file, (FLAC__StreamDecoderWriteStatus (*)(const OggFLAC__FileDecoder *, const FLAC__Frame *, const FLAC__int32 * const [], void *))write_callback);
-               OggFLAC__file_decoder_set_metadata_callback(decoder_session->decoder.ogg.file, (void (*)(const OggFLAC__FileDecoder *, const FLAC__StreamMetadata *, void *))metadata_callback);
-               OggFLAC__file_decoder_set_error_callback(decoder_session->decoder.ogg.file, (void (*)(const OggFLAC__FileDecoder *, FLAC__StreamDecoderErrorStatus, void *))error_callback);
-               OggFLAC__file_decoder_set_client_data(decoder_session->decoder.ogg.file, decoder_session);
-
-               if(OggFLAC__file_decoder_init(decoder_session->decoder.ogg.file) != OggFLAC__FILE_DECODER_OK) {
-                       print_error_with_state(decoder_session, "ERROR initializing decoder");
-                       return false;
-               }
+       decoder_session->decoder = FLAC__stream_decoder_new();
+
+       if(0 == decoder_session->decoder) {
+               flac__utils_printf(stderr, 1, "%s: ERROR creating the decoder instance\n", decoder_session->inbasefilename);
+               return false;
+       }
+
+       FLAC__stream_decoder_set_md5_checking(decoder_session->decoder, true);
+       if (0 != decoder_session->cue_specification)
+               FLAC__stream_decoder_set_metadata_respond(decoder_session->decoder, FLAC__METADATA_TYPE_CUESHEET);
+       if (decoder_session->replaygain.spec.apply)
+               FLAC__stream_decoder_set_metadata_respond(decoder_session->decoder, FLAC__METADATA_TYPE_VORBIS_COMMENT);
+
+#if FLAC__HAS_OGG
+       if(decoder_session->is_ogg) {
+               if(!decoder_session->use_first_serial_number)
+                       FLAC__stream_decoder_set_ogg_serial_number(decoder_session->decoder, decoder_session->serial_number);
+               init_status = FLAC__stream_decoder_init_ogg_file(decoder_session->decoder, strcmp(infilename, "-")? infilename : 0, write_callback, metadata_callback, error_callback, /*client_data=*/decoder_session);
        }
        else
-#else
-       (void)decode_options;
 #endif
        {
-               decoder_session->decoder.flac.file = FLAC__file_decoder_new();
-
-               if(0 == decoder_session->decoder.flac.file) {
-                       flac__utils_printf(stderr, 1, "%s: ERROR creating the decoder instance\n", decoder_session->inbasefilename);
-                       return false;
-               }
+               init_status = FLAC__stream_decoder_init_file(decoder_session->decoder, strcmp(infilename, "-")? infilename : 0, write_callback, metadata_callback, error_callback, /*client_data=*/decoder_session);
+       }
 
-               FLAC__file_decoder_set_md5_checking(decoder_session->decoder.flac.file, true);
-               FLAC__file_decoder_set_filename(decoder_session->decoder.flac.file, infilename);
-               if (0 != decoder_session->cue_specification)
-                       FLAC__file_decoder_set_metadata_respond(decoder_session->decoder.flac.file, FLAC__METADATA_TYPE_CUESHEET);
-               if (decoder_session->replaygain.spec.apply)
-                       FLAC__file_decoder_set_metadata_respond(decoder_session->decoder.flac.file, FLAC__METADATA_TYPE_VORBIS_COMMENT);
-               /*
-                * The three ugly casts here are to 'downcast' the 'void *' argument of
-                * the callback down to 'FLAC__FileDecoder *'.
-                */
-               FLAC__file_decoder_set_write_callback(decoder_session->decoder.flac.file, (FLAC__StreamDecoderWriteStatus (*)(const FLAC__FileDecoder *, const FLAC__Frame *, const FLAC__int32 * const [], void *))write_callback);
-               FLAC__file_decoder_set_metadata_callback(decoder_session->decoder.flac.file, (void (*)(const FLAC__FileDecoder *, const FLAC__StreamMetadata *, void *))metadata_callback);
-               FLAC__file_decoder_set_error_callback(decoder_session->decoder.flac.file, (void (*)(const FLAC__FileDecoder *, FLAC__StreamDecoderErrorStatus, void *))error_callback);
-               FLAC__file_decoder_set_client_data(decoder_session->decoder.flac.file, decoder_session);
-
-               if(FLAC__file_decoder_init(decoder_session->decoder.flac.file) != FLAC__FILE_DECODER_OK) {
-                       print_error_with_state(decoder_session, "ERROR initializing decoder");
-                       return false;
-               }
+       if(init_status != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
+               print_error_with_init_status(decoder_session, "ERROR initializing decoder", init_status);
+               return false;
        }
 
        return true;
@@ -391,176 +317,158 @@ FLAC__bool DecoderSession_init_decoder(DecoderSession *decoder_session, decode_o
 
 FLAC__bool DecoderSession_process(DecoderSession *d)
 {
-#ifdef FLAC__HAS_OGG
-       if(d->is_ogg) {
-               if(!OggFLAC__file_decoder_process_until_end_of_metadata(d->decoder.ogg.file)) {
-                       flac__utils_printf(stderr, 2, "\n");
-                       print_error_with_state(d, "ERROR while decoding metadata");
+       if(!FLAC__stream_decoder_process_until_end_of_metadata(d->decoder)) {
+               flac__utils_printf(stderr, 2, "\n");
+               print_error_with_state(d, "ERROR while decoding metadata");
+               return false;
+       }
+       if(FLAC__stream_decoder_get_state(d->decoder) > FLAC__STREAM_DECODER_END_OF_STREAM) {
+               flac__utils_printf(stderr, 2, "\n");
+               print_error_with_state(d, "ERROR during metadata decoding");
+               if(!d->continue_through_decode_errors)
                        return false;
+       }
+
+       if(d->abort_flag)
+               return false;
+
+       /* set channel mapping */
+       /* currently FLAC order matches SMPTE/WAVEFORMATEXTENSIBLE order, so no reordering is necessary; see encode.c */
+       /* only the channel mask must be set if it was not already picked up from the WAVEFORMATEXTENSIBLE_CHANNEL_MASK tag */
+       if(!d->channel_map_none && d->channel_mask == 0) {
+               if(d->channels == 1) {
+                       d->channel_mask = 0x0001;
                }
-               if(OggFLAC__file_decoder_get_state(d->decoder.ogg.file) != OggFLAC__FILE_DECODER_OK && OggFLAC__file_decoder_get_state(d->decoder.ogg.file) != OggFLAC__FILE_DECODER_END_OF_FILE) {
-                       flac__utils_printf(stderr, 2, "\n");
-                       print_error_with_state(d, "ERROR during metadata decoding");
-                       return false;
+               else if(d->channels == 2) {
+                       d->channel_mask = 0x0003;
                }
-       }
-       else
-#endif
-       {
-               if(!FLAC__file_decoder_process_until_end_of_metadata(d->decoder.flac.file)) {
-                       flac__utils_printf(stderr, 2, "\n");
-                       print_error_with_state(d, "ERROR while decoding metadata");
-                       return false;
+               else if(d->channels == 3) {
+                       d->channel_mask = 0x0007;
                }
-               if(FLAC__file_decoder_get_state(d->decoder.flac.file) != FLAC__FILE_DECODER_OK && FLAC__file_decoder_get_state(d->decoder.flac.file) != FLAC__FILE_DECODER_END_OF_FILE) {
-                       flac__utils_printf(stderr, 2, "\n");
-                       print_error_with_state(d, "ERROR during metadata decoding");
-                       return false;
+               else if(d->channels == 4) {
+                       d->channel_mask = 0x0033;
+               }
+               else if(d->channels == 5) {
+                       d->channel_mask = 0x0607;
+               }
+               else if(d->channels == 6) {
+                       d->channel_mask = 0x060f;
+               }
+               else if(d->channels == 7) {
+                       d->channel_mask = 0x070f;
+               }
+               else if(d->channels == 8) {
+                       d->channel_mask = 0x063f;
                }
        }
-       if(d->abort_flag)
-               return false;
 
        /* write the WAVE/AIFF headers if necessary */
-       if(!write_necessary_headers(d)) {
-               d->abort_flag = true;
-               return false;
+       if(!d->analysis_mode && !d->test_only && d->format != FORMAT_RAW) {
+               if(!write_iff_headers(d->fout, d, d->total_samples)) {
+                       d->abort_flag = true;
+                       return false;
+               }
        }
 
        if(d->skip_specification->value.samples > 0) {
                const FLAC__uint64 skip = (FLAC__uint64)d->skip_specification->value.samples;
 
-#ifdef FLAC__HAS_OGG
-               if(d->is_ogg) {
-                       if(!OggFLAC__file_decoder_seek_absolute(d->decoder.ogg.file, skip)) {
-                               print_error_with_state(d, "ERROR seeking while skipping bytes");
-                               return false;
-                       }
-                       if(!OggFLAC__file_decoder_process_until_end_of_file(d->decoder.ogg.file) && !d->aborting_due_to_until) {
-                               flac__utils_printf(stderr, 2, "\n");
-                               print_error_with_state(d, "ERROR while decoding frames");
-                               return false;
-                       }
-                       if(OggFLAC__file_decoder_get_state(d->decoder.ogg.file) != OggFLAC__FILE_DECODER_OK && OggFLAC__file_decoder_get_state(d->decoder.ogg.file) != OggFLAC__FILE_DECODER_END_OF_FILE && !d->aborting_due_to_until) {
-                               flac__utils_printf(stderr, 2, "\n");
-                               print_error_with_state(d, "ERROR during decoding");
-                               return false;
-                       }
-               }
-               else
-#endif
-               {
-                       if(!FLAC__file_decoder_seek_absolute(d->decoder.flac.file, skip)) {
-                               print_error_with_state(d, "ERROR seeking while skipping bytes");
-                               return false;
-                       }
-                       if(!FLAC__file_decoder_process_until_end_of_file(d->decoder.flac.file) && !d->aborting_due_to_until) {
-                               flac__utils_printf(stderr, 2, "\n");
-                               print_error_with_state(d, "ERROR while decoding frames");
-                               return false;
-                       }
-                       if(FLAC__file_decoder_get_state(d->decoder.flac.file) != FLAC__FILE_DECODER_OK && FLAC__file_decoder_get_state(d->decoder.flac.file) != FLAC__FILE_DECODER_END_OF_FILE && !d->aborting_due_to_until) {
-                               flac__utils_printf(stderr, 2, "\n");
-                               print_error_with_state(d, "ERROR during decoding");
-                               return false;
-                       }
+               if(!FLAC__stream_decoder_seek_absolute(d->decoder, skip)) {
+                       print_error_with_state(d, "ERROR seeking while skipping bytes");
+                       return false;
                }
        }
-       else {
-#ifdef FLAC__HAS_OGG
-               if(d->is_ogg) {
-                       if(!OggFLAC__file_decoder_process_until_end_of_file(d->decoder.ogg.file) && !d->aborting_due_to_until) {
-                               flac__utils_printf(stderr, 2, "\n");
-                               print_error_with_state(d, "ERROR while decoding data");
-                               return false;
-                       }
-                       if(OggFLAC__file_decoder_get_state(d->decoder.ogg.file) != OggFLAC__FILE_DECODER_OK && OggFLAC__file_decoder_get_state(d->decoder.ogg.file) != OggFLAC__FILE_DECODER_END_OF_FILE && !d->aborting_due_to_until) {
-                               flac__utils_printf(stderr, 2, "\n");
-                               print_error_with_state(d, "ERROR during decoding");
-                               return false;
-                       }
+       if(!FLAC__stream_decoder_process_until_end_of_stream(d->decoder) && !d->aborting_due_to_until) {
+               flac__utils_printf(stderr, 2, "\n");
+               print_error_with_state(d, "ERROR while decoding data");
+               if(!d->continue_through_decode_errors)
+                       return false;
+       }
+       if(
+               (d->abort_flag && !(d->aborting_due_to_until || d->continue_through_decode_errors)) ||
+               (FLAC__stream_decoder_get_state(d->decoder) > FLAC__STREAM_DECODER_END_OF_STREAM && !d->aborting_due_to_until)
+       ) {
+               flac__utils_printf(stderr, 2, "\n");
+               print_error_with_state(d, "ERROR during decoding");
+               return false;
+       }
+
+       /* write padding bytes for alignment if necessary */
+       if(!d->analysis_mode && !d->test_only && d->format != FORMAT_RAW) {
+               const FLAC__uint64 data_size = d->total_samples * d->channels * ((d->bps+7)/8);
+               unsigned padding;
+               if(d->format != FORMAT_WAVE64) {
+                       padding = (unsigned)(data_size & 1);
                }
-               else
-#endif
-               {
-                       if(!FLAC__file_decoder_process_until_end_of_file(d->decoder.flac.file) && !d->aborting_due_to_until) {
-                               flac__utils_printf(stderr, 2, "\n");
-                               print_error_with_state(d, "ERROR while decoding data");
-                               return false;
-                       }
-                       if(FLAC__file_decoder_get_state(d->decoder.flac.file) != FLAC__FILE_DECODER_OK && FLAC__file_decoder_get_state(d->decoder.flac.file) != FLAC__FILE_DECODER_END_OF_FILE && !d->aborting_due_to_until) {
-                               flac__utils_printf(stderr, 2, "\n");
-                               print_error_with_state(d, "ERROR during decoding");
+               else {
+                       /* 8-byte alignment for Wave64 */
+                       padding = (8 - (unsigned)(data_size & 7)) & 7;
+               }
+               for( ; padding > 0; --padding) {
+                       if(flac__utils_fwrite("\000", 1, 1, d->fout) != 1) {
+                               print_error_with_state(
+                                       d,
+                                       d->format == FORMAT_WAVE?   "ERROR writing pad byte to WAVE data chunk" :
+                                       d->format == FORMAT_WAVE64? "ERROR writing pad bytes to WAVE64 data chunk" :
+                                       d->format == FORMAT_RF64?   "ERROR writing pad byte to RF64 data chunk" :
+                                       "ERROR writing pad byte to AIFF SSND chunk"
+                               );
                                return false;
                        }
                }
        }
 
-       if((d->is_wave_out || d->is_aiff_out) && ((d->total_samples * d->channels * ((d->bps+7)/8)) & 1)) {
-               if(flac__utils_fwrite("\000", 1, 1, d->fout) != 1) {
-                       print_error_with_state(d, d->is_wave_out?
-                               "ERROR writing pad byte to WAVE data chunk" :
-                               "ERROR writing pad byte to AIFF SSND chunk"
-                       );
-                       return false;
-               }
-       }
-
        return true;
 }
 
 int DecoderSession_finish_ok(DecoderSession *d)
 {
-       FLAC__bool md5_failure = false;
-
-#ifdef FLAC__HAS_OGG
-       if(d->is_ogg) {
-               if(d->decoder.ogg.file) {
-                       md5_failure = !OggFLAC__file_decoder_finish(d->decoder.ogg.file) && !d->aborting_due_to_until;
-                       print_stats(d);
-                       OggFLAC__file_decoder_delete(d->decoder.ogg.file);
-               }
-       }
-       else
-#endif
-       {
-               if(d->decoder.flac.file) {
-                       md5_failure = !FLAC__file_decoder_finish(d->decoder.flac.file) && !d->aborting_due_to_until;
-                       print_stats(d);
-                       FLAC__file_decoder_delete(d->decoder.flac.file);
-               }
+       FLAC__bool ok = true, md5_failure = false;
+
+       if(d->decoder) {
+               md5_failure = !FLAC__stream_decoder_finish(d->decoder) && !d->aborting_due_to_until;
+               print_stats(d);
+               FLAC__stream_decoder_delete(d->decoder);
        }
        if(d->analysis_mode)
                flac__analyze_finish(d->aopts);
        if(md5_failure) {
-               flac__utils_printf(stderr, 1, "\r%s: WARNING, MD5 signature mismatch\n", d->inbasefilename);
+               flac__utils_printf(stderr, 1, "\r%s: ERROR, MD5 signature mismatch\n", d->inbasefilename);
+               ok = d->continue_through_decode_errors;
        }
        else {
+               if(!d->got_stream_info) {
+                       flac__utils_printf(stderr, 1, "\r%s: WARNING, cannot check MD5 signature since there was no STREAMINFO\n", d->inbasefilename);
+                       ok = !d->treat_warnings_as_errors;
+               }
+               else if(!d->has_md5sum) {
+                       flac__utils_printf(stderr, 1, "\r%s: WARNING, cannot check MD5 signature since it was unset in the STREAMINFO\n", d->inbasefilename);
+                       ok = !d->treat_warnings_as_errors;
+               }
                flac__utils_printf(stderr, 2, "\r%s: %s         \n", d->inbasefilename, d->test_only? "ok           ":d->analysis_mode?"done           ":"done");
        }
-       DecoderSession_destroy(d, /*error_occurred=*/false);
-       if((d->is_wave_out || d->is_aiff_out) && d->wave_chunk_size_fixup.needs_fixup)
-               if(!fixup_wave_chunk_size(d->outfilename, d->is_wave_out, d->wave_chunk_size_fixup.riff_offset, d->wave_chunk_size_fixup.data_offset, d->wave_chunk_size_fixup.frames_offset, (FLAC__uint32)d->samples_processed, d->channels, d->bps))
-                       return 1;
-       return 0;
+       DecoderSession_destroy(d, /*error_occurred=*/!ok);
+       if(!d->analysis_mode && !d->test_only && d->format != FORMAT_RAW) {
+               if(d->iff_headers_need_fixup || (!d->got_stream_info && strcmp(d->outfilename, "-"))) {
+                       if(!fixup_iff_headers(d))
+                               return 1;
+               }
+               if(d->foreign_metadata) {
+                       const char *error;
+                       if(!flac__foreign_metadata_write_to_iff(d->foreign_metadata, d->infilename, d->outfilename, d->fm_offset1, d->fm_offset2, d->fm_offset3, &error)) {
+                               flac__utils_printf(stderr, 1, "ERROR updating foreign metadata from %s to %s: %s\n", d->infilename, d->outfilename, error);
+                               return 1;
+                       }
+               }
+       }
+       return ok? 0 : 1;
 }
 
 int DecoderSession_finish_error(DecoderSession *d)
 {
-#ifdef FLAC__HAS_OGG
-       if(d->is_ogg) {
-               if(d->decoder.ogg.file) {
-                       OggFLAC__file_decoder_finish(d->decoder.ogg.file);
-                       OggFLAC__file_decoder_delete(d->decoder.ogg.file);
-               }
-       }
-       else
-#endif
-       {
-               if(d->decoder.flac.file) {
-                       FLAC__file_decoder_finish(d->decoder.flac.file);
-                       FLAC__file_decoder_delete(d->decoder.flac.file);
-               }
+       if(d->decoder) {
+               (void)FLAC__stream_decoder_finish(d->decoder);
+               FLAC__stream_decoder_delete(d->decoder);
        }
        if(d->analysis_mode)
                flac__analyze_finish(d->aopts);
@@ -613,121 +521,318 @@ FLAC__bool canonicalize_until_specification(utils__SkipUntilSpecification *spec,
        return true;
 }
 
-FLAC__bool write_necessary_headers(DecoderSession *decoder_session)
+FLAC__bool write_iff_headers(FILE *f, DecoderSession *decoder_session, FLAC__uint64 samples)
 {
-       /* write the WAVE/AIFF headers if necessary */
-       if(!decoder_session->analysis_mode && !decoder_session->test_only && (decoder_session->is_wave_out || decoder_session->is_aiff_out)) {
-               const char *fmt_desc = decoder_session->is_wave_out? "WAVE" : "AIFF";
-               FLAC__uint64 data_size = decoder_session->total_samples * decoder_session->channels * ((decoder_session->bps+7)/8);
-               const FLAC__uint32 aligned_data_size = (FLAC__uint32)((data_size+1) & (~1U)); /* we'll check for overflow later */
-               if(decoder_session->total_samples == 0) {
-                       if(decoder_session->fout == stdout) {
-                               flac__utils_printf(stderr, 1, "%s: WARNING, don't have accurate sample count available for %s header.\n", decoder_session->inbasefilename, fmt_desc);
-                               flac__utils_printf(stderr, 1, "             Generated %s file will have a data chunk size of 0.  Try\n", fmt_desc);
-                               flac__utils_printf(stderr, 1, "             decoding directly to a file instead.\n");
-                       }
-                       else {
-                               decoder_session->wave_chunk_size_fixup.needs_fixup = true;
-                       }
+       const FileFormat format = decoder_session->format;
+       const char *fmt_desc =
+               format==FORMAT_WAVE? "WAVE" :
+               format==FORMAT_WAVE64? "Wave64" :
+               format==FORMAT_RF64? "RF64" :
+               "AIFF";
+       const FLAC__bool is_waveformatextensible =
+               (format == FORMAT_WAVE || format == FORMAT_WAVE64 || format == FORMAT_RF64) &&
+               (
+                       decoder_session->channel_mask == 2 ||
+                       decoder_session->channel_mask > 3 ||
+                       decoder_session->bps%8 ||
+                       decoder_session->channels > 2
+               );
+       const FLAC__uint64 data_size = samples * decoder_session->channels * ((decoder_session->bps+7)/8);
+       const FLAC__uint64 aligned_data_size =
+               format == FORMAT_WAVE64?
+                       (data_size+7) & (~(FLAC__uint64)7) :
+                       (data_size+1) & (~(FLAC__uint64)1);
+
+       FLAC__uint64 iff_size;
+       unsigned foreign_metadata_size = 0; /* size of all non-audio non-fmt/COMM foreign metadata chunks */
+       foreign_metadata_t *fm = decoder_session->foreign_metadata;
+       size_t i;
+
+       FLAC__ASSERT(
+               format == FORMAT_WAVE ||
+               format == FORMAT_WAVE64 ||
+               format == FORMAT_RF64 ||
+               format == FORMAT_AIFF ||
+               format == FORMAT_AIFF_C
+       );
+
+       if(samples == 0) {
+               if(f == stdout) {
+                       flac__utils_printf(stderr, 1, "%s: WARNING, don't have accurate sample count available for %s header.\n", decoder_session->inbasefilename, fmt_desc);
+                       flac__utils_printf(stderr, 1, "             Generated %s file will have a data chunk size of 0.  Try\n", fmt_desc);
+                       flac__utils_printf(stderr, 1, "             decoding directly to a file instead.\n");
+                       if(decoder_session->treat_warnings_as_errors)
+                               return false;
                }
-               if(data_size >= 0xFFFFFFDC) {
-                       flac__utils_printf(stderr, 1, "%s: ERROR: stream is too big to fit in a single %s file chunk\n", decoder_session->inbasefilename, fmt_desc);
-                       return false;
+               else {
+                       decoder_session->iff_headers_need_fixup = true;
                }
-               if(decoder_session->is_wave_out) {
-                       if(flac__utils_fwrite("RIFF", 1, 4, decoder_session->fout) != 4)
-                               return false;
+       }
 
-                       if(decoder_session->wave_chunk_size_fixup.needs_fixup)
-                               decoder_session->wave_chunk_size_fixup.riff_offset = ftell(decoder_session->fout);
+       if(fm) {
+               FLAC__ASSERT(fm->format_block);
+               FLAC__ASSERT(fm->audio_block);
+               FLAC__ASSERT(fm->format_block < fm->audio_block);
+               /* calc foreign metadata size; we always skip the first chunk, ds64 chunk, format chunk, and sound chunk since we write our own */
+               for(i = format==FORMAT_RF64?2:1; i < fm->num_blocks; i++) {
+                       if(i != fm->format_block && i != fm->audio_block)
+                               foreign_metadata_size += fm->blocks[i].size;
+               }
+       }
 
-                       if(!write_little_endian_uint32(decoder_session->fout, aligned_data_size+36)) /* filesize-8 */
-                               return false;
+       if(samples == 0)
+               iff_size = 0;
+       else if(format == FORMAT_WAVE || format == FORMAT_RF64)
+               /* 4 for WAVE form bytes */
+               /* +{36,0} for ds64 chunk */
+               /* +8+{40,16} for fmt chunk header and body */
+               /* +8 for data chunk header */
+               iff_size = 4 + (format==FORMAT_RF64?36:0) + 8+(is_waveformatextensible?40:16) + 8 + foreign_metadata_size + aligned_data_size;
+       else if(format == FORMAT_WAVE64)
+               /* 16+8 for RIFF GUID and size field */
+               /* +16 for WAVE GUID */
+               /* +16+8+{40,16} for fmt chunk header (GUID and size field) and body */
+               /* +16+8 for data chunk header (GUID and size field) */
+               iff_size = 16+8 + 16 + 16+8+(is_waveformatextensible?40:16) + 16+8 + foreign_metadata_size + aligned_data_size;
+       else /* AIFF */
+               iff_size = 46 + foreign_metadata_size + aligned_data_size;
+
+       if(format != FORMAT_WAVE64 && format != FORMAT_RF64 && iff_size >= 0xFFFFFFF4) {
+               flac__utils_printf(stderr, 1, "%s: ERROR: stream is too big to fit in a single %s file\n", decoder_session->inbasefilename, fmt_desc);
+               return false;
+       }
 
-                       if(flac__utils_fwrite("WAVEfmt ", 1, 8, decoder_session->fout) != 8)
+       if(format == FORMAT_WAVE || format == FORMAT_WAVE64 || format == FORMAT_RF64) {
+               /* RIFF header */
+               switch(format) {
+                       case FORMAT_WAVE:
+                               if(flac__utils_fwrite("RIFF", 1, 4, f) != 4)
+                                       return false;
+                               if(!write_little_endian_uint32(f, (FLAC__uint32)iff_size)) /* filesize-8 */
+                                       return false;
+                               if(flac__utils_fwrite("WAVE", 1, 4, f) != 4)
+                                       return false;
+                               break;
+                       case FORMAT_WAVE64:
+                               /* RIFF GUID 66666972-912E-11CF-A5D6-28DB04C10000 */
+                               if(flac__utils_fwrite("\x72\x69\x66\x66\x2E\x91\xCF\x11\xD6\xA5\x28\xDB\x04\xC1\x00\x00", 1, 16, f) != 16)
+                                       return false;
+                               if(!write_little_endian_uint64(f, iff_size))
+                                       return false;
+                               /* WAVE GUID 65766177-ACF3-11D3-8CD1-00C04F8EDB8A */
+                               if(flac__utils_fwrite("\x77\x61\x76\x65\xF3\xAC\xD3\x11\xD1\x8C\x00\xC0\x4F\x8E\xDB\x8A", 1, 16, f) != 16)
+                                       return false;
+                               break;
+                       case FORMAT_RF64:
+                               if(flac__utils_fwrite("RF64", 1, 4, f) != 4)
+                                       return false;
+                               if(!write_little_endian_uint32(f, 0xffffffff))
+                                       return false;
+                               if(flac__utils_fwrite("WAVE", 1, 4, f) != 4)
+                                       return false;
+                               break;
+                       default:
                                return false;
+               }
 
-                       if(flac__utils_fwrite("\020\000\000\000", 1, 4, decoder_session->fout) != 4) /* chunk size = 16 */
+               /* ds64 chunk for RF64 */
+               if(format == FORMAT_RF64) {
+                       if(flac__utils_fwrite("ds64", 1, 4, f) != 4)
                                return false;
 
-                       if(flac__utils_fwrite("\001\000", 1, 2, decoder_session->fout) != 2) /* compression code == 1 */
+                       if(!write_little_endian_uint32(f, 28)) /* chunk size */
                                return false;
 
-                       if(!write_little_endian_uint16(decoder_session->fout, (FLAC__uint16)(decoder_session->channels)))
+                       if(!write_little_endian_uint64(f, iff_size))
                                return false;
 
-                       if(!write_little_endian_uint32(decoder_session->fout, decoder_session->sample_rate))
+                       if(!write_little_endian_uint64(f, data_size))
                                return false;
 
-                       if(!write_little_endian_uint32(decoder_session->fout, decoder_session->sample_rate * decoder_session->channels * ((decoder_session->bps+7) / 8))) /* @@@ or is it (sample_rate*channels*bps) / 8 ??? */
+                       if(!write_little_endian_uint64(f, samples)) /*@@@@@@ correct? */
                                return false;
 
-                       if(!write_little_endian_uint16(decoder_session->fout, (FLAC__uint16)(decoder_session->channels * ((decoder_session->bps+7) / 8)))) /* block align */
+                       if(!write_little_endian_uint32(f, 0)) /* table size */
                                return false;
+               }
 
-                       if(!write_little_endian_uint16(decoder_session->fout, (FLAC__uint16)(decoder_session->bps))) /* bits per sample */
-                               return false;
+               decoder_session->fm_offset1 = ftello(f);
+
+               if(fm) {
+                       /* seek forward to {allocate} or {skip over already-written chunks} before "fmt " */
+                       for(i = format==FORMAT_RF64?2:1; i < fm->format_block; i++) {
+                               if(fseeko(f, fm->blocks[i].size, SEEK_CUR) < 0) {
+                                       flac__utils_printf(stderr, 1, "%s: ERROR: allocating/skipping foreign metadata before \"fmt \"\n", decoder_session->inbasefilename);
+                                       return false;
+                               }
+                       }
+               }
 
-                       if(flac__utils_fwrite("data", 1, 4, decoder_session->fout) != 4)
+               if(format != FORMAT_WAVE64) {
+                       if(flac__utils_fwrite("fmt ", 1, 4, f) != 4)
+                               return false;
+                       if(!write_little_endian_uint32(f, is_waveformatextensible? 40 : 16)) /* chunk size */
                                return false;
+               }
+               else { /* Wave64 */
+                       /* fmt GUID 20746D66-ACF3-11D3-8CD1-00C04F8EDB8A */
+                       if(flac__utils_fwrite("\x66\x6D\x74\x20\xF3\xAC\xD3\x11\xD1\x8C\x00\xC0\x4F\x8E\xDB\x8A", 1, 16, f) != 16)
+                               return false;
+                       /* chunk size (+16+8 for GUID and size fields) */
+                       if(!write_little_endian_uint64(f, 16+8+(is_waveformatextensible?40:16)))
+                               return false;
+               }
+
+               if(!write_riff_wave_fmt_chunk_body(f, is_waveformatextensible, decoder_session->bps, decoder_session->channels, decoder_session->sample_rate, decoder_session->channel_mask))
+                       return false;
 
-                       if(decoder_session->wave_chunk_size_fixup.needs_fixup)
-                               decoder_session->wave_chunk_size_fixup.data_offset = ftell(decoder_session->fout);
+               decoder_session->fm_offset2 = ftello(f);
 
-                       if(!write_little_endian_uint32(decoder_session->fout, (FLAC__uint32)data_size)) /* data size */
+               if(fm) {
+                       /* seek forward to {allocate} or {skip over already-written chunks} after "fmt " but before "data" */
+                       for(i = fm->format_block+1; i < fm->audio_block; i++) {
+                               if(fseeko(f, fm->blocks[i].size, SEEK_CUR) < 0) {
+                                       flac__utils_printf(stderr, 1, "%s: ERROR: allocating/skipping foreign metadata after \"fmt \"\n", decoder_session->inbasefilename);
+                                       return false;
+                               }
+                       }
+               }
+
+               if(format != FORMAT_WAVE64) {
+                       if(flac__utils_fwrite("data", 1, 4, f) != 4)
+                               return false;
+                       if(!write_little_endian_uint32(f, format==FORMAT_RF64? 0xffffffff : (FLAC__uint32)data_size))
                                return false;
                }
-               else {
-                       if(flac__utils_fwrite("FORM", 1, 4, decoder_session->fout) != 4)
+               else { /* Wave64 */
+                       /* data GUID 61746164-ACF3-11D3-8CD1-00C04F8EDB8A */
+                       if(flac__utils_fwrite("\x64\x61\x74\x61\xF3\xAC\xD3\x11\xD1\x8C\x00\xC0\x4F\x8E\xDB\x8A", 1, 16, f) != 16)
+                               return false;
+                       /* +16+8 for GUID and size fields */
+                       if(!write_little_endian_uint64(f, 16+8 + data_size))
                                return false;
+               }
 
-                       if(decoder_session->wave_chunk_size_fixup.needs_fixup)
-                               decoder_session->wave_chunk_size_fixup.riff_offset = ftell(decoder_session->fout);
+               decoder_session->fm_offset3 = ftello(f) + aligned_data_size;
+       }
+       else {
+               if(flac__utils_fwrite("FORM", 1, 4, f) != 4)
+                       return false;
 
-                       if(!write_big_endian_uint32(decoder_session->fout, aligned_data_size+46)) /* filesize-8 */
-                               return false;
+               if(!write_big_endian_uint32(f, (FLAC__uint32)iff_size)) /* filesize-8 */
+                       return false;
 
-                       if(flac__utils_fwrite("AIFFCOMM", 1, 8, decoder_session->fout) != 8)
-                               return false;
+               if(flac__utils_fwrite("AIFF", 1, 4, f) != 4)
+                       return false;
 
-                       if(flac__utils_fwrite("\000\000\000\022", 1, 4, decoder_session->fout) != 4) /* chunk size = 18 */
-                               return false;
+               decoder_session->fm_offset1 = ftello(f);
 
-                       if(!write_big_endian_uint16(decoder_session->fout, (FLAC__uint16)(decoder_session->channels)))
-                               return false;
+               if(fm) {
+                       /* seek forward to {allocate} or {skip over already-written chunks} before "COMM" */
+                       for(i = 1; i < fm->format_block; i++) {
+                               if(fseeko(f, fm->blocks[i].size, SEEK_CUR) < 0) {
+                                       flac__utils_printf(stderr, 1, "%s: ERROR: allocating/skipping foreign metadata before \"COMM\"\n", decoder_session->inbasefilename);
+                                       return false;
+                               }
+                       }
+               }
 
-                       if(decoder_session->wave_chunk_size_fixup.needs_fixup)
-                               decoder_session->wave_chunk_size_fixup.frames_offset = ftell(decoder_session->fout);
+               if(!write_aiff_form_comm_chunk(f, samples, decoder_session->bps, decoder_session->channels, decoder_session->sample_rate))
+                       return false;
 
-                       if(!write_big_endian_uint32(decoder_session->fout, (FLAC__uint32)decoder_session->total_samples))
-                               return false;
+               decoder_session->fm_offset2 = ftello(f);
 
-                       if(!write_big_endian_uint16(decoder_session->fout, (FLAC__uint16)(decoder_session->bps)))
-                               return false;
+               if(fm) {
+                       /* seek forward to {allocate} or {skip over already-written chunks} after "COMM" but before "SSND" */
+                       for(i = fm->format_block+1; i < fm->audio_block; i++) {
+                               if(fseeko(f, fm->blocks[i].size, SEEK_CUR) < 0) {
+                                       flac__utils_printf(stderr, 1, "%s: ERROR: allocating/skipping foreign metadata after \"COMM\"\n", decoder_session->inbasefilename);
+                                       return false;
+                               }
+                       }
+               }
 
-                       if(!write_sane_extended(decoder_session->fout, decoder_session->sample_rate))
-                               return false;
+               if(flac__utils_fwrite("SSND", 1, 4, f) != 4)
+                       return false;
 
-                       if(flac__utils_fwrite("SSND", 1, 4, decoder_session->fout) != 4)
-                               return false;
+               if(!write_big_endian_uint32(f, (FLAC__uint32)data_size + 8)) /* data size */
+                       return false;
 
-                       if(decoder_session->wave_chunk_size_fixup.needs_fixup)
-                               decoder_session->wave_chunk_size_fixup.data_offset = ftell(decoder_session->fout);
+               if(!write_big_endian_uint32(f, 0/*offset_size*/))
+                       return false;
 
-                       if(!write_big_endian_uint32(decoder_session->fout, (FLAC__uint32)data_size+8)) /* data size */
-                               return false;
+               if(!write_big_endian_uint32(f, 0/*block_size*/))
+                       return false;
 
-                       if(!write_big_endian_uint32(decoder_session->fout, 0/*offset*/))
-                               return false;
+               decoder_session->fm_offset3 = ftello(f) + aligned_data_size;
+       }
 
-                       if(!write_big_endian_uint32(decoder_session->fout, 0/*block_size*/))
-                               return false;
-               }
+       return true;
+}
+
+FLAC__bool write_riff_wave_fmt_chunk_body(FILE *f, FLAC__bool is_waveformatextensible, unsigned bps, unsigned channels, unsigned sample_rate, FLAC__uint32 channel_mask)
+{
+       if(!write_little_endian_uint16(f, (FLAC__uint16)(is_waveformatextensible? 65534 : 1))) /* compression code */
+               return false;
+
+       if(!write_little_endian_uint16(f, (FLAC__uint16)channels))
+               return false;
+
+       if(!write_little_endian_uint32(f, sample_rate))
+               return false;
+
+       if(!write_little_endian_uint32(f, sample_rate * channels * ((bps+7) / 8)))
+               return false;
+
+       if(!write_little_endian_uint16(f, (FLAC__uint16)(channels * ((bps+7) / 8)))) /* block align */
+               return false;
+
+       if(!write_little_endian_uint16(f, (FLAC__uint16)(((bps+7)/8)*8))) /* bits per sample */
+               return false;
+
+       if(is_waveformatextensible) {
+               if(!write_little_endian_uint16(f, (FLAC__uint16)22)) /* cbSize */
+                       return false;
+
+               if(!write_little_endian_uint16(f, (FLAC__uint16)bps)) /* validBitsPerSample */
+                       return false;
+
+               if(!write_little_endian_uint32(f, channel_mask))
+                       return false;
+
+               /* GUID = {0x00000001, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}} */
+               if(flac__utils_fwrite("\x01\x00\x00\x00\x00\x00\x10\x00\x80\x00\x00\xaa\x00\x38\x9b\x71", 1, 16, f) != 16)
+                       return false;
        }
 
        return true;
 }
 
+FLAC__bool write_aiff_form_comm_chunk(FILE *f, FLAC__uint64 samples, unsigned bps, unsigned channels, unsigned sample_rate)
+{
+       FLAC__ASSERT(samples <= 0xffffffff);
+
+       if(flac__utils_fwrite("COMM", 1, 4, f) != 4)
+               return false;
+
+       if(!write_big_endian_uint32(f, 18)) /* chunk size = 18 */
+               return false;
+
+       if(!write_big_endian_uint16(f, (FLAC__uint16)channels))
+               return false;
+
+       if(!write_big_endian_uint32(f, (FLAC__uint32)samples))
+               return false;
+
+       if(!write_big_endian_uint16(f, (FLAC__uint16)bps))
+               return false;
+
+       if(!write_sane_extended(f, sample_rate))
+               return false;
+
+       return true;
+}
+
 FLAC__bool write_little_endian_uint16(FILE *f, FLAC__uint16 val)
 {
        FLAC__byte *b = (FLAC__byte*)(&val);
@@ -749,6 +854,19 @@ FLAC__bool write_little_endian_uint32(FILE *f, FLAC__uint32 val)
        return flac__utils_fwrite(b, 1, 4, f) == 4;
 }
 
+FLAC__bool write_little_endian_uint64(FILE *f, FLAC__uint64 val)
+{
+       FLAC__byte *b = (FLAC__byte*)(&val);
+       if(is_big_endian_host_) {
+               FLAC__byte tmp;
+               tmp = b[7]; b[7] = b[0]; b[0] = tmp;
+               tmp = b[6]; b[6] = b[1]; b[1] = tmp;
+               tmp = b[5]; b[5] = b[2]; b[2] = tmp;
+               tmp = b[4]; b[4] = b[3]; b[3] = tmp;
+       }
+       return flac__utils_fwrite(b, 1, 8, f) == 8;
+}
+
 FLAC__bool write_big_endian_uint16(FILE *f, FLAC__uint16 val)
 {
        FLAC__byte *b = (FLAC__byte*)(&val);
@@ -801,66 +919,47 @@ FLAC__bool write_sane_extended(FILE *f, unsigned val)
        return true;
 }
 
-FLAC__bool fixup_wave_chunk_size(const char *outfilename, FLAC__bool is_wave_out, unsigned riff_offset, unsigned data_offset, unsigned frames_offset, FLAC__uint32 total_samples, unsigned channels, unsigned bps)
+FLAC__bool fixup_iff_headers(DecoderSession *d)
 {
-       const char *fmt_desc = (is_wave_out? "WAVE" : "AIFF");
-       FLAC__bool (*write_it)(FILE *, FLAC__uint32) = (is_wave_out? write_little_endian_uint32 : write_big_endian_uint32);
-       FILE *f = fopen(outfilename, "r+b");
-       FLAC__uint32 data_size, aligned_data_size;
+       const char *fmt_desc =
+               d->format==FORMAT_WAVE? "WAVE" :
+               d->format==FORMAT_WAVE64? "Wave64" :
+               d->format==FORMAT_RF64? "RF64" :
+               "AIFF";
+       FILE *f = fopen(d->outfilename, "r+b"); /* stream is positioned at beginning of file */
 
        if(0 == f) {
-               flac__utils_printf(stderr, 1, "ERROR, couldn't open file %s while fixing up %s chunk size\n", outfilename, fmt_desc);
+               flac__utils_printf(stderr, 1, "ERROR, couldn't open file %s while fixing up %s chunk size: %s\n", d->outfilename, fmt_desc, strerror(errno));
                return false;
        }
 
-       data_size = aligned_data_size = total_samples * channels * ((bps+7)/8);
-       if(aligned_data_size & 1)
-               aligned_data_size++;
-
-       if(fseek(f, riff_offset, SEEK_SET) < 0) {
-               flac__utils_printf(stderr, 1, "ERROR, couldn't seek in file %s while fixing up %s chunk size\n", outfilename, fmt_desc);
-               fclose(f);
-               return false;
-       }
-       if(!write_it(f, aligned_data_size + (is_wave_out? 36 : 46))) {
-               flac__utils_printf(stderr, 1, "ERROR, couldn't write size in file %s while fixing up %s chunk size\n", outfilename, fmt_desc);
-               fclose(f);
-               return false;
-       }
-       if(!is_wave_out) {
-               if(fseek(f, frames_offset, SEEK_SET) < 0) {
-                       flac__utils_printf(stderr, 1, "ERROR, couldn't seek in file %s while fixing up %s chunk size\n", outfilename, fmt_desc);
-                       fclose(f);
-                       return false;
-               }
-               if(!write_it(f, total_samples)) {
-                       flac__utils_printf(stderr, 1, "ERROR, couldn't write size in file %s while fixing up %s chunk size\n", outfilename, fmt_desc);
-                       fclose(f);
-                       return false;
-               }
-       }
-       if(fseek(f, data_offset, SEEK_SET) < 0) {
-               flac__utils_printf(stderr, 1, "ERROR, couldn't seek in file %s while fixing up %s chunk size\n", outfilename, fmt_desc);
-               fclose(f);
-               return false;
-       }
-       if(!write_it(f, data_size + (is_wave_out? 0 : 8))) {
-               flac__utils_printf(stderr, 1, "ERROR, couldn't write size in file %s while fixing up %s chunk size\n", outfilename, fmt_desc);
+       if(!write_iff_headers(f, d, d->samples_processed)) {
                fclose(f);
                return false;
        }
+
        fclose(f);
        return true;
 }
 
-FLAC__StreamDecoderWriteStatus write_callback(const void *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
+FLAC__StreamDecoderWriteStatus write_callback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
 {
        DecoderSession *decoder_session = (DecoderSession*)client_data;
        FILE *fout = decoder_session->fout;
        const unsigned bps = frame->header.bits_per_sample, channels = frame->header.channels;
-       FLAC__bool is_big_endian = (decoder_session->is_aiff_out? true : (decoder_session->is_wave_out? false : decoder_session->is_big_endian));
-       FLAC__bool is_unsigned_samples = (decoder_session->is_aiff_out? false : (decoder_session->is_wave_out? bps<=8 : decoder_session->is_unsigned_samples));
+       const unsigned shift = (decoder_session->format != FORMAT_RAW && (bps%8))? 8-(bps%8): 0;
+       FLAC__bool is_big_endian = (
+               decoder_session->format == FORMAT_AIFF || decoder_session->format == FORMAT_AIFF_C ? true : (
+               decoder_session->format == FORMAT_WAVE || decoder_session->format == FORMAT_WAVE64 || decoder_session->format == FORMAT_RF64 ? false :
+               decoder_session->is_big_endian
+       ));
+       FLAC__bool is_unsigned_samples = (
+               decoder_session->format == FORMAT_AIFF || decoder_session->format == FORMAT_AIFF_C ? false : (
+               decoder_session->format == FORMAT_WAVE || decoder_session->format == FORMAT_WAVE64 || decoder_session->format == FORMAT_RF64 ? bps<=8 :
+               decoder_session->is_unsigned_samples
+       ));
        unsigned wide_samples = frame->header.blocksize, wide_sample, sample, channel, byte;
+       unsigned frame_bytes = 0;
        static FLAC__int8 s8buffer[FLAC__MAX_BLOCK_SIZE * FLAC__MAX_CHANNELS * sizeof(FLAC__int32)]; /* WATCHOUT: can be up to 2 megs */
        FLAC__uint8  *u8buffer  = (FLAC__uint8  *)s8buffer;
        FLAC__int16  *s16buffer = (FLAC__int16  *)s8buffer;
@@ -874,25 +973,79 @@ FLAC__StreamDecoderWriteStatus write_callback(const void *decoder, const FLAC__F
        if(decoder_session->abort_flag)
                return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
 
-       if(bps != decoder_session->bps) {
-               flac__utils_printf(stderr, 1, "%s: ERROR, bits-per-sample is %u in frame but %u in STREAMINFO\n", decoder_session->inbasefilename, bps, decoder_session->bps);
-               return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
+       /* sanity-check the bits-per-sample */
+       if(decoder_session->bps) {
+               if(bps != decoder_session->bps) {
+                       if(decoder_session->got_stream_info)
+                               flac__utils_printf(stderr, 1, "%s: ERROR, bits-per-sample is %u in frame but %u in STREAMINFO\n", decoder_session->inbasefilename, bps, decoder_session->bps);
+                       else
+                               flac__utils_printf(stderr, 1, "%s: ERROR, bits-per-sample is %u in this frame but %u in previous frames\n", decoder_session->inbasefilename, bps, decoder_session->bps);
+                       if(!decoder_session->continue_through_decode_errors)
+                               return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
+               }
        }
-       if(channels != decoder_session->channels) {
-               flac__utils_printf(stderr, 1, "%s: ERROR, channels is %u in frame but %u in STREAMINFO\n", decoder_session->inbasefilename, channels, decoder_session->channels);
-               return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
+       else {
+               /* must not have gotten STREAMINFO, save the bps from the frame header */
+               FLAC__ASSERT(!decoder_session->got_stream_info);
+               decoder_session->bps = bps;
        }
-       if(frame->header.sample_rate != decoder_session->sample_rate) {
-               flac__utils_printf(stderr, 1, "%s: ERROR, sample rate is %u in frame but %u in STREAMINFO\n", decoder_session->inbasefilename, frame->header.sample_rate, decoder_session->sample_rate);
-               return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
+
+       /* sanity-check the #channels */
+       if(decoder_session->channels) {
+               if(channels != decoder_session->channels) {
+                       if(decoder_session->got_stream_info)
+                               flac__utils_printf(stderr, 1, "%s: ERROR, channels is %u in frame but %u in STREAMINFO\n", decoder_session->inbasefilename, channels, decoder_session->channels);
+                       else
+                               flac__utils_printf(stderr, 1, "%s: ERROR, channels is %u in this frame but %u in previous frames\n", decoder_session->inbasefilename, channels, decoder_session->channels);
+                       if(!decoder_session->continue_through_decode_errors)
+                               return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
+               }
+       }
+       else {
+               /* must not have gotten STREAMINFO, save the #channels from the frame header */
+               FLAC__ASSERT(!decoder_session->got_stream_info);
+               decoder_session->channels = channels;
+       }
+
+       /* sanity-check the sample rate */
+       if(decoder_session->sample_rate) {
+               if(frame->header.sample_rate != decoder_session->sample_rate) {
+                       if(decoder_session->got_stream_info)
+                               flac__utils_printf(stderr, 1, "%s: ERROR, sample rate is %u in frame but %u in STREAMINFO\n", decoder_session->inbasefilename, frame->header.sample_rate, decoder_session->sample_rate);
+                       else
+                               flac__utils_printf(stderr, 1, "%s: ERROR, sample rate is %u in this frame but %u in previous frames\n", decoder_session->inbasefilename, frame->header.sample_rate, decoder_session->sample_rate);
+                       if(!decoder_session->continue_through_decode_errors)
+                               return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
+               }
+       }
+       else {
+               /* must not have gotten STREAMINFO, save the sample rate from the frame header */
+               FLAC__ASSERT(!decoder_session->got_stream_info);
+               decoder_session->sample_rate = frame->header.sample_rate;
        }
 
        /*
         * limit the number of samples to accept based on --until
         */
        FLAC__ASSERT(!decoder_session->skip_specification->is_relative);
+       /* if we never got the total_samples from the metadata, the skip and until specs would never have been canonicalized, so protect against that: */
+       if(decoder_session->skip_specification->is_relative) {
+               if(decoder_session->skip_specification->value.samples == 0) /* special case for when no --skip was given */
+                       decoder_session->skip_specification->is_relative = false; /* convert to our meaning of beginning-of-stream */
+               else {
+                       flac__utils_printf(stderr, 1, "%s: ERROR, cannot use --skip because the total sample count was not found in the metadata\n", decoder_session->inbasefilename);
+                       return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
+               }
+       }
+       if(decoder_session->until_specification->is_relative) {
+               if(decoder_session->until_specification->value.samples == 0) /* special case for when no --until was given */
+                       decoder_session->until_specification->is_relative = false; /* convert to our meaning of end-of-stream */
+               else {
+                       flac__utils_printf(stderr, 1, "%s: ERROR, cannot use --until because the total sample count was not found in the metadata\n", decoder_session->inbasefilename);
+                       return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
+               }
+       }
        FLAC__ASSERT(decoder_session->skip_specification->value.samples >= 0);
-       FLAC__ASSERT(!decoder_session->until_specification->is_relative);
        FLAC__ASSERT(decoder_session->until_specification->value.samples >= 0);
        if(decoder_session->until_specification->value.samples > 0) {
                const FLAC__uint64 skip = (FLAC__uint64)decoder_session->skip_specification->value.samples;
@@ -908,6 +1061,13 @@ FLAC__StreamDecoderWriteStatus write_callback(const void *decoder, const FLAC__F
                }
        }
 
+       if(decoder_session->analysis_mode) {
+               FLAC__uint64 dpos;
+               FLAC__stream_decoder_get_decode_position(decoder_session->decoder, &dpos);
+               frame_bytes = (unsigned)(dpos-decoder_session->decode_position);
+               decoder_session->decode_position = dpos;
+       }
+
        if(wide_samples > 0) {
                decoder_session->samples_processed += wide_samples;
                decoder_session->frame_counter++;
@@ -916,10 +1076,15 @@ FLAC__StreamDecoderWriteStatus write_callback(const void *decoder, const FLAC__F
                        print_stats(decoder_session);
 
                if(decoder_session->analysis_mode) {
-                       flac__analyze_frame(frame, decoder_session->frame_counter-1, decoder_session->aopts, fout);
+                       flac__analyze_frame(frame, decoder_session->frame_counter-1, decoder_session->decode_position-frame_bytes, frame_bytes, decoder_session->aopts, fout);
                }
                else if(!decoder_session->test_only) {
-                       if (decoder_session->replaygain.apply) {
+                       if(shift && !decoder_session->replaygain.apply) {
+                               for(wide_sample = 0; wide_sample < wide_samples; wide_sample++)
+                                       for(channel = 0; channel < channels; channel++)
+                                               ((FLAC__int32**)buffer)[channel][wide_sample] <<= shift;/*@@@@@@un-const'ing the buffer is hacky but safe*/
+                       }
+                       if(decoder_session->replaygain.apply) {
                                bytes_to_write = FLAC__replaygain_synthesis__apply_gain(
                                        u8buffer,
                                        !is_big_endian,
@@ -928,36 +1093,65 @@ FLAC__StreamDecoderWriteStatus write_callback(const void *decoder, const FLAC__F
                                        wide_samples,
                                        channels,
                                        bps, /* source_bps */
-                                       bps, /* target_bps */
+                                       bps+shift, /* target_bps */
                                        decoder_session->replaygain.scale,
                                        decoder_session->replaygain.spec.limiter == RGSS_LIMIT__HARD, /* hard_limit */
                                        decoder_session->replaygain.spec.noise_shaping != NOISE_SHAPING_NONE, /* do_dithering */
                                        &decoder_session->replaygain.dither_context
                                );
                        }
-                       else if(bps == 8) {
-                               if(is_unsigned_samples) {
-                                       for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
-                                               for(channel = 0; channel < channels; channel++, sample++)
-                                                       u8buffer[sample] = (FLAC__uint8)(buffer[channel][wide_sample] + 0x80);
-                               }
-                               else {
-                                       for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
-                                               for(channel = 0; channel < channels; channel++, sample++)
-                                                       s8buffer[sample] = (FLAC__int8)(buffer[channel][wide_sample]);
-                               }
-                               bytes_to_write = sample;
+                       /* first some special code for common cases */
+                       else if(is_big_endian == is_big_endian_host_ && !is_unsigned_samples && channels == 2 && bps+shift == 16) {
+                               FLAC__int16 *buf1_ = s16buffer + 1;
+                               if(is_big_endian)
+                                       memcpy(s16buffer, ((FLAC__byte*)(buffer[0]))+2, sizeof(FLAC__int32) * wide_samples - 2);
+                               else
+                                       memcpy(s16buffer, buffer[0], sizeof(FLAC__int32) * wide_samples);
+                               for(sample = 0; sample < wide_samples; sample++, buf1_+=2)
+                                       *buf1_ = (FLAC__int16)buffer[1][sample];
+                               bytes_to_write = 4 * sample;
+                       }
+                       else if(is_big_endian == is_big_endian_host_ && !is_unsigned_samples && channels == 1 && bps+shift == 16) {
+                               FLAC__int16 *buf1_ = s16buffer;
+                               for(sample = 0; sample < wide_samples; sample++)
+                                       *buf1_++ = (FLAC__int16)buffer[0][sample];
+                               bytes_to_write = 2 * sample;
                        }
-                       else if(bps == 16) {
+                       /* generic code for the rest */
+                       else if(bps+shift == 16) {
                                if(is_unsigned_samples) {
-                                       for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
-                                               for(channel = 0; channel < channels; channel++, sample++)
-                                                       u16buffer[sample] = (FLAC__uint16)(buffer[channel][wide_sample] + 0x8000);
+                                       if(channels == 2) {
+                                               for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++) {
+                                                       u16buffer[sample++] = (FLAC__uint16)(buffer[0][wide_sample] + 0x8000);
+                                                       u16buffer[sample++] = (FLAC__uint16)(buffer[1][wide_sample] + 0x8000);
+                                               }
+                                       }
+                                       else if(channels == 1) {
+                                               for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
+                                                       u16buffer[sample++] = (FLAC__uint16)(buffer[0][wide_sample] + 0x8000);
+                                       }
+                                       else { /* works for any 'channels' but above flavors are faster for 1 and 2 */
+                                               for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
+                                                       for(channel = 0; channel < channels; channel++, sample++)
+                                                               u16buffer[sample] = (FLAC__uint16)(buffer[channel][wide_sample] + 0x8000);
+                                       }
                                }
                                else {
-                                       for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
-                                               for(channel = 0; channel < channels; channel++, sample++)
-                                                       s16buffer[sample] = (FLAC__int16)(buffer[channel][wide_sample]);
+                                       if(channels == 2) {
+                                               for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++) {
+                                                       s16buffer[sample++] = (FLAC__int16)(buffer[0][wide_sample]);
+                                                       s16buffer[sample++] = (FLAC__int16)(buffer[1][wide_sample]);
+                                               }
+                                       }
+                                       else if(channels == 1) {
+                                               for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
+                                                       s16buffer[sample++] = (FLAC__int16)(buffer[0][wide_sample]);
+                                       }
+                                       else { /* works for any 'channels' but above flavors are faster for 1 and 2 */
+                                               for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
+                                                       for(channel = 0; channel < channels; channel++, sample++)
+                                                               s16buffer[sample] = (FLAC__int16)(buffer[channel][wide_sample]);
+                                       }
                                }
                                if(is_big_endian != is_big_endian_host_) {
                                        unsigned char tmp;
@@ -970,7 +1164,7 @@ FLAC__StreamDecoderWriteStatus write_callback(const void *decoder, const FLAC__F
                                }
                                bytes_to_write = 2 * sample;
                        }
-                       else if(bps == 24) {
+                       else if(bps+shift == 24) {
                                if(is_unsigned_samples) {
                                        for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
                                                for(channel = 0; channel < channels; channel++, sample++)
@@ -1015,8 +1209,24 @@ FLAC__StreamDecoderWriteStatus write_callback(const void *decoder, const FLAC__F
                                }
                                bytes_to_write = 3 * sample;
                        }
+                       else if(bps+shift == 8) {
+                               if(is_unsigned_samples) {
+                                       for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
+                                               for(channel = 0; channel < channels; channel++, sample++)
+                                                       u8buffer[sample] = (FLAC__uint8)(buffer[channel][wide_sample] + 0x80);
+                               }
+                               else {
+                                       for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
+                                               for(channel = 0; channel < channels; channel++, sample++)
+                                                       s8buffer[sample] = (FLAC__int8)(buffer[channel][wide_sample]);
+                               }
+                               bytes_to_write = sample;
+                       }
                        else {
                                FLAC__ASSERT(0);
+                               /* double protection */
+                               decoder_session->abort_flag = true;
+                               return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
                        }
                }
        }
@@ -1032,12 +1242,17 @@ FLAC__StreamDecoderWriteStatus write_callback(const void *decoder, const FLAC__F
        return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
 }
 
-void metadata_callback(const void *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
+void metadata_callback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
 {
        DecoderSession *decoder_session = (DecoderSession*)client_data;
-       (void)decoder;
+
+       if(decoder_session->analysis_mode)
+               FLAC__stream_decoder_get_decode_position(decoder, &decoder_session->decode_position);
+
        if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
                FLAC__uint64 skip, until;
+               decoder_session->got_stream_info = true;
+               decoder_session->has_md5sum = memcmp(metadata->data.stream_info.md5sum, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16);
                decoder_session->bps = metadata->data.stream_info.bits_per_sample;
                decoder_session->channels = metadata->data.stream_info.channels;
                decoder_session->sample_rate = metadata->data.stream_info.sample_rate;
@@ -1074,8 +1289,8 @@ void metadata_callback(const void *decoder, const FLAC__StreamMetadata *metadata
                        decoder_session->total_samples -= (metadata->data.stream_info.total_samples - until);
                }
 
-               if(decoder_session->bps != 8 && decoder_session->bps != 16 && decoder_session->bps != 24) {
-                       flac__utils_printf(stderr, 1, "%s: ERROR: bits per sample is not 8/16/24\n", decoder_session->inbasefilename);
+               if(decoder_session->bps < 4 || decoder_session->bps > 24) {
+                       flac__utils_printf(stderr, 1, "%s: ERROR: bits per sample is %u, must be 4-24\n", decoder_session->inbasefilename, decoder_session->bps);
                        decoder_session->abort_flag = true;
                        return;
                }
@@ -1105,9 +1320,13 @@ void metadata_callback(const void *decoder, const FLAC__StreamMetadata *metadata
        }
        else if(metadata->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
                if (decoder_session->replaygain.spec.apply) {
-                       double gain, peak;
-                       if (!(decoder_session->replaygain.apply = grabbag__replaygain_load_from_vorbiscomment(metadata, decoder_session->replaygain.spec.use_album_gain, &gain, &peak))) {
-                               flac__utils_printf(stderr, 1, "%s: WARNING: can't get %s ReplayGain tag\n", decoder_session->inbasefilename, decoder_session->replaygain.spec.use_album_gain? "album":"track");
+                       double reference, gain, peak;
+                       if (!(decoder_session->replaygain.apply = grabbag__replaygain_load_from_vorbiscomment(metadata, decoder_session->replaygain.spec.use_album_gain, /*strict=*/false, &reference, &gain, &peak))) {
+                               flac__utils_printf(stderr, 1, "%s: WARNING: can't get %s (or even %s) ReplayGain tags\n", decoder_session->inbasefilename, decoder_session->replaygain.spec.use_album_gain? "album":"track", decoder_session->replaygain.spec.use_album_gain? "track":"album");
+                               if(decoder_session->treat_warnings_as_errors) {
+                                       decoder_session->abort_flag = true;
+                                       return;
+                               }
                        }
                        else {
                                const char *ls[] = { "no", "peak", "hard" };
@@ -1117,70 +1336,85 @@ void metadata_callback(const void *decoder, const FLAC__StreamMetadata *metadata
                                FLAC__replaygain_synthesis__init_dither_context(&decoder_session->replaygain.dither_context, decoder_session->bps, decoder_session->replaygain.spec.noise_shaping);
                                flac__utils_printf(stderr, 1, "%s: INFO: applying %s ReplayGain (gain=%0.2fdB+preamp=%0.1fdB, %s noise shaping, %s limiting) to output\n", decoder_session->inbasefilename, decoder_session->replaygain.spec.use_album_gain? "album":"track", gain, decoder_session->replaygain.spec.preamp, ns[decoder_session->replaygain.spec.noise_shaping], ls[decoder_session->replaygain.spec.limiter]);
                                flac__utils_printf(stderr, 1, "%s: WARNING: applying ReplayGain is not lossless\n", decoder_session->inbasefilename);
+                               /* don't check if(decoder_session->treat_warnings_as_errors) because the user explicitly asked for it */
                        }
                }
+               (void)flac__utils_get_channel_mask_tag(metadata, &decoder_session->channel_mask);
        }
 }
 
-void error_callback(const void *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
+void error_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
 {
        DecoderSession *decoder_session = (DecoderSession*)client_data;
        (void)decoder;
-       flac__utils_printf(stderr, 1, "%s: *** Got error code %d:%s\n", decoder_session->inbasefilename, status, FLAC__StreamDecoderErrorStatusString[status]);
-       if(!decoder_session->continue_through_decode_errors)
+       if(!decoder_session->error_callback_suppress_messages)
+               flac__utils_printf(stderr, 1, "%s: *** Got error code %d:%s\n", decoder_session->inbasefilename, status, FLAC__StreamDecoderErrorStatusString[status]);
+       if(!decoder_session->continue_through_decode_errors) {
+               /* if we got a sync error while looking for metadata, either it's not a FLAC file (more likely) or the file is corrupted */
+               if(
+                       !decoder_session->error_callback_suppress_messages &&
+                       status == FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC &&
+                       FLAC__stream_decoder_get_state(decoder) == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA
+               ) {
+                       flac__utils_printf(stderr, 1,
+                               "\n"
+                               "The input file is either not a FLAC file or is corrupted.  If you are\n"
+                               "convinced it is a FLAC file, you can rerun the same command and add the\n"
+                               "-F parameter to try and recover as much as possible from the file.\n"
+                       );
+                       decoder_session->error_callback_suppress_messages = true;
+               }
+               else if(status == FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM)
+                       decoder_session->aborting_due_to_unparseable = true;
                decoder_session->abort_flag = true;
+       }
 }
 
-void print_error_with_state(const DecoderSession *d, const char *message)
+void print_error_with_init_status(const DecoderSession *d, const char *message, FLAC__StreamDecoderInitStatus init_status)
 {
        const int ilen = strlen(d->inbasefilename) + 1;
-       const char *state_string;
 
        flac__utils_printf(stderr, 1, "\n%s: %s\n", d->inbasefilename, message);
 
-#ifdef FLAC__HAS_OGG
-       if(d->is_ogg) {
-               state_string = OggFLAC__file_decoder_get_resolved_state_string(d->decoder.ogg.file);
-       }
-       else
-#endif
-       {
-               state_string = FLAC__file_decoder_get_resolved_state_string(d->decoder.flac.file);
+       flac__utils_printf(stderr, 1, "%*s init status = %s\n", ilen, "", FLAC__StreamDecoderInitStatusString[init_status]);
+
+       /* print out some more info for some errors: */
+       if (init_status == FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE) {
+               flac__utils_printf(stderr, 1,
+                       "\n"
+                       "An error occurred opening the input file; it is likely that it does not exist\n"
+                       "or is not readable.\n"
+               );
        }
+}
+
+void print_error_with_state(const DecoderSession *d, const char *message)
+{
+       const int ilen = strlen(d->inbasefilename) + 1;
 
-       flac__utils_printf(stderr, 1, "%*s state = %s\n", ilen, "", state_string);
+       flac__utils_printf(stderr, 1, "\n%s: %s\n", d->inbasefilename, message);
+       flac__utils_printf(stderr, 1, "%*s state = %s\n", ilen, "", FLAC__stream_decoder_get_resolved_state_string(d->decoder));
 
        /* print out some more info for some errors: */
-       if (0 == strcmp(state_string, FLAC__StreamDecoderStateString[FLAC__STREAM_DECODER_UNPARSEABLE_STREAM])) {
+       if (d->aborting_due_to_unparseable) {
                flac__utils_printf(stderr, 1,
                        "\n"
                        "The FLAC stream may have been created by a more advanced encoder.  Try\n"
                        "  metaflac --show-vendor-tag %s\n"
                        "If the version number is greater than %s, this decoder is probably\n"
-                       "not able to decode the file.  If the version number is not, you may\n"
-                       "have found a bug.  In this case please submit a bug report to\n"
+                       "not able to decode the file.  If the version number is not, the file\n"
+                       "may be corrupted, or you may have found a bug.  In this case please\n"
+                       "submit a bug report to\n"
                        "    http://sourceforge.net/bugs/?func=addbug&group_id=13478\n"
-                       "Make sure to include an email contact in the comment and/or use the\n"
-                       "\"Monitor\" feature to monitor the bug status.\n",
+                       "Make sure to use the \"Monitor\" feature to monitor the bug status.\n",
                        d->inbasefilename, FLAC__VERSION_STRING
                );
        }
-       else if (
-               0 == strcmp(state_string, FLAC__FileDecoderStateString[FLAC__FILE_DECODER_ERROR_OPENING_FILE])
-#ifdef FLAC__HAS_OGG
-               || 0 == strcmp(state_string, OggFLAC__FileDecoderStateString[OggFLAC__FILE_DECODER_ERROR_OPENING_FILE])
-#endif
-       ) {
-               flac__utils_printf(stderr, 1,
-                       "\n"
-                       "An error occurred opening the input file; it is likely that it does not exist\n"
-                       "or is not readable.\n"
-               );
-       }
 }
 
 void print_stats(const DecoderSession *decoder_session)
 {
+       static int count = 0;
        if(flac__utils_verbosity_ >= 2) {
 #if defined _MSC_VER || defined __MINGW32__
                /* with MSVC you have to spoon feed it the casting */
@@ -1189,7 +1423,13 @@ void print_stats(const DecoderSession *decoder_session)
                const double progress = (double)decoder_session->samples_processed / (double)decoder_session->total_samples * 100.0;
 #endif
                if(decoder_session->total_samples > 0) {
-                       fprintf(stderr, "\r%s: %s%u%% complete",
+                       while (count > 0 && count--)
+                               fprintf(stderr, "\b");
+
+                       if ((unsigned)floor(progress + 0.5) == 100)
+                               return;
+
+                       count = fprintf(stderr, "%s: %s%u%% complete",
                                decoder_session->inbasefilename,
                                decoder_session->test_only? "testing, " : decoder_session->analysis_mode? "analyzing, " : "",
                                (unsigned)floor(progress + 0.5)