add FLAC__file_decoder_get_decode_position()
[flac.git] / src / libFLAC / file_decoder.c
index e6d5026..a6311cd 100644 (file)
@@ -1,5 +1,5 @@
 /* libFLAC - Free Lossless Audio Codec library
- * Copyright (C) 2000,2001  Josh Coalson
+ * Copyright (C) 2000,2001,2002  Josh Coalson
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
  * Boston, MA  02111-1307, USA.
  */
 
-#include <assert.h>
 #include <stdio.h>
 #include <stdlib.h> /* for malloc() */
 #include <string.h> /* for strcmp() */
-#include "FLAC/file_decoder.h"
-#include "protected/stream_decoder.h"
+#include <sys/stat.h> /* for stat() */
+#if defined _MSC_VER || defined __MINGW32__
+#include <io.h> /* for _setmode() */
+#include <fcntl.h> /* for _O_BINARY */
+#elif defined __CYGWIN__
+#include <io.h> /* for setmode(), O_BINARY */
+#include <fcntl.h> /* for _O_BINARY */
+#endif
+#include "FLAC/assert.h"
+#include "protected/file_decoder.h"
+#include "protected/seekable_stream_decoder.h"
 #include "private/md5.h"
 
+/***********************************************************************
+ *
+ * Private class method prototypes
+ *
+ ***********************************************************************/
+
+static void set_defaults_(FLAC__FileDecoder *decoder);
+static FILE *get_binary_stdin_();
+static FLAC__SeekableStreamDecoderReadStatus read_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data);
+static FLAC__SeekableStreamDecoderSeekStatus seek_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data);
+static FLAC__SeekableStreamDecoderTellStatus tell_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
+static FLAC__SeekableStreamDecoderLengthStatus length_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data);
+static FLAC__bool eof_callback_(const FLAC__SeekableStreamDecoder *decoder, void *client_data);
+static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__SeekableStreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
+static void metadata_callback_(const FLAC__SeekableStreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
+static void error_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
+
+/***********************************************************************
+ *
+ * Private class data
+ *
+ ***********************************************************************/
+
 typedef struct FLAC__FileDecoderPrivate {
-       FLAC__StreamDecoderWriteStatus (*write_callback)(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const int32 *buffer[], void *client_data);
-       void (*metadata_callback)(const FLAC__FileDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data);
-       void (*error_callback)(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
+       FLAC__FileDecoderWriteCallback write_callback;
+       FLAC__FileDecoderMetadataCallback metadata_callback;
+       FLAC__FileDecoderErrorCallback error_callback;
        void *client_data;
        FILE *file;
-       FLAC__StreamDecoder *stream;
-       struct MD5Context md5context;
-       byte stored_md5sum[16]; /* this is what is stored in the metadata */
-       byte computed_md5sum[16]; /* this is the sum we computed from the decoded data */
-       /* the rest of these are only used for seeking: */
-       FLAC__StreamMetaData_Encoding metadata; /* we keep this around so we can figure out how to seek quickly */
-       FLAC__Frame last_frame; /* holds the info of the last frame we seeked to */
-       uint64 target_sample;
+       char *filename; /* == NULL if stdin */
+       FLAC__SeekableStreamDecoder *seekable_stream_decoder;
 } FLAC__FileDecoderPrivate;
 
-static FLAC__StreamDecoderReadStatus read_callback_(const FLAC__StreamDecoder *decoder, byte buffer[], unsigned *bytes, void *client_data);
-static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const int32 *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 bool seek_to_absolute_sample_(FLAC__FileDecoder *decoder, long filesize, uint64 target_sample);
+/***********************************************************************
+ *
+ * Public static class data
+ *
+ ***********************************************************************/
 
-const char *FLAC__FileDecoderStateString[] = {
+FLAC_API const char * const FLAC__FileDecoderStateString[] = {
        "FLAC__FILE_DECODER_OK",
-       "FLAC__FILE_DECODER_SEEKING",
        "FLAC__FILE_DECODER_END_OF_FILE",
        "FLAC__FILE_DECODER_ERROR_OPENING_FILE",
        "FLAC__FILE_DECODER_MEMORY_ALLOCATION_ERROR",
        "FLAC__FILE_DECODER_SEEK_ERROR",
-       "FLAC__FILE_DECODER_STREAM_ERROR",
+       "FLAC__FILE_DECODER_SEEKABLE_STREAM_DECODER_ERROR",
+       "FLAC__FILE_DECODER_ALREADY_INITIALIZED",
+       "FLAC__FILE_DECODER_INVALID_CALLBACK",
        "FLAC__FILE_DECODER_UNINITIALIZED"
 };
 
-FLAC__FileDecoder *FLAC__file_decoder_get_new_instance()
+/***********************************************************************
+ *
+ * Class constructor/destructor
+ *
+ ***********************************************************************/
+
+FLAC_API FLAC__FileDecoder *FLAC__file_decoder_new()
 {
-       FLAC__FileDecoder *decoder = (FLAC__FileDecoder*)malloc(sizeof(FLAC__FileDecoder));
-       if(decoder != 0) {
-               decoder->state = FLAC__FILE_DECODER_UNINITIALIZED;
-               decoder->guts = 0;
+       FLAC__FileDecoder *decoder;
+
+       FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
+
+       decoder = (FLAC__FileDecoder*)calloc(1, sizeof(FLAC__FileDecoder));
+       if(decoder == 0) {
+               return 0;
        }
+
+       decoder->protected_ = (FLAC__FileDecoderProtected*)calloc(1, sizeof(FLAC__FileDecoderProtected));
+       if(decoder->protected_ == 0) {
+               free(decoder);
+               return 0;
+       }
+
+       decoder->private_ = (FLAC__FileDecoderPrivate*)calloc(1, sizeof(FLAC__FileDecoderPrivate));
+       if(decoder->private_ == 0) {
+               free(decoder->protected_);
+               free(decoder);
+               return 0;
+       }
+
+       decoder->private_->seekable_stream_decoder = FLAC__seekable_stream_decoder_new();
+       if(0 == decoder->private_->seekable_stream_decoder) {
+               free(decoder->private_);
+               free(decoder->protected_);
+               free(decoder);
+               return 0;
+       }
+
+       decoder->private_->file = 0;
+
+       set_defaults_(decoder);
+
+       decoder->protected_->state = FLAC__FILE_DECODER_UNINITIALIZED;
+
        return decoder;
 }
 
-void FLAC__file_decoder_free_instance(FLAC__FileDecoder *decoder)
+FLAC_API void FLAC__file_decoder_delete(FLAC__FileDecoder *decoder)
 {
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->protected_);
+       FLAC__ASSERT(0 != decoder->private_);
+       FLAC__ASSERT(0 != decoder->private_->seekable_stream_decoder);
+
+       (void)FLAC__file_decoder_finish(decoder);
+
+       FLAC__seekable_stream_decoder_delete(decoder->private_->seekable_stream_decoder);
+
+       free(decoder->private_);
+       free(decoder->protected_);
        free(decoder);
 }
 
-FLAC__FileDecoderState FLAC__file_decoder_init(
-       FLAC__FileDecoder *decoder,
-       const char *filename,
-       FLAC__StreamDecoderWriteStatus (*write_callback)(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const int32 *buffer[], void *client_data),
-       void (*metadata_callback)(const FLAC__FileDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data),
-       void (*error_callback)(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data),
-       void *client_data
-)
-{
-       assert(sizeof(int) >= 4); /* we want to die right away if this is not true */
-       assert(decoder != 0);
-       assert(write_callback != 0);
-       assert(metadata_callback != 0);
-       assert(error_callback != 0);
-       assert(decoder->state == FLAC__FILE_DECODER_UNINITIALIZED);
-       assert(decoder->guts == 0);
-
-       decoder->state = FLAC__FILE_DECODER_OK;
-
-       decoder->guts = (FLAC__FileDecoderPrivate*)malloc(sizeof(FLAC__FileDecoderPrivate));
-       if(decoder->guts == 0)
-               return decoder->state = FLAC__FILE_DECODER_MEMORY_ALLOCATION_ERROR;
-
-       decoder->guts->write_callback = write_callback;
-       decoder->guts->metadata_callback = metadata_callback;
-       decoder->guts->error_callback = error_callback;
-       decoder->guts->client_data = client_data;
-       decoder->guts->stream = 0;
-
-       if(0 == strcmp(filename, "-"))
-               decoder->guts->file = stdin;
+/***********************************************************************
+ *
+ * Public class methods
+ *
+ ***********************************************************************/
+
+FLAC_API FLAC__FileDecoderState FLAC__file_decoder_init(FLAC__FileDecoder *decoder)
+{
+       FLAC__ASSERT(0 != decoder);
+
+       if(decoder->protected_->state != FLAC__FILE_DECODER_UNINITIALIZED)
+               return decoder->protected_->state = FLAC__FILE_DECODER_ALREADY_INITIALIZED;
+
+       if(0 == decoder->private_->write_callback || 0 == decoder->private_->metadata_callback || 0 == decoder->private_->error_callback)
+               return decoder->protected_->state = FLAC__FILE_DECODER_INVALID_CALLBACK;
+
+       if(0 == decoder->private_->filename)
+               decoder->private_->file = get_binary_stdin_();
        else
-               decoder->guts->file = fopen(filename, "rb");
-       if(decoder->guts->file == 0)
-               return decoder->state = FLAC__FILE_DECODER_ERROR_OPENING_FILE;
-
-       /* We initialize the MD5Context even though we may never use it.  This is
-        * because check_md5 may be turned on to start and then turned off if a
-        * seek occurs.  So we always init the context here and finalize it in
-        * FLAC__file_decoder_finish() to make sure things are always cleaned up
-        *properly.
-        */
-       MD5Init(&decoder->guts->md5context);
+               decoder->private_->file = fopen(decoder->private_->filename, "rb");
+
+       if(decoder->private_->file == 0)
+               return decoder->protected_->state = FLAC__FILE_DECODER_ERROR_OPENING_FILE;
 
-       decoder->guts->stream = FLAC__stream_decoder_get_new_instance();
-       if(FLAC__stream_decoder_init(decoder->guts->stream, read_callback_, write_callback_, metadata_callback_, error_callback_, decoder) != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA)
-               return decoder->state = FLAC__FILE_DECODER_MEMORY_ALLOCATION_ERROR; /* this is based on internal knowledge of FLAC__stream_decoder_init() */
+       FLAC__seekable_stream_decoder_set_read_callback(decoder->private_->seekable_stream_decoder, read_callback_);
+       FLAC__seekable_stream_decoder_set_seek_callback(decoder->private_->seekable_stream_decoder, seek_callback_);
+       FLAC__seekable_stream_decoder_set_tell_callback(decoder->private_->seekable_stream_decoder, tell_callback_);
+       FLAC__seekable_stream_decoder_set_length_callback(decoder->private_->seekable_stream_decoder, length_callback_);
+       FLAC__seekable_stream_decoder_set_eof_callback(decoder->private_->seekable_stream_decoder, eof_callback_);
+       FLAC__seekable_stream_decoder_set_write_callback(decoder->private_->seekable_stream_decoder, write_callback_);
+       FLAC__seekable_stream_decoder_set_metadata_callback(decoder->private_->seekable_stream_decoder, metadata_callback_);
+       FLAC__seekable_stream_decoder_set_error_callback(decoder->private_->seekable_stream_decoder, error_callback_);
+       FLAC__seekable_stream_decoder_set_client_data(decoder->private_->seekable_stream_decoder, decoder);
 
-       return decoder->state;
+       if(FLAC__seekable_stream_decoder_init(decoder->private_->seekable_stream_decoder) != FLAC__SEEKABLE_STREAM_DECODER_OK)
+               return decoder->protected_->state = FLAC__FILE_DECODER_SEEKABLE_STREAM_DECODER_ERROR;
+
+       return decoder->protected_->state = FLAC__FILE_DECODER_OK;
 }
 
-bool FLAC__file_decoder_finish(FLAC__FileDecoder *decoder)
+FLAC_API FLAC__bool FLAC__file_decoder_finish(FLAC__FileDecoder *decoder)
 {
-       bool md5_failed = false;
+       FLAC__ASSERT(0 != decoder);
 
-       assert(decoder != 0);
-       if(decoder->state == FLAC__FILE_DECODER_UNINITIALIZED)
+       if(decoder->protected_->state == FLAC__FILE_DECODER_UNINITIALIZED)
                return true;
-       if(decoder->guts != 0) {
-               if(decoder->guts->file != 0 && decoder->guts->file != stdin)
-                       fclose(decoder->guts->file);
-               /* see the comment in FLAC__file_decoder_init() as to why we always
-                * call MD5Final()
-                */
-               MD5Final(decoder->guts->computed_md5sum, &decoder->guts->md5context);
-               if(decoder->guts->stream != 0) {
-                       FLAC__stream_decoder_finish(decoder->guts->stream);
-                       FLAC__stream_decoder_free_instance(decoder->guts->stream);
-               }
-               if(decoder->check_md5) {
-                       if(memcmp(decoder->guts->stored_md5sum, decoder->guts->computed_md5sum, 16))
-                               md5_failed = true;
+
+       FLAC__ASSERT(0 != decoder->private_->seekable_stream_decoder);
+
+       if(0 != decoder->private_->file && decoder->private_->file != stdin) {
+               fclose(decoder->private_->file);
+               decoder->private_->file = 0;
+       }
+
+       if(0 != decoder->private_->filename) {
+               free(decoder->private_->filename);
+               decoder->private_->filename = 0;
+       }
+
+       set_defaults_(decoder);
+
+       decoder->protected_->state = FLAC__FILE_DECODER_UNINITIALIZED;
+
+       return FLAC__seekable_stream_decoder_finish(decoder->private_->seekable_stream_decoder);
+}
+
+FLAC_API FLAC__bool FLAC__file_decoder_set_md5_checking(FLAC__FileDecoder *decoder, FLAC__bool value)
+{
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->private_);
+       FLAC__ASSERT(0 != decoder->protected_);
+       FLAC__ASSERT(0 != decoder->private_->seekable_stream_decoder);
+       if(decoder->protected_->state != FLAC__FILE_DECODER_UNINITIALIZED)
+               return false;
+       return FLAC__seekable_stream_decoder_set_md5_checking(decoder->private_->seekable_stream_decoder, value);
+}
+
+FLAC_API FLAC__bool FLAC__file_decoder_set_filename(FLAC__FileDecoder *decoder, const char *value)
+{
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->private_);
+       FLAC__ASSERT(0 != decoder->protected_);
+       FLAC__ASSERT(0 != value);
+       if(decoder->protected_->state != FLAC__FILE_DECODER_UNINITIALIZED)
+               return false;
+       if(0 != decoder->private_->filename) {
+               free(decoder->private_->filename);
+               decoder->private_->filename = 0;
+       }
+       if(0 != strcmp(value, "-")) {
+               if(0 == (decoder->private_->filename = (char*)malloc(strlen(value)+1))) {
+                       decoder->protected_->state = FLAC__FILE_DECODER_MEMORY_ALLOCATION_ERROR;
+                       return false;
                }
-               free(decoder->guts);
-               decoder->guts = 0;
+               strcpy(decoder->private_->filename, value);
        }
-       decoder->state = FLAC__FILE_DECODER_UNINITIALIZED;
-       return !md5_failed;
+       return true;
 }
 
-bool FLAC__file_decoder_process_whole_file(FLAC__FileDecoder *decoder)
+FLAC_API FLAC__bool FLAC__file_decoder_set_write_callback(FLAC__FileDecoder *decoder, FLAC__FileDecoderWriteCallback value)
 {
-       bool ret;
-       assert(decoder != 0);
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->private_);
+       FLAC__ASSERT(0 != decoder->protected_);
+       if(decoder->protected_->state != FLAC__FILE_DECODER_UNINITIALIZED)
+               return false;
+       decoder->private_->write_callback = value;
+       return true;
+}
 
-       if(decoder->state == FLAC__FILE_DECODER_END_OF_FILE)
-               return true;
+FLAC_API FLAC__bool FLAC__file_decoder_set_metadata_callback(FLAC__FileDecoder *decoder, FLAC__FileDecoderMetadataCallback value)
+{
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->private_);
+       FLAC__ASSERT(0 != decoder->protected_);
+       if(decoder->protected_->state != FLAC__FILE_DECODER_UNINITIALIZED)
+               return false;
+       decoder->private_->metadata_callback = value;
+       return true;
+}
 
-       assert(decoder->state == FLAC__FILE_DECODER_OK);
+FLAC_API FLAC__bool FLAC__file_decoder_set_error_callback(FLAC__FileDecoder *decoder, FLAC__FileDecoderErrorCallback value)
+{
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->private_);
+       FLAC__ASSERT(0 != decoder->protected_);
+       if(decoder->protected_->state != FLAC__FILE_DECODER_UNINITIALIZED)
+               return false;
+       decoder->private_->error_callback = value;
+       return true;
+}
 
-       ret = FLAC__stream_decoder_process_whole_stream(decoder->guts->stream);
-       if(!ret)
-               decoder->state = FLAC__FILE_DECODER_STREAM_ERROR;
+FLAC_API FLAC__bool FLAC__file_decoder_set_client_data(FLAC__FileDecoder *decoder, void *value)
+{
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->private_);
+       FLAC__ASSERT(0 != decoder->protected_);
+       if(decoder->protected_->state != FLAC__FILE_DECODER_UNINITIALIZED)
+               return false;
+       decoder->private_->client_data = value;
+       return true;
+}
 
-       return ret;
+FLAC_API FLAC__bool FLAC__file_decoder_set_metadata_respond(FLAC__FileDecoder *decoder, FLAC__MetadataType type)
+{
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->private_);
+       FLAC__ASSERT(0 != decoder->protected_);
+       FLAC__ASSERT(0 != decoder->private_->seekable_stream_decoder);
+       if(decoder->protected_->state != FLAC__FILE_DECODER_UNINITIALIZED)
+               return false;
+       return FLAC__seekable_stream_decoder_set_metadata_respond(decoder->private_->seekable_stream_decoder, type);
 }
 
-bool FLAC__file_decoder_process_metadata(FLAC__FileDecoder *decoder)
+FLAC_API FLAC__bool FLAC__file_decoder_set_metadata_respond_application(FLAC__FileDecoder *decoder, const FLAC__byte id[4])
 {
-       bool ret;
-       assert(decoder != 0);
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->private_);
+       FLAC__ASSERT(0 != decoder->protected_);
+       FLAC__ASSERT(0 != decoder->private_->seekable_stream_decoder);
+       if(decoder->protected_->state != FLAC__FILE_DECODER_UNINITIALIZED)
+               return false;
+       return FLAC__seekable_stream_decoder_set_metadata_respond_application(decoder->private_->seekable_stream_decoder, id);
+}
 
-       if(decoder->state == FLAC__FILE_DECODER_END_OF_FILE)
-               return true;
+FLAC_API FLAC__bool FLAC__file_decoder_set_metadata_respond_all(FLAC__FileDecoder *decoder)
+{
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->private_);
+       FLAC__ASSERT(0 != decoder->protected_);
+       FLAC__ASSERT(0 != decoder->private_->seekable_stream_decoder);
+       if(decoder->protected_->state != FLAC__FILE_DECODER_UNINITIALIZED)
+               return false;
+       return FLAC__seekable_stream_decoder_set_metadata_respond_all(decoder->private_->seekable_stream_decoder);
+}
 
-       assert(decoder->state == FLAC__FILE_DECODER_OK);
+FLAC_API FLAC__bool FLAC__file_decoder_set_metadata_ignore(FLAC__FileDecoder *decoder, FLAC__MetadataType type)
+{
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->private_);
+       FLAC__ASSERT(0 != decoder->protected_);
+       FLAC__ASSERT(0 != decoder->private_->seekable_stream_decoder);
+       if(decoder->protected_->state != FLAC__FILE_DECODER_UNINITIALIZED)
+               return false;
+       return FLAC__seekable_stream_decoder_set_metadata_ignore(decoder->private_->seekable_stream_decoder, type);
+}
 
-       ret = FLAC__stream_decoder_process_metadata(decoder->guts->stream);
-       if(!ret)
-               decoder->state = FLAC__FILE_DECODER_STREAM_ERROR;
+FLAC_API FLAC__bool FLAC__file_decoder_set_metadata_ignore_application(FLAC__FileDecoder *decoder, const FLAC__byte id[4])
+{
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->private_);
+       FLAC__ASSERT(0 != decoder->protected_);
+       FLAC__ASSERT(0 != decoder->private_->seekable_stream_decoder);
+       if(decoder->protected_->state != FLAC__FILE_DECODER_UNINITIALIZED)
+               return false;
+       return FLAC__seekable_stream_decoder_set_metadata_ignore_application(decoder->private_->seekable_stream_decoder, id);
+}
 
-       return ret;
+FLAC_API FLAC__bool FLAC__file_decoder_set_metadata_ignore_all(FLAC__FileDecoder *decoder)
+{
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->private_);
+       FLAC__ASSERT(0 != decoder->protected_);
+       FLAC__ASSERT(0 != decoder->private_->seekable_stream_decoder);
+       if(decoder->protected_->state != FLAC__FILE_DECODER_UNINITIALIZED)
+               return false;
+       return FLAC__seekable_stream_decoder_set_metadata_ignore_all(decoder->private_->seekable_stream_decoder);
+}
+
+FLAC_API FLAC__FileDecoderState FLAC__file_decoder_get_state(const FLAC__FileDecoder *decoder)
+{
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->protected_);
+       return decoder->protected_->state;
+}
+
+FLAC_API FLAC__SeekableStreamDecoderState FLAC__file_decoder_get_seekable_stream_decoder_state(const FLAC__FileDecoder *decoder)
+{
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->private_);
+       return FLAC__seekable_stream_decoder_get_state(decoder->private_->seekable_stream_decoder);
+}
+
+FLAC_API FLAC__StreamDecoderState FLAC__file_decoder_get_stream_decoder_state(const FLAC__FileDecoder *decoder)
+{
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->private_);
+       return FLAC__seekable_stream_decoder_get_stream_decoder_state(decoder->private_->seekable_stream_decoder);
+}
+
+FLAC_API const char *FLAC__file_decoder_get_resolved_state_string(const FLAC__FileDecoder *decoder)
+{
+       if(decoder->protected_->state != FLAC__FILE_DECODER_SEEKABLE_STREAM_DECODER_ERROR)
+               return FLAC__FileDecoderStateString[decoder->protected_->state];
+       else
+               return FLAC__seekable_stream_decoder_get_resolved_state_string(decoder->private_->seekable_stream_decoder);
+}
+
+FLAC_API FLAC__bool FLAC__file_decoder_get_md5_checking(const FLAC__FileDecoder *decoder)
+{
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->private_);
+       return FLAC__seekable_stream_decoder_get_md5_checking(decoder->private_->seekable_stream_decoder);
+}
+
+FLAC_API unsigned FLAC__file_decoder_get_channels(const FLAC__FileDecoder *decoder)
+{
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->private_);
+       return FLAC__seekable_stream_decoder_get_channels(decoder->private_->seekable_stream_decoder);
+}
+
+FLAC_API FLAC__ChannelAssignment FLAC__file_decoder_get_channel_assignment(const FLAC__FileDecoder *decoder)
+{
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->private_);
+       return FLAC__seekable_stream_decoder_get_channel_assignment(decoder->private_->seekable_stream_decoder);
+}
+
+FLAC_API unsigned FLAC__file_decoder_get_bits_per_sample(const FLAC__FileDecoder *decoder)
+{
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->private_);
+       return FLAC__seekable_stream_decoder_get_bits_per_sample(decoder->private_->seekable_stream_decoder);
+}
+
+FLAC_API unsigned FLAC__file_decoder_get_sample_rate(const FLAC__FileDecoder *decoder)
+{
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->private_);
+       return FLAC__seekable_stream_decoder_get_sample_rate(decoder->private_->seekable_stream_decoder);
+}
+
+FLAC_API unsigned FLAC__file_decoder_get_blocksize(const FLAC__FileDecoder *decoder)
+{
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->private_);
+       return FLAC__seekable_stream_decoder_get_blocksize(decoder->private_->seekable_stream_decoder);
+}
+
+FLAC_API FLAC__bool FLAC__file_decoder_get_decode_position(const FLAC__FileDecoder *decoder, FLAC__uint64 *position)
+{
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->private_);
+       return FLAC__seekable_stream_decoder_get_decode_position(decoder->private_->seekable_stream_decoder, position);
 }
 
-bool FLAC__file_decoder_process_one_frame(FLAC__FileDecoder *decoder)
+FLAC_API FLAC__bool FLAC__file_decoder_process_single(FLAC__FileDecoder *decoder)
 {
-       bool ret;
-       assert(decoder != 0);
+       FLAC__bool ret;
+       FLAC__ASSERT(0 != decoder);
 
-       if(decoder->state == FLAC__FILE_DECODER_END_OF_FILE)
+       if(decoder->private_->seekable_stream_decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM)
+               decoder->protected_->state = FLAC__FILE_DECODER_END_OF_FILE;
+
+       if(decoder->protected_->state == FLAC__FILE_DECODER_END_OF_FILE)
                return true;
 
-       assert(decoder->state == FLAC__FILE_DECODER_OK);
+       FLAC__ASSERT(decoder->protected_->state == FLAC__FILE_DECODER_OK);
 
-       ret = FLAC__stream_decoder_process_one_frame(decoder->guts->stream);
+       ret = FLAC__seekable_stream_decoder_process_single(decoder->private_->seekable_stream_decoder);
        if(!ret)
-               decoder->state = FLAC__FILE_DECODER_STREAM_ERROR;
+               decoder->protected_->state = FLAC__FILE_DECODER_SEEKABLE_STREAM_DECODER_ERROR;
 
        return ret;
 }
 
-bool FLAC__file_decoder_process_remaining_frames(FLAC__FileDecoder *decoder)
+FLAC_API FLAC__bool FLAC__file_decoder_process_until_end_of_metadata(FLAC__FileDecoder *decoder)
 {
-       bool ret;
-       assert(decoder != 0);
+       FLAC__bool ret;
+       FLAC__ASSERT(0 != decoder);
+
+       if(decoder->private_->seekable_stream_decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM)
+               decoder->protected_->state = FLAC__FILE_DECODER_END_OF_FILE;
 
-       if(decoder->state == FLAC__FILE_DECODER_END_OF_FILE)
+       if(decoder->protected_->state == FLAC__FILE_DECODER_END_OF_FILE)
                return true;
 
-       assert(decoder->state == FLAC__FILE_DECODER_OK);
+       FLAC__ASSERT(decoder->protected_->state == FLAC__FILE_DECODER_OK);
 
-       ret = FLAC__stream_decoder_process_remaining_frames(decoder->guts->stream);
+       ret = FLAC__seekable_stream_decoder_process_until_end_of_metadata(decoder->private_->seekable_stream_decoder);
        if(!ret)
-               decoder->state = FLAC__FILE_DECODER_STREAM_ERROR;
+               decoder->protected_->state = FLAC__FILE_DECODER_SEEKABLE_STREAM_DECODER_ERROR;
 
        return ret;
 }
 
-bool FLAC__file_decoder_seek_absolute(FLAC__FileDecoder *decoder, uint64 sample)
+FLAC_API FLAC__bool FLAC__file_decoder_process_until_end_of_file(FLAC__FileDecoder *decoder)
 {
-       long filesize;
+       FLAC__bool ret;
+       FLAC__ASSERT(0 != decoder);
 
-       assert(decoder != 0);
-       assert(decoder->state == FLAC__FILE_DECODER_OK);
+       if(decoder->private_->seekable_stream_decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM)
+               decoder->protected_->state = FLAC__FILE_DECODER_END_OF_FILE;
 
-       decoder->state = FLAC__FILE_DECODER_SEEKING;
+       if(decoder->protected_->state == FLAC__FILE_DECODER_END_OF_FILE)
+               return true;
 
-       /* turn off md5 checking if a seek is attempted */
-       decoder->check_md5 = false;
+       FLAC__ASSERT(decoder->protected_->state == FLAC__FILE_DECODER_OK);
 
-       if(!FLAC__stream_decoder_reset(decoder->guts->stream)) {
-               decoder->state = FLAC__FILE_DECODER_STREAM_ERROR;
-               return false;
-       }
-       /* get the file length */
-       if(0 != fseek(decoder->guts->file, 0, SEEK_END)) {
-               decoder->state = FLAC__FILE_DECODER_SEEK_ERROR;
-               return false;
-       }
-       fflush(decoder->guts->file);
-       if(-1 == (filesize = ftell(decoder->guts->file))) {
-               decoder->state = FLAC__FILE_DECODER_SEEK_ERROR;
-               return false;
-       }
-       /* rewind */
-       if(0 != fseek(decoder->guts->file, 0, SEEK_SET)) {
-               decoder->state = FLAC__FILE_DECODER_SEEK_ERROR;
+       ret = FLAC__seekable_stream_decoder_process_until_end_of_stream(decoder->private_->seekable_stream_decoder);
+       if(!ret)
+               decoder->protected_->state = FLAC__FILE_DECODER_SEEKABLE_STREAM_DECODER_ERROR;
+
+       return ret;
+}
+
+FLAC_API FLAC__bool FLAC__file_decoder_seek_absolute(FLAC__FileDecoder *decoder, FLAC__uint64 sample)
+{
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(decoder->protected_->state == FLAC__FILE_DECODER_OK || decoder->protected_->state == FLAC__FILE_DECODER_END_OF_FILE);
+
+       if(decoder->private_->filename == 0) { /* means the file is stdin... */
+               decoder->protected_->state = FLAC__FILE_DECODER_SEEK_ERROR;
                return false;
        }
-       if(!FLAC__stream_decoder_process_metadata(decoder->guts->stream)) {
-               decoder->state = FLAC__FILE_DECODER_STREAM_ERROR;
+
+       if(!FLAC__seekable_stream_decoder_seek_absolute(decoder->private_->seekable_stream_decoder, sample)) {
+               decoder->protected_->state = FLAC__FILE_DECODER_SEEK_ERROR;
                return false;
        }
-       if(sample > decoder->guts->metadata.total_samples) {
-               decoder->state = FLAC__FILE_DECODER_SEEK_ERROR;
-               return false;
+       else {
+               decoder->protected_->state = FLAC__FILE_DECODER_OK;
+               return true;
        }
+}
+
 
-       return seek_to_absolute_sample_(decoder, filesize, sample);
+/***********************************************************************
+ *
+ * Private class methods
+ *
+ ***********************************************************************/
+
+void set_defaults_(FLAC__FileDecoder *decoder)
+{
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->private_);
+
+       decoder->private_->filename = 0;
+       decoder->private_->write_callback = 0;
+       decoder->private_->metadata_callback = 0;
+       decoder->private_->error_callback = 0;
+       decoder->private_->client_data = 0;
 }
 
-FLAC__StreamDecoderReadStatus read_callback_(const FLAC__StreamDecoder *decoder, byte buffer[], unsigned *bytes, void *client_data)
+/*
+ * This will forcibly set stdin to binary mode (for OSes that require it)
+ */
+FILE *get_binary_stdin_()
+{
+       /* if something breaks here it is probably due to the presence or
+        * absence of an underscore before the identifiers 'setmode',
+        * 'fileno', and/or 'O_BINARY'; check your system header files.
+        */
+#if defined _MSC_VER || defined __MINGW32__
+       _setmode(_fileno(stdin), _O_BINARY);
+#elif defined __CYGWIN__
+       /* almost certainly not needed for any modern Cygwin, but let's be safe... */
+       setmode(_fileno(stdin), _O_BINARY);
+#endif
+
+       return stdin;
+}
+
+FLAC__SeekableStreamDecoderReadStatus read_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
 {
        FLAC__FileDecoder *file_decoder = (FLAC__FileDecoder *)client_data;
        (void)decoder;
-       if(feof(file_decoder->guts->file)) {
-               file_decoder->state = FLAC__FILE_DECODER_END_OF_FILE;
-               return FLAC__STREAM_DECODER_READ_END_OF_STREAM;
-       }
-       else if(*bytes > 0) {
-               size_t bytes_read = fread(buffer, sizeof(byte), *bytes, file_decoder->guts->file);
-               if(bytes_read == 0) {
-                       if(feof(file_decoder->guts->file)) {
-                               file_decoder->state = FLAC__FILE_DECODER_END_OF_FILE;
-                               return FLAC__STREAM_DECODER_READ_END_OF_STREAM;
-                       }
-                       else
-                               return FLAC__STREAM_DECODER_READ_ABORT;
+
+       if(*bytes > 0) {
+               size_t bytes_read = fread(buffer, sizeof(FLAC__byte), *bytes, file_decoder->private_->file);
+               if(bytes_read == 0 && !feof(file_decoder->private_->file)) {
+                       return FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_ERROR;
                }
                else {
                        *bytes = (unsigned)bytes_read;
-                       return FLAC__STREAM_DECODER_READ_CONTINUE;
+                       return FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_OK;
                }
        }
        else
-               return FLAC__STREAM_DECODER_READ_ABORT; /* abort to avoid a deadlock */
+               return FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_ERROR; /* abort to avoid a deadlock */
 }
 
-FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const int32 *buffer[], void *client_data)
+FLAC__SeekableStreamDecoderSeekStatus seek_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data)
 {
        FLAC__FileDecoder *file_decoder = (FLAC__FileDecoder *)client_data;
        (void)decoder;
 
-       if(file_decoder->state == FLAC__FILE_DECODER_SEEKING) {
-               uint64 this_frame_sample = frame->header.number.sample_number;
-               uint64 next_frame_sample = this_frame_sample + (uint64)frame->header.blocksize;
-               uint64 target_sample = file_decoder->guts->target_sample;
-
-               file_decoder->guts->last_frame = *frame; /* save the frame in the guts */
-               if(this_frame_sample <= target_sample && target_sample < next_frame_sample) { /* we hit our target frame */
-                       unsigned delta = (unsigned)(target_sample - this_frame_sample);
-                       /* kick out of seek mode */
-                       file_decoder->state = FLAC__FILE_DECODER_OK;
-                       /* shift out the samples before target_sample */
-                       if(delta > 0) {
-                               unsigned channel;
-                               const int32 *newbuffer[FLAC__MAX_CHANNELS];
-                               for(channel = 0; channel < frame->header.channels; channel++)
-                                       newbuffer[channel] = buffer[channel] + delta;
-                               file_decoder->guts->last_frame.header.blocksize -= delta;
-                               file_decoder->guts->last_frame.header.number.sample_number += (uint64)delta;
-                               /* write the relevant samples */
-                               return file_decoder->guts->write_callback(file_decoder, &file_decoder->guts->last_frame, newbuffer, file_decoder->guts->client_data);
-                       }
-                       else {
-                               /* write the relevant samples */
-                               return file_decoder->guts->write_callback(file_decoder, frame, buffer, file_decoder->guts->client_data);
-                       }
-               }
-               else {
-                       return FLAC__STREAM_DECODER_WRITE_CONTINUE;
-               }
-       }
+       if(fseek(file_decoder->private_->file, (long)absolute_byte_offset, SEEK_SET) < 0)
+               return FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_ERROR;
+       else
+               return FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK;
+}
+
+FLAC__SeekableStreamDecoderTellStatus tell_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
+{
+       FLAC__FileDecoder *file_decoder = (FLAC__FileDecoder *)client_data;
+       long pos;
+       (void)decoder;
+
+       if((pos = ftell(file_decoder->private_->file)) < 0)
+               return FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_ERROR;
        else {
-               if(file_decoder->check_md5) {
-                       if(!FLAC__MD5Accumulate(&file_decoder->guts->md5context, buffer, frame->header.channels, frame->header.blocksize, (frame->header.bits_per_sample+7) / 8))
-                               return FLAC__STREAM_DECODER_WRITE_ABORT;
-               }
-               return file_decoder->guts->write_callback(file_decoder, frame, buffer, file_decoder->guts->client_data);
+               *absolute_byte_offset = (FLAC__uint64)pos;
+               return FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK;
        }
 }
 
-void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data)
+FLAC__SeekableStreamDecoderLengthStatus length_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data)
 {
        FLAC__FileDecoder *file_decoder = (FLAC__FileDecoder *)client_data;
+       struct stat filestats;
        (void)decoder;
 
-       if(metadata->type == FLAC__METADATA_TYPE_ENCODING) {
-               file_decoder->guts->metadata = metadata->data.encoding;
-               /* save the MD5 signature for comparison later */
-               memcpy(file_decoder->guts->stored_md5sum, metadata->data.encoding.md5sum, 16);
-               if(0 == memcmp(file_decoder->guts->stored_md5sum, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16))
-                       file_decoder->check_md5 = false;
+       if(0 == file_decoder->private_->filename || stat(file_decoder->private_->filename, &filestats) != 0)
+               return FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_ERROR;
+       else {
+               *stream_length = (FLAC__uint64)filestats.st_size;
+               return FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_OK;
        }
-       if(file_decoder->state != FLAC__FILE_DECODER_SEEKING)
-               file_decoder->guts->metadata_callback(file_decoder, metadata, file_decoder->guts->client_data);
 }
 
-void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
+FLAC__bool eof_callback_(const FLAC__SeekableStreamDecoder *decoder, void *client_data)
 {
        FLAC__FileDecoder *file_decoder = (FLAC__FileDecoder *)client_data;
        (void)decoder;
 
-       if(file_decoder->state != FLAC__FILE_DECODER_SEEKING)
-               file_decoder->guts->error_callback(file_decoder, status, file_decoder->guts->client_data);
+       return feof(file_decoder->private_->file);
 }
 
-bool seek_to_absolute_sample_(FLAC__FileDecoder *decoder, long filesize, uint64 target_sample)
+FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__SeekableStreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
 {
-       long l, r, pos, last_pos = -1;
-       unsigned approx_bytes_per_frame;
-       uint64 last_frame_sample = 0xffffffffffffffff;
-       bool needs_seek;
-       const bool is_variable_blocksize_stream = (decoder->guts->metadata.min_blocksize != decoder->guts->metadata.max_blocksize);
+       FLAC__FileDecoder *file_decoder = (FLAC__FileDecoder *)client_data;
+       (void)decoder;
 
-       if(!is_variable_blocksize_stream) {
-               /* we are just guessing here, but we want to guess high, not low */
-               /* note there are no () around 'decoder->guts->metadata.bits_per_sample/8' to keep precision up since it's an integer calulation */
-               approx_bytes_per_frame = decoder->guts->metadata.min_blocksize * decoder->guts->metadata.channels * decoder->guts->metadata.bits_per_sample/8 + 64;
-       }
-       else
-               approx_bytes_per_frame = 1152 * decoder->guts->metadata.channels * decoder->guts->metadata.bits_per_sample/8 + 64;
+       return file_decoder->private_->write_callback(file_decoder, frame, buffer, file_decoder->private_->client_data);
+}
 
-       /* Now we need to use the metadata and the filelength to search to the frame with the correct sample */
-       if(-1 == (l = ftell(decoder->guts->file))) {
-               decoder->state = FLAC__FILE_DECODER_SEEK_ERROR;
-               return false;
-       }
-       l -= FLAC__stream_decoder_input_bytes_unconsumed(decoder->guts->stream);
-#ifdef _MSC_VER
-       /* with VC++ you have to spoon feed it the casting */
-       pos = l + (long)((double)(int64)target_sample / (double)(int64)decoder->guts->metadata.total_samples * (double)(filesize-l+1)) - approx_bytes_per_frame;
-#else
-       pos = l + (long)((double)target_sample / (double)decoder->guts->metadata.total_samples * (double)(filesize-l+1)) - approx_bytes_per_frame;
-#endif
-       r = filesize - ((decoder->guts->metadata.channels * decoder->guts->metadata.bits_per_sample * FLAC__MAX_BLOCK_SIZE) / 8 + 64);
-       if(pos >= r)
-               pos = r-1;
-       if(pos < l)
-               pos = l;
-       needs_seek = true;
-
-       decoder->guts->target_sample = target_sample;
-       while(1) {
-               if(needs_seek) {
-                       if(-1 == fseek(decoder->guts->file, pos, SEEK_SET)) {
-                               decoder->state = FLAC__FILE_DECODER_SEEK_ERROR;
-                               return false;
-                       }
-                       if(!FLAC__stream_decoder_flush(decoder->guts->stream)) {
-                               decoder->state = FLAC__FILE_DECODER_STREAM_ERROR;
-                               return false;
-                       }
-               }
-               if(!FLAC__stream_decoder_process_one_frame(decoder->guts->stream)) {
-                       decoder->state = FLAC__FILE_DECODER_SEEK_ERROR;
-                       return false;
-               }
-               /* our write callback will change the state when it gets to the target frame */
-               if(decoder->state != FLAC__FILE_DECODER_SEEKING) {
-                       break;
-               }
-               else { /* we need to narrow the search */
-                       uint64 this_frame_sample = decoder->guts->last_frame.header.number.sample_number;
-                       if(this_frame_sample == last_frame_sample) {
-                               /* our last move backwards wasn't big enough */
-                               pos -= (last_pos - pos);
-                               needs_seek = true;
-                       }
-                       else {
-                               if(target_sample < this_frame_sample) {
-                                       last_pos = pos;
-                                       approx_bytes_per_frame = decoder->guts->last_frame.header.blocksize * decoder->guts->last_frame.header.channels * decoder->guts->last_frame.header.bits_per_sample/8 + 64;
-                                       pos -= approx_bytes_per_frame;
-                                       needs_seek = true;
-                               }
-                               else {
-                                       last_pos = pos;
-                                       if(-1 == (pos = ftell(decoder->guts->file))) {
-                                               decoder->state = FLAC__FILE_DECODER_SEEK_ERROR;
-                                               return false;
-                                       }
-                                       pos -= FLAC__stream_decoder_input_bytes_unconsumed(decoder->guts->stream);
-                                       needs_seek = false;
-                               }
-                       }
-                       if(pos < l)
-                               pos = l;
-                       last_frame_sample = this_frame_sample;
-               }
-       }
+void metadata_callback_(const FLAC__SeekableStreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
+{
+       FLAC__FileDecoder *file_decoder = (FLAC__FileDecoder *)client_data;
+       (void)decoder;
 
-       return true;
+       file_decoder->private_->metadata_callback(file_decoder, metadata, file_decoder->private_->client_data);
+}
+
+void error_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
+{
+       FLAC__FileDecoder *file_decoder = (FLAC__FileDecoder *)client_data;
+       (void)decoder;
+
+       file_decoder->private_->error_callback(file_decoder, status, file_decoder->private_->client_data);
 }