be more precise in long long int literal specification for ANSI compilers (gcc3 requi...
[flac.git] / src / libFLAC / file_encoder.c
index 3e1e050..e1ab735 100644 (file)
@@ -1,28 +1,44 @@
 /* libFLAC - Free Lossless Audio Codec library
- * Copyright (C) 2002  Josh Coalson
+ * Copyright (C) 2002,2003,2004  Josh Coalson
  *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library General Public
- * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
  *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Library General Public License for more details.
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
  *
- * You should have received a copy of the GNU Library General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA  02111-1307, USA.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * - Neither the name of the Xiph.org Foundation nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
 #include <stdio.h>
 #include <stdlib.h> /* for malloc() */
-#include <string.h> /* for memcpy() */
+#include <string.h> /* for strlen(), strcpy() */
 #include "FLAC/assert.h"
 #include "protected/file_encoder.h"
-#include "protected/seekable_stream_encoder.h"
+
+#ifdef max
+#undef max
+#endif
+#define max(x,y) ((x)>(y)?(x):(y))
 
 /***********************************************************************
  *
  *
  ***********************************************************************/
 
+/* unpublished debug routines */
+extern FLAC__bool FLAC__seekable_stream_encoder_disable_constant_subframes(FLAC__SeekableStreamEncoder *encoder, FLAC__bool value);
+extern FLAC__bool FLAC__seekable_stream_encoder_disable_fixed_subframes(FLAC__SeekableStreamEncoder *encoder, FLAC__bool value);
+extern FLAC__bool FLAC__seekable_stream_encoder_disable_verbatim_subframes(FLAC__SeekableStreamEncoder *encoder, FLAC__bool value);
+
 static void set_defaults_(FLAC__FileEncoder *encoder);
-static FLAC__StreamEncoderWriteStatus file_encoder_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
-static void file_encoder_metadata_callback_(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetadata *metadata, void *client_data);
+static FLAC__SeekableStreamEncoderSeekStatus seek_callback_(const FLAC__SeekableStreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data);
+static FLAC__SeekableStreamEncoderTellStatus tell_callback_(const FLAC__SeekableStreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
+static FLAC__StreamEncoderWriteStatus write_callback_(const FLAC__SeekableStreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
 
 /***********************************************************************
  *
@@ -41,9 +63,15 @@ static void file_encoder_metadata_callback_(const FLAC__StreamEncoder *encoder,
  ***********************************************************************/
 
 typedef struct FLAC__FileEncoderPrivate {
-       FILE *file;
+       FLAC__FileEncoderProgressCallback progress_callback;
+       void *client_data;
        char *filename;
+       FLAC__uint64 bytes_written;
+       FLAC__uint64 samples_written;
+       unsigned frames_written;
+       unsigned total_frames_estimate;
        FLAC__SeekableStreamEncoder *seekable_stream_encoder;
+       FILE *file;
 } FLAC__FileEncoderPrivate;
 
 /***********************************************************************
@@ -52,7 +80,7 @@ typedef struct FLAC__FileEncoderPrivate {
  *
  ***********************************************************************/
 
-const char * const FLAC__FileEncoderStateString[] = {
+FLAC_API const char * const FLAC__FileEncoderStateString[] = {
        "FLAC__FILE_ENCODER_OK",
        "FLAC__FILE_ENCODER_NO_FILENAME",
        "FLAC__FILE_ENCODER_SEEKABLE_STREAM_ENCODER_ERROR",
@@ -69,22 +97,25 @@ const char * const FLAC__FileEncoderStateString[] = {
  * Class constructor/destructor
  *
  ***********************************************************************/
-FLAC__FileEncoder *FLAC__file_encoder_new()
+
+FLAC_API FLAC__FileEncoder *FLAC__file_encoder_new()
 {
        FLAC__FileEncoder *encoder;
 
        FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
 
-       encoder = (FLAC__FileEncoder*)malloc(sizeof(FLAC__FileEncoder));
+       encoder = (FLAC__FileEncoder*)calloc(1, sizeof(FLAC__FileEncoder));
        if(encoder == 0) {
                return 0;
        }
-       encoder->protected_ = (FLAC__FileEncoderProtected*)malloc(sizeof(FLAC__FileEncoderProtected));
+
+       encoder->protected_ = (FLAC__FileEncoderProtected*)calloc(1, sizeof(FLAC__FileEncoderProtected));
        if(encoder->protected_ == 0) {
                free(encoder);
                return 0;
        }
-       encoder->private_ = (FLAC__FileEncoderPrivate*)malloc(sizeof(FLAC__FileEncoderPrivate));
+
+       encoder->private_ = (FLAC__FileEncoderPrivate*)calloc(1, sizeof(FLAC__FileEncoderPrivate));
        if(encoder->private_ == 0) {
                free(encoder->protected_);
                free(encoder);
@@ -92,7 +123,6 @@ FLAC__FileEncoder *FLAC__file_encoder_new()
        }
 
        encoder->private_->seekable_stream_encoder = FLAC__seekable_stream_encoder_new();
-
        if(0 == encoder->private_->seekable_stream_encoder) {
                free(encoder->private_);
                free(encoder->protected_);
@@ -109,7 +139,7 @@ FLAC__FileEncoder *FLAC__file_encoder_new()
        return encoder;
 }
 
-void FLAC__file_encoder_delete(FLAC__FileEncoder *encoder)
+FLAC_API void FLAC__file_encoder_delete(FLAC__FileEncoder *encoder)
 {
        FLAC__ASSERT(0 != encoder);
        FLAC__ASSERT(0 != encoder->protected_);
@@ -131,7 +161,7 @@ void FLAC__file_encoder_delete(FLAC__FileEncoder *encoder)
  *
  ***********************************************************************/
 
-FLAC__FileEncoderState FLAC__file_encoder_init(FLAC__FileEncoder *encoder)
+FLAC_API FLAC__FileEncoderState FLAC__file_encoder_init(FLAC__FileEncoder *encoder)
 {
        FLAC__ASSERT(0 != encoder);
 
@@ -141,22 +171,34 @@ FLAC__FileEncoderState FLAC__file_encoder_init(FLAC__FileEncoder *encoder)
        if(0 == encoder->private_->filename)
                return encoder->protected_->state = FLAC__FILE_ENCODER_NO_FILENAME;
 
-       encoder->private_->file = fopen(encoder->private_->filename, "wb");
+       encoder->private_->file = fopen(encoder->private_->filename, "w+b");
 
        if(encoder->private_->file == 0)
                return encoder->protected_->state = FLAC__FILE_ENCODER_ERROR_OPENING_FILE;
 
+       encoder->private_->bytes_written = 0;
+       encoder->private_->samples_written = 0;
+       encoder->private_->frames_written = 0;
+
+       FLAC__seekable_stream_encoder_set_seek_callback(encoder->private_->seekable_stream_encoder, seek_callback_);
+       FLAC__seekable_stream_encoder_set_tell_callback(encoder->private_->seekable_stream_encoder, tell_callback_);
        FLAC__seekable_stream_encoder_set_write_callback(encoder->private_->seekable_stream_encoder, write_callback_);
-       FLAC__seekable_stream_encoder_set_metadata_callback(encoder->private_->seekable_stream_encoder, metadata_callback_);
        FLAC__seekable_stream_encoder_set_client_data(encoder->private_->seekable_stream_encoder, encoder);
 
-       if(FLAC__seekable_seekable_stream_encoder_init(encoder->private_->seekable_stream_encoder) != FLAC__SEEKABLE_STREAM_ENCODER_OK)
+       if(FLAC__seekable_stream_encoder_init(encoder->private_->seekable_stream_encoder) != FLAC__SEEKABLE_STREAM_ENCODER_OK)
                return encoder->protected_->state = FLAC__FILE_ENCODER_SEEKABLE_STREAM_ENCODER_ERROR;
 
-       return decoder->protected_->state = FLAC__FILE_ENCODER_OK;
+       {
+               unsigned blocksize = FLAC__file_encoder_get_blocksize(encoder);
+
+               FLAC__ASSERT(blocksize != 0);
+               encoder->private_->total_frames_estimate = (unsigned)((FLAC__file_encoder_get_total_samples_estimate(encoder) + blocksize - 1) / blocksize);
+       }
+
+       return encoder->protected_->state = FLAC__FILE_ENCODER_OK;
 }
 
-void FLAC__file_encoder_finish(FLAC__FileEncoder *encoder)
+FLAC_API void FLAC__file_encoder_finish(FLAC__FileEncoder *encoder)
 {
        FLAC__ASSERT(0 != encoder);
 
@@ -165,6 +207,10 @@ void FLAC__file_encoder_finish(FLAC__FileEncoder *encoder)
 
        FLAC__ASSERT(0 != encoder->private_->seekable_stream_encoder);
 
+       /* FLAC__seekable_stream_encoder_finish() might write data so we must close the file after it. */
+
+       FLAC__seekable_stream_encoder_finish(encoder->private_->seekable_stream_encoder);
+
        if(0 != encoder->private_->file) {
                fclose(encoder->private_->file);
                encoder->private_->file = 0;
@@ -178,11 +224,20 @@ void FLAC__file_encoder_finish(FLAC__FileEncoder *encoder)
        set_defaults_(encoder);
 
        encoder->protected_->state = FLAC__FILE_ENCODER_UNINITIALIZED;
+}
 
-       return FLAC__seekable_stream_encoder_finish(encoder->private_->seekable_stream_encoder);
+FLAC_API FLAC__bool FLAC__file_encoder_set_verify(FLAC__FileEncoder *encoder, FLAC__bool value)
+{
+       FLAC__ASSERT(0 != encoder);
+       FLAC__ASSERT(0 != encoder->private_);
+       FLAC__ASSERT(0 != encoder->protected_);
+       FLAC__ASSERT(0 != encoder->private_->seekable_stream_encoder);
+       if(encoder->protected_->state != FLAC__FILE_ENCODER_UNINITIALIZED)
+               return false;
+       return FLAC__seekable_stream_encoder_set_verify(encoder->private_->seekable_stream_encoder, value);
 }
 
-FLAC__bool FLAC__file_encoder_set_streamable_subset(FLAC__FileEncoder *encoder, FLAC__bool value)
+FLAC_API FLAC__bool FLAC__file_encoder_set_streamable_subset(FLAC__FileEncoder *encoder, FLAC__bool value)
 {
        FLAC__ASSERT(0 != encoder);
        FLAC__ASSERT(0 != encoder->private_);
@@ -193,7 +248,7 @@ FLAC__bool FLAC__file_encoder_set_streamable_subset(FLAC__FileEncoder *encoder,
        return FLAC__seekable_stream_encoder_set_streamable_subset(encoder->private_->seekable_stream_encoder, value);
 }
 
-FLAC__bool FLAC__file_encoder_set_do_mid_side_stereo(FLAC__FileEncoder *encoder, FLAC__bool value)
+FLAC_API FLAC__bool FLAC__file_encoder_set_do_mid_side_stereo(FLAC__FileEncoder *encoder, FLAC__bool value)
 {
        FLAC__ASSERT(0 != encoder);
        FLAC__ASSERT(0 != encoder->private_);
@@ -204,7 +259,7 @@ FLAC__bool FLAC__file_encoder_set_do_mid_side_stereo(FLAC__FileEncoder *encoder,
        return FLAC__seekable_stream_encoder_set_do_mid_side_stereo(encoder->private_->seekable_stream_encoder, value);
 }
 
-FLAC__bool FLAC__file_encoder_set_loose_mid_side_stereo(FLAC__FileEncoder *encoder, FLAC__bool value)
+FLAC_API FLAC__bool FLAC__file_encoder_set_loose_mid_side_stereo(FLAC__FileEncoder *encoder, FLAC__bool value)
 {
        FLAC__ASSERT(0 != encoder);
        FLAC__ASSERT(0 != encoder->private_);
@@ -215,7 +270,7 @@ FLAC__bool FLAC__file_encoder_set_loose_mid_side_stereo(FLAC__FileEncoder *encod
        return FLAC__seekable_stream_encoder_set_loose_mid_side_stereo(encoder->private_->seekable_stream_encoder, value);
 }
 
-FLAC__bool FLAC__file_encoder_set_channels(FLAC__FileEncoder *encoder, unsigned value)
+FLAC_API FLAC__bool FLAC__file_encoder_set_channels(FLAC__FileEncoder *encoder, unsigned value)
 {
        FLAC__ASSERT(0 != encoder);
        FLAC__ASSERT(0 != encoder->private_);
@@ -226,7 +281,7 @@ FLAC__bool FLAC__file_encoder_set_channels(FLAC__FileEncoder *encoder, unsigned
        return FLAC__seekable_stream_encoder_set_channels(encoder->private_->seekable_stream_encoder, value);
 }
 
-FLAC__bool FLAC__file_encoder_set_bits_per_sample(FLAC__FileEncoder *encoder, unsigned value)
+FLAC_API FLAC__bool FLAC__file_encoder_set_bits_per_sample(FLAC__FileEncoder *encoder, unsigned value)
 {
        FLAC__ASSERT(0 != encoder);
        FLAC__ASSERT(0 != encoder->private_);
@@ -237,7 +292,7 @@ FLAC__bool FLAC__file_encoder_set_bits_per_sample(FLAC__FileEncoder *encoder, un
        return FLAC__seekable_stream_encoder_set_bits_per_sample(encoder->private_->seekable_stream_encoder, value);
 }
 
-FLAC__bool FLAC__file_encoder_set_sample_rate(FLAC__FileEncoder *encoder, unsigned value)
+FLAC_API FLAC__bool FLAC__file_encoder_set_sample_rate(FLAC__FileEncoder *encoder, unsigned value)
 {
        FLAC__ASSERT(0 != encoder);
        FLAC__ASSERT(0 != encoder->private_);
@@ -248,7 +303,7 @@ FLAC__bool FLAC__file_encoder_set_sample_rate(FLAC__FileEncoder *encoder, unsign
        return FLAC__seekable_stream_encoder_set_sample_rate(encoder->private_->seekable_stream_encoder, value);
 }
 
-FLAC__bool FLAC__file_encoder_set_blocksize(FLAC__FileEncoder *encoder, unsigned value)
+FLAC_API FLAC__bool FLAC__file_encoder_set_blocksize(FLAC__FileEncoder *encoder, unsigned value)
 {
        FLAC__ASSERT(0 != encoder);
        FLAC__ASSERT(0 != encoder->private_);
@@ -259,7 +314,7 @@ FLAC__bool FLAC__file_encoder_set_blocksize(FLAC__FileEncoder *encoder, unsigned
        return FLAC__seekable_stream_encoder_set_blocksize(encoder->private_->seekable_stream_encoder, value);
 }
 
-FLAC__bool FLAC__file_encoder_set_max_lpc_order(FLAC__FileEncoder *encoder, unsigned value)
+FLAC_API FLAC__bool FLAC__file_encoder_set_max_lpc_order(FLAC__FileEncoder *encoder, unsigned value)
 {
        FLAC__ASSERT(0 != encoder);
        FLAC__ASSERT(0 != encoder->private_);
@@ -270,7 +325,7 @@ FLAC__bool FLAC__file_encoder_set_max_lpc_order(FLAC__FileEncoder *encoder, unsi
        return FLAC__seekable_stream_encoder_set_max_lpc_order(encoder->private_->seekable_stream_encoder, value);
 }
 
-FLAC__bool FLAC__file_encoder_set_qlp_coeff_precision(FLAC__FileEncoder *encoder, unsigned value)
+FLAC_API FLAC__bool FLAC__file_encoder_set_qlp_coeff_precision(FLAC__FileEncoder *encoder, unsigned value)
 {
        FLAC__ASSERT(0 != encoder);
        FLAC__ASSERT(0 != encoder->private_);
@@ -281,7 +336,7 @@ FLAC__bool FLAC__file_encoder_set_qlp_coeff_precision(FLAC__FileEncoder *encoder
        return FLAC__seekable_stream_encoder_set_qlp_coeff_precision(encoder->private_->seekable_stream_encoder, value);
 }
 
-FLAC__bool FLAC__file_encoder_set_do_qlp_coeff_prec_search(FLAC__FileEncoder *encoder, FLAC__bool value)
+FLAC_API FLAC__bool FLAC__file_encoder_set_do_qlp_coeff_prec_search(FLAC__FileEncoder *encoder, FLAC__bool value)
 {
        FLAC__ASSERT(0 != encoder);
        FLAC__ASSERT(0 != encoder->private_);
@@ -292,7 +347,7 @@ FLAC__bool FLAC__file_encoder_set_do_qlp_coeff_prec_search(FLAC__FileEncoder *en
        return FLAC__seekable_stream_encoder_set_do_qlp_coeff_prec_search(encoder->private_->seekable_stream_encoder, value);
 }
 
-FLAC__bool FLAC__file_encoder_set_do_escape_coding(FLAC__FileEncoder *encoder, FLAC__bool value)
+FLAC_API FLAC__bool FLAC__file_encoder_set_do_escape_coding(FLAC__FileEncoder *encoder, FLAC__bool value)
 {
        FLAC__ASSERT(0 != encoder);
        FLAC__ASSERT(0 != encoder->private_);
@@ -303,7 +358,7 @@ FLAC__bool FLAC__file_encoder_set_do_escape_coding(FLAC__FileEncoder *encoder, F
        return FLAC__seekable_stream_encoder_set_do_escape_coding(encoder->private_->seekable_stream_encoder, value);
 }
 
-FLAC__bool FLAC__file_encoder_set_do_exhaustive_model_search(FLAC__FileEncoder *encoder, FLAC__bool value)
+FLAC_API FLAC__bool FLAC__file_encoder_set_do_exhaustive_model_search(FLAC__FileEncoder *encoder, FLAC__bool value)
 {
        FLAC__ASSERT(0 != encoder);
        FLAC__ASSERT(0 != encoder->private_);
@@ -314,7 +369,7 @@ FLAC__bool FLAC__file_encoder_set_do_exhaustive_model_search(FLAC__FileEncoder *
        return FLAC__seekable_stream_encoder_set_do_exhaustive_model_search(encoder->private_->seekable_stream_encoder, value);
 }
 
-FLAC__bool FLAC__file_encoder_set_min_residual_partition_order(FLAC__FileEncoder *encoder, unsigned value)
+FLAC_API FLAC__bool FLAC__file_encoder_set_min_residual_partition_order(FLAC__FileEncoder *encoder, unsigned value)
 {
        FLAC__ASSERT(0 != encoder);
        FLAC__ASSERT(0 != encoder->private_);
@@ -325,7 +380,7 @@ FLAC__bool FLAC__file_encoder_set_min_residual_partition_order(FLAC__FileEncoder
        return FLAC__seekable_stream_encoder_set_min_residual_partition_order(encoder->private_->seekable_stream_encoder, value);
 }
 
-FLAC__bool FLAC__file_encoder_set_max_residual_partition_order(FLAC__FileEncoder *encoder, unsigned value)
+FLAC_API FLAC__bool FLAC__file_encoder_set_max_residual_partition_order(FLAC__FileEncoder *encoder, unsigned value)
 {
        FLAC__ASSERT(0 != encoder);
        FLAC__ASSERT(0 != encoder->private_);
@@ -336,7 +391,7 @@ FLAC__bool FLAC__file_encoder_set_max_residual_partition_order(FLAC__FileEncoder
        return FLAC__seekable_stream_encoder_set_max_residual_partition_order(encoder->private_->seekable_stream_encoder, value);
 }
 
-FLAC__bool FLAC__file_encoder_set_rice_parameter_search_dist(FLAC__FileEncoder *encoder, unsigned value)
+FLAC_API FLAC__bool FLAC__file_encoder_set_rice_parameter_search_dist(FLAC__FileEncoder *encoder, unsigned value)
 {
        FLAC__ASSERT(0 != encoder);
        FLAC__ASSERT(0 != encoder->private_);
@@ -347,7 +402,7 @@ FLAC__bool FLAC__file_encoder_set_rice_parameter_search_dist(FLAC__FileEncoder *
        return FLAC__seekable_stream_encoder_set_rice_parameter_search_dist(encoder->private_->seekable_stream_encoder, value);
 }
 
-FLAC__bool FLAC__file_encoder_set_total_samples_estimate(FLAC__FileEncoder *encoder, FLAC__uint64 value)
+FLAC_API FLAC__bool FLAC__file_encoder_set_total_samples_estimate(FLAC__FileEncoder *encoder, FLAC__uint64 value)
 {
        FLAC__ASSERT(0 != encoder);
        FLAC__ASSERT(0 != encoder->private_);
@@ -358,7 +413,7 @@ FLAC__bool FLAC__file_encoder_set_total_samples_estimate(FLAC__FileEncoder *enco
        return FLAC__seekable_stream_encoder_set_total_samples_estimate(encoder->private_->seekable_stream_encoder, value);
 }
 
-FLAC__bool FLAC__file_encoder_set_metadata(FLAC__FileEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks)
+FLAC_API FLAC__bool FLAC__file_encoder_set_metadata(FLAC__FileEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks)
 {
        FLAC__ASSERT(0 != encoder);
        FLAC__ASSERT(0 != encoder->private_);
@@ -366,10 +421,10 @@ FLAC__bool FLAC__file_encoder_set_metadata(FLAC__FileEncoder *encoder, FLAC__Str
        FLAC__ASSERT(0 != encoder->private_->seekable_stream_encoder);
        if(encoder->protected_->state != FLAC__FILE_ENCODER_UNINITIALIZED)
                return false;
-       return FLAC__seekable_stream_encoder_set_metadata(encoder->private_->stream_encoder, value);
+       return FLAC__seekable_stream_encoder_set_metadata(encoder->private_->seekable_stream_encoder, metadata, num_blocks);
 }
 
-FLAC__bool FLAC__file_encoder_set_filename(FLAC__FileEncoder *encoder, const char *value)
+FLAC_API FLAC__bool FLAC__file_encoder_set_filename(FLAC__FileEncoder *encoder, const char *value)
 {
        FLAC__ASSERT(0 != encoder);
        FLAC__ASSERT(0 != encoder->private_);
@@ -389,140 +444,250 @@ FLAC__bool FLAC__file_encoder_set_filename(FLAC__FileEncoder *encoder, const cha
        return true;
 }
 
-FLAC__FileEncoderState FLAC__file_encoder_get_state(const FLAC__FileEncoder *encoder)
+FLAC_API FLAC__bool FLAC__file_encoder_set_progress_callback(FLAC__FileEncoder *encoder, FLAC__FileEncoderProgressCallback value)
+{
+       FLAC__ASSERT(0 != encoder);
+       FLAC__ASSERT(0 != encoder->private_);
+       FLAC__ASSERT(0 != encoder->protected_);
+       if(encoder->protected_->state != FLAC__FILE_ENCODER_UNINITIALIZED)
+               return false;
+       encoder->private_->progress_callback = value;
+       return true;
+}
+
+FLAC_API FLAC__bool FLAC__file_encoder_set_client_data(FLAC__FileEncoder *encoder, void *value)
+{
+       FLAC__ASSERT(0 != encoder);
+       FLAC__ASSERT(0 != encoder->private_);
+       FLAC__ASSERT(0 != encoder->protected_);
+       if(encoder->protected_->state != FLAC__FILE_ENCODER_UNINITIALIZED)
+               return false;
+       encoder->private_->client_data = value;
+       return true;
+}
+
+/*
+ * These three functions are not static, but not publically exposed in
+ * include/FLAC/ either.  They are used by the test suite.
+ */
+FLAC_API FLAC__bool FLAC__file_encoder_disable_constant_subframes(FLAC__FileEncoder *encoder, FLAC__bool value)
+{
+       FLAC__ASSERT(0 != encoder);
+       FLAC__ASSERT(0 != encoder->private_);
+       FLAC__ASSERT(0 != encoder->protected_);
+       if(encoder->protected_->state != FLAC__FILE_ENCODER_UNINITIALIZED)
+               return false;
+       return FLAC__seekable_stream_encoder_disable_constant_subframes(encoder->private_->seekable_stream_encoder, value);
+}
+
+FLAC_API FLAC__bool FLAC__file_encoder_disable_fixed_subframes(FLAC__FileEncoder *encoder, FLAC__bool value)
+{
+       FLAC__ASSERT(0 != encoder);
+       FLAC__ASSERT(0 != encoder->private_);
+       FLAC__ASSERT(0 != encoder->protected_);
+       if(encoder->protected_->state != FLAC__FILE_ENCODER_UNINITIALIZED)
+               return false;
+       return FLAC__seekable_stream_encoder_disable_fixed_subframes(encoder->private_->seekable_stream_encoder, value);
+}
+
+FLAC_API FLAC__bool FLAC__file_encoder_disable_verbatim_subframes(FLAC__FileEncoder *encoder, FLAC__bool value)
+{
+       FLAC__ASSERT(0 != encoder);
+       FLAC__ASSERT(0 != encoder->private_);
+       FLAC__ASSERT(0 != encoder->protected_);
+       if(encoder->protected_->state != FLAC__FILE_ENCODER_UNINITIALIZED)
+               return false;
+       return FLAC__seekable_stream_encoder_disable_verbatim_subframes(encoder->private_->seekable_stream_encoder, value);
+}
+
+FLAC_API FLAC__FileEncoderState FLAC__file_encoder_get_state(const FLAC__FileEncoder *encoder)
 {
        FLAC__ASSERT(0 != encoder);
        FLAC__ASSERT(0 != encoder->protected_);
        return encoder->protected_->state;
 }
 
-FLAC__FileEncoderState FLAC__file_encoder_get_seekable_stream_encoder_state(const FLAC__FileEncoder *encoder)
+FLAC_API FLAC__SeekableStreamEncoderState FLAC__file_encoder_get_seekable_stream_encoder_state(const FLAC__FileEncoder *encoder)
 {
        FLAC__ASSERT(0 != encoder);
        FLAC__ASSERT(0 != encoder->private_);
        return FLAC__seekable_stream_encoder_get_state(encoder->private_->seekable_stream_encoder);
 }
 
-FLAC__bool FLAC__file_encoder_get_streamable_subset(const FLAC__FileEncoder *encoder)
+FLAC_API FLAC__StreamEncoderState FLAC__file_encoder_get_stream_encoder_state(const FLAC__FileEncoder *encoder)
+{
+       FLAC__ASSERT(0 != encoder);
+       FLAC__ASSERT(0 != encoder->private_);
+       return FLAC__seekable_stream_encoder_get_stream_encoder_state(encoder->private_->seekable_stream_encoder);
+}
+
+FLAC_API FLAC__StreamDecoderState FLAC__file_encoder_get_verify_decoder_state(const FLAC__FileEncoder *encoder)
+{
+       FLAC__ASSERT(0 != encoder);
+       FLAC__ASSERT(0 != encoder->private_);
+       return FLAC__seekable_stream_encoder_get_verify_decoder_state(encoder->private_->seekable_stream_encoder);
+}
+
+FLAC_API const char *FLAC__file_encoder_get_resolved_state_string(const FLAC__FileEncoder *encoder)
+{
+       if(encoder->protected_->state != FLAC__FILE_ENCODER_SEEKABLE_STREAM_ENCODER_ERROR)
+               return FLAC__FileEncoderStateString[encoder->protected_->state];
+       else
+               return FLAC__seekable_stream_encoder_get_resolved_state_string(encoder->private_->seekable_stream_encoder);
+}
+
+FLAC_API void FLAC__file_encoder_get_verify_decoder_error_stats(const FLAC__FileEncoder *encoder, FLAC__uint64 *absolute_sample, unsigned *frame_number, unsigned *channel, unsigned *sample, FLAC__int32 *expected, FLAC__int32 *got)
+{
+       FLAC__ASSERT(0 != encoder);
+       FLAC__ASSERT(0 != encoder->private_);
+       FLAC__seekable_stream_encoder_get_verify_decoder_error_stats(encoder->private_->seekable_stream_encoder, absolute_sample, frame_number, channel, sample, expected, got);
+}
+
+FLAC_API FLAC__bool FLAC__file_encoder_get_verify(const FLAC__FileEncoder *encoder)
+{
+       FLAC__ASSERT(0 != encoder);
+       FLAC__ASSERT(0 != encoder->private_);
+       return FLAC__seekable_stream_encoder_get_verify(encoder->private_->seekable_stream_encoder);
+}
+
+FLAC_API FLAC__bool FLAC__file_encoder_get_streamable_subset(const FLAC__FileEncoder *encoder)
 {
        FLAC__ASSERT(0 != encoder);
        FLAC__ASSERT(0 != encoder->private_);
        return FLAC__seekable_stream_encoder_get_streamable_subset(encoder->private_->seekable_stream_encoder);
 }
 
-FLAC__bool FLAC__file_encoder_get_do_mid_side_stereo(const FLAC__FileEncoder *encoder)
+FLAC_API FLAC__bool FLAC__file_encoder_get_do_mid_side_stereo(const FLAC__FileEncoder *encoder)
 {
        FLAC__ASSERT(0 != encoder);
        FLAC__ASSERT(0 != encoder->private_);
        return FLAC__seekable_stream_encoder_get_do_mid_side_stereo(encoder->private_->seekable_stream_encoder);
 }
 
-FLAC__bool FLAC__file_encoder_get_loose_mid_side_stereo(const FLAC__FileEncoder *encoder)
+FLAC_API FLAC__bool FLAC__file_encoder_get_loose_mid_side_stereo(const FLAC__FileEncoder *encoder)
 {
        FLAC__ASSERT(0 != encoder);
        FLAC__ASSERT(0 != encoder->private_);
        return FLAC__seekable_stream_encoder_get_loose_mid_side_stereo(encoder->private_->seekable_stream_encoder);
 }
 
-unsigned FLAC__file_encoder_get_channels(const FLAC__FileEncoder *encoder)
+FLAC_API unsigned FLAC__file_encoder_get_channels(const FLAC__FileEncoder *encoder)
 {
        FLAC__ASSERT(0 != encoder);
        FLAC__ASSERT(0 != encoder->private_);
        return FLAC__seekable_stream_encoder_get_channels(encoder->private_->seekable_stream_encoder);
 }
 
-unsigned FLAC__file_encoder_get_bits_per_sample(const FLAC__FileEncoder *encoder)
+FLAC_API unsigned FLAC__file_encoder_get_bits_per_sample(const FLAC__FileEncoder *encoder)
 {
        FLAC__ASSERT(0 != encoder);
        FLAC__ASSERT(0 != encoder->private_);
        return FLAC__seekable_stream_encoder_get_bits_per_sample(encoder->private_->seekable_stream_encoder);
 }
 
-unsigned FLAC__file_encoder_get_sample_rate(const FLAC__FileEncoder *encoder)
+FLAC_API unsigned FLAC__file_encoder_get_sample_rate(const FLAC__FileEncoder *encoder)
 {
        FLAC__ASSERT(0 != encoder);
        FLAC__ASSERT(0 != encoder->private_);
        return FLAC__seekable_stream_encoder_get_sample_rate(encoder->private_->seekable_stream_encoder);
 }
 
-unsigned FLAC__file_encoder_get_blocksize(const FLAC__FileEncoder *encoder)
+FLAC_API unsigned FLAC__file_encoder_get_blocksize(const FLAC__FileEncoder *encoder)
 {
        FLAC__ASSERT(0 != encoder);
        FLAC__ASSERT(0 != encoder->private_);
        return FLAC__seekable_stream_encoder_get_blocksize(encoder->private_->seekable_stream_encoder);
 }
 
-unsigned FLAC__file_encoder_get_max_lpc_order(const FLAC__FileEncoder *encoder)
+FLAC_API unsigned FLAC__file_encoder_get_max_lpc_order(const FLAC__FileEncoder *encoder)
 {
        FLAC__ASSERT(0 != encoder);
        FLAC__ASSERT(0 != encoder->private_);
        return FLAC__seekable_stream_encoder_get_max_lpc_order(encoder->private_->seekable_stream_encoder);
 }
 
-unsigned FLAC__file_encoder_get_qlp_coeff_precision(const FLAC__FileEncoder *encoder)
+FLAC_API unsigned FLAC__file_encoder_get_qlp_coeff_precision(const FLAC__FileEncoder *encoder)
 {
        FLAC__ASSERT(0 != encoder);
        FLAC__ASSERT(0 != encoder->private_);
        return FLAC__seekable_stream_encoder_get_qlp_coeff_precision(encoder->private_->seekable_stream_encoder);
 }
 
-FLAC__bool FLAC__file_encoder_get_do_qlp_coeff_prec_search(const FLAC__FileEncoder *encoder)
+FLAC_API FLAC__bool FLAC__file_encoder_get_do_qlp_coeff_prec_search(const FLAC__FileEncoder *encoder)
 {
        FLAC__ASSERT(0 != encoder);
        FLAC__ASSERT(0 != encoder->private_);
        return FLAC__seekable_stream_encoder_get_do_qlp_coeff_prec_search(encoder->private_->seekable_stream_encoder);
 }
 
-FLAC__bool FLAC__file_encoder_get_do_escape_coding(const FLAC__FileEncoder *encoder)
+FLAC_API FLAC__bool FLAC__file_encoder_get_do_escape_coding(const FLAC__FileEncoder *encoder)
 {
        FLAC__ASSERT(0 != encoder);
        FLAC__ASSERT(0 != encoder->private_);
        return FLAC__seekable_stream_encoder_get_do_escape_coding(encoder->private_->seekable_stream_encoder);
 }
 
-FLAC__bool FLAC__file_encoder_get_do_exhaustive_model_search(const FLAC__FileEncoder *encoder)
+FLAC_API FLAC__bool FLAC__file_encoder_get_do_exhaustive_model_search(const FLAC__FileEncoder *encoder)
 {
        FLAC__ASSERT(0 != encoder);
        FLAC__ASSERT(0 != encoder->private_);
        return FLAC__seekable_stream_encoder_get_do_exhaustive_model_search(encoder->private_->seekable_stream_encoder);
 }
 
-unsigned FLAC__file_encoder_get_min_residual_partition_order(const FLAC__FileEncoder *encoder)
+FLAC_API unsigned FLAC__file_encoder_get_min_residual_partition_order(const FLAC__FileEncoder *encoder)
 {
        FLAC__ASSERT(0 != encoder);
        FLAC__ASSERT(0 != encoder->private_);
        return FLAC__seekable_stream_encoder_get_min_residual_partition_order(encoder->private_->seekable_stream_encoder);
 }
 
-unsigned FLAC__file_encoder_get_max_residual_partition_order(const FLAC__FileEncoder *encoder)
+FLAC_API unsigned FLAC__file_encoder_get_max_residual_partition_order(const FLAC__FileEncoder *encoder)
 {
        FLAC__ASSERT(0 != encoder);
        FLAC__ASSERT(0 != encoder->private_);
        return FLAC__seekable_stream_encoder_get_max_residual_partition_order(encoder->private_->seekable_stream_encoder);
 }
 
-unsigned FLAC__file_encoder_get_rice_parameter_search_dist(const FLAC__FileEncoder *encoder)
+FLAC_API unsigned FLAC__file_encoder_get_rice_parameter_search_dist(const FLAC__FileEncoder *encoder)
 {
        FLAC__ASSERT(0 != encoder);
        FLAC__ASSERT(0 != encoder->private_);
        return FLAC__seekable_stream_encoder_get_rice_parameter_search_dist(encoder->private_->seekable_stream_encoder);
 }
 
-FLAC__bool FLAC__file_encoder_process(FLAC__FileEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples)
+FLAC_API FLAC__uint64 FLAC__file_encoder_get_total_samples_estimate(const FLAC__FileEncoder *encoder)
 {
        FLAC__ASSERT(0 != encoder);
        FLAC__ASSERT(0 != encoder->private_);
-       return FLAC__seekable_stream_encoder_process(encoder->private_->seekable_stream_encoder, buffer, samples);
+       return FLAC__seekable_stream_encoder_get_total_samples_estimate(encoder->private_->seekable_stream_encoder);
+}
+
+FLAC_API FLAC__bool FLAC__file_encoder_process(FLAC__FileEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples)
+{
+       FLAC__ASSERT(0 != encoder);
+       FLAC__ASSERT(0 != encoder->private_);
+       if(!FLAC__seekable_stream_encoder_process(encoder->private_->seekable_stream_encoder, buffer, samples)) {
+               encoder->protected_->state = FLAC__FILE_ENCODER_SEEKABLE_STREAM_ENCODER_ERROR;
+               return false;
+       }
+       else
+               return true;
 }
 
 /* 'samples' is channel-wide samples, e.g. for 1 second at 44100Hz, 'samples' = 44100 regardless of the number of channels */
-FLAC__bool FLAC__file_encoder_process_interleaved(FLAC__FileEncoder *encoder, const FLAC__int32 buffer[], unsigned samples)
+FLAC_API FLAC__bool FLAC__file_encoder_process_interleaved(FLAC__FileEncoder *encoder, const FLAC__int32 buffer[], unsigned samples)
 {
        FLAC__ASSERT(0 != encoder);
        FLAC__ASSERT(0 != encoder->private_);
-       return FLAC__seekable_stream_encoder_process_interleaved(encoder->private_->seekable_stream_encoder, buffer, samples);
+       if(!FLAC__seekable_stream_encoder_process_interleaved(encoder->private_->seekable_stream_encoder, buffer, samples)) {
+               encoder->protected_->state = FLAC__FILE_ENCODER_SEEKABLE_STREAM_ENCODER_ERROR;
+               return false;
+       }
+       else
+               return true;
 }
 
+
 /***********************************************************************
  *
  * Private class methods
@@ -533,208 +698,79 @@ void set_defaults_(FLAC__FileEncoder *encoder)
 {
        FLAC__ASSERT(0 != encoder);
        FLAC__ASSERT(0 != encoder->private_);
-       FLAC__ASSERT(0 != encoder->protected_);
 
-       encoder->protected_->filename = 0;
+       encoder->private_->progress_callback = 0;
+       encoder->private_->client_data = 0;
+       encoder->private_->total_frames_estimate = 0;
+       encoder->private_->filename = 0;
 }
 
-FLAC__StreamEncoderWriteStatus file_encoder_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
+FLAC__SeekableStreamEncoderSeekStatus seek_callback_(const FLAC__SeekableStreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data)
 {
        FLAC__FileEncoder *file_encoder = (FLAC__FileEncoder*)client_data;
 
-       /* mark the current seek point if hit (if stream_offset == 0 that means we're still writing metadata and haven't hit the first frame yet) */
-       if(file_encoder->private_->stream_offset > 0 && file_encoder->private_->seek_table.num_points > 0) {
-               FLAC__uint64 current_sample = (FLAC__uint64)current_frame * (FLAC__uint64)FLAC__stream_encoder_get_blocksize(encoder), test_sample;
-               unsigned i;
-               for(i = file_encoder->private_->first_seek_point_to_check; i < file_encoder->private_->seek_table.num_points; i++) {
-                       test_sample = file_encoder->private_->seek_table.points[i].sample_number;
-                       if(test_sample > current_sample) {
-                               break;
-                       }
-                       else if(test_sample == current_sample) {
-                               file_encoder->private_->seek_table.points[i].stream_offset = file_encoder->private_->bytes_written - file_encoder->private_->stream_offset;
-                               file_encoder->private_->seek_table.points[i].frame_samples = FLAC__stream_encoder_get_blocksize(encoder);
-                               file_encoder->private_->first_seek_point_to_check++;
-                               break;
-                       }
-                       else {
-                               file_encoder->private_->first_seek_point_to_check++;
-                       }
-               }
-       }
-
-       file_encoder->private_->bytes_written += bytes;
-       file_encoder->private_->samples_written += samples;
-       file_encoder->private_->current_frame = current_frame;
-
-       if(samples && file_encoder->private_->verbose && file_encoder->private_->total_samples_to_encode > 0 && !(current_frame & mask))
-               print_stats(file_encoder->private_);
-
-       if(file_encoder->private_->verify) {
-               file_encoder->private_->verify_fifo.encoded_signal = buffer;
-               file_encoder->private_->verify_fifo.encoded_bytes = bytes;
-               if(file_encoder->private_->verify_fifo.into_frames) {
-                       if(!FLAC__stream_decoder_process_one_frame(file_encoder->private_->verify_fifo.decoder)) {
-                               file_encoder->private_->verify_fifo.result = FLAC__VERIFY_FAILED_IN_FRAME;
-                               return FLAC__STREAM_ENCODER_WRITE_FATAL_ERROR;
-                       }
-               }
-               else {
-                       if(!FLAC__stream_decoder_process_metadata(file_encoder->private_->verify_fifo.decoder)) {
-                               file_encoder->private_->verify_fifo.result = FLAC__VERIFY_FAILED_IN_METADATA;
-                               return FLAC__STREAM_ENCODER_WRITE_FATAL_ERROR;
-                       }
-               }
-       }
+       (void)encoder;
 
-#ifdef FLAC__HAS_OGG
-       if(file_encoder->private_->use_ogg) {
-               ogg_packet op;
-
-               memset(&op, 0, sizeof(op));
-               op.packet = (unsigned char *)buffer;
-               op.granulepos = file_encoder->private_->samples_written - 1;
-               /*@@@ WATCHOUT:
-                * this depends on the behavior of libFLAC that we will get one
-                * write_callback first with all the metadata (and 'samples'
-                * will be 0), then one write_callback for each frame.
-                */
-               op.packetno = (samples == 0? -1 : (int)file_encoder->private_->current_frame);
-               op.bytes = bytes;
+       FLAC__ASSERT(0 != file_encoder);
 
-               if (file_encoder->private_->bytes_written == bytes)
-                       op.b_o_s = 1;
+       if(fseek(file_encoder->private_->file, (long)absolute_byte_offset, SEEK_SET) < 0)
+               return FLAC__SEEKABLE_STREAM_ENCODER_SEEK_STATUS_ERROR;
+       else
+               return FLAC__SEEKABLE_STREAM_ENCODER_SEEK_STATUS_OK;
+}
 
-               if (file_encoder->private_->total_samples_to_encode == file_encoder->private_->samples_written)
-                       op.e_o_s = 1;
+FLAC__SeekableStreamEncoderTellStatus tell_callback_(const FLAC__SeekableStreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
+{
+       FLAC__FileEncoder *file_encoder = (FLAC__FileEncoder*)client_data;
+       long offset;
 
-               ogg_stream_packetin(&file_encoder->private_->ogg.os, &op);
+       (void)encoder;
 
-               while(ogg_stream_pageout(&file_encoder->private_->ogg.os, &file_encoder->private_->ogg.og) != 0) {
-                       int written;
-                       written = fwrite(file_encoder->private_->ogg.og.header, 1, file_encoder->private_->ogg.og.header_len, file_encoder->private_->fout);
-                       if (written != file_encoder->private_->ogg.og.header_len)
-                               return FLAC__STREAM_ENCODER_WRITE_FATAL_ERROR;
+       FLAC__ASSERT(0 != file_encoder);
 
-                       written = fwrite(file_encoder->private_->ogg.og.body, 1, file_encoder->private_->ogg.og.body_len, file_encoder->private_->fout);
-                       if (written != file_encoder->private_->ogg.og.body_len)
-                               return FLAC__STREAM_ENCODER_WRITE_FATAL_ERROR;
-               }
+       offset = ftell(file_encoder->private_->file);
 
-               return FLAC__STREAM_ENCODER_WRITE_OK;
+       if(offset < 0) {
+               return FLAC__SEEKABLE_STREAM_ENCODER_TELL_STATUS_ERROR;
        }
-       else
-#endif
-       {
-               if(fwrite(buffer, sizeof(FLAC__byte), bytes, file_encoder->private_->fout) == bytes)
-                       return FLAC__STREAM_ENCODER_WRITE_OK;
-               else
-                       return FLAC__STREAM_ENCODER_WRITE_FATAL_ERROR;
+       else {
+               *absolute_byte_offset = (FLAC__uint64)offset;
+               return FLAC__SEEKABLE_STREAM_ENCODER_TELL_STATUS_OK;
        }
 }
 
-void file_encoder_metadata_callback_(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetadata *metadata, void *client_data)
+#ifdef FLAC__VALGRIND_TESTING
+static size_t local__fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
 {
-       file_encoder->private_ *file_encoder->private_ = (file_encoder->private_ *)client_data;
-       FLAC__byte b;
-       FILE *f = file_encoder->private_->fout;
-       const FLAC__uint64 samples = metadata->data.stream_info.total_samples;
-       const unsigned min_framesize = metadata->data.stream_info.min_framesize;
-       const unsigned max_framesize = metadata->data.stream_info.max_framesize;
-
-       FLAC__ASSERT(metadata->type == FLAC__METADATA_TYPE_STREAMINFO);
-
-       /*
-        * If we are writing to an ogg stream, there is no need to go back
-        * and update the STREAMINFO or SEEKTABLE blocks; the values we would
-        * update are not necessary with Ogg as the transport.  We can't do
-        * it reliably anyway without knowing the Ogg structure.
-        */
-#ifdef FLAC__HAS_OGG
-       if(file_encoder->private_->use_ogg)
-               return;
+       size_t ret = fwrite(ptr, size, nmemb, stream);
+       if(!ferror(stream))
+               fflush(stream);
+       return ret;
+}
+#else
+#define local__fwrite fwrite
 #endif
 
-       /*
-        * we get called by the encoder when the encoding process has
-        * finished so that we can update the STREAMINFO and SEEKTABLE
-        * blocks.
-        */
+FLAC__StreamEncoderWriteStatus write_callback_(const FLAC__SeekableStreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
+{
+       FLAC__FileEncoder *file_encoder = (FLAC__FileEncoder*)client_data;
 
-       (void)encoder; /* silence compiler warning about unused parameter */
+       (void)encoder, (void)samples, (void)current_frame;
 
-       if(f != stdout) {
-               fclose(file_encoder->private_->fout);
-               if(0 == (f = fopen(file_encoder->private_->outfilename, "r+b")))
-                       return;
-       }
+       FLAC__ASSERT(0 != file_encoder);
 
-       /* all this is based on intimate knowledge of the stream header
-        * layout, but a change to the header format that would break this
-        * would also break all streams encoded in the previous format.
-        */
-
-       if(-1 == fseek(f, 26, SEEK_SET)) goto end_;
-       fwrite(metadata->data.stream_info.md5sum, 1, 16, f);
-
-       /* if we get this far we know we can seek so no need to check the
-        * return value from fseek()
-        */
-       fseek(f, 21, SEEK_SET);
-       if(fread(&b, 1, 1, f) != 1) goto framesize_;
-       fseek(f, 21, SEEK_SET);
-       b = (b & 0xf0) | (FLAC__byte)((samples >> 32) & 0x0F);
-       if(fwrite(&b, 1, 1, f) != 1) goto framesize_;
-       b = (FLAC__byte)((samples >> 24) & 0xFF);
-       if(fwrite(&b, 1, 1, f) != 1) goto framesize_;
-       b = (FLAC__byte)((samples >> 16) & 0xFF);
-       if(fwrite(&b, 1, 1, f) != 1) goto framesize_;
-       b = (FLAC__byte)((samples >> 8) & 0xFF);
-       if(fwrite(&b, 1, 1, f) != 1) goto framesize_;
-       b = (FLAC__byte)(samples & 0xFF);
-       if(fwrite(&b, 1, 1, f) != 1) goto framesize_;
-
-framesize_:
-       fseek(f, 12, SEEK_SET);
-       b = (FLAC__byte)((min_framesize >> 16) & 0xFF);
-       if(fwrite(&b, 1, 1, f) != 1) goto seektable_;
-       b = (FLAC__byte)((min_framesize >> 8) & 0xFF);
-       if(fwrite(&b, 1, 1, f) != 1) goto seektable_;
-       b = (FLAC__byte)(min_framesize & 0xFF);
-       if(fwrite(&b, 1, 1, f) != 1) goto seektable_;
-       b = (FLAC__byte)((max_framesize >> 16) & 0xFF);
-       if(fwrite(&b, 1, 1, f) != 1) goto seektable_;
-       b = (FLAC__byte)((max_framesize >> 8) & 0xFF);
-       if(fwrite(&b, 1, 1, f) != 1) goto seektable_;
-       b = (FLAC__byte)(max_framesize & 0xFF);
-       if(fwrite(&b, 1, 1, f) != 1) goto seektable_;
-
-seektable_:
-       if(file_encoder->private_->seek_table.num_points > 0) {
-               long pos;
-               unsigned i;
-
-               /* convert any unused seek points to placeholders */
-               for(i = 0; i < file_encoder->private_->seek_table.num_points; i++) {
-                       if(file_encoder->private_->seek_table.points[i].sample_number == FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER)
-                               break;
-                       else if(file_encoder->private_->seek_table.points[i].frame_samples == 0)
-                               file_encoder->private_->seek_table.points[i].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
-               }
-
-               /* the offset of the seek table data 'pos' should be after then stream sync and STREAMINFO block and SEEKTABLE header */
-               pos = (FLAC__STREAM_SYNC_LEN + FLAC__STREAM_METADATA_IS_LAST_LEN + FLAC__STREAM_METADATA_TYPE_LEN + FLAC__STREAM_METADATA_LENGTH_LEN) / 8;
-               pos += metadata->length;
-               pos += (FLAC__STREAM_METADATA_IS_LAST_LEN + FLAC__STREAM_METADATA_TYPE_LEN + FLAC__STREAM_METADATA_LENGTH_LEN) / 8;
-               fseek(f, pos, SEEK_SET);
-               for(i = 0; i < file_encoder->private_->seek_table.num_points; i++) {
-                       if(!write_big_endian_uint64(f, file_encoder->private_->seek_table.points[i].sample_number)) goto end_;
-                       if(!write_big_endian_uint64(f, file_encoder->private_->seek_table.points[i].stream_offset)) goto end_;
-                       if(!write_big_endian_uint16(f, (FLAC__uint16)file_encoder->private_->seek_table.points[i].frame_samples)) goto end_;
-               }
+       if(local__fwrite(buffer, sizeof(FLAC__byte), bytes, file_encoder->private_->file) == bytes) {
+               file_encoder->private_->bytes_written += bytes;
+               file_encoder->private_->samples_written += samples;
+               /* we keep a high watermark on the number of frames written because
+                * when the encoder goes back to write metadata, 'current_frame'
+                * will drop back to 0.
+                */
+               file_encoder->private_->frames_written = max(file_encoder->private_->frames_written, current_frame+1);
+               if(0 != file_encoder->private_->progress_callback && samples > 0)
+                       file_encoder->private_->progress_callback(file_encoder, file_encoder->private_->bytes_written, file_encoder->private_->samples_written, file_encoder->private_->frames_written, file_encoder->private_->total_frames_estimate, file_encoder->private_->client_data);
+               return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
        }
-
-end_:
-       fclose(f);
-       return;
+       else
+               return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
 }