fix a calcuation bug in FLAC__lpc_compute_best_order()
[flac.git] / src / libFLAC / file_encoder.c
index e6c5cbd..2501779 100644 (file)
@@ -1,28 +1,44 @@
 /* libFLAC - Free Lossless Audio Codec library
- * Copyright (C) 2002  Josh Coalson
+ * Copyright (C) 2002,2003,2004,2005,2006  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__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);
 
 /***********************************************************************
@@ -44,6 +66,9 @@ typedef struct FLAC__FileEncoderPrivate {
        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;
@@ -55,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",
@@ -73,32 +98,29 @@ const char * const FLAC__FileEncoderStateString[] = {
  *
  ***********************************************************************/
 
-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;
        }
-       memset(encoder, 0, sizeof(FLAC__FileEncoder));
 
-       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;
        }
-       memset(encoder->protected_, 0, sizeof(FLAC__FileEncoderProtected));
 
-       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);
                return 0;
        }
-       memset(encoder->private_, 0, sizeof(FLAC__FileEncoderPrivate));
 
        encoder->private_->seekable_stream_encoder = FLAC__seekable_stream_encoder_new();
        if(0 == encoder->private_->seekable_stream_encoder) {
@@ -117,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_);
@@ -139,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);
 
@@ -154,7 +176,12 @@ FLAC__FileEncoderState FLAC__file_encoder_init(FLAC__FileEncoder *encoder)
        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_client_data(encoder->private_->seekable_stream_encoder, encoder);
 
@@ -165,13 +192,13 @@ FLAC__FileEncoderState FLAC__file_encoder_init(FLAC__FileEncoder *encoder)
                unsigned blocksize = FLAC__file_encoder_get_blocksize(encoder);
 
                FLAC__ASSERT(blocksize != 0);
-               encoder->private_->total_frames_estimate = (FLAC__file_encoder_get_total_samples_estimate(encoder) + blocksize - 1) / blocksize;
+               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);
 
@@ -199,7 +226,7 @@ void FLAC__file_encoder_finish(FLAC__FileEncoder *encoder)
        encoder->protected_->state = FLAC__FILE_ENCODER_UNINITIALIZED;
 }
 
-FLAC__bool FLAC__file_encoder_set_verify(FLAC__FileEncoder *encoder, FLAC__bool value)
+FLAC_API FLAC__bool FLAC__file_encoder_set_verify(FLAC__FileEncoder *encoder, FLAC__bool value)
 {
        FLAC__ASSERT(0 != encoder);
        FLAC__ASSERT(0 != encoder->private_);
@@ -210,7 +237,7 @@ FLAC__bool FLAC__file_encoder_set_verify(FLAC__FileEncoder *encoder, FLAC__bool
        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_);
@@ -221,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_);
@@ -232,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_);
@@ -243,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_);
@@ -254,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_);
@@ -265,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_);
@@ -276,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_);
@@ -287,7 +314,18 @@ 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_apodization(FLAC__FileEncoder *encoder, const char *specification)
+{
+       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_apodization(encoder->private_->seekable_stream_encoder, specification);
+}
+
+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_);
@@ -298,7 +336,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_);
@@ -309,7 +347,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_);
@@ -320,7 +358,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_);
@@ -331,7 +369,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_);
@@ -342,7 +380,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_);
@@ -353,7 +391,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_);
@@ -364,7 +402,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_);
@@ -375,7 +413,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_);
@@ -386,7 +424,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_);
@@ -397,7 +435,7 @@ FLAC__bool FLAC__file_encoder_set_metadata(FLAC__FileEncoder *encoder, FLAC__Str
        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_);
@@ -417,7 +455,7 @@ FLAC__bool FLAC__file_encoder_set_filename(FLAC__FileEncoder *encoder, const cha
        return true;
 }
 
-FLAC__bool FLAC__file_encoder_set_progress_callback(FLAC__FileEncoder *encoder, FLAC__FileEncoderProgressCallback value)
+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_);
@@ -428,7 +466,7 @@ FLAC__bool FLAC__file_encoder_set_progress_callback(FLAC__FileEncoder *encoder,
        return true;
 }
 
-FLAC__bool FLAC__file_encoder_set_client_data(FLAC__FileEncoder *encoder, void *value)
+FLAC_API FLAC__bool FLAC__file_encoder_set_client_data(FLAC__FileEncoder *encoder, void *value)
 {
        FLAC__ASSERT(0 != encoder);
        FLAC__ASSERT(0 != encoder->private_);
@@ -439,154 +477,203 @@ FLAC__bool FLAC__file_encoder_set_client_data(FLAC__FileEncoder *encoder, void *
        return true;
 }
 
-FLAC__FileEncoderState FLAC__file_encoder_get_state(const FLAC__FileEncoder *encoder)
+/*
+ * 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__SeekableStreamEncoderState 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__StreamEncoderState FLAC__file_encoder_get_stream_encoder_state(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__StreamDecoderState FLAC__file_encoder_get_verify_decoder_state(const FLAC__FileEncoder *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__bool FLAC__file_encoder_get_verify(const FLAC__FileEncoder *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__bool FLAC__file_encoder_get_streamable_subset(const FLAC__FileEncoder *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__uint64 FLAC__file_encoder_get_total_samples_estimate(const FLAC__FileEncoder *encoder)
+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_get_total_samples_estimate(encoder->private_->seekable_stream_encoder);
 }
 
-FLAC__bool FLAC__file_encoder_process(FLAC__FileEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples)
+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_);
@@ -599,7 +686,7 @@ FLAC__bool FLAC__file_encoder_process(FLAC__FileEncoder *encoder, const FLAC__in
 }
 
 /* '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_);
@@ -637,12 +724,44 @@ FLAC__SeekableStreamEncoderSeekStatus seek_callback_(const FLAC__SeekableStreamE
 
        FLAC__ASSERT(0 != file_encoder);
 
-       if(fseek(file_encoder->private_->file, absolute_byte_offset, SEEK_SET) < 0)
+       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;
 }
 
+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;
+
+       (void)encoder;
+
+       FLAC__ASSERT(0 != file_encoder);
+
+       offset = ftell(file_encoder->private_->file);
+
+       if(offset < 0) {
+               return FLAC__SEEKABLE_STREAM_ENCODER_TELL_STATUS_ERROR;
+       }
+       else {
+               *absolute_byte_offset = (FLAC__uint64)offset;
+               return FLAC__SEEKABLE_STREAM_ENCODER_TELL_STATUS_OK;
+       }
+}
+
+#ifdef FLAC__VALGRIND_TESTING
+static size_t local__fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
+{
+       size_t ret = fwrite(ptr, size, nmemb, stream);
+       if(!ferror(stream))
+               fflush(stream);
+       return ret;
+}
+#else
+#define local__fwrite fwrite
+#endif
+
 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;
@@ -651,9 +770,16 @@ FLAC__StreamEncoderWriteStatus write_callback_(const FLAC__SeekableStreamEncoder
 
        FLAC__ASSERT(0 != file_encoder);
 
-       if(fwrite(buffer, sizeof(FLAC__byte), bytes, file_encoder->private_->file) == bytes) {
-               if(0 != file_encoder->private_->progress_callback)
-                       file_encoder->private_->progress_callback(file_encoder, current_frame, file_encoder->private_->total_frames_estimate, file_encoder->private_->client_data);
+       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;
        }
        else