rename FLAC__Encoder to FLAC__StreamEncoder, OOPize encoder and decoder interfaces
authorJosh Coalson <jcoalson@users.sourceforce.net>
Wed, 13 Jun 2001 17:59:57 +0000 (17:59 +0000)
committerJosh Coalson <jcoalson@users.sourceforce.net>
Wed, 13 Jun 2001 17:59:57 +0000 (17:59 +0000)
21 files changed:
doc/documentation.html
include/FLAC/Makefile.am
include/FLAC/all.h
include/FLAC/file_decoder.h
include/FLAC/stream_decoder.h
include/FLAC/stream_encoder.h
src/flac/decode.c
src/flac/encode.c
src/flac/main.c
src/libFLAC/Makefile.am
src/libFLAC/Makefile.lite
src/libFLAC/Makefile.vc
src/libFLAC/file_decoder.c
src/libFLAC/include/private/Makefile.am
src/libFLAC/include/private/all.h
src/libFLAC/include/private/stream_encoder_framing.h
src/libFLAC/include/protected/Makefile.am
src/libFLAC/include/protected/stream_decoder.h
src/libFLAC/stream_decoder.c
src/libFLAC/stream_encoder.c
src/libFLAC/stream_encoder_framing.c

index 5b480e6..573a379 100644 (file)
                The basic usage of <B><TT>libFLAC</TT></B> is as follows:
                <OL>
                        <LI>The program creates an instance of a decoder or encoder.</LI>
-                       <LI>The program initialized the instance and provides <B><TT>libFLAC</TT></B> with callbacks for reading, writing, error reporting, and metadata reporting.</LI>
-                       <LI>The program calls <B><TT>libFLAC</TT></B> to encode or decode data, which subsequently calls the callbacks.</LI>
-                       <LI>The program finalizes the instance, which flushes the input and output.</LI>
+                       <LI>The program initializes the instance and provides <B><TT>libFLAC</TT></B> with callbacks for reading, writing, error reporting, and metadata reporting.</LI>
+                       <LI>The program calls <B><TT>libFLAC</TT></B> functions to encode or decode data, which subsequently calls the callbacks.</LI>
+                       <LI>The program finishes the instance, which flushes the input and output.</LI>
                </OL>
        </P>
        <P>
-               For decoding, <B><TT>libFLAC</TT></B> provides two layers of access.  The lowest layer is stream-level decoding, and the highest level is file-level decoding, which is a wrapper around the stream decoder.  The interfaces are described in <TT>stream_decoder.h</TT> and <TT>file_decoder.h</TT> respectively.  The file decoder supplies the read callback internally and provides seek functions.  Currently there is only one level of encoder implementation which is at the stream level (<TT>encoder.h</TT>).  Structures and constants related to the format are defined in <TT>format.h</TT>.
+               For decoding, <B><TT>libFLAC</TT></B> provides two layers of access.  The lowest layer is stream-level decoding, and the highest level is file-level decoding, which is a wrapper around the stream decoder.  The interfaces are described in <TT>stream_decoder.h</TT> and <TT>file_decoder.h</TT> respectively.  The file decoder supplies the read callback internally and provides seek functions.  Currently there is only one level of encoder implementation which is at the stream level (<TT>stream_encoder.h</TT>).  There is no currently no file encoder because seeking within a file while encoding seemed like too obscure a feature.
+       </P>
+       <P>
+               Structures and constants related to the format are defined in <TT>format.h</TT>.
        </P>
        <P>
                <B>STREAM DECODER</B>
        </P>
        <P>
-               First we discuss the stream decoder.  The instance type is <TT>FLAC__StreamDecoder</TT>.  Typically the program will create a new instance by calling <TT>FLAC__stream_decoder_get_new_instance()</TT>, then call <TT>FLAC__stream_decoder_init()</TT> with the addresses of the required callbacks.  The program can also supply a client_data pointer to <TT>FLAC__stream_decoder_init()</TT> which will be included when calling the callbacks.
+               First we discuss the stream decoder.  The instance type is <TT>FLAC__StreamDecoder</TT>.  Typically the program will create a new instance by calling <TT>FLAC__stream_decoder_new()</TT>, then call <TT>FLAC__stream_decoder_init()</TT> with the addresses of the required callbacks.  The program can also supply a client_data pointer to <TT>FLAC__stream_decoder_init()</TT> which will be included when calling the callbacks.
                <UL>
-                       <LI>Read callback - This function will be called when the decoder needs more input data.  The address of the buffer to be filled is supplied, along with the number of bytes the buffer can hold.  The callback may choose to supply less data and modify the byte count but must be careful not to overflow the buffer.  The callback then returns a status code chosen from FLAC__StreamDecoderReadStatusString[].</LI>
+                       <LI>Read callback - This function will be called when the decoder needs more input data.  The address of the buffer to be filled is supplied, along with the number of bytes the buffer can hold.  The callback may choose to supply less data and modify the byte count but must be careful not to overflow the buffer.  The callback then returns a status code chosen from FLAC__StreamDecoderReadStatus.</LI>
                        <LI>Write callback - This function will be called when the decoder has decoded a single frame of data.  The decoder will pass the frame metadata as well as an array of pointers (one for each channel) pointing to the decoded audio.</LI>
                        <LI>Metadata callback - This function will be called when the decoder has decoded a metadata block.  There will always be one STREAMINFO block per stream, followed by zero or more other metadata blocks.  These will be supplied by the decoder in the same order as they appear in the stream and always before the first audio frame.</LI>
-                       <LI>Error callback - This function will be called whenever an error is encountered.</LI>
+                       <LI>Error callback - This function will be called whenever an error occurs during decoding.</LI>
                </UL>
        </P>
        <P>
                </UL>
        </P>
        <P>
-               When the decoder has finished decoding (normally or through an abort), the instance is finished by calling <TT>FLAC__stream_decoder_finish()</TT>, which ensures the decoder is in the correct state and frees memory.
+               When the decoder has finished decoding (normally or through an abort), the instance is finished by calling <TT>FLAC__stream_decoder_finish()</TT>, which ensures the decoder is in the correct state and frees memory.  Then the instance may be deleted with <TT>FLAC__stream_decoder_delete()</TT> or initialized again to decode another stream.
        </P>
        <P>
                Note that the stream decoder has no real concept of stream position, it just converts data.  To seek within a stream the callbacks have only to flush the decoder using <TT>FLAC__stream_decoder_flush()</TT> and start feeding data from the new position through the read callback.  The file decoder does just this.
                <B>FILE DECODER</B>
        </P>
        <P>
-               The file decoder is a wrapper around the stream decoder meant to simplfy the process of decoding from a file.  The instance type is <TT>FLAC__FileDecoder</TT>.  The flow and callbacks are similar to that of the stream decoder.  However, a file path replaces the read callback argument during initialization.  The program need only provide the path to the file and the file decoder handles the read callbacks.  The remaining callbacks and process functions are analogous to their stream decoder counterparts.
+               The file decoder is a wrapper around the stream decoder meant to simplfy the process of decoding from a file.  The instance type is <TT>FLAC__FileDecoder</TT>.  The flow and callbacks are similar to that of the stream decoder.  However, a file path replaces the read callback argument during initialization.  The program needs only to provide the path to the file and the file decoder handles the read callbacks.  The remaining callbacks and process functions are analogous to their stream decoder counterparts.
        </P>
        <P>
                Since the file decoder manages the input automatically, it also can provide seeking.  This is exposed through the <TT>FLAC__file_decoder_seek_absolute()</TT> method.  At any point after the file decoder has been initialized, the program can call this function to seek to an exact sample within the file.  Subsequently, the first time the write callback is called it will contain a (possibly partial) block starting at that sample.
        </P>
        <P>
-               <B>ENCODER</B>
+               <B>STREAM ENCODER</B>
        </P>
        <P>
-               The encoder functions similarly to the stream decoder.  Currently there is no file encoder but some of the code from <B><TT>flac</TT></B> may be incorporated to do this.  The instance type is <TT>FLAC__Encoder</TT>.  Typically the program will create a new instance by calling <TT>FLAC__encoder_get_new_instance().  Once the instance is created, but before initialization with <TT>FLAC__encoder_init()</TT>, the program should set the required encoding parameters directly.
+               The stream encoder functions similarly to the stream decoder, but has fewer callbacks and more options.  The instance type is <TT>FLAC__StreamEncoder</TT>.  Typically the program will create a new instance by calling <TT>FLAC__stream_encoder_new(), then initialize it by calling <TT>FLAC__stream_encoder_init()</TT>.
        </P>
        <P>
-               Unlike the decoding process, FLAC encoding has many options that can affect the speed and compression ratio.  There are so many in fact that they are not passed as parameters to <TT>FLAC__encoder_init()</TT>, they are written directly to encoder instance public variables by the program.  When the program calls <TT>FLAC__encoder_init()</TT> the encoder will validate the values.  When setting these parameters you should have some basic knowledge of the format (see the <A HREF="#format">user-level documentation</A> or the <A HREF="format.html">formal description</A>) but the required parameters are summarized here:
+               Unlike the decoding process, FLAC encoding has many options that can affect the speed and compression ratio.  When the program calls <TT>FLAC__stream_encoder_init()</TT> the encoder will validate the values, so you should make sure to check the returned state to see that it is FLAC__STREAM_ENCODER_OK.  When setting these parameters you should have some basic knowledge of the format (see the <A HREF="#format">user-level documentation</A> or the <A HREF="format.html">formal description</A>) but the required parameters are summarized here:
                <UL>
                        <LI><B><TT>streamable_subset</TT></B> - true to force the encoder to generate a <A HREF="format.html#subset">Subset stream</A>, else false.</LI>
                        <LI><B><TT>do_mid_side_stereo</TT></B> - true to try mid-side encoding on stereo input, else false.  <TT>channels</TT> must be 2.</LI>
                        <LI><B><TT>seek_table</TT></B> - Optional seek table to prepend; NULL implies no seek table.</LI>
                        <LI><B><TT>padding</TT></B> - Size of PADDING block to add (goes after seek table); 0 implies do not add a PADDING block.</LI>
                </UL>
-               Note that these parameters must be set before <TT>FLAC__encoder_init()</TT> and must not be changed anytime thereafter.
        </P>
        <P>
-               After setting the parameters the program should call <TT>FLAC__encoder_init()</TT> to validate them and register the following callbacks:
+               The program must also give <TT>FLAC__stream_encoder_init()</TT> addresses for the following callbacks:
                <UL>
                        <LI>Write callback - This function is called anytime there is raw encoded data to write.  It may include metadata mixed with encoded audio frames and the data is not guaranteed to be aligned on frame or metadata block boundaries.</LI>
                        <LI>Metadata callback - This function is called once at the end of encoding with the populated STREAMINFO structure.  This is so file encoders can seek back to the beginning of the file and write the STREAMINFO block with the correct statistics after encoding (like minimum/maximum frame size).</LI>
                </UL>
-               The call to <TT>FLAC__encoder_init()</TT> currently will also immediately call the write callback with the "fLaC" signature and all the encoded metadata.
+               The call to <TT>FLAC__stream_encoder_init()</TT> currently will also immediately call the write callback with the "fLaC" signature and all the encoded metadata.
        </P>
        <P>
                After initializing the instance, the program may feed audio data to the encoder in one of two ways:
                <UL>
-                       <LI>Channel separate, through <B><TT>FLAC__encoder_process()</TT></B> - The program will pass an array of pointers to buffers, one for each channel, to the encoder, each of the same length.  The samples need not be block-aligned.</LI>
-                       <LI>Channel interleaved, through <B><TT>FLAC__encoder_process_interleaved()</TT></B> - The program will pass a single pointer to data that is channel-interleaved (i.e. <TT>channel0_sample0, channel1_sample0, ... , channelN_sample0, channel0_sample1, ...</TT>).  Again, the samples need not be block-aligned but they must be sample-aligned, i.e. the first value should be channel0_sampleX and the last value channelN_sampleY.</LI>
+                       <LI>Channel separate, through <B><TT>FLAC__stream_encoder_process()</TT></B> - The program will pass an array of pointers to buffers, one for each channel, to the encoder, each of the same length.  The samples need not be block-aligned.</LI>
+                       <LI>Channel interleaved, through <B><TT>FLAC__stream_encoder_process_interleaved()</TT></B> - The program will pass a single pointer to data that is channel-interleaved (i.e. <TT>channel0_sample0, channel1_sample0, ... , channelN_sample0, channel0_sample1, ...</TT>).  Again, the samples need not be block-aligned but they must be sample-aligned, i.e. the first value should be channel0_sampleX and the last value channelN_sampleY.</LI>
                </UL>
        </P>
        <P>
-               When the program is finished encoding data, it calls <TT>FLAC__encoder_finish()</TT>, which causes the encoder to encode any data still in its input pipe, and call the metadata callback with the correct encoding statistics.
+               When the program is finished encoding data, it calls <TT>FLAC__stream_encoder_finish()</TT>, which causes the encoder to encode any data still in its input pipe, and call the metadata callback with the correct encoding statistics.  Then the instance may be deleted with <TT>FLAC__stream_encoder_delete()</TT> or initialized again to encode another stream.
        </P>
        <P>
                <B>MISCELLANEOUS</B>
                For programs that write their own APPLICATION metadata, it is advantageous to instruct the encoder to write a PADDING block of the correct size, so that instead of rewriting the whole stream after encoding, the program can just overwrite the PADDING block.  If only the maximum size of the APPLICATION block is known, the program can write a slightly larger padding block, then split it after encoding into an APPLICATION block and a PADDING block.
        </P>
        <P>
-               In the case where the size of the APPLICATION block data is known ahead of time, the required size of the padding block can be easily calculated.  If the APPLICATION block data length in bytes (not including the APPLICATION metadata block header) is N bytes, the size given to the FLAC__Encoder instance before initialization is simply N+4.  This accounts for the extra space needed to store the APPLICATION ID.
+               In the case where the size of the APPLICATION block data is known ahead of time, the required size of the padding block can be easily calculated.  If the APPLICATION block data length in bytes (not including the APPLICATION metadata block header) is N bytes, the size given to the FLAC__StreamEncoder instance before initialization is simply N+4.  This accounts for the extra space needed to store the APPLICATION ID.
        </P>
        <P>
                In the case where only the maximum size is known, say, to be N bytes, the required padding size would be N+8.  Four for the APPLICATION ID as before, and four for the extra PADDING block that will fill up the remainder.  At the end of the encoding, when the APPLICATION block data length is known, say, to be M bytes, the original PADDING block would be overwritten with the APPLICATION block and a PADDING block of size N-M.
index 7bfd4f7..a79041a 100644 (file)
 
 includedir = ${prefix}/include/FLAC
 
-include_HEADERS = all.h \
+include_HEADERS = \
+       all.h \
        assert.h \
-       encoder.h \
        file_decoder.h \
        format.h \
        ordinals.h \
        seek_table.h \
-       stream_decoder.h
+       stream_decoder.h \
+       stream_encoder.h
index cc19d24..3a4c13d 100644 (file)
 #define FLAC__ALL_H
 
 #include "assert.h"
-#include "encoder.h"
 #include "file_decoder.h"
 #include "format.h"
 #include "ordinals.h"
 #include "seek_table.h"
 #include "stream_decoder.h"
+#include "stream_encoder.h"
 
 #endif
index c219a8a..e64507e 100644 (file)
@@ -31,35 +31,75 @@ typedef enum {
        FLAC__FILE_DECODER_SEEK_ERROR,
        FLAC__FILE_DECODER_STREAM_ERROR,
        FLAC__FILE_DECODER_MD5_ERROR,
+       FLAC__FILE_DECODER_STREAM_DECODER_ERROR,
+    FLAC__FILE_DECODER_ALREADY_INITIALIZED,
     FLAC__FILE_DECODER_UNINITIALIZED
 } FLAC__FileDecoderState;
 extern const char *FLAC__FileDecoderStateString[];
 
+/***********************************************************************
+ *
+ * class FLAC__FileDecoder : public FLAC__StreamDecoder
+ *
+ ***********************************************************************/
+
+struct FLAC__FileDecoderProtected;
 struct FLAC__FileDecoderPrivate;
 typedef struct {
-       /* this field may not change once FLAC__file_decoder_init() is called */
-       bool check_md5; /* if true, generate MD5 signature of decoded data and compare against signature in the Encoding metadata block */
-
-       FLAC__FileDecoderState state; /* must be FLAC__FILE_DECODER_UNINITIALIZED when passed to FLAC__file_decoder_init() */
-       struct FLAC__FileDecoderPrivate *guts; /* must be 0 when passed to FLAC__file_decoder_init() */
+       struct FLAC__FileDecoderProtected *protected;
+       struct FLAC__FileDecoderPrivate *private;
 } FLAC__FileDecoder;
 
-FLAC__FileDecoder *FLAC__file_decoder_get_new_instance();
-void FLAC__file_decoder_free_instance(FLAC__FileDecoder *decoder);
+/***********************************************************************
+ *
+ * Class constructor/destructor
+ *
+ ***********************************************************************/
+
+FLAC__FileDecoder *FLAC__file_decoder_new();
+void FLAC__file_decoder_delete(FLAC__FileDecoder *);
+
+/***********************************************************************
+ *
+ * Public class method prototypes
+ *
+ ***********************************************************************/
+
+/*
+ * Initialize the instance; should be called after construction and
+ * before any other calls.  Will set and return the decoder state,
+ * which will be FLAC__FILE_DECODER_OK if initialization succeeded.
+ */
 FLAC__FileDecoderState FLAC__file_decoder_init(
        FLAC__FileDecoder *decoder,
+       bool check_md5,
        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
 );
-/* only returns false if check_md5 is set AND the stored MD5 sum is non-zero AND the stored MD5 sum and computed MD5 sum do not match */
+/*
+ * only returns false if check_md5 is set AND the stored MD5 sum
+ * is non-zero AND the stored MD5 sum and computed MD5 sum do not
+ * match
+ */
 bool FLAC__file_decoder_finish(FLAC__FileDecoder *decoder);
+
+/*
+ * methods to return the file decoder state and check_md5 flag
+ */
+FLAC__FileDecoderState FLAC__file_decoder_state(const FLAC__FileDecoder *decoder);
+bool FLAC__file_decoder_check_md5(const FLAC__FileDecoder *decoder);
+
+/*
+ * methods for decoding the data
+ */
 bool FLAC__file_decoder_process_whole_file(FLAC__FileDecoder *decoder);
 bool FLAC__file_decoder_process_metadata(FLAC__FileDecoder *decoder);
 bool FLAC__file_decoder_process_one_frame(FLAC__FileDecoder *decoder);
 bool FLAC__file_decoder_process_remaining_frames(FLAC__FileDecoder *decoder);
+
 bool FLAC__file_decoder_seek_absolute(FLAC__FileDecoder *decoder, uint64 sample);
 
 #endif
index 46ade23..a34ba80 100644 (file)
@@ -31,6 +31,7 @@ typedef enum {
        FLAC__STREAM_DECODER_ABORTED,
        FLAC__STREAM_DECODER_UNPARSEABLE_STREAM,
        FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR,
+       FLAC__STREAM_DECODER_ALREADY_INITIALIZED,
        FLAC__STREAM_DECODER_UNINITIALIZED
 } FLAC__StreamDecoderState;
 extern const char *FLAC__StreamDecoderStateString[];
@@ -55,20 +56,40 @@ typedef enum {
 } FLAC__StreamDecoderErrorStatus;
 extern const char *FLAC__StreamDecoderErrorStatusString[];
 
+/***********************************************************************
+ *
+ * class FLAC__StreamDecoder
+ *
+ ***********************************************************************/
+
+struct FLAC__StreamDecoderProtected;
 struct FLAC__StreamDecoderPrivate;
 typedef struct {
-       /* these fields are read-only and valid as of the last write_callback() */
-       unsigned channels;
-       FLAC__ChannelAssignment channel_assignment;
-       unsigned bits_per_sample;
-       unsigned sample_rate; /* in Hz */
-       unsigned blocksize; /* in samples (per channel) */
-       FLAC__StreamDecoderState state; /* must be FLAC__STREAM_DECODER_UNINITIALIZED when passed to FLAC__stream_decoder_init() */
-       struct FLAC__StreamDecoderPrivate *guts; /* must be 0 when passed to FLAC__stream_decoder_init() */
+       struct FLAC__StreamDecoderProtected *protected;
+       struct FLAC__StreamDecoderPrivate *private;
 } FLAC__StreamDecoder;
 
-FLAC__StreamDecoder *FLAC__stream_decoder_get_new_instance();
-void FLAC__stream_decoder_free_instance(FLAC__StreamDecoder *decoder);
+/***********************************************************************
+ *
+ * Class constructor/destructor
+ *
+ ***********************************************************************/
+
+FLAC__StreamDecoder *FLAC__stream_decoder_new();
+void FLAC__stream_decoder_delete(FLAC__StreamDecoder *);
+
+/***********************************************************************
+ *
+ * Public class method prototypes
+ *
+ ***********************************************************************/
+
+/*
+ * Initialize the instance; should be called after construction and
+ * before any other calls.  Will set and return the decoder state which
+ * will be FLAC__STREAM_DECODER_SEARCH_FOR_METADATA if initialization
+ * succeeded.
+ */
 FLAC__StreamDecoderState FLAC__stream_decoder_init(
        FLAC__StreamDecoder *decoder,
        FLAC__StreamDecoderReadStatus (*read_callback)(const FLAC__StreamDecoder *decoder, byte buffer[], unsigned *bytes, void *client_data),
@@ -78,8 +99,28 @@ FLAC__StreamDecoderState FLAC__stream_decoder_init(
        void *client_data
 );
 void FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder);
+
+/*
+ * methods to return the stream decoder state, number of channels,
+ * channel assignment, bits-per-sample, sample rate in Hz, and
+ * blocksize in samples.
+ */
+FLAC__StreamDecoderState FLAC__stream_decoder_state(const FLAC__StreamDecoder *decoder);
+unsigned FLAC__stream_decoder_channels(const FLAC__StreamDecoder *decoder);
+FLAC__ChannelAssignment FLAC__stream_decoder_channel_assignment(const FLAC__StreamDecoder *decoder);
+unsigned FLAC__stream_decoder_bits_per_sample(const FLAC__StreamDecoder *decoder);
+unsigned FLAC__stream_decoder_sample_rate(const FLAC__StreamDecoder *decoder);
+unsigned FLAC__stream_decoder_blocksize(const FLAC__StreamDecoder *decoder);
+
+/*
+ * state control methods
+ */
 bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder);
 bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder);
+
+/*
+ * methods for decoding the data
+ */
 bool FLAC__stream_decoder_process_whole_stream(FLAC__StreamDecoder *decoder);
 bool FLAC__stream_decoder_process_metadata(FLAC__StreamDecoder *decoder);
 bool FLAC__stream_decoder_process_one_frame(FLAC__StreamDecoder *decoder);
index ff12fab..8f50e01 100644 (file)
  * Boston, MA  02111-1307, USA.
  */
 
-#ifndef FLAC__ENCODER_H
-#define FLAC__ENCODER_H
+#ifndef FLAC__STREAM_ENCODER_H
+#define FLAC__STREAM_ENCODER_H
 
 #include "format.h"
 
 typedef enum {
-       FLAC__ENCODER_WRITE_OK = 0,
-       FLAC__ENCODER_WRITE_FATAL_ERROR
-} FLAC__EncoderWriteStatus;
-extern const char *FLAC__EncoderWriteStatusString[];
+       FLAC__STREAM_ENCODER_OK = 0,
+       FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS,
+       FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE,
+       FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE,
+       FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE,
+       FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION,
+       FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH,
+       FLAC__STREAM_ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH,
+       FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE,
+       FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER,
+       FLAC__STREAM_ENCODER_NOT_STREAMABLE,
+       FLAC__STREAM_ENCODER_FRAMING_ERROR,
+       FLAC__STREAM_ENCODER_INVALID_SEEK_TABLE,
+       FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING,
+       FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING, /* that is, the write_callback returned an error */
+       FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR,
+       FLAC__STREAM_ENCODER_ALREADY_INITIALIZED,
+       FLAC__STREAM_ENCODER_UNINITIALIZED
+} FLAC__StreamEncoderState;
+extern const char *FLAC__StreamEncoderStateString[];
 
 typedef enum {
-       FLAC__ENCODER_OK = 0,
-       FLAC__ENCODER_UNINITIALIZED,
-       FLAC__ENCODER_INVALID_NUMBER_OF_CHANNELS,
-       FLAC__ENCODER_INVALID_BITS_PER_SAMPLE,
-       FLAC__ENCODER_INVALID_SAMPLE_RATE,
-       FLAC__ENCODER_INVALID_BLOCK_SIZE,
-       FLAC__ENCODER_INVALID_QLP_COEFF_PRECISION,
-       FLAC__ENCODER_MID_SIDE_CHANNELS_MISMATCH,
-       FLAC__ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH,
-       FLAC__ENCODER_ILLEGAL_MID_SIDE_FORCE,
-       FLAC__ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER,
-       FLAC__ENCODER_NOT_STREAMABLE,
-       FLAC__ENCODER_FRAMING_ERROR,
-       FLAC__ENCODER_INVALID_SEEK_TABLE,
-       FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING,
-       FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING, /* that is, the write_callback returned an error */
-       FLAC__ENCODER_MEMORY_ALLOCATION_ERROR
-} FLAC__EncoderState;
-extern const char *FLAC__EncoderStateString[];
+       FLAC__STREAM_ENCODER_WRITE_OK = 0,
+       FLAC__STREAM_ENCODER_WRITE_FATAL_ERROR
+} FLAC__StreamEncoderWriteStatus;
+extern const char *FLAC__StreamEncoderWriteStatusString[];
 
-struct FLAC__EncoderPrivate;
+/***********************************************************************
+ *
+ * class FLAC__StreamEncoder
+ *
+ ***********************************************************************/
+
+struct FLAC__StreamEncoderProtected;
+struct FLAC__StreamEncoderPrivate;
 typedef struct {
-       /*
-        * none of these fields may change once FLAC__encoder_init() is called
-        */
-       struct FLAC__EncoderPrivate *guts;    /* must be 0 when passed to FLAC__encoder_init() */
-       FLAC__EncoderState state;             /* must be FLAC__ENCODER_UNINITIALIZED when passed to FLAC__encoder_init() */
-       bool     streamable_subset;
-       bool     do_mid_side_stereo;          /* 0 or 1; 1 only if channels==2 */
-       bool     loose_mid_side_stereo;       /* 0 or 1; 1 only if channels==2 and do_mid_side_stereo==true */
-       unsigned channels;                    /* must be <= FLAC__MAX_CHANNELS */
-       unsigned bits_per_sample;             /* do not give the encoder wider data than what you specify here or bad things will happen! */
-       unsigned sample_rate;
-       unsigned blocksize;
-       unsigned max_lpc_order;               /* 0 => encoder will not try general LPC, only fixed predictors; must be <= FLAC__MAX_LPC_ORDER */
-       unsigned qlp_coeff_precision;         /* >= FLAC__MIN_QLP_COEFF_PRECISION, or 0 to let encoder select based on blocksize; */
+       struct FLAC__StreamEncoderProtected *protected;
+       struct FLAC__StreamEncoderPrivate *private;
+} FLAC__StreamEncoder;
+
+/***********************************************************************
+ *
+ * Class constructor/destructor
+ *
+ ***********************************************************************/
+
+FLAC__StreamEncoder *FLAC__stream_encoder_new();
+void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder);
+
+/***********************************************************************
+ *
+ * Public class method prototypes
+ *
+ ***********************************************************************/
+
+/*
+ * Initialize the instance; should be called after construction and
+ * before any other calls.  Will set and return the encoder state,
+ * which will be FLAC__STREAM_ENCODER_OK if initialization succeeded.
+ */
+FLAC__StreamEncoderState FLAC__stream_encoder_init(
+       FLAC__StreamEncoder *encoder,
+       bool     streamable_subset,
+       bool     do_mid_side_stereo,          /* 0 or 1; 1 only if channels==2 */
+       bool     loose_mid_side_stereo,       /* 0 or 1; 1 only if channels==2 and do_mid_side_stereo==true */
+       unsigned channels,                    /* must be <= FLAC__MAX_CHANNELS */
+       unsigned bits_per_sample,             /* do not give the encoder wider data than what you specify here or bad things will happen! */
+       unsigned sample_rate,
+       unsigned blocksize,
+       unsigned max_lpc_order,               /* 0 => encoder will not try general LPC, only fixed predictors; must be <= FLAC__MAX_LPC_ORDER */
+       unsigned qlp_coeff_precision,         /* >= FLAC__MIN_QLP_COEFF_PRECISION, or 0 to let encoder select based on blocksize; */
                                              /* qlp_coeff_precision+bits_per_sample must be < 32 */
-       bool     do_qlp_coeff_prec_search;    /* 0 => use qlp_coeff_precision, 1 => search around qlp_coeff_precision, take best */
-       bool     do_exhaustive_model_search;  /* 0 => use estimated bits per residual for scoring, 1 => generate all, take shortest */
-       unsigned min_residual_partition_order; /* 0 => estimate Rice parameter based on residual variance; >0 => partition residual, use parameter for each */
-       unsigned max_residual_partition_order; /*      based on mean; min_ and max_ specify the min and max Rice partition order */
-       unsigned rice_parameter_search_dist;  /* 0 => try only calc'd parameter k; else try all [k-dist..k+dist] parameters, use best */
-       uint64   total_samples_estimate;      /* may be 0 if unknown.  this will be a placeholder in the metadata block until the actual total is calculated */
-       const FLAC__StreamMetaData_SeekTable *seek_table; /* optional seek_table to prepend, 0 => no seek table */
-       unsigned padding;                     /* size of PADDING block to add (goes after seek table); 0 => do not add a PADDING block */
-} FLAC__Encoder;
+       bool     do_qlp_coeff_prec_search,    /* 0 => use qlp_coeff_precision, 1 => search around qlp_coeff_precision, take best */
+       bool     do_exhaustive_model_search,  /* 0 => use estimated bits per residual for scoring, 1 => generate all, take shortest */
+       unsigned min_residual_partition_order, /* 0 => estimate Rice parameter based on residual variance; >0 => partition residual, use parameter for each */
+       unsigned max_residual_partition_order, /*      based on mean; min_ and max_ specify the min and max Rice partition order */
+       unsigned rice_parameter_search_dist,  /* 0 => try only calc'd parameter k; else try all [k-dist..k+dist] parameters, use best */
+       uint64   total_samples_estimate,      /* may be 0 if unknown.  this will be a placeholder in the metadata block until the actual total is calculated */
+       const FLAC__StreamMetaData_SeekTable *seek_table, /* optional seek_table to prepend, 0 => no seek table */
+       unsigned padding,                     /* size of PADDING block to add (goes after seek table); 0 => do not add a PADDING block */
+       FLAC__StreamEncoderWriteStatus (*write_callback)(const FLAC__StreamEncoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data),
+       void (*metadata_callback)(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data),
+       void *client_data
+);
+void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder);
 
+/*
+ * various "get" methods
+ */
+FLAC__StreamEncoderState FLAC__stream_encoder_state(const FLAC__StreamEncoder *encoder);
+bool     FLAC__stream_encoder_streamable_subset(const FLAC__StreamEncoder *encoder);
+bool     FLAC__stream_encoder_do_mid_side_stereo(const FLAC__StreamEncoder *encoder);
+bool     FLAC__stream_encoder_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder);
+unsigned FLAC__stream_encoder_channels(const FLAC__StreamEncoder *encoder);
+unsigned FLAC__stream_encoder_bits_per_sample(const FLAC__StreamEncoder *encoder);
+unsigned FLAC__stream_encoder_sample_rate(const FLAC__StreamEncoder *encoder);
+unsigned FLAC__stream_encoder_blocksize(const FLAC__StreamEncoder *encoder);
+unsigned FLAC__stream_encoder_max_lpc_order(const FLAC__StreamEncoder *encoder);
+unsigned FLAC__stream_encoder_qlp_coeff_precision(const FLAC__StreamEncoder *encoder);
+bool     FLAC__stream_encoder_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder);
+bool     FLAC__stream_encoder_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder);
+unsigned FLAC__stream_encoder_min_residual_partition_order(const FLAC__StreamEncoder *encoder);
+unsigned FLAC__stream_encoder_max_residual_partition_order(const FLAC__StreamEncoder *encoder);
+unsigned FLAC__stream_encoder_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder);
 
-FLAC__Encoder *FLAC__encoder_get_new_instance();
-void FLAC__encoder_free_instance(FLAC__Encoder *encoder);
-FLAC__EncoderState FLAC__encoder_init(FLAC__Encoder *encoder, FLAC__EncoderWriteStatus (*write_callback)(const FLAC__Encoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data), void (*metadata_callback)(const FLAC__Encoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data), void *client_data);
-void FLAC__encoder_finish(FLAC__Encoder *encoder);
-bool FLAC__encoder_process(FLAC__Encoder *encoder, const int32 *buf[], unsigned samples);
-bool FLAC__encoder_process_interleaved(FLAC__Encoder *encoder, const int32 buf[], unsigned samples);
+/*
+ * methods for encoding the data
+ */
+bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const int32 *buf[], unsigned samples);
+bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const int32 buf[], unsigned samples);
 
 #endif
index ba676c6..9f40423 100644 (file)
@@ -99,7 +99,7 @@ int flac__decode_wav(const char *infile, const char *outfile, bool analysis_mode
 
        if(skip > 0) {
                if(!FLAC__file_decoder_process_metadata(decoder)) {
-                       fprintf(stderr, "%s: ERROR while decoding metadata, state=%d:%s\n", infile, decoder->state, FLAC__FileDecoderStateString[decoder->state]);
+                       fprintf(stderr, "%s: ERROR while decoding metadata, state=%d:%s\n", infile, FLAC__file_decoder_state(decoder), FLAC__FileDecoderStateString[FLAC__file_decoder_state(decoder)]);
                        goto wav_abort_;
                }
                if(stream_info.skip_count_too_high) {
@@ -107,38 +107,38 @@ int flac__decode_wav(const char *infile, const char *outfile, bool analysis_mode
                        goto wav_abort_;
                }
                if(!FLAC__file_decoder_seek_absolute(decoder, skip)) {
-                       fprintf(stderr, "%s: ERROR seeking while skipping bytes, state=%d:%s\n", infile, decoder->state, FLAC__FileDecoderStateString[decoder->state]);
+                       fprintf(stderr, "%s: ERROR seeking while skipping bytes, state=%d:%s\n", infile, FLAC__file_decoder_state(decoder), FLAC__FileDecoderStateString[FLAC__file_decoder_state(decoder)]);
                        goto wav_abort_;
                }
                if(!FLAC__file_decoder_process_remaining_frames(decoder)) {
                        if(verbose) fprintf(stderr, "\n");
-                       fprintf(stderr, "%s: ERROR while decoding frames, state=%d:%s\n", infile, decoder->state, FLAC__FileDecoderStateString[decoder->state]);
+                       fprintf(stderr, "%s: ERROR while decoding frames, state=%d:%s\n", infile, FLAC__file_decoder_state(decoder), FLAC__FileDecoderStateString[FLAC__file_decoder_state(decoder)]);
                        goto wav_abort_;
                }
-               if(decoder->state != FLAC__FILE_DECODER_OK && decoder->state != FLAC__FILE_DECODER_END_OF_FILE) {
+               if(FLAC__file_decoder_state(decoder) != FLAC__FILE_DECODER_OK && FLAC__file_decoder_state(decoder) != FLAC__FILE_DECODER_END_OF_FILE) {
                        if(verbose) fprintf(stderr, "\n");
-                       fprintf(stderr, "%s: ERROR during decoding, state=%d:%s\n", infile, decoder->state, FLAC__FileDecoderStateString[decoder->state]);
+                       fprintf(stderr, "%s: ERROR during decoding, state=%d:%s\n", infile, FLAC__file_decoder_state(decoder), FLAC__FileDecoderStateString[FLAC__file_decoder_state(decoder)]);
                        goto wav_abort_;
                }
        }
        else {
                if(!FLAC__file_decoder_process_whole_file(decoder)) {
                        if(verbose) fprintf(stderr, "\n");
-                       fprintf(stderr, "%s: ERROR while decoding data, state=%d:%s\n", infile, decoder->state, FLAC__FileDecoderStateString[decoder->state]);
+                       fprintf(stderr, "%s: ERROR while decoding data, state=%d:%s\n", infile, FLAC__file_decoder_state(decoder), FLAC__FileDecoderStateString[FLAC__file_decoder_state(decoder)]);
                        goto wav_abort_;
                }
-               if(decoder->state != FLAC__FILE_DECODER_OK && decoder->state != FLAC__FILE_DECODER_END_OF_FILE) {
+               if(FLAC__file_decoder_state(decoder) != FLAC__FILE_DECODER_OK && FLAC__file_decoder_state(decoder) != FLAC__FILE_DECODER_END_OF_FILE) {
                        if(verbose) fprintf(stderr, "\n");
-                       fprintf(stderr, "%s: ERROR during decoding, state=%d:%s\n", infile, decoder->state, FLAC__FileDecoderStateString[decoder->state]);
+                       fprintf(stderr, "%s: ERROR during decoding, state=%d:%s\n", infile, FLAC__file_decoder_state(decoder), FLAC__FileDecoderStateString[FLAC__file_decoder_state(decoder)]);
                        goto wav_abort_;
                }
        }
 
        if(decoder) {
-               if(decoder->state != FLAC__FILE_DECODER_UNINITIALIZED)
+               if(FLAC__file_decoder_state(decoder) != FLAC__FILE_DECODER_UNINITIALIZED)
                        md5_failure = !FLAC__file_decoder_finish(decoder);
                print_stats(&stream_info);
-               FLAC__file_decoder_free_instance(decoder);
+               FLAC__file_decoder_delete(decoder);
        }
        if(0 != stream_info.fout && stream_info.fout != stdout)
                fclose(stream_info.fout);
@@ -157,9 +157,9 @@ int flac__decode_wav(const char *infile, const char *outfile, bool analysis_mode
        return 0;
 wav_abort_:
        if(decoder) {
-               if(decoder->state != FLAC__FILE_DECODER_UNINITIALIZED)
+               if(FLAC__file_decoder_state(decoder) != FLAC__FILE_DECODER_UNINITIALIZED)
                        FLAC__file_decoder_finish(decoder);
-               FLAC__file_decoder_free_instance(decoder);
+               FLAC__file_decoder_delete(decoder);
        }
        if(0 != stream_info.fout && stream_info.fout != stdout) {
                fclose(stream_info.fout);
@@ -212,7 +212,7 @@ int flac__decode_raw(const char *infile, const char *outfile, bool analysis_mode
 
        if(skip > 0) {
                if(!FLAC__file_decoder_process_metadata(decoder)) {
-                       fprintf(stderr, "%s: ERROR while decoding metadata, state=%d:%s\n", infile, decoder->state, FLAC__FileDecoderStateString[decoder->state]);
+                       fprintf(stderr, "%s: ERROR while decoding metadata, state=%d:%s\n", infile, FLAC__file_decoder_state(decoder), FLAC__FileDecoderStateString[FLAC__file_decoder_state(decoder)]);
                        goto raw_abort_;
                }
                if(stream_info.skip_count_too_high) {
@@ -220,38 +220,38 @@ int flac__decode_raw(const char *infile, const char *outfile, bool analysis_mode
                        goto raw_abort_;
                }
                if(!FLAC__file_decoder_seek_absolute(decoder, skip)) {
-                       fprintf(stderr, "%s: ERROR seeking while skipping bytes, state=%d:%s\n", infile, decoder->state, FLAC__FileDecoderStateString[decoder->state]);
+                       fprintf(stderr, "%s: ERROR seeking while skipping bytes, state=%d:%s\n", infile, FLAC__file_decoder_state(decoder), FLAC__FileDecoderStateString[FLAC__file_decoder_state(decoder)]);
                        goto raw_abort_;
                }
                if(!FLAC__file_decoder_process_remaining_frames(decoder)) {
                        if(verbose) fprintf(stderr, "\n");
-                       fprintf(stderr, "%s: ERROR while decoding frames, state=%d:%s\n", infile, decoder->state, FLAC__FileDecoderStateString[decoder->state]);
+                       fprintf(stderr, "%s: ERROR while decoding frames, state=%d:%s\n", infile, FLAC__file_decoder_state(decoder), FLAC__FileDecoderStateString[FLAC__file_decoder_state(decoder)]);
                        goto raw_abort_;
                }
-               if(decoder->state != FLAC__FILE_DECODER_OK && decoder->state != FLAC__FILE_DECODER_END_OF_FILE) {
+               if(FLAC__file_decoder_state(decoder) != FLAC__FILE_DECODER_OK && FLAC__file_decoder_state(decoder) != FLAC__FILE_DECODER_END_OF_FILE) {
                        if(verbose) fprintf(stderr, "\n");
-                       fprintf(stderr, "%s: ERROR during decoding, state=%d:%s\n", infile, decoder->state, FLAC__FileDecoderStateString[decoder->state]);
+                       fprintf(stderr, "%s: ERROR during decoding, state=%d:%s\n", infile, FLAC__file_decoder_state(decoder), FLAC__FileDecoderStateString[FLAC__file_decoder_state(decoder)]);
                        goto raw_abort_;
                }
        }
        else {
                if(!FLAC__file_decoder_process_whole_file(decoder)) {
                        if(verbose) fprintf(stderr, "\n");
-                       fprintf(stderr, "%s: ERROR while decoding data, state=%d:%s\n", infile, decoder->state, FLAC__FileDecoderStateString[decoder->state]);
+                       fprintf(stderr, "%s: ERROR while decoding data, state=%d:%s\n", infile, FLAC__file_decoder_state(decoder), FLAC__FileDecoderStateString[FLAC__file_decoder_state(decoder)]);
                        goto raw_abort_;
                }
-               if(decoder->state != FLAC__FILE_DECODER_OK && decoder->state != FLAC__FILE_DECODER_END_OF_FILE) {
+               if(FLAC__file_decoder_state(decoder) != FLAC__FILE_DECODER_OK && FLAC__file_decoder_state(decoder) != FLAC__FILE_DECODER_END_OF_FILE) {
                        if(verbose) fprintf(stderr, "\n");
-                       fprintf(stderr, "%s: ERROR during decoding, state=%d:%s\n", infile, decoder->state, FLAC__FileDecoderStateString[decoder->state]);
+                       fprintf(stderr, "%s: ERROR during decoding, state=%d:%s\n", infile, FLAC__file_decoder_state(decoder), FLAC__FileDecoderStateString[FLAC__file_decoder_state(decoder)]);
                        goto raw_abort_;
                }
        }
 
        if(decoder) {
-               if(decoder->state != FLAC__FILE_DECODER_UNINITIALIZED)
+               if(FLAC__file_decoder_state(decoder) != FLAC__FILE_DECODER_UNINITIALIZED)
                        md5_failure = !FLAC__file_decoder_finish(decoder);
                print_stats(&stream_info);
-               FLAC__file_decoder_free_instance(decoder);
+               FLAC__file_decoder_delete(decoder);
        }
        if(0 != stream_info.fout && stream_info.fout != stdout)
                fclose(stream_info.fout);
@@ -270,9 +270,9 @@ int flac__decode_raw(const char *infile, const char *outfile, bool analysis_mode
        return 0;
 raw_abort_:
        if(decoder) {
-               if(decoder->state != FLAC__FILE_DECODER_UNINITIALIZED)
+               if(FLAC__file_decoder_state(decoder) != FLAC__FILE_DECODER_UNINITIALIZED)
                        FLAC__file_decoder_finish(decoder);
-               FLAC__file_decoder_free_instance(decoder);
+               FLAC__file_decoder_delete(decoder);
        }
        if(0 != stream_info.fout && stream_info.fout != stdout) {
                fclose(stream_info.fout);
@@ -289,15 +289,14 @@ bool init(const char *infile, stream_info_struct *stream_info)
 
        is_big_endian_host = (*((byte*)(&test)))? false : true;
 
-       decoder = FLAC__file_decoder_get_new_instance();
+       decoder = FLAC__file_decoder_new();
        if(0 == decoder) {
                fprintf(stderr, "ERROR creating the decoder instance\n");
                return false;
        }
-       decoder->check_md5 = true;
 
-       if(FLAC__file_decoder_init(decoder, infile, write_callback, metadata_callback, error_callback, stream_info) != FLAC__FILE_DECODER_OK) {
-               fprintf(stderr, "ERROR initializing decoder, state=%d:%s\n", decoder->state, FLAC__FileDecoderStateString[decoder->state]);
+       if(FLAC__file_decoder_init(decoder, true /*check_md5*/, infile, write_callback, metadata_callback, error_callback, stream_info) != FLAC__FILE_DECODER_OK) {
+               fprintf(stderr, "ERROR initializing decoder, state=%d:%s\n", FLAC__file_decoder_state(decoder), FLAC__FileDecoderStateString[FLAC__file_decoder_state(decoder)]);
                return false;
        }
 
index 9110bb0..47d8665 100644 (file)
@@ -16,8 +16,6 @@
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  */
 
-/*@@@ need to "_finish()" the verify decoder */
-
 #if defined _WIN32 && !defined __CYGWIN__
 /* where MSVC puts unlink() */
 # include <io.h>
@@ -64,7 +62,7 @@ typedef struct {
 typedef struct {
        FILE *fout;
        const char *outfilename;
-       FLAC__Encoder *encoder;
+       FLAC__StreamEncoder *encoder;
        bool verify;
        bool verbose;
        uint64 unencoded_size;
@@ -95,8 +93,8 @@ static bool convert_to_seek_table(char *requested_seek_points, int num_requested
 static void append_point_to_seek_table(FLAC__StreamMetaData_SeekTable *seek_table, uint64 sample, uint64 stream_samples, uint64 blocksize);
 static int seekpoint_compare(const FLAC__StreamMetaData_SeekPoint *l, const FLAC__StreamMetaData_SeekPoint *r);
 static void format_input(unsigned wide_samples, bool is_big_endian, bool is_unsigned_samples, unsigned channels, unsigned bps, encoder_wrapper_struct *encoder_wrapper);
-static FLAC__EncoderWriteStatus write_callback(const FLAC__Encoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
-static void metadata_callback(const FLAC__Encoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data);
+static FLAC__StreamEncoderWriteStatus write_callback(const FLAC__StreamEncoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
+static void metadata_callback(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data);
 static FLAC__StreamDecoderReadStatus verify_read_callback(const FLAC__StreamDecoder *decoder, byte buffer[], unsigned *bytes, void *client_data);
 static FLAC__StreamDecoderWriteStatus verify_write_callback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const int32 *buffer[], void *client_data);
 static void verify_metadata_callback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data);
@@ -272,8 +270,8 @@ int flac__encode_wav(FILE *infile, long infilesize, const char *infilename, cons
                                                        format_input(wide_samples, false, is_unsigned_samples, channels, bps, &encoder_wrapper);
 
                                                        /* NOTE: some versions of GCC can't figure out const-ness right and will give you an 'incompatible pointer type' warning on arg 2 here: */
-                                                       if(!FLAC__encoder_process(encoder_wrapper.encoder, input, wide_samples)) {
-                                                               fprintf(stderr, "ERROR during encoding, state = %d:%s\n", encoder_wrapper.encoder->state, FLAC__EncoderStateString[encoder_wrapper.encoder->state]);
+                                                       if(!FLAC__stream_encoder_process(encoder_wrapper.encoder, input, wide_samples)) {
+                                                               fprintf(stderr, "ERROR during encoding, state = %d:%s\n", FLAC__stream_encoder_state(encoder_wrapper.encoder), FLAC__StreamEncoderStateString[FLAC__stream_encoder_state(encoder_wrapper.encoder)]);
                                                                goto wav_abort_;
                                                        }
                                                        data_bytes -= bytes_read;
@@ -310,9 +308,9 @@ int flac__encode_wav(FILE *infile, long infilesize, const char *infilename, cons
        }
 
        if(encoder_wrapper.encoder) {
-               if(encoder_wrapper.encoder->state == FLAC__ENCODER_OK)
-                       FLAC__encoder_finish(encoder_wrapper.encoder);
-               FLAC__encoder_free_instance(encoder_wrapper.encoder);
+               if(FLAC__stream_encoder_state(encoder_wrapper.encoder) == FLAC__STREAM_ENCODER_OK)
+                       FLAC__stream_encoder_finish(encoder_wrapper.encoder);
+               FLAC__stream_encoder_delete(encoder_wrapper.encoder);
        }
        if(encoder_wrapper.verbose && encoder_wrapper.total_samples_to_encode > 0) {
                print_stats(&encoder_wrapper);
@@ -321,6 +319,8 @@ int flac__encode_wav(FILE *infile, long infilesize, const char *infilename, cons
        if(0 != encoder_wrapper.seek_table.points)
                free(encoder_wrapper.seek_table.points);
        if(verify) {
+               FLAC__stream_decoder_finish(encoder_wrapper.verify_fifo.decoder);
+               FLAC__stream_decoder_delete(encoder_wrapper.verify_fifo.decoder);
                if(encoder_wrapper.verify_fifo.result != FLAC__VERIFY_OK) {
                        fprintf(stderr, "%s: Verify FAILED! (%s)  Do not trust %s\n", infilename, verify_code_string[encoder_wrapper.verify_fifo.result], outfilename);
                        return 1;
@@ -336,13 +336,15 @@ wav_abort_:
        if(encoder_wrapper.verbose && encoder_wrapper.total_samples_to_encode > 0)
                fprintf(stderr, "\n");
        if(encoder_wrapper.encoder) {
-               if(encoder_wrapper.encoder->state == FLAC__ENCODER_OK)
-                       FLAC__encoder_finish(encoder_wrapper.encoder);
-               FLAC__encoder_free_instance(encoder_wrapper.encoder);
+               if(FLAC__stream_encoder_state(encoder_wrapper.encoder) == FLAC__STREAM_ENCODER_OK)
+                       FLAC__stream_encoder_finish(encoder_wrapper.encoder);
+               FLAC__stream_encoder_delete(encoder_wrapper.encoder);
        }
        if(0 != encoder_wrapper.seek_table.points)
                free(encoder_wrapper.seek_table.points);
        if(verify) {
+               FLAC__stream_decoder_finish(encoder_wrapper.verify_fifo.decoder);
+               FLAC__stream_decoder_delete(encoder_wrapper.verify_fifo.decoder);
                if(encoder_wrapper.verify_fifo.result != FLAC__VERIFY_OK) {
                        fprintf(stderr, "%s: Verify FAILED! (%s)  Do not trust %s\n", infilename, verify_code_string[encoder_wrapper.verify_fifo.result], outfilename);
                        return 1;
@@ -466,17 +468,17 @@ int flac__encode_raw(FILE *infile, long infilesize, const char *infilename, cons
                        format_input(wide_samples, is_big_endian, is_unsigned_samples, channels, bps, &encoder_wrapper);
 
                        /* NOTE: some versions of GCC can't figure out const-ness right and will give you an 'incompatible pointer type' warning on arg 2 here: */
-                       if(!FLAC__encoder_process(encoder_wrapper.encoder, input, wide_samples)) {
-                               fprintf(stderr, "ERROR during encoding, state = %d:%s\n", encoder_wrapper.encoder->state, FLAC__EncoderStateString[encoder_wrapper.encoder->state]);
+                       if(!FLAC__stream_encoder_process(encoder_wrapper.encoder, input, wide_samples)) {
+                               fprintf(stderr, "ERROR during encoding, state = %d:%s\n", FLAC__stream_encoder_state(encoder_wrapper.encoder), FLAC__StreamEncoderStateString[FLAC__stream_encoder_state(encoder_wrapper.encoder)]);
                                goto raw_abort_;
                        }
                }
        }
 
        if(encoder_wrapper.encoder) {
-               if(encoder_wrapper.encoder->state == FLAC__ENCODER_OK)
-                       FLAC__encoder_finish(encoder_wrapper.encoder);
-               FLAC__encoder_free_instance(encoder_wrapper.encoder);
+               if(FLAC__stream_encoder_state(encoder_wrapper.encoder) == FLAC__STREAM_ENCODER_OK)
+                       FLAC__stream_encoder_finish(encoder_wrapper.encoder);
+               FLAC__stream_encoder_delete(encoder_wrapper.encoder);
        }
        if(encoder_wrapper.verbose && encoder_wrapper.total_samples_to_encode > 0) {
                print_stats(&encoder_wrapper);
@@ -485,6 +487,8 @@ int flac__encode_raw(FILE *infile, long infilesize, const char *infilename, cons
        if(0 != encoder_wrapper.seek_table.points)
                free(encoder_wrapper.seek_table.points);
        if(verify) {
+               FLAC__stream_decoder_finish(encoder_wrapper.verify_fifo.decoder);
+               FLAC__stream_decoder_delete(encoder_wrapper.verify_fifo.decoder);
                if(encoder_wrapper.verify_fifo.result != FLAC__VERIFY_OK) {
                        fprintf(stderr, "%s: Verify FAILED! (%s)  Do not trust %s\n", infilename, verify_code_string[encoder_wrapper.verify_fifo.result], outfilename);
                        return 1;
@@ -500,13 +504,15 @@ raw_abort_:
        if(encoder_wrapper.verbose && encoder_wrapper.total_samples_to_encode > 0)
                fprintf(stderr, "\n");
        if(encoder_wrapper.encoder) {
-               if(encoder_wrapper.encoder->state == FLAC__ENCODER_OK)
-                       FLAC__encoder_finish(encoder_wrapper.encoder);
-               FLAC__encoder_free_instance(encoder_wrapper.encoder);
+               if(FLAC__stream_encoder_state(encoder_wrapper.encoder) == FLAC__STREAM_ENCODER_OK)
+                       FLAC__stream_encoder_finish(encoder_wrapper.encoder);
+               FLAC__stream_encoder_delete(encoder_wrapper.encoder);
        }
        if(0 != encoder_wrapper.seek_table.points)
                free(encoder_wrapper.seek_table.points);
        if(verify) {
+               FLAC__stream_decoder_finish(encoder_wrapper.verify_fifo.decoder);
+               FLAC__stream_decoder_delete(encoder_wrapper.verify_fifo.decoder);
                if(encoder_wrapper.verify_fifo.result != FLAC__VERIFY_OK) {
                        fprintf(stderr, "%s: Verify FAILED! (%s)  Do not trust %s\n", infilename, verify_code_string[encoder_wrapper.verify_fifo.result], outfilename);
                        return 1;
@@ -531,7 +537,7 @@ bool init(encoder_wrapper_struct *encoder_wrapper)
        for(i = 0; i < FLAC__MAX_CHANNELS; i++)
                input[i] = &(in[i][0]);
 
-       encoder_wrapper->encoder = FLAC__encoder_get_new_instance();
+       encoder_wrapper->encoder = FLAC__stream_encoder_new();
        if(0 == encoder_wrapper->encoder) {
                fprintf(stderr, "ERROR creating the encoder instance\n");
                return false;
@@ -561,13 +567,13 @@ bool init_encoder(bool lax, bool do_mid_side, bool loose_mid_side, bool do_exhau
                encoder_wrapper->verify_fifo.result = FLAC__VERIFY_OK;
 
                /* set up a stream decoder for verification */
-               encoder_wrapper->verify_fifo.decoder = FLAC__stream_decoder_get_new_instance();
+               encoder_wrapper->verify_fifo.decoder = FLAC__stream_decoder_new();
                if(0 == encoder_wrapper->verify_fifo.decoder) {
                        fprintf(stderr, "ERROR creating the verify decoder instance\n");
                        return false;
                }
                if(FLAC__stream_decoder_init(encoder_wrapper->verify_fifo.decoder, verify_read_callback, verify_write_callback, verify_metadata_callback, verify_error_callback, encoder_wrapper) != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA) {
-                       fprintf(stderr, "ERROR initializing decoder, state = %d:%s\n", encoder_wrapper->verify_fifo.decoder->state, FLAC__StreamDecoderStateString[encoder_wrapper->verify_fifo.decoder->state]);
+                       fprintf(stderr, "ERROR initializing decoder, state = %d:%s\n", FLAC__stream_decoder_state(encoder_wrapper->verify_fifo.decoder), FLAC__StreamDecoderStateString[FLAC__stream_decoder_state(encoder_wrapper->verify_fifo.decoder)]);
                        return false;
                }
        }
@@ -577,26 +583,8 @@ bool init_encoder(bool lax, bool do_mid_side, bool loose_mid_side, bool do_exhau
                return false;
        }
 
-       encoder_wrapper->encoder->streamable_subset = !lax;
-       encoder_wrapper->encoder->channels = channels;
-       encoder_wrapper->encoder->bits_per_sample = bps;
-       encoder_wrapper->encoder->sample_rate = sample_rate;
-       encoder_wrapper->encoder->blocksize = blocksize;
-       encoder_wrapper->encoder->qlp_coeff_precision = qlp_coeff_precision;
-       encoder_wrapper->encoder->max_lpc_order = max_lpc_order;
-       encoder_wrapper->encoder->do_mid_side_stereo = do_mid_side;
-       encoder_wrapper->encoder->loose_mid_side_stereo = loose_mid_side;
-       encoder_wrapper->encoder->do_exhaustive_model_search = do_exhaustive_model_search;
-       encoder_wrapper->encoder->do_qlp_coeff_prec_search = do_qlp_coeff_prec_search;
-       encoder_wrapper->encoder->min_residual_partition_order = min_residual_partition_order;
-       encoder_wrapper->encoder->max_residual_partition_order = max_residual_partition_order;
-       encoder_wrapper->encoder->rice_parameter_search_dist = rice_parameter_search_dist;
-       encoder_wrapper->encoder->total_samples_estimate = encoder_wrapper->total_samples_to_encode;
-       encoder_wrapper->encoder->seek_table = (encoder_wrapper->seek_table.num_points > 0)? &encoder_wrapper->seek_table : 0;
-       encoder_wrapper->encoder->padding = padding;
-
-       if(FLAC__encoder_init(encoder_wrapper->encoder, write_callback, metadata_callback, encoder_wrapper) != FLAC__ENCODER_OK) {
-               fprintf(stderr, "ERROR initializing encoder, state = %d:%s\n", encoder_wrapper->encoder->state, FLAC__EncoderStateString[encoder_wrapper->encoder->state]);
+       if(FLAC__stream_encoder_init(encoder_wrapper->encoder, !lax, do_mid_side, loose_mid_side, channels, bps, sample_rate, blocksize, max_lpc_order, qlp_coeff_precision, do_qlp_coeff_prec_search, do_exhaustive_model_search, min_residual_partition_order, max_residual_partition_order, rice_parameter_search_dist, encoder_wrapper->total_samples_to_encode, (encoder_wrapper->seek_table.num_points > 0)? &encoder_wrapper->seek_table : 0, padding, write_callback, metadata_callback, encoder_wrapper) != FLAC__STREAM_ENCODER_OK) {
+               fprintf(stderr, "ERROR initializing encoder, state = %d:%s\n", FLAC__stream_encoder_state(encoder_wrapper->encoder), FLAC__StreamEncoderStateString[FLAC__stream_encoder_state(encoder_wrapper->encoder)]);
                return false;
        }
 
@@ -798,14 +786,14 @@ void format_input(unsigned wide_samples, bool is_big_endian, bool is_unsigned_sa
        }
 }
 
-FLAC__EncoderWriteStatus write_callback(const FLAC__Encoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
+FLAC__StreamEncoderWriteStatus write_callback(const FLAC__StreamEncoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
 {
        encoder_wrapper_struct *encoder_wrapper = (encoder_wrapper_struct *)client_data;
-       unsigned mask = (encoder->do_exhaustive_model_search || encoder->do_qlp_coeff_prec_search)? 0x1f : 0x7f;
+       const unsigned mask = (FLAC__stream_encoder_do_exhaustive_model_search(encoder) || FLAC__stream_encoder_do_qlp_coeff_prec_search(encoder))? 0x1f : 0x7f;
 
        /* 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(encoder_wrapper->stream_offset > 0 && encoder_wrapper->seek_table.num_points > 0) {
-               uint64 current_sample = (uint64)current_frame * (uint64)encoder->blocksize, test_sample;
+               uint64 current_sample = (uint64)current_frame * (uint64)FLAC__stream_encoder_blocksize(encoder), test_sample;
                unsigned i;
                for(i = encoder_wrapper->first_seek_point_to_check; i < encoder_wrapper->seek_table.num_points; i++) {
                        test_sample = encoder_wrapper->seek_table.points[i].sample_number;
@@ -814,7 +802,7 @@ FLAC__EncoderWriteStatus write_callback(const FLAC__Encoder *encoder, const byte
                        }
                        else if(test_sample == current_sample) {
                                encoder_wrapper->seek_table.points[i].stream_offset = encoder_wrapper->bytes_written - encoder_wrapper->stream_offset;
-                               encoder_wrapper->seek_table.points[i].frame_samples = encoder->blocksize;
+                               encoder_wrapper->seek_table.points[i].frame_samples = FLAC__stream_encoder_blocksize(encoder);
                                encoder_wrapper->first_seek_point_to_check++;
                                break;
                        }
@@ -837,24 +825,24 @@ FLAC__EncoderWriteStatus write_callback(const FLAC__Encoder *encoder, const byte
                if(encoder_wrapper->verify_fifo.into_frames) {
                        if(!FLAC__stream_decoder_process_one_frame(encoder_wrapper->verify_fifo.decoder)) {
                                encoder_wrapper->verify_fifo.result = FLAC__VERIFY_FAILED_IN_FRAME;
-                               return FLAC__ENCODER_WRITE_FATAL_ERROR;
+                               return FLAC__STREAM_ENCODER_WRITE_FATAL_ERROR;
                        }
                }
                else {
                        if(!FLAC__stream_decoder_process_metadata(encoder_wrapper->verify_fifo.decoder)) {
                                encoder_wrapper->verify_fifo.result = FLAC__VERIFY_FAILED_IN_METADATA;
-                               return FLAC__ENCODER_WRITE_FATAL_ERROR;
+                               return FLAC__STREAM_ENCODER_WRITE_FATAL_ERROR;
                        }
                }
        }
 
        if(fwrite(buffer, sizeof(byte), bytes, encoder_wrapper->fout) == bytes)
-               return FLAC__ENCODER_WRITE_OK;
+               return FLAC__STREAM_ENCODER_WRITE_OK;
        else
-               return FLAC__ENCODER_WRITE_FATAL_ERROR;
+               return FLAC__STREAM_ENCODER_WRITE_FATAL_ERROR;
 }
 
-void metadata_callback(const FLAC__Encoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data)
+void metadata_callback(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data)
 {
        encoder_wrapper_struct *encoder_wrapper = (encoder_wrapper_struct *)client_data;
        byte b;
@@ -971,9 +959,11 @@ FLAC__StreamDecoderWriteStatus verify_write_callback(const FLAC__StreamDecoder *
 {
        encoder_wrapper_struct *encoder_wrapper = (encoder_wrapper_struct *)client_data;
        unsigned channel, l, r;
+       const unsigned channels = FLAC__stream_decoder_channels(decoder);
+       const unsigned bytes_per_block = sizeof(int32) * FLAC__stream_decoder_blocksize(decoder);
 
-       for(channel = 0; channel < decoder->channels; channel++) {
-               if(0 != memcmp(buffer[channel], encoder_wrapper->verify_fifo.original[channel], sizeof(int32) * decoder->blocksize)) {
+       for(channel = 0; channel < channels; channel++) {
+               if(0 != memcmp(buffer[channel], encoder_wrapper->verify_fifo.original[channel], bytes_per_block)) {
                        fprintf(stderr, "\nERROR: mismatch in decoded data, verify FAILED!\n");
                        fprintf(stderr, "       Please submit a bug report to\n");
                        fprintf(stderr, "           http://sourceforge.net/bugs/?func=addbug&group_id=13478\n");
@@ -983,7 +973,7 @@ FLAC__StreamDecoderWriteStatus verify_write_callback(const FLAC__StreamDecoder *
                }
        }
        /* dequeue the frame from the fifo */
-       for(channel = 0; channel < decoder->channels; channel++) {
+       for(channel = 0; channel < channels; channel++) {
                for(l = 0, r = frame->header.blocksize; r < encoder_wrapper->verify_fifo.tail; l++, r++) {
                        encoder_wrapper->verify_fifo.original[channel][l] = encoder_wrapper->verify_fifo.original[channel][r];
                }
index e1f5cda..9e1d6a6 100644 (file)
@@ -513,7 +513,7 @@ int encode_file(const char *infilename, const char *forced_outfilename)
        char outfilename[4096]; /* @@@ bad MAGIC NUMBER */
        char *p;
        byte lookahead[12];
-       unsigned lookahead_length;
+       unsigned lookahead_length = 0;
        int retval;
        long infilesize;
 
index c5223bd..1a7c935 100644 (file)
@@ -42,8 +42,6 @@ libFLAC_la_SOURCES = \
        bitmath.c \
        cpu.c \
        crc.c \
-       encoder.c \
-       encoder_framing.c \
        file_decoder.c \
        fixed.c \
        format.c \
@@ -51,4 +49,6 @@ libFLAC_la_SOURCES = \
        md5.c \
        memory.c \
        seek_table.c \
-       stream_decoder.c
+       stream_decoder.c \
+       stream_encoder.c \
+       stream_encoder_framing.c
index 635fed2..4a0462d 100644 (file)
@@ -30,8 +30,6 @@ OBJS = \
        bitmath.o \
        cpu.o \
        crc.o \
-       encoder.o \
-       encoder_framing.o \
        file_decoder.o \
        fixed.o \
        format.o \
@@ -40,6 +38,8 @@ OBJS = \
        memory.o \
        seek_table.o \
        stream_decoder.o \
+       stream_encoder.o \
+       stream_encoder_framing.o \
        i386/cpu_asm.o \
        i386/fixed_asm.o \
        i386/lpc_asm.o
index 4ac2aff..d20c86b 100644 (file)
@@ -33,8 +33,6 @@ C_FILES= \
        bitmath.c \\r
        cpu.c \\r
        crc.c \\r
-       encoder.c \\r
-       encoder_framing.c \\r
        file_decoder.c \\r
        fixed.c \\r
        format.c \\r
@@ -42,7 +40,9 @@ C_FILES= \
        md5.c \\r
        memory.c \\r
        seek_table.c \\r
-       stream_decoder.c\r
+       stream_decoder.c \\r
+       stream_encoder.c \\r
+       stream_encoder_framing.c\r
 \r
 NASM_FILES= \\r
        i386/cpu_asm.s \\r
index 079dad0..2c45c9e 100644 (file)
 #include <string.h> /* for strcmp() */
 #include <sys/stat.h> /* for stat() */
 #include "FLAC/assert.h"
-#include "FLAC/file_decoder.h"
+#include "protected/file_decoder.h"
 #include "protected/stream_decoder.h"
 #include "private/md5.h"
 
+/***********************************************************************
+ *
+ * Private class method prototypes
+ *
+ ***********************************************************************/
+
+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);
+
+/***********************************************************************
+ *
+ * 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);
@@ -33,7 +51,7 @@ typedef struct FLAC__FileDecoderPrivate {
        void *client_data;
        FILE *file;
        char *filename; /* == NULL if stdin */
-       FLAC__StreamDecoder *stream;
+       FLAC__StreamDecoder *stream_decoder;
        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 */
@@ -44,11 +62,11 @@ typedef struct FLAC__FileDecoderPrivate {
        uint64 target_sample;
 } 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__FILE_DECODER_OK",
@@ -58,26 +76,64 @@ const char *FLAC__FileDecoderStateString[] = {
        "FLAC__FILE_DECODER_MEMORY_ALLOCATION_ERROR",
        "FLAC__FILE_DECODER_SEEK_ERROR",
        "FLAC__FILE_DECODER_STREAM_ERROR",
+       "FLAC__FILE_DECODER_STREAM_DECODER_ERROR",
+       "FLAC__FILE_DECODER_ALREADY_INITIALIZED",
        "FLAC__FILE_DECODER_UNINITIALIZED"
 };
 
-FLAC__FileDecoder *FLAC__file_decoder_get_new_instance()
+/***********************************************************************
+ *
+ * Class constructor/destructor
+ *
+ ***********************************************************************/
+
+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*)malloc(sizeof(FLAC__FileDecoder));
+       if(decoder == 0) {
+               return 0;
        }
+       decoder->protected = (FLAC__FileDecoderProtected*)malloc(sizeof(FLAC__FileDecoderProtected));
+       if(decoder->protected == 0) {
+               free(decoder);
+               return 0;
+       }
+       decoder->private = (FLAC__FileDecoderPrivate*)malloc(sizeof(FLAC__FileDecoderPrivate));
+       if(decoder->private == 0) {
+               free(decoder->protected);
+               free(decoder);
+               return 0;
+       }
+
+       decoder->protected->state = FLAC__FILE_DECODER_UNINITIALIZED;
+
        return decoder;
 }
 
-void FLAC__file_decoder_free_instance(FLAC__FileDecoder *decoder)
+void FLAC__file_decoder_delete(FLAC__FileDecoder *decoder)
 {
+       FLAC__ASSERT(decoder != 0);
+       FLAC__ASSERT(decoder->protected != 0);
+       FLAC__ASSERT(decoder->private != 0);
+
+       free(decoder->private);
+       free(decoder->protected);
        free(decoder);
 }
 
+/***********************************************************************
+ *
+ * Public class methods
+ *
+ ***********************************************************************/
+
 FLAC__FileDecoderState FLAC__file_decoder_init(
        FLAC__FileDecoder *decoder,
+       bool check_md5,
        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),
@@ -85,41 +141,39 @@ FLAC__FileDecoderState FLAC__file_decoder_init(
        void *client_data
 )
 {
-       FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
        FLAC__ASSERT(decoder != 0);
        FLAC__ASSERT(write_callback != 0);
        FLAC__ASSERT(metadata_callback != 0);
        FLAC__ASSERT(error_callback != 0);
-       FLAC__ASSERT(decoder->state == FLAC__FILE_DECODER_UNINITIALIZED);
-       FLAC__ASSERT(decoder->guts == 0);
 
-       decoder->state = FLAC__FILE_DECODER_OK;
+       if(decoder->protected->state != FLAC__FILE_DECODER_UNINITIALIZED)
+               return decoder->protected->state = FLAC__FILE_DECODER_ALREADY_INITIALIZED;
+
+       decoder->protected->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->protected->check_md5 = check_md5;
 
-       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;
-       decoder->guts->file = 0;
-       decoder->guts->filename = 0;
-       decoder->guts->seek_table = 0;
+       decoder->private->write_callback = write_callback;
+       decoder->private->metadata_callback = metadata_callback;
+       decoder->private->error_callback = error_callback;
+       decoder->private->client_data = client_data;
+       decoder->private->file = 0;
+       decoder->private->stream_decoder = 0;
+       decoder->private->filename = 0;
+       decoder->private->seek_table = 0;
 
        if(0 == strcmp(filename, "-")) {
-               decoder->guts->file = stdin;
+               decoder->private->file = stdin;
        }
        else {
-               if(0 == (decoder->guts->filename = (char*)malloc(strlen(filename)+1)))
-                       return decoder->state = FLAC__FILE_DECODER_MEMORY_ALLOCATION_ERROR;
-               strcpy(decoder->guts->filename, filename);
-               decoder->guts->file = fopen(filename, "rb");
+               if(0 == (decoder->private->filename = (char*)malloc(strlen(filename)+1)))
+                       return decoder->protected->state = FLAC__FILE_DECODER_MEMORY_ALLOCATION_ERROR;
+               strcpy(decoder->private->filename, filename);
+               decoder->private->file = fopen(filename, "rb");
        }
 
-       if(decoder->guts->file == 0)
-               return decoder->state = FLAC__FILE_DECODER_ERROR_OPENING_FILE;
+       if(decoder->private->file == 0)
+               return decoder->protected->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
@@ -127,13 +181,13 @@ FLAC__FileDecoderState FLAC__file_decoder_init(
         * FLAC__file_decoder_finish() to make sure things are always cleaned up
         * properly.
         */
-       MD5Init(&decoder->guts->md5context);
+       MD5Init(&decoder->private->md5context);
 
-       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() */
+       decoder->private->stream_decoder = FLAC__stream_decoder_new();
+       if(FLAC__stream_decoder_init(decoder->private->stream_decoder, read_callback_, write_callback_, metadata_callback_, error_callback_, decoder) != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA)
+               return decoder->protected->state = FLAC__FILE_DECODER_STREAM_DECODER_ERROR;
 
-       return decoder->state;
+       return decoder->protected->state;
 }
 
 bool FLAC__file_decoder_finish(FLAC__FileDecoder *decoder)
@@ -141,48 +195,54 @@ bool FLAC__file_decoder_finish(FLAC__FileDecoder *decoder)
        bool md5_failed = false;
 
        FLAC__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);
-               if(0 != decoder->guts->filename)
-                       free(decoder->guts->filename);
-               /* 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;
-               }
-               free(decoder->guts);
-               decoder->guts = 0;
+       if(decoder->private->file != 0 && decoder->private->file != stdin)
+               fclose(decoder->private->file);
+       if(0 != decoder->private->filename)
+               free(decoder->private->filename);
+       /* see the comment in FLAC__file_decoder_init() as to why we always
+        * call MD5Final()
+        */
+       MD5Final(decoder->private->computed_md5sum, &decoder->private->md5context);
+       if(decoder->private->stream_decoder != 0) {
+               FLAC__stream_decoder_finish(decoder->private->stream_decoder);
+               FLAC__stream_decoder_delete(decoder->private->stream_decoder);
+       }
+       if(decoder->protected->check_md5) {
+               if(memcmp(decoder->private->stored_md5sum, decoder->private->computed_md5sum, 16))
+                       md5_failed = true;
        }
-       decoder->state = FLAC__FILE_DECODER_UNINITIALIZED;
+       decoder->protected->state = FLAC__FILE_DECODER_UNINITIALIZED;
        return !md5_failed;
 }
 
+FLAC__FileDecoderState FLAC__file_decoder_state(const FLAC__FileDecoder *decoder)
+{
+       return decoder->protected->state;
+}
+
+bool FLAC__file_decoder_check_md5(const FLAC__FileDecoder *decoder)
+{
+       return decoder->protected->check_md5;
+}
+
 bool FLAC__file_decoder_process_whole_file(FLAC__FileDecoder *decoder)
 {
        bool ret;
        FLAC__ASSERT(decoder != 0);
 
-       if(decoder->guts->stream->state == FLAC__STREAM_DECODER_END_OF_STREAM)
-               decoder->state = FLAC__FILE_DECODER_END_OF_FILE;
+       if(decoder->private->stream_decoder->protected->state == FLAC__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;
 
-       FLAC__ASSERT(decoder->state == FLAC__FILE_DECODER_OK);
+       FLAC__ASSERT(decoder->protected->state == FLAC__FILE_DECODER_OK);
 
-       ret = FLAC__stream_decoder_process_whole_stream(decoder->guts->stream);
+       ret = FLAC__stream_decoder_process_whole_stream(decoder->private->stream_decoder);
        if(!ret)
-               decoder->state = FLAC__FILE_DECODER_STREAM_ERROR;
+               decoder->protected->state = FLAC__FILE_DECODER_STREAM_ERROR;
 
        return ret;
 }
@@ -192,17 +252,17 @@ bool FLAC__file_decoder_process_metadata(FLAC__FileDecoder *decoder)
        bool ret;
        FLAC__ASSERT(decoder != 0);
 
-       if(decoder->guts->stream->state == FLAC__STREAM_DECODER_END_OF_STREAM)
-               decoder->state = FLAC__FILE_DECODER_END_OF_FILE;
+       if(decoder->private->stream_decoder->protected->state == FLAC__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;
 
-       FLAC__ASSERT(decoder->state == FLAC__FILE_DECODER_OK);
+       FLAC__ASSERT(decoder->protected->state == FLAC__FILE_DECODER_OK);
 
-       ret = FLAC__stream_decoder_process_metadata(decoder->guts->stream);
+       ret = FLAC__stream_decoder_process_metadata(decoder->private->stream_decoder);
        if(!ret)
-               decoder->state = FLAC__FILE_DECODER_STREAM_ERROR;
+               decoder->protected->state = FLAC__FILE_DECODER_STREAM_ERROR;
 
        return ret;
 }
@@ -212,17 +272,17 @@ bool FLAC__file_decoder_process_one_frame(FLAC__FileDecoder *decoder)
        bool ret;
        FLAC__ASSERT(decoder != 0);
 
-       if(decoder->guts->stream->state == FLAC__STREAM_DECODER_END_OF_STREAM)
-               decoder->state = FLAC__FILE_DECODER_END_OF_FILE;
+       if(decoder->private->stream_decoder->protected->state == FLAC__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;
 
-       FLAC__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__stream_decoder_process_one_frame(decoder->private->stream_decoder);
        if(!ret)
-               decoder->state = FLAC__FILE_DECODER_STREAM_ERROR;
+               decoder->protected->state = FLAC__FILE_DECODER_STREAM_ERROR;
 
        return ret;
 }
@@ -232,60 +292,66 @@ bool FLAC__file_decoder_process_remaining_frames(FLAC__FileDecoder *decoder)
        bool ret;
        FLAC__ASSERT(decoder != 0);
 
-       if(decoder->guts->stream->state == FLAC__STREAM_DECODER_END_OF_STREAM)
-               decoder->state = FLAC__FILE_DECODER_END_OF_FILE;
+       if(decoder->private->stream_decoder->protected->state == FLAC__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;
 
-       FLAC__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__stream_decoder_process_remaining_frames(decoder->private->stream_decoder);
        if(!ret)
-               decoder->state = FLAC__FILE_DECODER_STREAM_ERROR;
+               decoder->protected->state = FLAC__FILE_DECODER_STREAM_ERROR;
 
        return ret;
 }
 
+/***********************************************************************
+ *
+ * Private class methods
+ *
+ ***********************************************************************/
+
 bool FLAC__file_decoder_seek_absolute(FLAC__FileDecoder *decoder, uint64 sample)
 {
        long filesize;
        struct stat filestats;
 
        FLAC__ASSERT(decoder != 0);
-       FLAC__ASSERT(decoder->state == FLAC__FILE_DECODER_OK);
+       FLAC__ASSERT(decoder->protected->state == FLAC__FILE_DECODER_OK);
 
-       if(decoder->guts->filename == 0) { /* means the file is stdin... */
-               decoder->state = FLAC__FILE_DECODER_SEEK_ERROR;
+       if(decoder->private->filename == 0) { /* means the file is stdin... */
+               decoder->protected->state = FLAC__FILE_DECODER_SEEK_ERROR;
                return false;
        }
 
-       decoder->state = FLAC__FILE_DECODER_SEEKING;
+       decoder->protected->state = FLAC__FILE_DECODER_SEEKING;
 
        /* turn off md5 checking if a seek is attempted */
-       decoder->check_md5 = false;
+       decoder->protected->check_md5 = false;
 
-       if(!FLAC__stream_decoder_reset(decoder->guts->stream)) {
-               decoder->state = FLAC__FILE_DECODER_STREAM_ERROR;
+       if(!FLAC__stream_decoder_reset(decoder->private->stream_decoder)) {
+               decoder->protected->state = FLAC__FILE_DECODER_STREAM_ERROR;
                return false;
        }
        /* get the file length */
-       if(stat(decoder->guts->filename, &filestats) != 0) {
-               decoder->state = FLAC__FILE_DECODER_SEEK_ERROR;
+       if(stat(decoder->private->filename, &filestats) != 0) {
+               decoder->protected->state = FLAC__FILE_DECODER_SEEK_ERROR;
                return false;
        }
        filesize = filestats.st_size;
        /* rewind */
-       if(0 != fseek(decoder->guts->file, 0, SEEK_SET)) {
-               decoder->state = FLAC__FILE_DECODER_SEEK_ERROR;
+       if(0 != fseek(decoder->private->file, 0, SEEK_SET)) {
+               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__stream_decoder_process_metadata(decoder->private->stream_decoder)) {
+               decoder->protected->state = FLAC__FILE_DECODER_STREAM_ERROR;
                return false;
        }
-       if(sample > decoder->guts->stream_info.total_samples) {
-               decoder->state = FLAC__FILE_DECODER_SEEK_ERROR;
+       if(sample > decoder->private->stream_info.total_samples) {
+               decoder->protected->state = FLAC__FILE_DECODER_SEEK_ERROR;
                return false;
        }
 
@@ -296,15 +362,15 @@ FLAC__StreamDecoderReadStatus read_callback_(const FLAC__StreamDecoder *decoder,
 {
        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;
+       if(feof(file_decoder->private->file)) {
+               file_decoder->protected->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);
+               size_t bytes_read = fread(buffer, sizeof(byte), *bytes, file_decoder->private->file);
                if(bytes_read == 0) {
-                       if(feof(file_decoder->guts->file)) {
-                               file_decoder->state = FLAC__FILE_DECODER_END_OF_FILE;
+                       if(feof(file_decoder->private->file)) {
+                               file_decoder->protected->state = FLAC__FILE_DECODER_END_OF_FILE;
                                return FLAC__STREAM_DECODER_READ_END_OF_STREAM;
                        }
                        else
@@ -324,30 +390,30 @@ FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decode
        FLAC__FileDecoder *file_decoder = (FLAC__FileDecoder *)client_data;
        (void)decoder;
 
-       if(file_decoder->state == FLAC__FILE_DECODER_SEEKING) {
+       if(file_decoder->protected->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;
+               uint64 target_sample = file_decoder->private->target_sample;
 
-               file_decoder->guts->last_frame = *frame; /* save the frame in the guts */
+               file_decoder->private->last_frame = *frame; /* save the frame in the private */
                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;
+                       file_decoder->protected->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;
+                               file_decoder->private->last_frame.header.blocksize -= delta;
+                               file_decoder->private->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);
+                               return file_decoder->private->write_callback(file_decoder, &file_decoder->private->last_frame, newbuffer, file_decoder->private->client_data);
                        }
                        else {
                                /* write the relevant samples */
-                               return file_decoder->guts->write_callback(file_decoder, frame, buffer, file_decoder->guts->client_data);
+                               return file_decoder->private->write_callback(file_decoder, frame, buffer, file_decoder->private->client_data);
                        }
                }
                else {
@@ -355,11 +421,11 @@ FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decode
                }
        }
        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))
+               if(file_decoder->protected->check_md5) {
+                       if(!FLAC__MD5Accumulate(&file_decoder->private->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);
+               return file_decoder->private->write_callback(file_decoder, frame, buffer, file_decoder->private->client_data);
        }
 }
 
@@ -369,18 +435,18 @@ void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMe
        (void)decoder;
 
        if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
-               file_decoder->guts->stream_info = metadata->data.stream_info;
+               file_decoder->private->stream_info = metadata->data.stream_info;
                /* save the MD5 signature for comparison later */
-               memcpy(file_decoder->guts->stored_md5sum, metadata->data.stream_info.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;
+               memcpy(file_decoder->private->stored_md5sum, metadata->data.stream_info.md5sum, 16);
+               if(0 == memcmp(file_decoder->private->stored_md5sum, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16))
+                       file_decoder->protected->check_md5 = false;
        }
        else if(metadata->type == FLAC__METADATA_TYPE_SEEKTABLE) {
-               file_decoder->guts->seek_table = &metadata->data.seek_table;
+               file_decoder->private->seek_table = &metadata->data.seek_table;
        }
 
-       if(file_decoder->state != FLAC__FILE_DECODER_SEEKING)
-               file_decoder->guts->metadata_callback(file_decoder, metadata, file_decoder->guts->client_data);
+       if(file_decoder->protected->state != FLAC__FILE_DECODER_SEEKING)
+               file_decoder->private->metadata_callback(file_decoder, metadata, file_decoder->private->client_data);
 }
 
 void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
@@ -388,8 +454,8 @@ void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErro
        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);
+       if(file_decoder->protected->state != FLAC__FILE_DECODER_SEEKING)
+               file_decoder->private->error_callback(file_decoder, status, file_decoder->private->client_data);
 }
 
 bool seek_to_absolute_sample_(FLAC__FileDecoder *decoder, long filesize, uint64 target_sample)
@@ -399,18 +465,18 @@ bool seek_to_absolute_sample_(FLAC__FileDecoder *decoder, long filesize, uint64
        unsigned approx_bytes_per_frame;
        uint64 last_frame_sample = 0xffffffffffffffff;
        bool needs_seek;
-       const bool is_variable_blocksize_stream = (decoder->guts->stream_info.min_blocksize != decoder->guts->stream_info.max_blocksize);
+       const bool is_variable_blocksize_stream = (decoder->private->stream_info.min_blocksize != decoder->private->stream_info.max_blocksize);
 
        /* we are just guessing here, but we want to guess high, not low */
-       if(decoder->guts->stream_info.max_framesize > 0) {
-               approx_bytes_per_frame = decoder->guts->stream_info.max_framesize;
+       if(decoder->private->stream_info.max_framesize > 0) {
+               approx_bytes_per_frame = decoder->private->stream_info.max_framesize;
        }
        else if(!is_variable_blocksize_stream) {
-               /* note there are no () around 'decoder->guts->stream_info.bits_per_sample/8' to keep precision up since it's an integer calulation */
-               approx_bytes_per_frame = decoder->guts->stream_info.min_blocksize * decoder->guts->stream_info.channels * decoder->guts->stream_info.bits_per_sample/8 + 64;
+               /* note there are no () around 'decoder->private->stream_info.bits_per_sample/8' to keep precision up since it's an integer calulation */
+               approx_bytes_per_frame = decoder->private->stream_info.min_blocksize * decoder->private->stream_info.channels * decoder->private->stream_info.bits_per_sample/8 + 64;
        }
        else
-               approx_bytes_per_frame = 1152 * decoder->guts->stream_info.channels * decoder->guts->stream_info.bits_per_sample/8 + 64;
+               approx_bytes_per_frame = 1152 * decoder->private->stream_info.channels * decoder->private->stream_info.bits_per_sample/8 + 64;
 
        /*
         * The file pointer is currently at the first frame plus any read
@@ -418,11 +484,11 @@ bool seek_to_absolute_sample_(FLAC__FileDecoder *decoder, long filesize, uint64
         * uncomsumed bytes to get the position of the first frame in the
         * file.
         */
-       if(-1 == (first_frame_offset = ftell(decoder->guts->file))) {
-               decoder->state = FLAC__FILE_DECODER_SEEK_ERROR;
+       if(-1 == (first_frame_offset = ftell(decoder->private->file))) {
+               decoder->protected->state = FLAC__FILE_DECODER_SEEK_ERROR;
                return false;
        }
-       first_frame_offset -= FLAC__stream_decoder_input_bytes_unconsumed(decoder->guts->stream);
+       first_frame_offset -= FLAC__stream_decoder_input_bytes_unconsumed(decoder->private->stream_decoder);
        FLAC__ASSERT(first_frame_offset >= 0);
 
        /*
@@ -434,34 +500,34 @@ bool seek_to_absolute_sample_(FLAC__FileDecoder *decoder, long filesize, uint64
        lower_bound = first_frame_offset;
 
        /* calc the upper_bound, beyond which we never want to seek */
-       if(decoder->guts->stream_info.max_framesize > 0)
-               upper_bound = filesize - (decoder->guts->stream_info.max_framesize + 128 + 2); /* 128 for a possible ID3V1 tag, 2 for indexing differences */
+       if(decoder->private->stream_info.max_framesize > 0)
+               upper_bound = filesize - (decoder->private->stream_info.max_framesize + 128 + 2); /* 128 for a possible ID3V1 tag, 2 for indexing differences */
        else
-               upper_bound = filesize - ((decoder->guts->stream_info.channels * decoder->guts->stream_info.bits_per_sample * FLAC__MAX_BLOCK_SIZE) / 8 + 128 + 2);
+               upper_bound = filesize - ((decoder->private->stream_info.channels * decoder->private->stream_info.bits_per_sample * FLAC__MAX_BLOCK_SIZE) / 8 + 128 + 2);
 
        /*
         * Now we refine the bounds if we have a seektable with
         * suitable points.  Note that according to the spec they
         * must be ordered by ascending sample number.
         */
-       if(0 != decoder->guts->seek_table) {
+       if(0 != decoder->private->seek_table) {
                /* find the closest seek point <= target_sample, if it exists */
-               for(i = (int)decoder->guts->seek_table->num_points - 1; i >= 0; i--) {
-                       if(decoder->guts->seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER && decoder->guts->seek_table->points[i].sample_number <= target_sample)
+               for(i = (int)decoder->private->seek_table->num_points - 1; i >= 0; i--) {
+                       if(decoder->private->seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER && decoder->private->seek_table->points[i].sample_number <= target_sample)
                                break;
                }
                if(i >= 0) { /* i.e. we found a suitable seek point... */
-                       lower_bound = first_frame_offset + decoder->guts->seek_table->points[i].stream_offset;
+                       lower_bound = first_frame_offset + decoder->private->seek_table->points[i].stream_offset;
                        lower_seek_point = i;
                }
 
                /* find the closest seek point > target_sample, if it exists */
-               for(i = 0; i < (int)decoder->guts->seek_table->num_points; i++) {
-                       if(decoder->guts->seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER && decoder->guts->seek_table->points[i].sample_number > target_sample)
+               for(i = 0; i < (int)decoder->private->seek_table->num_points; i++) {
+                       if(decoder->private->seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER && decoder->private->seek_table->points[i].sample_number > target_sample)
                                break;
                }
-               if(i < (int)decoder->guts->seek_table->num_points) { /* i.e. we found a suitable seek point... */
-                       upper_bound = first_frame_offset + decoder->guts->seek_table->points[i].stream_offset;
+               if(i < (int)decoder->private->seek_table->num_points) { /* i.e. we found a suitable seek point... */
+                       upper_bound = first_frame_offset + decoder->private->seek_table->points[i].stream_offset;
                        upper_seek_point = i;
                }
        }
@@ -472,12 +538,12 @@ bool seek_to_absolute_sample_(FLAC__FileDecoder *decoder, long filesize, uint64
         */
        if(lower_seek_point >= 0) {
                /* first see if our sample is within a few frames of the lower seekpoint */
-               if(decoder->guts->seek_table->points[lower_seek_point].sample_number <= target_sample && target_sample < decoder->guts->seek_table->points[lower_seek_point].sample_number + (decoder->guts->seek_table->points[lower_seek_point].frame_samples * 4)) {
+               if(decoder->private->seek_table->points[lower_seek_point].sample_number <= target_sample && target_sample < decoder->private->seek_table->points[lower_seek_point].sample_number + (decoder->private->seek_table->points[lower_seek_point].frame_samples * 4)) {
                        pos = lower_bound;
                }
                else if(upper_seek_point >= 0) {
-                       const uint64 target_offset = target_sample - decoder->guts->seek_table->points[lower_seek_point].sample_number;
-                       const uint64 range_samples = decoder->guts->seek_table->points[upper_seek_point].sample_number - decoder->guts->seek_table->points[lower_seek_point].sample_number;
+                       const uint64 target_offset = target_sample - decoder->private->seek_table->points[lower_seek_point].sample_number;
+                       const uint64 range_samples = decoder->private->seek_table->points[upper_seek_point].sample_number - decoder->private->seek_table->points[lower_seek_point].sample_number;
                        const long range_bytes = upper_bound - lower_bound;
 #ifdef _MSC_VER
                        /* with VC++ you have to spoon feed it the casting */
@@ -491,9 +557,9 @@ bool seek_to_absolute_sample_(FLAC__FileDecoder *decoder, long filesize, uint64
                /* We need to use the metadata and the filelength to estimate the position of the frame with the correct sample */
 #ifdef _MSC_VER
                /* with VC++ you have to spoon feed it the casting */
-               pos = first_frame_offset + (long)((double)(int64)target_sample / (double)(int64)decoder->guts->stream_info.total_samples * (double)(filesize-first_frame_offset-1)) - approx_bytes_per_frame;
+               pos = first_frame_offset + (long)((double)(int64)target_sample / (double)(int64)decoder->private->stream_info.total_samples * (double)(filesize-first_frame_offset-1)) - approx_bytes_per_frame;
 #else
-               pos = first_frame_offset + (long)((double)target_sample / (double)decoder->guts->stream_info.total_samples * (double)(filesize-first_frame_offset-1)) - approx_bytes_per_frame;
+               pos = first_frame_offset + (long)((double)target_sample / (double)decoder->private->stream_info.total_samples * (double)(filesize-first_frame_offset-1)) - approx_bytes_per_frame;
 #endif
        }
 
@@ -504,28 +570,28 @@ bool seek_to_absolute_sample_(FLAC__FileDecoder *decoder, long filesize, uint64
                pos = lower_bound;
        needs_seek = true;
 
-       decoder->guts->target_sample = target_sample;
+       decoder->private->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;
+                       if(-1 == fseek(decoder->private->file, pos, SEEK_SET)) {
+                               decoder->protected->state = FLAC__FILE_DECODER_SEEK_ERROR;
                                return false;
                        }
-                       if(!FLAC__stream_decoder_flush(decoder->guts->stream)) {
-                               decoder->state = FLAC__FILE_DECODER_STREAM_ERROR;
+                       if(!FLAC__stream_decoder_flush(decoder->private->stream_decoder)) {
+                               decoder->protected->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;
+               if(!FLAC__stream_decoder_process_one_frame(decoder->private->stream_decoder)) {
+                       decoder->protected->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) {
+               if(decoder->protected->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;
+                       uint64 this_frame_sample = decoder->private->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);
@@ -534,17 +600,17 @@ bool seek_to_absolute_sample_(FLAC__FileDecoder *decoder, long filesize, uint64
                        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;
+                                       approx_bytes_per_frame = decoder->private->last_frame.header.blocksize * decoder->private->last_frame.header.channels * decoder->private->last_frame.header.bits_per_sample/8 + 64;
                                        pos -= approx_bytes_per_frame;
                                        needs_seek = true;
                                }
                                else { /* target_sample >= this_frame_sample + this frame's blocksize */
                                        last_pos = pos;
-                                       if(-1 == (pos = ftell(decoder->guts->file))) {
-                                               decoder->state = FLAC__FILE_DECODER_SEEK_ERROR;
+                                       if(-1 == (pos = ftell(decoder->private->file))) {
+                                               decoder->protected->state = FLAC__FILE_DECODER_SEEK_ERROR;
                                                return false;
                                        }
-                                       pos -= FLAC__stream_decoder_input_bytes_unconsumed(decoder->guts->stream);
+                                       pos -= FLAC__stream_decoder_input_bytes_unconsumed(decoder->private->stream_decoder);
                                        needs_seek = false;
                                }
                        }
index a3bc2b1..21c3637 100644 (file)
 #  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 #  Boston, MA  02111-1307, USA.
 
-noinst_HEADERS = bitbuffer.h \
+noinst_HEADERS = \
        all.h \
+       bitbuffer.h \
        bitmath.h \
        cpu.h \
        crc.h \
-       encoder_framing.h \
        fixed.h \
        lpc.h \
        md5.h \
-       memory.h
+       memory.h \
+       stream_encoder_framing.h
index 3f6a17e..6480d41 100644 (file)
 #include "bitmath.h"
 #include "cpu.h"
 #include "crc.h"
-#include "encoder_framing.h"
 #include "fixed.h"
 #include "lpc.h"
 #include "md5.h"
 #include "memory.h"
+#include "stream_encoder_framing.h"
 
 #endif
index a61ed4f..f3056d8 100644 (file)
@@ -17,8 +17,8 @@
  * Boston, MA  02111-1307, USA.
  */
 
-#ifndef FLAC__PRIVATE__ENCODER_FRAMING_H
-#define FLAC__PRIVATE__ENCODER_FRAMING_H
+#ifndef FLAC__PRIVATE__STREAM_ENCODER_FRAMING_H
+#define FLAC__PRIVATE__STREAM_ENCODER_FRAMING_H
 
 #include "FLAC/format.h"
 #include "bitbuffer.h"
index 7dd4216..a9f7b55 100644 (file)
@@ -16,4 +16,7 @@
 #  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 #  Boston, MA  02111-1307, USA.
 
-noinst_HEADERS = stream_decoder.h
+noinst_HEADERS = \
+       file_decoder.h \
+       stream_decoder.h \
+       stream_encoder.h
index 3259dea..df15db2 100644 (file)
 
 #include "FLAC/stream_decoder.h"
 
-/* only useful to the file_decoder */
-unsigned FLAC__stream_decoder_input_bytes_unconsumed(FLAC__StreamDecoder *decoder);
+typedef struct FLAC__StreamDecoderProtected {
+       FLAC__StreamDecoderState state;
+       unsigned channels;
+       FLAC__ChannelAssignment channel_assignment;
+       unsigned bits_per_sample;
+       unsigned sample_rate; /* in Hz */
+       unsigned blocksize; /* in samples (per channel) */
+} FLAC__StreamDecoderProtected;
+
+/*
+ * return the number of input bytes consumed
+ */
+unsigned FLAC__stream_decoder_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder);
 
 #endif
index 9144077..0fb1bae 100644 (file)
 #include <stdlib.h> /* for malloc() */
 #include <string.h> /* for memset/memcpy() */
 #include "FLAC/assert.h"
-#include "FLAC/stream_decoder.h"
+#include "protected/stream_decoder.h"
 #include "private/bitbuffer.h"
 #include "private/cpu.h"
 #include "private/crc.h"
 #include "private/fixed.h"
 #include "private/lpc.h"
 
+/***********************************************************************
+ *
+ * Private static data
+ *
+ ***********************************************************************/
+
+static byte ID3V2_TAG_[3] = { 'I', 'D', '3' };
+
+/***********************************************************************
+ *
+ * Private class method prototypes
+ *
+ ***********************************************************************/
+
+static bool stream_decoder_allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels);
+static bool stream_decoder_find_metadata_(FLAC__StreamDecoder *decoder);
+static bool stream_decoder_read_metadata_(FLAC__StreamDecoder *decoder);
+static bool stream_decoder_skip_id3v2_tag_(FLAC__StreamDecoder *decoder);
+static bool stream_decoder_frame_sync_(FLAC__StreamDecoder *decoder);
+static bool stream_decoder_read_frame_(FLAC__StreamDecoder *decoder, bool *got_a_frame);
+static bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder);
+static bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
+static bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
+static bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
+static bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
+static bool stream_decoder_read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
+static bool stream_decoder_read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, int32 *residual);
+static bool stream_decoder_read_zero_padding_(FLAC__StreamDecoder *decoder);
+static bool read_callback_(byte buffer[], unsigned *bytes, void *client_data);
+
+/***********************************************************************
+ *
+ * Private class data
+ *
+ ***********************************************************************/
+
 typedef struct FLAC__StreamDecoderPrivate {
        FLAC__StreamDecoderReadStatus (*read_callback)(const FLAC__StreamDecoder *decoder, byte buffer[], unsigned *bytes, void *client_data);
        FLAC__StreamDecoderWriteStatus (*write_callback)(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const int32 *buffer[], void *client_data);
@@ -52,23 +88,11 @@ typedef struct FLAC__StreamDecoderPrivate {
        byte lookahead; /* temp storage when we need to look ahead one byte in the stream */
 } FLAC__StreamDecoderPrivate;
 
-static byte ID3V2_TAG_[3] = { 'I', 'D', '3' };
-
-static bool stream_decoder_allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels);
-static bool stream_decoder_find_metadata_(FLAC__StreamDecoder *decoder);
-static bool stream_decoder_read_metadata_(FLAC__StreamDecoder *decoder);
-static bool stream_decoder_skip_id3v2_tag_(FLAC__StreamDecoder *decoder);
-static bool stream_decoder_frame_sync_(FLAC__StreamDecoder *decoder);
-static bool stream_decoder_read_frame_(FLAC__StreamDecoder *decoder, bool *got_a_frame);
-static bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder);
-static bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
-static bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
-static bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
-static bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
-static bool stream_decoder_read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
-static bool stream_decoder_read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, int32 *residual);
-static bool stream_decoder_read_zero_padding_(FLAC__StreamDecoder *decoder);
-static bool read_callback_(byte buffer[], unsigned *bytes, void *client_data);
+/***********************************************************************
+ *
+ * Public static class data
+ *
+ ***********************************************************************/
 
 const char *FLAC__StreamDecoderStateString[] = {
        "FLAC__STREAM_DECODER_SEARCH_FOR_METADATA",
@@ -79,6 +103,7 @@ const char *FLAC__StreamDecoderStateString[] = {
        "FLAC__STREAM_DECODER_ABORTED",
        "FLAC__STREAM_DECODER_UNPARSEABLE_STREAM",
        "FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR",
+       "FLAC__STREAM_DECODER_ALREADY_INITIALIZED",
        "FLAC__STREAM_DECODER_UNINITIALIZED"
 };
 
@@ -99,130 +124,185 @@ const char *FLAC__StreamDecoderErrorStatusString[] = {
        "FLAC__STREAM_DECODER_ERROR_FRAME_CRC_MISMATCH"
 };
 
-FLAC__StreamDecoder *FLAC__stream_decoder_get_new_instance()
+/***********************************************************************
+ *
+ * Class constructor/destructor
+ *
+ ***********************************************************************/
+FLAC__StreamDecoder *FLAC__stream_decoder_new()
 {
-       FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder*)malloc(sizeof(FLAC__StreamDecoder));
-       if(decoder != 0) {
-               decoder->state = FLAC__STREAM_DECODER_UNINITIALIZED;
-               decoder->guts = 0;
+       FLAC__StreamDecoder *decoder;
+
+       FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
+
+       decoder = (FLAC__StreamDecoder*)malloc(sizeof(FLAC__StreamDecoder));
+       if(decoder == 0) {
+               return 0;
        }
+       decoder->protected = (FLAC__StreamDecoderProtected*)malloc(sizeof(FLAC__StreamDecoderProtected));
+       if(decoder->protected == 0) {
+               free(decoder);
+               return 0;
+       }
+       decoder->private = (FLAC__StreamDecoderPrivate*)malloc(sizeof(FLAC__StreamDecoderPrivate));
+       if(decoder->private == 0) {
+               free(decoder->protected);
+               free(decoder);
+               return 0;
+       }
+
+       decoder->protected->state = FLAC__STREAM_DECODER_UNINITIALIZED;
+
        return decoder;
 }
 
-void FLAC__stream_decoder_free_instance(FLAC__StreamDecoder *decoder)
+void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder)
 {
+       FLAC__ASSERT(decoder != 0);
+       FLAC__ASSERT(decoder->protected != 0);
+       FLAC__ASSERT(decoder->private != 0);
+
+       free(decoder->private);
+       free(decoder->protected);
        free(decoder);
 }
 
+/***********************************************************************
+ *
+ * Public class methods
+ *
+ ***********************************************************************/
+
 FLAC__StreamDecoderState FLAC__stream_decoder_init(
        FLAC__StreamDecoder *decoder,
-       FLAC__StreamDecoderReadStatus (*read_callback)(const FLAC__StreamDecoder *decoder, byte buffer[], unsigned *bytes, void *client_data),
-       FLAC__StreamDecoderWriteStatus (*write_callback)(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const int32 *buffer[], void *client_data),
-       void (*metadata_callback)(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data),
-       void (*error_callback)(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data),
-       void *client_data
-)
+    FLAC__StreamDecoderReadStatus (*read_callback)(const FLAC__StreamDecoder *decoder, byte buffer[], unsigned *bytes, void *client_data),
+    FLAC__StreamDecoderWriteStatus (*write_callback)(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const int32 *buffer[], void *client_data),
+    void (*metadata_callback)(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data),
+    void (*error_callback)(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data),
+    void *client_data)
 {
        unsigned i;
 
-       FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
        FLAC__ASSERT(decoder != 0);
        FLAC__ASSERT(read_callback != 0);
        FLAC__ASSERT(write_callback != 0);
        FLAC__ASSERT(metadata_callback != 0);
        FLAC__ASSERT(error_callback != 0);
-       FLAC__ASSERT(decoder->state == FLAC__STREAM_DECODER_UNINITIALIZED);
-       FLAC__ASSERT(decoder->guts == 0);
 
-       decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
+       if(decoder->protected->state != FLAC__STREAM_DECODER_UNINITIALIZED)
+               return decoder->protected->state = FLAC__STREAM_DECODER_ALREADY_INITIALIZED;
 
-       decoder->guts = (FLAC__StreamDecoderPrivate*)malloc(sizeof(FLAC__StreamDecoderPrivate));
-       if(decoder->guts == 0)
-               return decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
+       decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
 
-       decoder->guts->read_callback = read_callback;
-       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->private->read_callback = read_callback;
+       decoder->private->write_callback = write_callback;
+       decoder->private->metadata_callback = metadata_callback;
+       decoder->private->error_callback = error_callback;
+       decoder->private->client_data = client_data;
 
-       FLAC__bitbuffer_init(&decoder->guts->input);
+       FLAC__bitbuffer_init(&decoder->private->input);
 
        for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
-               decoder->guts->output[i] = 0;
-               decoder->guts->residual[i] = 0;
+               decoder->private->output[i] = 0;
+               decoder->private->residual[i] = 0;
        }
 
-       decoder->guts->output_capacity = 0;
-       decoder->guts->output_channels = 0;
-       decoder->guts->last_frame_number = 0;
-       decoder->guts->samples_decoded = 0;
-       decoder->guts->has_stream_info = false;
-       decoder->guts->has_seek_table = false;
-       decoder->guts->cached = false;
+       decoder->private->output_capacity = 0;
+       decoder->private->output_channels = 0;
+       decoder->private->last_frame_number = 0;
+       decoder->private->samples_decoded = 0;
+       decoder->private->has_stream_info = false;
+       decoder->private->has_seek_table = false;
+       decoder->private->cached = false;
 
        /*
         * get the CPU info and set the function pointers
         */
-       FLAC__cpu_info(&decoder->guts->cpuinfo);
+       FLAC__cpu_info(&decoder->private->cpuinfo);
        /* first default to the non-asm routines */
-       decoder->guts->local_lpc_restore_signal = FLAC__lpc_restore_signal;
-       decoder->guts->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal;
+       decoder->private->local_lpc_restore_signal = FLAC__lpc_restore_signal;
+       decoder->private->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal;
        /* now override with asm where appropriate */
 #ifndef FLAC__NO_ASM
-       FLAC__ASSERT(decoder->guts->cpuinfo.use_asm);
+       FLAC__ASSERT(decoder->private->cpuinfo.use_asm);
 #ifdef FLAC__CPU_IA32
-       FLAC__ASSERT(decoder->guts->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
+       FLAC__ASSERT(decoder->private->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
 #ifdef FLAC__HAS_NASM
-       if(decoder->guts->cpuinfo.data.ia32.mmx) {
-               decoder->guts->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_i386;
-               decoder->guts->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_i386_mmx;
+       if(decoder->private->cpuinfo.data.ia32.mmx) {
+               decoder->private->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_i386;
+               decoder->private->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_i386_mmx;
        }
        else {
-               decoder->guts->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_i386;
-               decoder->guts->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_i386;
+               decoder->private->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_i386;
+               decoder->private->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_i386;
        }
 #endif
 #endif
 #endif
 
-       return decoder->state;
+       return decoder->protected->state;
 }
 
 void FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
 {
        unsigned i;
        FLAC__ASSERT(decoder != 0);
-       if(decoder->state == FLAC__STREAM_DECODER_UNINITIALIZED)
+       if(decoder->protected->state == FLAC__STREAM_DECODER_UNINITIALIZED)
                return;
-       if(decoder->guts != 0) {
-               if(decoder->guts->has_seek_table) {
-                       free(decoder->guts->seek_table.data.seek_table.points);
-                       decoder->guts->seek_table.data.seek_table.points = 0;
+       if(decoder->private->has_seek_table) {
+               free(decoder->private->seek_table.data.seek_table.points);
+               decoder->private->seek_table.data.seek_table.points = 0;
+       }
+       FLAC__bitbuffer_free(&decoder->private->input);
+       for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
+               if(decoder->private->output[i] != 0) {
+                       free(decoder->private->output[i]);
+                       decoder->private->output[i] = 0;
                }
-               FLAC__bitbuffer_free(&decoder->guts->input);
-               for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
-                       if(decoder->guts->output[i] != 0) {
-                               free(decoder->guts->output[i]);
-                               decoder->guts->output[i] = 0;
-                       }
-                       if(decoder->guts->residual[i] != 0) {
-                               free(decoder->guts->residual[i]);
-                               decoder->guts->residual[i] = 0;
-                       }
+               if(decoder->private->residual[i] != 0) {
+                       free(decoder->private->residual[i]);
+                       decoder->private->residual[i] = 0;
                }
-               free(decoder->guts);
-               decoder->guts = 0;
        }
-       decoder->state = FLAC__STREAM_DECODER_UNINITIALIZED;
+       decoder->protected->state = FLAC__STREAM_DECODER_UNINITIALIZED;
+}
+
+FLAC__StreamDecoderState FLAC__stream_decoder_state(const FLAC__StreamDecoder *decoder)
+{
+       return decoder->protected->state;
+}
+
+unsigned FLAC__stream_decoder_channels(const FLAC__StreamDecoder *decoder)
+{
+       return decoder->protected->channels;
+}
+
+FLAC__ChannelAssignment FLAC__stream_decoder_channel_assignment(const FLAC__StreamDecoder *decoder)
+{
+       return decoder->protected->channel_assignment;
+}
+
+unsigned FLAC__stream_decoder_bits_per_sample(const FLAC__StreamDecoder *decoder)
+{
+       return decoder->protected->bits_per_sample;
+}
+
+unsigned FLAC__stream_decoder_sample_rate(const FLAC__StreamDecoder *decoder)
+{
+       return decoder->protected->sample_rate;
+}
+
+unsigned FLAC__stream_decoder_blocksize(const FLAC__StreamDecoder *decoder)
+{
+       return decoder->protected->blocksize;
 }
 
 bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
 {
        FLAC__ASSERT(decoder != 0);
 
-       if(!FLAC__bitbuffer_clear(&decoder->guts->input)) {
-               decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
+       if(!FLAC__bitbuffer_clear(&decoder->private->input)) {
+               decoder->protected->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
                return false;
        }
 
@@ -234,12 +314,12 @@ bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
        FLAC__ASSERT(decoder != 0);
 
        if(!FLAC__stream_decoder_flush(decoder)) {
-               decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
+               decoder->protected->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
                return false;
        }
-       decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
+       decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
 
-       decoder->guts->samples_decoded = 0;
+       decoder->private->samples_decoded = 0;
 
        return true;
 }
@@ -249,18 +329,18 @@ bool FLAC__stream_decoder_process_whole_stream(FLAC__StreamDecoder *decoder)
        bool dummy;
        FLAC__ASSERT(decoder != 0);
 
-       if(decoder->state == FLAC__STREAM_DECODER_END_OF_STREAM)
+       if(decoder->protected->state == FLAC__STREAM_DECODER_END_OF_STREAM)
                return true;
 
-       FLAC__ASSERT(decoder->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA);
+       FLAC__ASSERT(decoder->protected->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA);
 
        if(!FLAC__stream_decoder_reset(decoder)) {
-               decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
+               decoder->protected->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
                return false;
        }
 
        while(1) {
-               switch(decoder->state) {
+               switch(decoder->protected->state) {
                        case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
                                if(!stream_decoder_find_metadata_(decoder))
                                        return false; /* above function sets the status for us */
@@ -289,18 +369,18 @@ bool FLAC__stream_decoder_process_metadata(FLAC__StreamDecoder *decoder)
 {
        FLAC__ASSERT(decoder != 0);
 
-       if(decoder->state == FLAC__STREAM_DECODER_END_OF_STREAM)
+       if(decoder->protected->state == FLAC__STREAM_DECODER_END_OF_STREAM)
                return true;
 
-       FLAC__ASSERT(decoder->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA);
+       FLAC__ASSERT(decoder->protected->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA);
 
        if(!FLAC__stream_decoder_reset(decoder)) {
-               decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
+               decoder->protected->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
                return false;
        }
 
        while(1) {
-               switch(decoder->state) {
+               switch(decoder->protected->state) {
                        case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
                                if(!stream_decoder_find_metadata_(decoder))
                                        return false; /* above function sets the status for us */
@@ -325,13 +405,13 @@ bool FLAC__stream_decoder_process_one_frame(FLAC__StreamDecoder *decoder)
        bool got_a_frame;
        FLAC__ASSERT(decoder != 0);
 
-       if(decoder->state == FLAC__STREAM_DECODER_END_OF_STREAM)
+       if(decoder->protected->state == FLAC__STREAM_DECODER_END_OF_STREAM)
                return true;
 
-       FLAC__ASSERT(decoder->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC);
+       FLAC__ASSERT(decoder->protected->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC);
 
        while(1) {
-               switch(decoder->state) {
+               switch(decoder->protected->state) {
                        case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
                                if(!stream_decoder_frame_sync_(decoder))
                                        return true; /* above function sets the status for us */
@@ -355,13 +435,13 @@ bool FLAC__stream_decoder_process_remaining_frames(FLAC__StreamDecoder *decoder)
        bool dummy;
        FLAC__ASSERT(decoder != 0);
 
-       if(decoder->state == FLAC__STREAM_DECODER_END_OF_STREAM)
+       if(decoder->protected->state == FLAC__STREAM_DECODER_END_OF_STREAM)
                return true;
 
-       FLAC__ASSERT(decoder->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC);
+       FLAC__ASSERT(decoder->protected->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC);
 
        while(1) {
-               switch(decoder->state) {
+               switch(decoder->protected->state) {
                        case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
                                if(!stream_decoder_frame_sync_(decoder))
                                        return true; /* above function sets the status for us */
@@ -378,51 +458,63 @@ bool FLAC__stream_decoder_process_remaining_frames(FLAC__StreamDecoder *decoder)
        }
 }
 
-unsigned FLAC__stream_decoder_input_bytes_unconsumed(FLAC__StreamDecoder *decoder)
+/***********************************************************************
+ *
+ * Protected class methods
+ *
+ ***********************************************************************/
+
+unsigned FLAC__stream_decoder_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder)
 {
        FLAC__ASSERT(decoder != 0);
-       return decoder->guts->input.bytes - decoder->guts->input.consumed_bytes;
+       return decoder->private->input.bytes - decoder->private->input.consumed_bytes;
 }
 
+/***********************************************************************
+ *
+ * Private class methods
+ *
+ ***********************************************************************/
+
 bool stream_decoder_allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels)
 {
        unsigned i;
        int32 *tmp;
 
-       if(size <= decoder->guts->output_capacity && channels <= decoder->guts->output_channels)
+       if(size <= decoder->private->output_capacity && channels <= decoder->private->output_channels)
                return true;
 
        /* @@@ should change to use realloc() */
 
        for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
-               if(decoder->guts->output[i] != 0) {
-                       free(decoder->guts->output[i]);
-                       decoder->guts->output[i] = 0;
+               if(decoder->private->output[i] != 0) {
+                       free(decoder->private->output[i]);
+                       decoder->private->output[i] = 0;
                }
-               if(decoder->guts->residual[i] != 0) {
-                       free(decoder->guts->residual[i]);
-                       decoder->guts->residual[i] = 0;
+               if(decoder->private->residual[i] != 0) {
+                       free(decoder->private->residual[i]);
+                       decoder->private->residual[i] = 0;
                }
        }
 
        for(i = 0; i < channels; i++) {
                tmp = (int32*)malloc(sizeof(int32)*size);
                if(tmp == 0) {
-                       decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
+                       decoder->protected->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
                        return false;
                }
-               decoder->guts->output[i] = tmp;
+               decoder->private->output[i] = tmp;
 
                tmp = (int32*)malloc(sizeof(int32)*size);
                if(tmp == 0) {
-                       decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
+                       decoder->protected->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
                        return false;
                }
-               decoder->guts->residual[i] = tmp;
+               decoder->private->residual[i] = tmp;
        }
 
-       decoder->guts->output_capacity = size;
-       decoder->guts->output_channels = channels;
+       decoder->private->output_capacity = size;
+       decoder->private->output_channels = channels;
 
        return true;
 }
@@ -433,15 +525,15 @@ bool stream_decoder_find_metadata_(FLAC__StreamDecoder *decoder)
        unsigned i, id;
        bool first = true;
 
-       FLAC__ASSERT(decoder->guts->input.consumed_bits == 0); /* make sure we're byte aligned */
+       FLAC__ASSERT(decoder->private->input.consumed_bits == 0); /* make sure we're byte aligned */
 
        for(i = id = 0; i < 4; ) {
-               if(decoder->guts->cached) {
-                       x = (uint32)decoder->guts->lookahead;
-                       decoder->guts->cached = false;
+               if(decoder->private->cached) {
+                       x = (uint32)decoder->private->lookahead;
+                       decoder->private->cached = false;
                }
                else {
-                       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
+                       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8, read_callback_, decoder))
                                return false; /* the read_callback_ sets the state for us */
                }
                if(x == FLAC__STREAM_SYNC_STRING[i]) {
@@ -460,30 +552,30 @@ bool stream_decoder_find_metadata_(FLAC__StreamDecoder *decoder)
                        continue;
                }
                if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
-                       decoder->guts->header_warmup[0] = (byte)x;
-                       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
+                       decoder->private->header_warmup[0] = (byte)x;
+                       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8, read_callback_, decoder))
                                return false; /* the read_callback_ sets the state for us */
 
                        /* we have to check if we just read two 0xff's in a row; the second may actually be the beginning of the sync code */
                        /* else we have to check if the second byte is the end of a sync code */
                        if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
-                               decoder->guts->lookahead = (byte)x;
-                               decoder->guts->cached = true;
+                               decoder->private->lookahead = (byte)x;
+                               decoder->private->cached = true;
                        }
                        else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
-                               decoder->guts->header_warmup[1] = (byte)x;
-                               decoder->state = FLAC__STREAM_DECODER_READ_FRAME;
+                               decoder->private->header_warmup[1] = (byte)x;
+                               decoder->protected->state = FLAC__STREAM_DECODER_READ_FRAME;
                                return true;
                        }
                }
                i = 0;
                if(first) {
-                       decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
+                       decoder->private->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private->client_data);
                        first = false;
                }
        }
 
-       decoder->state = FLAC__STREAM_DECODER_READ_METADATA;
+       decoder->protected->state = FLAC__STREAM_DECODER_READ_METADATA;
        return true;
 }
 
@@ -492,63 +584,63 @@ bool stream_decoder_read_metadata_(FLAC__StreamDecoder *decoder)
        uint32 i, x, last_block, type, length;
        uint64 xx;
 
-       FLAC__ASSERT(decoder->guts->input.consumed_bits == 0); /* make sure we're byte aligned */
+       FLAC__ASSERT(decoder->private->input.consumed_bits == 0); /* make sure we're byte aligned */
 
-       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &last_block, FLAC__STREAM_METADATA_IS_LAST_LEN, read_callback_, decoder))
+       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &last_block, FLAC__STREAM_METADATA_IS_LAST_LEN, read_callback_, decoder))
                return false; /* the read_callback_ sets the state for us */
-       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &type, FLAC__STREAM_METADATA_TYPE_LEN, read_callback_, decoder))
+       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &type, FLAC__STREAM_METADATA_TYPE_LEN, read_callback_, decoder))
                return false; /* the read_callback_ sets the state for us */
-       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN, read_callback_, decoder))
+       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN, read_callback_, decoder))
                return false; /* the read_callback_ sets the state for us */
        if(type == FLAC__METADATA_TYPE_STREAMINFO) {
                unsigned used_bits = 0;
-               decoder->guts->stream_info.type = type;
-               decoder->guts->stream_info.is_last = last_block;
-               decoder->guts->stream_info.length = length;
+               decoder->private->stream_info.type = type;
+               decoder->private->stream_info.is_last = last_block;
+               decoder->private->stream_info.length = length;
 
-               if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN, read_callback_, decoder))
+               if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN, read_callback_, decoder))
                        return false; /* the read_callback_ sets the state for us */
-               decoder->guts->stream_info.data.stream_info.min_blocksize = x;
+               decoder->private->stream_info.data.stream_info.min_blocksize = x;
                used_bits += FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN;
 
-               if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN, read_callback_, decoder))
+               if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN, read_callback_, decoder))
                        return false; /* the read_callback_ sets the state for us */
-               decoder->guts->stream_info.data.stream_info.max_blocksize = x;
+               decoder->private->stream_info.data.stream_info.max_blocksize = x;
                used_bits += FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN;
 
-               if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN, read_callback_, decoder))
+               if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN, read_callback_, decoder))
                        return false; /* the read_callback_ sets the state for us */
-               decoder->guts->stream_info.data.stream_info.min_framesize = x;
+               decoder->private->stream_info.data.stream_info.min_framesize = x;
                used_bits += FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN;
 
-               if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN, read_callback_, decoder))
+               if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN, read_callback_, decoder))
                        return false; /* the read_callback_ sets the state for us */
-               decoder->guts->stream_info.data.stream_info.max_framesize = x;
+               decoder->private->stream_info.data.stream_info.max_framesize = x;
                used_bits += FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN;
 
-               if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN, read_callback_, decoder))
+               if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN, read_callback_, decoder))
                        return false; /* the read_callback_ sets the state for us */
-               decoder->guts->stream_info.data.stream_info.sample_rate = x;
+               decoder->private->stream_info.data.stream_info.sample_rate = x;
                used_bits += FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN;
 
-               if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN, read_callback_, decoder))
+               if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN, read_callback_, decoder))
                        return false; /* the read_callback_ sets the state for us */
-               decoder->guts->stream_info.data.stream_info.channels = x+1;
+               decoder->private->stream_info.data.stream_info.channels = x+1;
                used_bits += FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN;
 
-               if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN, read_callback_, decoder))
+               if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN, read_callback_, decoder))
                        return false; /* the read_callback_ sets the state for us */
-               decoder->guts->stream_info.data.stream_info.bits_per_sample = x+1;
+               decoder->private->stream_info.data.stream_info.bits_per_sample = x+1;
                used_bits += FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN;
 
-               if(!FLAC__bitbuffer_read_raw_uint64(&decoder->guts->input, &decoder->guts->stream_info.data.stream_info.total_samples, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN, read_callback_, decoder))
+               if(!FLAC__bitbuffer_read_raw_uint64(&decoder->private->input, &decoder->private->stream_info.data.stream_info.total_samples, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN, read_callback_, decoder))
                        return false; /* the read_callback_ sets the state for us */
                used_bits += FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN;
 
                for(i = 0; i < 16; i++) {
-                       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
+                       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8, read_callback_, decoder))
                                return false; /* the read_callback_ sets the state for us */
-                       decoder->guts->stream_info.data.stream_info.md5sum[i] = (byte)x;
+                       decoder->private->stream_info.data.stream_info.md5sum[i] = (byte)x;
                }
                used_bits += i*8;
 
@@ -556,57 +648,57 @@ bool stream_decoder_read_metadata_(FLAC__StreamDecoder *decoder)
                FLAC__ASSERT(used_bits % 8 == 0);
                length -= (used_bits / 8);
                for(i = 0; i < length; i++) {
-                       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
+                       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8, read_callback_, decoder))
                                return false; /* the read_callback_ sets the state for us */
                }
 
-               decoder->guts->has_stream_info = true;
-               decoder->guts->metadata_callback(decoder, &decoder->guts->stream_info, decoder->guts->client_data);
+               decoder->private->has_stream_info = true;
+               decoder->private->metadata_callback(decoder, &decoder->private->stream_info, decoder->private->client_data);
        }
        else if(type == FLAC__METADATA_TYPE_SEEKTABLE) {
                unsigned real_points;
 
-               decoder->guts->seek_table.type = type;
-               decoder->guts->seek_table.is_last = last_block;
-               decoder->guts->seek_table.length = length;
+               decoder->private->seek_table.type = type;
+               decoder->private->seek_table.is_last = last_block;
+               decoder->private->seek_table.length = length;
 
-               decoder->guts->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LEN;
+               decoder->private->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LEN;
 
-               if(0 == (decoder->guts->seek_table.data.seek_table.points = (FLAC__StreamMetaData_SeekPoint*)malloc(decoder->guts->seek_table.data.seek_table.num_points * sizeof(FLAC__StreamMetaData_SeekPoint)))) {
-                       decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
+               if(0 == (decoder->private->seek_table.data.seek_table.points = (FLAC__StreamMetaData_SeekPoint*)malloc(decoder->private->seek_table.data.seek_table.num_points * sizeof(FLAC__StreamMetaData_SeekPoint)))) {
+                       decoder->protected->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
                        return false;
                }
-               for(i = real_points = 0; i < decoder->guts->seek_table.data.seek_table.num_points; i++) {
-                       if(!FLAC__bitbuffer_read_raw_uint64(&decoder->guts->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN, read_callback_, decoder))
+               for(i = real_points = 0; i < decoder->private->seek_table.data.seek_table.num_points; i++) {
+                       if(!FLAC__bitbuffer_read_raw_uint64(&decoder->private->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN, read_callback_, decoder))
                                return false; /* the read_callback_ sets the state for us */
-                       decoder->guts->seek_table.data.seek_table.points[real_points].sample_number = xx;
+                       decoder->private->seek_table.data.seek_table.points[real_points].sample_number = xx;
 
-                       if(!FLAC__bitbuffer_read_raw_uint64(&decoder->guts->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN, read_callback_, decoder))
+                       if(!FLAC__bitbuffer_read_raw_uint64(&decoder->private->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN, read_callback_, decoder))
                                return false; /* the read_callback_ sets the state for us */
-                       decoder->guts->seek_table.data.seek_table.points[real_points].stream_offset = xx;
+                       decoder->private->seek_table.data.seek_table.points[real_points].stream_offset = xx;
 
-                       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN, read_callback_, decoder))
+                       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN, read_callback_, decoder))
                                return false; /* the read_callback_ sets the state for us */
-                       decoder->guts->seek_table.data.seek_table.points[real_points].frame_samples = x;
+                       decoder->private->seek_table.data.seek_table.points[real_points].frame_samples = x;
 
-                       if(decoder->guts->seek_table.data.seek_table.points[real_points].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER)
+                       if(decoder->private->seek_table.data.seek_table.points[real_points].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER)
                                real_points++;
                }
-               decoder->guts->seek_table.data.seek_table.num_points = real_points;
+               decoder->private->seek_table.data.seek_table.num_points = real_points;
 
-               decoder->guts->has_seek_table = true;
-               decoder->guts->metadata_callback(decoder, &decoder->guts->seek_table, decoder->guts->client_data);
+               decoder->private->has_seek_table = true;
+               decoder->private->metadata_callback(decoder, &decoder->private->seek_table, decoder->private->client_data);
        }
        else {
                /* skip other metadata blocks */
                for(i = 0; i < length; i++) {
-                       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
+                       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8, read_callback_, decoder))
                                return false; /* the read_callback_ sets the state for us */
                }
        }
 
        if(last_block)
-               decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
+               decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
 
        return true;
 }
@@ -617,19 +709,19 @@ bool stream_decoder_skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
        unsigned i, skip;
 
        /* skip the version and flags bytes */
-       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 24, read_callback_, decoder))
+       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 24, read_callback_, decoder))
                return false; /* the read_callback_ sets the state for us */
        /* get the size (in bytes) to skip */
        skip = 0;
        for(i = 0; i < 4; i++) {
-               if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
+               if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8, read_callback_, decoder))
                        return false; /* the read_callback_ sets the state for us */
                skip <<= 7;
                skip |= (x & 0x7f);
        }
        /* skip the rest of the tag */
        for(i = 0; i < skip; i++) {
-               if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
+               if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8, read_callback_, decoder))
                        return false; /* the read_callback_ sets the state for us */
        }
        return true;
@@ -642,47 +734,47 @@ bool stream_decoder_frame_sync_(FLAC__StreamDecoder *decoder)
 
        /* If we know the total number of samples in the stream, stop if we've read that many. */
        /* This will stop us, for example, from wasting time trying to sync on an ID3V1 tag. */
-       if(decoder->guts->has_stream_info && decoder->guts->stream_info.data.stream_info.total_samples) {
-               if(decoder->guts->samples_decoded >= decoder->guts->stream_info.data.stream_info.total_samples) {
-                       decoder->state = FLAC__STREAM_DECODER_END_OF_STREAM;
+       if(decoder->private->has_stream_info && decoder->private->stream_info.data.stream_info.total_samples) {
+               if(decoder->private->samples_decoded >= decoder->private->stream_info.data.stream_info.total_samples) {
+                       decoder->protected->state = FLAC__STREAM_DECODER_END_OF_STREAM;
                        return true;
                }
        }
 
        /* make sure we're byte aligned */
-       if(decoder->guts->input.consumed_bits != 0) {
-               if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8-decoder->guts->input.consumed_bits, read_callback_, decoder))
+       if(decoder->private->input.consumed_bits != 0) {
+               if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8-decoder->private->input.consumed_bits, read_callback_, decoder))
                        return false; /* the read_callback_ sets the state for us */
        }
 
        while(1) {
-               if(decoder->guts->cached) {
-                       x = (uint32)decoder->guts->lookahead;
-                       decoder->guts->cached = false;
+               if(decoder->private->cached) {
+                       x = (uint32)decoder->private->lookahead;
+                       decoder->private->cached = false;
                }
                else {
-                       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
+                       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8, read_callback_, decoder))
                                return false; /* the read_callback_ sets the state for us */
                }
                if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
-                       decoder->guts->header_warmup[0] = (byte)x;
-                       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
+                       decoder->private->header_warmup[0] = (byte)x;
+                       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8, read_callback_, decoder))
                                return false; /* the read_callback_ sets the state for us */
 
                        /* we have to check if we just read two 0xff's in a row; the second may actually be the beginning of the sync code */
                        /* else we have to check if the second byte is the end of a sync code */
                        if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
-                               decoder->guts->lookahead = (byte)x;
-                               decoder->guts->cached = true;
+                               decoder->private->lookahead = (byte)x;
+                               decoder->private->cached = true;
                        }
                        else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
-                               decoder->guts->header_warmup[1] = (byte)x;
-                               decoder->state = FLAC__STREAM_DECODER_READ_FRAME;
+                               decoder->private->header_warmup[1] = (byte)x;
+                               decoder->protected->state = FLAC__STREAM_DECODER_READ_FRAME;
                                return true;
                        }
                }
                if(first) {
-                       decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
+                       decoder->private->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private->client_data);
                        first = 0;
                }
        }
@@ -702,37 +794,37 @@ bool stream_decoder_read_frame_(FLAC__StreamDecoder *decoder, bool *got_a_frame)
 
        /* init the CRC */
        frame_crc = 0;
-       FLAC__CRC16_UPDATE(decoder->guts->header_warmup[0], frame_crc);
-       FLAC__CRC16_UPDATE(decoder->guts->header_warmup[1], frame_crc);
-       FLAC__bitbuffer_init_read_crc16(&decoder->guts->input, frame_crc);
+       FLAC__CRC16_UPDATE(decoder->private->header_warmup[0], frame_crc);
+       FLAC__CRC16_UPDATE(decoder->private->header_warmup[1], frame_crc);
+       FLAC__bitbuffer_init_read_crc16(&decoder->private->input, frame_crc);
 
        if(!stream_decoder_read_frame_header_(decoder))
                return false;
-       if(decoder->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC)
+       if(decoder->protected->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC)
                return true;
-       if(!stream_decoder_allocate_output_(decoder, decoder->guts->frame.header.blocksize, decoder->guts->frame.header.channels))
+       if(!stream_decoder_allocate_output_(decoder, decoder->private->frame.header.blocksize, decoder->private->frame.header.channels))
                return false;
-       for(channel = 0; channel < decoder->guts->frame.header.channels; channel++) {
+       for(channel = 0; channel < decoder->private->frame.header.channels; channel++) {
                /*
                 * first figure the correct bits-per-sample of the subframe
                 */
-               unsigned bps = decoder->guts->frame.header.bits_per_sample;
-               switch(decoder->guts->frame.header.channel_assignment) {
+               unsigned bps = decoder->private->frame.header.bits_per_sample;
+               switch(decoder->private->frame.header.channel_assignment) {
                        case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
                                /* no adjustment needed */
                                break;
                        case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
-                               FLAC__ASSERT(decoder->guts->frame.header.channels == 2);
+                               FLAC__ASSERT(decoder->private->frame.header.channels == 2);
                                if(channel == 1)
                                        bps++;
                                break;
                        case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
-                               FLAC__ASSERT(decoder->guts->frame.header.channels == 2);
+                               FLAC__ASSERT(decoder->private->frame.header.channels == 2);
                                if(channel == 0)
                                        bps++;
                                break;
                        case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
-                               FLAC__ASSERT(decoder->guts->frame.header.channels == 2);
+                               FLAC__ASSERT(decoder->private->frame.header.channels == 2);
                                if(channel == 1)
                                        bps++;
                                break;
@@ -744,8 +836,8 @@ bool stream_decoder_read_frame_(FLAC__StreamDecoder *decoder, bool *got_a_frame)
                 */
                if(!stream_decoder_read_subframe_(decoder, channel, bps))
                        return false;
-               if(decoder->state != FLAC__STREAM_DECODER_READ_FRAME) {
-                       decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
+               if(decoder->protected->state != FLAC__STREAM_DECODER_READ_FRAME) {
+                       decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
                        return true;
                }
        }
@@ -755,37 +847,37 @@ bool stream_decoder_read_frame_(FLAC__StreamDecoder *decoder, bool *got_a_frame)
        /*
         * Read the frame CRC-16 from the footer and check
         */
-       frame_crc = decoder->guts->input.read_crc16;
-       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__FRAME_FOOTER_CRC_LEN, read_callback_, decoder))
+       frame_crc = decoder->private->input.read_crc16;
+       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, FLAC__FRAME_FOOTER_CRC_LEN, read_callback_, decoder))
                return false; /* the read_callback_ sets the state for us */
        if(frame_crc == (uint16)x) {
                /* Undo any special channel coding */
-               switch(decoder->guts->frame.header.channel_assignment) {
+               switch(decoder->private->frame.header.channel_assignment) {
                        case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
                                /* do nothing */
                                break;
                        case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
-                               FLAC__ASSERT(decoder->guts->frame.header.channels == 2);
-                               for(i = 0; i < decoder->guts->frame.header.blocksize; i++)
-                                       decoder->guts->output[1][i] = decoder->guts->output[0][i] - decoder->guts->output[1][i];
+                               FLAC__ASSERT(decoder->private->frame.header.channels == 2);
+                               for(i = 0; i < decoder->private->frame.header.blocksize; i++)
+                                       decoder->private->output[1][i] = decoder->private->output[0][i] - decoder->private->output[1][i];
                                break;
                        case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
-                               FLAC__ASSERT(decoder->guts->frame.header.channels == 2);
-                               for(i = 0; i < decoder->guts->frame.header.blocksize; i++)
-                                       decoder->guts->output[0][i] += decoder->guts->output[1][i];
+                               FLAC__ASSERT(decoder->private->frame.header.channels == 2);
+                               for(i = 0; i < decoder->private->frame.header.blocksize; i++)
+                                       decoder->private->output[0][i] += decoder->private->output[1][i];
                                break;
                        case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
-                               FLAC__ASSERT(decoder->guts->frame.header.channels == 2);
-                               for(i = 0; i < decoder->guts->frame.header.blocksize; i++) {
-                                       mid = decoder->guts->output[0][i];
-                                       side = decoder->guts->output[1][i];
+                               FLAC__ASSERT(decoder->private->frame.header.channels == 2);
+                               for(i = 0; i < decoder->private->frame.header.blocksize; i++) {
+                                       mid = decoder->private->output[0][i];
+                                       side = decoder->private->output[1][i];
                                        mid <<= 1;
                                        if(side & 1) /* i.e. if 'side' is odd... */
                                                mid++;
                                        left = mid + side;
                                        right = mid - side;
-                                       decoder->guts->output[0][i] = left >> 1;
-                                       decoder->guts->output[1][i] = right >> 1;
+                                       decoder->private->output[0][i] = left >> 1;
+                                       decoder->private->output[1][i] = right >> 1;
                                }
                                break;
                        default:
@@ -795,29 +887,29 @@ bool stream_decoder_read_frame_(FLAC__StreamDecoder *decoder, bool *got_a_frame)
        }
        else {
                /* Bad frame, emit error and zero the output signal */
-               decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_FRAME_CRC_MISMATCH, decoder->guts->client_data);
-               for(channel = 0; channel < decoder->guts->frame.header.channels; channel++) {
-                       memset(decoder->guts->output[channel], 0, sizeof(int32) * decoder->guts->frame.header.blocksize);
+               decoder->private->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_FRAME_CRC_MISMATCH, decoder->private->client_data);
+               for(channel = 0; channel < decoder->private->frame.header.channels; channel++) {
+                       memset(decoder->private->output[channel], 0, sizeof(int32) * decoder->private->frame.header.blocksize);
                }
        }
 
        *got_a_frame = true;
 
        /* put the latest values into the public section of the decoder instance */
-       decoder->channels = decoder->guts->frame.header.channels;
-       decoder->channel_assignment = decoder->guts->frame.header.channel_assignment;
-       decoder->bits_per_sample = decoder->guts->frame.header.bits_per_sample;
-       decoder->sample_rate = decoder->guts->frame.header.sample_rate;
-       decoder->blocksize = decoder->guts->frame.header.blocksize;
+       decoder->protected->channels = decoder->private->frame.header.channels;
+       decoder->protected->channel_assignment = decoder->private->frame.header.channel_assignment;
+       decoder->protected->bits_per_sample = decoder->private->frame.header.bits_per_sample;
+       decoder->protected->sample_rate = decoder->private->frame.header.sample_rate;
+       decoder->protected->blocksize = decoder->private->frame.header.blocksize;
 
-       decoder->guts->samples_decoded = decoder->guts->frame.header.number.sample_number + decoder->guts->frame.header.blocksize;
+       decoder->private->samples_decoded = decoder->private->frame.header.number.sample_number + decoder->private->frame.header.blocksize;
 
        /* write it */
        /* NOTE: some versions of GCC can't figure out const-ness right and will give you an 'incompatible pointer type' warning on arg 3 here: */
-       if(decoder->guts->write_callback(decoder, &decoder->guts->frame, decoder->guts->output, decoder->guts->client_data) != FLAC__STREAM_DECODER_WRITE_CONTINUE)
+       if(decoder->private->write_callback(decoder, &decoder->private->frame, decoder->private->output, decoder->private->client_data) != FLAC__STREAM_DECODER_WRITE_CONTINUE)
                return false;
 
-       decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
+       decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
        return true;
 }
 
@@ -829,14 +921,14 @@ bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder)
        byte crc8, raw_header[16]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */
        unsigned raw_header_len;
        bool is_unparseable = false;
-       const bool is_known_variable_blocksize_stream = (decoder->guts->has_stream_info && decoder->guts->stream_info.data.stream_info.min_blocksize != decoder->guts->stream_info.data.stream_info.max_blocksize);
-       const bool is_known_fixed_blocksize_stream = (decoder->guts->has_stream_info && decoder->guts->stream_info.data.stream_info.min_blocksize == decoder->guts->stream_info.data.stream_info.max_blocksize);
+       const bool is_known_variable_blocksize_stream = (decoder->private->has_stream_info && decoder->private->stream_info.data.stream_info.min_blocksize != decoder->private->stream_info.data.stream_info.max_blocksize);
+       const bool is_known_fixed_blocksize_stream = (decoder->private->has_stream_info && decoder->private->stream_info.data.stream_info.min_blocksize == decoder->private->stream_info.data.stream_info.max_blocksize);
 
-       FLAC__ASSERT(decoder->guts->input.consumed_bits == 0); /* make sure we're byte aligned */
+       FLAC__ASSERT(decoder->private->input.consumed_bits == 0); /* make sure we're byte aligned */
 
        /* init the raw header with the saved bits from synchronization */
-       raw_header[0] = decoder->guts->header_warmup[0];
-       raw_header[1] = decoder->guts->header_warmup[1];
+       raw_header[0] = decoder->private->header_warmup[0];
+       raw_header[1] = decoder->private->header_warmup[1];
        raw_header_len = 2;
 
        /*
@@ -856,14 +948,14 @@ bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder)
         * read in the raw header as bytes so we can CRC it, and parse it on the way
         */
        for(i = 0; i < 2; i++) {
-               if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
+               if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8, read_callback_, decoder))
                        return false; /* the read_callback_ sets the state for us */
                if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
                        /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */
-                       decoder->guts->lookahead = (byte)x;
-                       decoder->guts->cached = true;
-                       decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->guts->client_data);
-                       decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
+                       decoder->private->lookahead = (byte)x;
+                       decoder->private->cached = true;
+                       decoder->private->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private->client_data);
+                       decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
                        return true;
                }
                raw_header[raw_header_len++] = (byte)x;
@@ -872,18 +964,18 @@ bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder)
        switch(x = raw_header[2] >> 4) {
                case 0:
                        if(is_known_fixed_blocksize_stream)
-                               decoder->guts->frame.header.blocksize = decoder->guts->stream_info.data.stream_info.min_blocksize;
+                               decoder->private->frame.header.blocksize = decoder->private->stream_info.data.stream_info.min_blocksize;
                        else
                                is_unparseable = true;
                        break;
                case 1:
-                       decoder->guts->frame.header.blocksize = 192;
+                       decoder->private->frame.header.blocksize = 192;
                        break;
                case 2:
                case 3:
                case 4:
                case 5:
-                       decoder->guts->frame.header.blocksize = 576 << (x-2);
+                       decoder->private->frame.header.blocksize = 576 << (x-2);
                        break;
                case 6:
                case 7:
@@ -897,7 +989,7 @@ bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder)
                case 13:
                case 14:
                case 15:
-                       decoder->guts->frame.header.blocksize = 256 << (x-8);
+                       decoder->private->frame.header.blocksize = 256 << (x-8);
                        break;
                default:
                        FLAC__ASSERT(0);
@@ -906,8 +998,8 @@ bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder)
 
        switch(x = raw_header[2] & 0x0f) {
                case 0:
-                       if(decoder->guts->has_stream_info)
-                               decoder->guts->frame.header.sample_rate = decoder->guts->stream_info.data.stream_info.sample_rate;
+                       if(decoder->private->has_stream_info)
+                               decoder->private->frame.header.sample_rate = decoder->private->stream_info.data.stream_info.sample_rate;
                        else
                                is_unparseable = true;
                        break;
@@ -917,28 +1009,28 @@ bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder)
                        is_unparseable = true;
                        break;
                case 4:
-                       decoder->guts->frame.header.sample_rate = 8000;
+                       decoder->private->frame.header.sample_rate = 8000;
                        break;
                case 5:
-                       decoder->guts->frame.header.sample_rate = 16000;
+                       decoder->private->frame.header.sample_rate = 16000;
                        break;
                case 6:
-                       decoder->guts->frame.header.sample_rate = 22050;
+                       decoder->private->frame.header.sample_rate = 22050;
                        break;
                case 7:
-                       decoder->guts->frame.header.sample_rate = 24000;
+                       decoder->private->frame.header.sample_rate = 24000;
                        break;
                case 8:
-                       decoder->guts->frame.header.sample_rate = 32000;
+                       decoder->private->frame.header.sample_rate = 32000;
                        break;
                case 9:
-                       decoder->guts->frame.header.sample_rate = 44100;
+                       decoder->private->frame.header.sample_rate = 44100;
                        break;
                case 10:
-                       decoder->guts->frame.header.sample_rate = 48000;
+                       decoder->private->frame.header.sample_rate = 48000;
                        break;
                case 11:
-                       decoder->guts->frame.header.sample_rate = 96000;
+                       decoder->private->frame.header.sample_rate = 96000;
                        break;
                case 12:
                case 13:
@@ -946,8 +1038,8 @@ bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder)
                        sample_rate_hint = x;
                        break;
                case 15:
-                       decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->guts->client_data);
-                       decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
+                       decoder->private->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private->client_data);
+                       decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
                        return true;
                default:
                        FLAC__ASSERT(0);
@@ -955,16 +1047,16 @@ bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder)
 
        x = (unsigned)(raw_header[3] >> 4);
        if(x & 8) {
-               decoder->guts->frame.header.channels = 2;
+               decoder->private->frame.header.channels = 2;
                switch(x & 7) {
                        case 0:
-                               decoder->guts->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
+                               decoder->private->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
                                break;
                        case 1:
-                               decoder->guts->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
+                               decoder->private->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
                                break;
                        case 2:
-                               decoder->guts->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
+                               decoder->private->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
                                break;
                        default:
                                is_unparseable = true;
@@ -972,31 +1064,31 @@ bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder)
                }
        }
        else {
-               decoder->guts->frame.header.channels = (unsigned)x + 1;
-               decoder->guts->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
+               decoder->private->frame.header.channels = (unsigned)x + 1;
+               decoder->private->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
        }
 
        switch(x = (unsigned)(raw_header[3] & 0x0e) >> 1) {
                case 0:
-                       if(decoder->guts->has_stream_info)
-                               decoder->guts->frame.header.bits_per_sample = decoder->guts->stream_info.data.stream_info.bits_per_sample;
+                       if(decoder->private->has_stream_info)
+                               decoder->private->frame.header.bits_per_sample = decoder->private->stream_info.data.stream_info.bits_per_sample;
                        else
                                is_unparseable = true;
                        break;
                case 1:
-                       decoder->guts->frame.header.bits_per_sample = 8;
+                       decoder->private->frame.header.bits_per_sample = 8;
                        break;
                case 2:
-                       decoder->guts->frame.header.bits_per_sample = 12;
+                       decoder->private->frame.header.bits_per_sample = 12;
                        break;
                case 4:
-                       decoder->guts->frame.header.bits_per_sample = 16;
+                       decoder->private->frame.header.bits_per_sample = 16;
                        break;
                case 5:
-                       decoder->guts->frame.header.bits_per_sample = 20;
+                       decoder->private->frame.header.bits_per_sample = 20;
                        break;
                case 6:
-                       decoder->guts->frame.header.bits_per_sample = 24;
+                       decoder->private->frame.header.bits_per_sample = 24;
                        break;
                case 3:
                case 7:
@@ -1008,36 +1100,36 @@ bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder)
        }
 
        if(raw_header[3] & 0x01) { /* this should be a zero padding bit */
-               decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->guts->client_data);
-               decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
+               decoder->private->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private->client_data);
+               decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
                return true;
        }
 
        if(blocksize_hint && is_known_variable_blocksize_stream) {
-               if(!FLAC__bitbuffer_read_utf8_uint64(&decoder->guts->input, &xx, read_callback_, decoder, raw_header, &raw_header_len))
+               if(!FLAC__bitbuffer_read_utf8_uint64(&decoder->private->input, &xx, read_callback_, decoder, raw_header, &raw_header_len))
                        return false; /* the read_callback_ sets the state for us */
                if(xx == 0xffffffffffffffff) { /* i.e. non-UTF8 code... */
-                       decoder->guts->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
-                       decoder->guts->cached = true;
-                       decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->guts->client_data);
-                       decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
+                       decoder->private->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
+                       decoder->private->cached = true;
+                       decoder->private->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private->client_data);
+                       decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
                        return true;
                }
-               decoder->guts->frame.header.number.sample_number = xx;
+               decoder->private->frame.header.number.sample_number = xx;
        }
        else {
-               if(!FLAC__bitbuffer_read_utf8_uint32(&decoder->guts->input, &x, read_callback_, decoder, raw_header, &raw_header_len))
+               if(!FLAC__bitbuffer_read_utf8_uint32(&decoder->private->input, &x, read_callback_, decoder, raw_header, &raw_header_len))
                        return false; /* the read_callback_ sets the state for us */
                if(x == 0xffffffff) { /* i.e. non-UTF8 code... */
-                       decoder->guts->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
-                       decoder->guts->cached = true;
-                       decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->guts->client_data);
-                       decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
+                       decoder->private->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
+                       decoder->private->cached = true;
+                       decoder->private->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private->client_data);
+                       decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
                        return true;
                }
-               decoder->guts->last_frame_number = x;
-               if(decoder->guts->has_stream_info) {
-                       decoder->guts->frame.header.number.sample_number = (int64)decoder->guts->stream_info.data.stream_info.min_blocksize * (int64)x;
+               decoder->private->last_frame_number = x;
+               if(decoder->private->has_stream_info) {
+                       decoder->private->frame.header.number.sample_number = (int64)decoder->private->stream_info.data.stream_info.min_blocksize * (int64)x;
                }
                else {
                        is_unparseable = true;
@@ -1045,51 +1137,51 @@ bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder)
        }
 
        if(blocksize_hint) {
-               if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
+               if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8, read_callback_, decoder))
                        return false; /* the read_callback_ sets the state for us */
                raw_header[raw_header_len++] = (byte)x;
                if(blocksize_hint == 7) {
                        uint32 _x;
-                       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &_x, 8, read_callback_, decoder))
+                       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &_x, 8, read_callback_, decoder))
                                return false; /* the read_callback_ sets the state for us */
                        raw_header[raw_header_len++] = (byte)_x;
                        x = (x << 8) | _x;
                }
-               decoder->guts->frame.header.blocksize = x+1;
+               decoder->private->frame.header.blocksize = x+1;
        }
 
        if(sample_rate_hint) {
-               if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
+               if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8, read_callback_, decoder))
                        return false; /* the read_callback_ sets the state for us */
                raw_header[raw_header_len++] = (byte)x;
                if(sample_rate_hint != 12) {
                        uint32 _x;
-                       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &_x, 8, read_callback_, decoder))
+                       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &_x, 8, read_callback_, decoder))
                                return false; /* the read_callback_ sets the state for us */
                        raw_header[raw_header_len++] = (byte)_x;
                        x = (x << 8) | _x;
                }
                if(sample_rate_hint == 12)
-                       decoder->guts->frame.header.sample_rate = x*1000;
+                       decoder->private->frame.header.sample_rate = x*1000;
                else if(sample_rate_hint == 13)
-                       decoder->guts->frame.header.sample_rate = x;
+                       decoder->private->frame.header.sample_rate = x;
                else
-                       decoder->guts->frame.header.sample_rate = x*10;
+                       decoder->private->frame.header.sample_rate = x*10;
        }
 
        /* read the CRC-8 byte */
-       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
+       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8, read_callback_, decoder))
                return false; /* the read_callback_ sets the state for us */
        crc8 = (byte)x;
 
        if(FLAC__crc8(raw_header, raw_header_len) != crc8) {
-               decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->guts->client_data);
-               decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
+               decoder->private->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private->client_data);
+               decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
                return true;
        }
 
        if(is_unparseable) {
-               decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
+               decoder->protected->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
                return false;
        }
 
@@ -1101,7 +1193,7 @@ bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channe
        uint32 x;
        bool wasted_bits;
 
-       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder)) /* MAGIC NUMBER */
+       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8, read_callback_, decoder)) /* MAGIC NUMBER */
                return false; /* the read_callback_ sets the state for us */
 
        wasted_bits = (x & 1);
@@ -1109,20 +1201,20 @@ bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channe
 
        if(wasted_bits) {
                unsigned u;
-               if(!FLAC__bitbuffer_read_unary_unsigned(&decoder->guts->input, &u, read_callback_, decoder))
+               if(!FLAC__bitbuffer_read_unary_unsigned(&decoder->private->input, &u, read_callback_, decoder))
                        return false; /* the read_callback_ sets the state for us */
-               decoder->guts->frame.subframes[channel].wasted_bits = u+1;
-               bps -= decoder->guts->frame.subframes[channel].wasted_bits;
+               decoder->private->frame.subframes[channel].wasted_bits = u+1;
+               bps -= decoder->private->frame.subframes[channel].wasted_bits;
        }
        else
-               decoder->guts->frame.subframes[channel].wasted_bits = 0;
+               decoder->private->frame.subframes[channel].wasted_bits = 0;
 
        /*
         * Lots of magic numbers here
         */
        if(x & 0x80) {
-               decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
-               decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
+               decoder->private->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private->client_data);
+               decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
                return true;
        }
        else if(x == 0) {
@@ -1134,7 +1226,7 @@ bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channe
                        return false;
        }
        else if(x < 16) {
-               decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
+               decoder->protected->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
                return false;
        }
        else if(x <= 24) {
@@ -1142,7 +1234,7 @@ bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channe
                        return false;
        }
        else if(x < 64) {
-               decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
+               decoder->protected->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
                return false;
        }
        else {
@@ -1152,9 +1244,9 @@ bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channe
 
        if(wasted_bits) {
                unsigned i;
-               x = decoder->guts->frame.subframes[channel].wasted_bits;
-               for(i = 0; i < decoder->guts->frame.header.blocksize; i++)
-                       decoder->guts->output[channel][i] <<= x;
+               x = decoder->private->frame.subframes[channel].wasted_bits;
+               for(i = 0; i < decoder->private->frame.header.blocksize; i++)
+                       decoder->private->output[channel][i] <<= x;
        }
 
        return true;
@@ -1162,20 +1254,20 @@ bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channe
 
 bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
 {
-       FLAC__Subframe_Constant *subframe = &decoder->guts->frame.subframes[channel].data.constant;
+       FLAC__Subframe_Constant *subframe = &decoder->private->frame.subframes[channel].data.constant;
        int32 x;
        unsigned i;
-       int32 *output = decoder->guts->output[channel];
+       int32 *output = decoder->private->output[channel];
 
-       decoder->guts->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;
+       decoder->private->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;
 
-       if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &x, bps, read_callback_, decoder))
+       if(!FLAC__bitbuffer_read_raw_int32(&decoder->private->input, &x, bps, read_callback_, decoder))
                return false; /* the read_callback_ sets the state for us */
 
        subframe->value = x;
 
        /* decode the subframe */
-       for(i = 0; i < decoder->guts->frame.header.blocksize; i++)
+       for(i = 0; i < decoder->private->frame.header.blocksize; i++)
                output[i] = x;
 
        return true;
@@ -1183,42 +1275,42 @@ bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsign
 
 bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
 {
-       FLAC__Subframe_Fixed *subframe = &decoder->guts->frame.subframes[channel].data.fixed;
+       FLAC__Subframe_Fixed *subframe = &decoder->private->frame.subframes[channel].data.fixed;
        int32 i32;
        uint32 u32;
        unsigned u;
 
-       decoder->guts->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED;
+       decoder->private->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED;
 
-       subframe->residual = decoder->guts->residual[channel];
+       subframe->residual = decoder->private->residual[channel];
        subframe->order = order;
 
        /* read warm-up samples */
        for(u = 0; u < order; u++) {
-               if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, bps, read_callback_, decoder))
+               if(!FLAC__bitbuffer_read_raw_int32(&decoder->private->input, &i32, bps, read_callback_, decoder))
                        return false; /* the read_callback_ sets the state for us */
                subframe->warmup[u] = i32;
        }
 
        /* read entropy coding method info */
-       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
+       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
                return false; /* the read_callback_ sets the state for us */
        subframe->entropy_coding_method.type = u32;
        switch(subframe->entropy_coding_method.type) {
                case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
-                       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
+                       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
                                return false; /* the read_callback_ sets the state for us */
                        subframe->entropy_coding_method.data.partitioned_rice.order = u32;
                        break;
                default:
-                       decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
+                       decoder->protected->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
                        return false;
        }
 
        /* read residual */
        switch(subframe->entropy_coding_method.type) {
                case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
-                       if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, subframe->entropy_coding_method.data.partitioned_rice.order, decoder->guts->residual[channel]))
+                       if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, subframe->entropy_coding_method.data.partitioned_rice.order, decoder->private->residual[channel]))
                                return false;
                        break;
                default:
@@ -1226,72 +1318,72 @@ bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned
        }
 
        /* decode the subframe */
-       memcpy(decoder->guts->output[channel], subframe->warmup, sizeof(int32) * order);
-       FLAC__fixed_restore_signal(decoder->guts->residual[channel], decoder->guts->frame.header.blocksize-order, order, decoder->guts->output[channel]+order);
+       memcpy(decoder->private->output[channel], subframe->warmup, sizeof(int32) * order);
+       FLAC__fixed_restore_signal(decoder->private->residual[channel], decoder->private->frame.header.blocksize-order, order, decoder->private->output[channel]+order);
 
        return true;
 }
 
 bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
 {
-       FLAC__Subframe_LPC *subframe = &decoder->guts->frame.subframes[channel].data.lpc;
+       FLAC__Subframe_LPC *subframe = &decoder->private->frame.subframes[channel].data.lpc;
        int32 i32;
        uint32 u32;
        unsigned u;
 
-       decoder->guts->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC;
+       decoder->private->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC;
 
-       subframe->residual = decoder->guts->residual[channel];
+       subframe->residual = decoder->private->residual[channel];
        subframe->order = order;
 
        /* read warm-up samples */
        for(u = 0; u < order; u++) {
-               if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, bps, read_callback_, decoder))
+               if(!FLAC__bitbuffer_read_raw_int32(&decoder->private->input, &i32, bps, read_callback_, decoder))
                        return false; /* the read_callback_ sets the state for us */
                subframe->warmup[u] = i32;
        }
 
        /* read qlp coeff precision */
-       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN, read_callback_, decoder))
+       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN, read_callback_, decoder))
                return false; /* the read_callback_ sets the state for us */
        if(u32 == (1u << FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN) - 1) {
-               decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
-               decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
+               decoder->private->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private->client_data);
+               decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
                return true;
        }
        subframe->qlp_coeff_precision = u32+1;
 
        /* read qlp shift */
-       if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN, read_callback_, decoder))
+       if(!FLAC__bitbuffer_read_raw_int32(&decoder->private->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN, read_callback_, decoder))
                return false; /* the read_callback_ sets the state for us */
        subframe->quantization_level = i32;
 
        /* read quantized lp coefficiencts */
        for(u = 0; u < order; u++) {
-               if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, subframe->qlp_coeff_precision, read_callback_, decoder))
+               if(!FLAC__bitbuffer_read_raw_int32(&decoder->private->input, &i32, subframe->qlp_coeff_precision, read_callback_, decoder))
                        return false; /* the read_callback_ sets the state for us */
                subframe->qlp_coeff[u] = i32;
        }
 
        /* read entropy coding method info */
-       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
+       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
                return false; /* the read_callback_ sets the state for us */
        subframe->entropy_coding_method.type = u32;
        switch(subframe->entropy_coding_method.type) {
                case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
-                       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
+                       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
                                return false; /* the read_callback_ sets the state for us */
                        subframe->entropy_coding_method.data.partitioned_rice.order = u32;
                        break;
                default:
-                       decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
+                       decoder->protected->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
                        return false;
        }
 
        /* read residual */
        switch(subframe->entropy_coding_method.type) {
                case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
-                       if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, subframe->entropy_coding_method.data.partitioned_rice.order, decoder->guts->residual[channel]))
+                       if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, subframe->entropy_coding_method.data.partitioned_rice.order, decoder->private->residual[channel]))
                                return false;
                        break;
                default:
@@ -1299,33 +1391,33 @@ bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned ch
        }
 
        /* decode the subframe */
-       memcpy(decoder->guts->output[channel], subframe->warmup, sizeof(int32) * order);
+       memcpy(decoder->private->output[channel], subframe->warmup, sizeof(int32) * order);
        if(bps <= 16 && subframe->qlp_coeff_precision <= 16)
-               decoder->guts->local_lpc_restore_signal_16bit(decoder->guts->residual[channel], decoder->guts->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->guts->output[channel]+order);
+               decoder->private->local_lpc_restore_signal_16bit(decoder->private->residual[channel], decoder->private->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private->output[channel]+order);
        else
-               decoder->guts->local_lpc_restore_signal(decoder->guts->residual[channel], decoder->guts->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->guts->output[channel]+order);
+               decoder->private->local_lpc_restore_signal(decoder->private->residual[channel], decoder->private->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private->output[channel]+order);
 
        return true;
 }
 
 bool stream_decoder_read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
 {
-       FLAC__Subframe_Verbatim *subframe = &decoder->guts->frame.subframes[channel].data.verbatim;
-       int32 x, *residual = decoder->guts->residual[channel];
+       FLAC__Subframe_Verbatim *subframe = &decoder->private->frame.subframes[channel].data.verbatim;
+       int32 x, *residual = decoder->private->residual[channel];
        unsigned i;
 
-       decoder->guts->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM;
+       decoder->private->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM;
 
        subframe->data = residual;
 
-       for(i = 0; i < decoder->guts->frame.header.blocksize; i++) {
-               if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &x, bps, read_callback_, decoder))
+       for(i = 0; i < decoder->private->frame.header.blocksize; i++) {
+               if(!FLAC__bitbuffer_read_raw_int32(&decoder->private->input, &x, bps, read_callback_, decoder))
                        return false; /* the read_callback_ sets the state for us */
                residual[i] = x;
        }
 
        /* decode the subframe */
-       memcpy(decoder->guts->output[channel], subframe->data, sizeof(int32) * decoder->guts->frame.header.blocksize);
+       memcpy(decoder->private->output[channel], subframe->data, sizeof(int32) * decoder->private->frame.header.blocksize);
 
        return true;
 }
@@ -1336,29 +1428,29 @@ bool stream_decoder_read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder
        int i;
        unsigned partition, sample, u;
        const unsigned partitions = 1u << partition_order;
-       const unsigned partition_samples = partition_order > 0? decoder->guts->frame.header.blocksize >> partition_order : decoder->guts->frame.header.blocksize - predictor_order;
+       const unsigned partition_samples = partition_order > 0? decoder->private->frame.header.blocksize >> partition_order : decoder->private->frame.header.blocksize - predictor_order;
 
        sample = 0;
        for(partition = 0; partition < partitions; partition++) {
-               if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN, read_callback_, decoder))
+               if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN, read_callback_, decoder))
                        return false; /* the read_callback_ sets the state for us */
                if(rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
                        for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
 #ifdef FLAC__SYMMETRIC_RICE
-                               if(!FLAC__bitbuffer_read_symmetric_rice_signed(&decoder->guts->input, &i, rice_parameter, read_callback_, decoder))
+                               if(!FLAC__bitbuffer_read_symmetric_rice_signed(&decoder->private->input, &i, rice_parameter, read_callback_, decoder))
                                        return false; /* the read_callback_ sets the state for us */
 #else
-                               if(!FLAC__bitbuffer_read_rice_signed(&decoder->guts->input, &i, rice_parameter, read_callback_, decoder))
+                               if(!FLAC__bitbuffer_read_rice_signed(&decoder->private->input, &i, rice_parameter, read_callback_, decoder))
                                        return false; /* the read_callback_ sets the state for us */
 #endif
                                residual[sample] = i;
                        }
                }
                else {
-                       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN, read_callback_, decoder))
+                       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN, read_callback_, decoder))
                                return false; /* the read_callback_ sets the state for us */
                        for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
-                               if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i, rice_parameter, read_callback_, decoder))
+                               if(!FLAC__bitbuffer_read_raw_int32(&decoder->private->input, &i, rice_parameter, read_callback_, decoder))
                                        return false; /* the read_callback_ sets the state for us */
                                residual[sample] = i;
                        }
@@ -1370,13 +1462,13 @@ bool stream_decoder_read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder
 
 bool stream_decoder_read_zero_padding_(FLAC__StreamDecoder *decoder)
 {
-       if(decoder->guts->input.consumed_bits != 0) {
+       if(decoder->private->input.consumed_bits != 0) {
                uint32 zero = 0;
-               if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &zero, 8-decoder->guts->input.consumed_bits, read_callback_, decoder))
+               if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &zero, 8-decoder->private->input.consumed_bits, read_callback_, decoder))
                        return false; /* the read_callback_ sets the state for us */
                if(zero != 0) {
-                       decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
-                       decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
+                       decoder->private->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private->client_data);
+                       decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
                }
        }
        return true;
@@ -1386,10 +1478,10 @@ bool read_callback_(byte buffer[], unsigned *bytes, void *client_data)
 {
        FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;
        FLAC__StreamDecoderReadStatus status;
-       status = decoder->guts->read_callback(decoder, buffer, bytes, decoder->guts->client_data);
+       status = decoder->private->read_callback(decoder, buffer, bytes, decoder->private->client_data);
        if(status == FLAC__STREAM_DECODER_READ_END_OF_STREAM)
-               decoder->state = FLAC__STREAM_DECODER_END_OF_STREAM;
+               decoder->protected->state = FLAC__STREAM_DECODER_END_OF_STREAM;
        else if(status == FLAC__STREAM_DECODER_READ_ABORT)
-               decoder->state = FLAC__STREAM_DECODER_ABORTED;
+               decoder->protected->state = FLAC__STREAM_DECODER_ABORTED;
        return status == FLAC__STREAM_DECODER_READ_CONTINUE;
 }
index 4a96b5f..c24932f 100644 (file)
 #include <stdlib.h> /* for malloc() */
 #include <string.h> /* for memcpy() */
 #include "FLAC/assert.h"
-#include "FLAC/encoder.h"
 #include "FLAC/seek_table.h"
+#include "protected/stream_encoder.h"
 #include "private/bitbuffer.h"
 #include "private/bitmath.h"
 #include "private/crc.h"
 #include "private/cpu.h"
-#include "private/encoder_framing.h"
+#include "private/stream_encoder_framing.h"
 #include "private/fixed.h"
 #include "private/lpc.h"
 #include "private/md5.h"
 #endif
 #define max(x,y) ((x)>(y)?(x):(y))
 
-typedef struct FLAC__EncoderPrivate {
+/***********************************************************************
+ *
+ * Private class method prototypes
+ *
+ ***********************************************************************/
+
+static bool stream_encoder_resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size);
+static bool stream_encoder_process_frame_(FLAC__StreamEncoder *encoder, bool is_last_frame);
+static bool stream_encoder_process_subframes_(FLAC__StreamEncoder *encoder, bool is_last_frame);
+static bool stream_encoder_process_subframe_(FLAC__StreamEncoder *encoder, unsigned min_partition_order, unsigned max_partition_order, bool verbatim_only, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const int32 integer_signal[], const real real_signal[], FLAC__Subframe *subframe[2], int32 *residual[2], unsigned *best_subframe, unsigned *best_bits);
+static bool stream_encoder_add_subframe_(FLAC__StreamEncoder *encoder, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame);
+static unsigned stream_encoder_evaluate_constant_subframe_(const int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe);
+static unsigned stream_encoder_evaluate_fixed_subframe_(const int32 signal[], int32 residual[], uint32 abs_residual[], uint32 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned blocksize, unsigned subframe_bps, unsigned order, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe);
+static unsigned stream_encoder_evaluate_lpc_subframe_(FLAC__StreamEncoder *encoder, const int32 signal[], int32 residual[], uint32 abs_residual[], uint32 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], const real lp_coeff[], unsigned blocksize, unsigned subframe_bps, unsigned order, unsigned qlp_coeff_precision, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe);
+static unsigned stream_encoder_evaluate_verbatim_subframe_(const int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe);
+static unsigned stream_encoder_find_best_partition_order_(const int32 residual[], uint32 abs_residual[], uint32 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned residual_samples, unsigned predictor_order, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, unsigned rice_parameter_search_dist, unsigned *best_partition_order, unsigned best_parameters[], unsigned best_raw_bits[]);
+#if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES)
+static unsigned stream_encoder_precompute_partition_info_(const int32 residual[], uint32 abs_residual[], uint32 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned residual_samples, unsigned predictor_order, unsigned min_partition_order, unsigned max_partition_order);
+#endif
+static bool stream_encoder_set_partitioned_rice_(const uint32 abs_residual[], const uint32 abs_residual_partition_sums[], const unsigned raw_bits_per_partition[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, unsigned parameters[], unsigned raw_bits[], unsigned *bits);
+static unsigned stream_encoder_get_wasted_bits_(int32 signal[], unsigned samples);
+
+/***********************************************************************
+ *
+ * Private class data
+ *
+ ***********************************************************************/
+
+typedef struct FLAC__StreamEncoderPrivate {
        unsigned input_capacity;                    /* current size (in samples) of the signal and residual buffers */
        int32 *integer_signal[FLAC__MAX_CHANNELS];  /* the integer version of the input signal */
        int32 *integer_signal_mid_side[2];          /* the integer version of the mid-side input signal (stereo only) */
@@ -79,8 +107,8 @@ typedef struct FLAC__EncoderPrivate {
        void (*local_lpc_compute_residual_from_qlp_coefficients)(const int32 data[], unsigned data_len, const int32 qlp_coeff[], unsigned order, int lp_quantization, int32 residual[]);
        void (*local_lpc_compute_residual_from_qlp_coefficients_16bit)(const int32 data[], unsigned data_len, const int32 qlp_coeff[], unsigned order, int lp_quantization, int32 residual[]);
        bool use_slow;                              /* use slow 64-bit versions of some functions */
-       FLAC__EncoderWriteStatus (*write_callback)(const FLAC__Encoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
-       void (*metadata_callback)(const FLAC__Encoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data);
+       FLAC__StreamEncoderWriteStatus (*write_callback)(const FLAC__StreamEncoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
+       void (*metadata_callback)(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data);
        void *client_data;
        /* unaligned (original) pointers to allocated data */
        int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
@@ -92,464 +120,546 @@ typedef struct FLAC__EncoderPrivate {
        uint32 *abs_residual_unaligned;
        uint32 *abs_residual_partition_sums_unaligned;
        unsigned *raw_bits_per_partition_unaligned;
-} FLAC__EncoderPrivate;
-
-static bool encoder_resize_buffers_(FLAC__Encoder *encoder, unsigned new_size);
-static bool encoder_process_frame_(FLAC__Encoder *encoder, bool is_last_frame);
-static bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame);
-static bool encoder_process_subframe_(FLAC__Encoder *encoder, unsigned min_partition_order, unsigned max_partition_order, bool verbatim_only, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const int32 integer_signal[], const real real_signal[], FLAC__Subframe *subframe[2], int32 *residual[2], unsigned *best_subframe, unsigned *best_bits);
-static bool encoder_add_subframe_(FLAC__Encoder *encoder, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame);
-static unsigned encoder_evaluate_constant_subframe_(const int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe);
-static unsigned encoder_evaluate_fixed_subframe_(const int32 signal[], int32 residual[], uint32 abs_residual[], uint32 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned blocksize, unsigned subframe_bps, unsigned order, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe);
-static unsigned encoder_evaluate_lpc_subframe_(FLAC__Encoder *encoder, const int32 signal[], int32 residual[], uint32 abs_residual[], uint32 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], const real lp_coeff[], unsigned blocksize, unsigned subframe_bps, unsigned order, unsigned qlp_coeff_precision, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe);
-static unsigned encoder_evaluate_verbatim_subframe_(const int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe);
-static unsigned encoder_find_best_partition_order_(const int32 residual[], uint32 abs_residual[], uint32 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned residual_samples, unsigned predictor_order, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, unsigned rice_parameter_search_dist, unsigned *best_partition_order, unsigned best_parameters[], unsigned best_raw_bits[]);
-#if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES)
-static unsigned encoder_precompute_partition_info_(const int32 residual[], uint32 abs_residual[], uint32 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned residual_samples, unsigned predictor_order, unsigned min_partition_order, unsigned max_partition_order);
-#endif
-static bool encoder_set_partitioned_rice_(const uint32 abs_residual[], const uint32 abs_residual_partition_sums[], const unsigned raw_bits_per_partition[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, unsigned parameters[], unsigned raw_bits[], unsigned *bits);
-static unsigned encoder_get_wasted_bits_(int32 signal[], unsigned samples);
+} FLAC__StreamEncoderPrivate;
 
-const char *FLAC__EncoderWriteStatusString[] = {
-       "FLAC__ENCODER_WRITE_OK",
-       "FLAC__ENCODER_WRITE_FATAL_ERROR"
+/***********************************************************************
+ *
+ * Public static class data
+ *
+ ***********************************************************************/
+
+const char *FLAC__StreamEncoderStateString[] = {
+       "FLAC__STREAM_ENCODER_OK",
+       "FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS",
+       "FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE",
+       "FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE",
+       "FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE",
+       "FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION",
+       "FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH",
+       "FLAC__STREAM_ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH",
+       "FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE",
+       "FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
+       "FLAC__STREAM_ENCODER_NOT_STREAMABLE",
+       "FLAC__STREAM_ENCODER_FRAMING_ERROR",
+       "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING",
+       "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING",
+       "FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR",
+       "FLAC__STREAM_ENCODER_ALREADY_INITIALIZED",
+       "FLAC__STREAM_ENCODER_UNINITIALIZED"
 };
 
-const char *FLAC__EncoderStateString[] = {
-       "FLAC__ENCODER_OK",
-       "FLAC__ENCODER_UNINITIALIZED",
-       "FLAC__ENCODER_INVALID_NUMBER_OF_CHANNELS",
-       "FLAC__ENCODER_INVALID_BITS_PER_SAMPLE",
-       "FLAC__ENCODER_INVALID_SAMPLE_RATE",
-       "FLAC__ENCODER_INVALID_BLOCK_SIZE",
-       "FLAC__ENCODER_INVALID_QLP_COEFF_PRECISION",
-       "FLAC__ENCODER_MID_SIDE_CHANNELS_MISMATCH",
-       "FLAC__ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH",
-       "FLAC__ENCODER_ILLEGAL_MID_SIDE_FORCE",
-       "FLAC__ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
-       "FLAC__ENCODER_NOT_STREAMABLE",
-       "FLAC__ENCODER_FRAMING_ERROR",
-       "FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING",
-       "FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING",
-       "FLAC__ENCODER_MEMORY_ALLOCATION_ERROR"
+const char *FLAC__StreamEncoderWriteStatusString[] = {
+       "FLAC__STREAM_ENCODER_WRITE_OK",
+       "FLAC__STREAM_ENCODER_WRITE_FATAL_ERROR"
 };
 
-
-bool encoder_resize_buffers_(FLAC__Encoder *encoder, unsigned new_size)
+/***********************************************************************
+ *
+ * Class constructor/destructor
+ *
+ ***********************************************************************/
+FLAC__StreamEncoder *FLAC__stream_encoder_new()
 {
-       bool ok;
-       unsigned i, channel;
-
-       FLAC__ASSERT(new_size > 0);
-       FLAC__ASSERT(encoder->state == FLAC__ENCODER_OK);
-       FLAC__ASSERT(encoder->guts->current_sample_number == 0);
+       FLAC__StreamEncoder *encoder;
 
-       /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
-       if(new_size <= encoder->guts->input_capacity)
-               return true;
+       FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
 
-       ok = true;
-       for(i = 0; ok && i < encoder->channels; i++) {
-               ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->guts->integer_signal_unaligned[i], &encoder->guts->integer_signal[i]);
-               ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->guts->real_signal_unaligned[i], &encoder->guts->real_signal[i]);
+       encoder = (FLAC__StreamEncoder*)malloc(sizeof(FLAC__StreamEncoder));
+       if(encoder == 0) {
+               return 0;
        }
-       for(i = 0; ok && i < 2; i++) {
-               ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->guts->integer_signal_mid_side_unaligned[i], &encoder->guts->integer_signal_mid_side[i]);
-               ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->guts->real_signal_mid_side_unaligned[i], &encoder->guts->real_signal_mid_side[i]);
+       encoder->protected = (FLAC__StreamEncoderProtected*)malloc(sizeof(FLAC__StreamEncoderProtected));
+       if(encoder->protected == 0) {
+               free(encoder);
+               return 0;
        }
-       for(channel = 0; ok && channel < encoder->channels; channel++) {
-               for(i = 0; ok && i < 2; i++) {
-                       ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->guts->residual_workspace_unaligned[channel][i], &encoder->guts->residual_workspace[channel][i]);
-               }
+       encoder->private = (FLAC__StreamEncoderPrivate*)malloc(sizeof(FLAC__StreamEncoderPrivate));
+       if(encoder->private == 0) {
+               free(encoder->protected);
+               free(encoder);
+               return 0;
        }
-       for(channel = 0; ok && channel < 2; channel++) {
-               for(i = 0; ok && i < 2; i++) {
-                       ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->guts->residual_workspace_mid_side_unaligned[channel][i], &encoder->guts->residual_workspace_mid_side[channel][i]);
-               }
-       }
-       ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size, &encoder->guts->abs_residual_unaligned, &encoder->guts->abs_residual);
-#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
-       ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size * 2, &encoder->guts->abs_residual_partition_sums_unaligned, &encoder->guts->abs_residual_partition_sums);
-#endif
-#ifdef FLAC__SEARCH_FOR_ESCAPES
-       ok = ok && FLAC__memory_alloc_aligned_unsigned_array(new_size * 2, &encoder->guts->raw_bits_per_partition_unaligned, &encoder->guts->raw_bits_per_partition);
-#endif
 
-       if(ok)
-               encoder->guts->input_capacity = new_size;
-       else
-               encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
-
-       return ok;
-}
+       encoder->protected->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
 
-FLAC__Encoder *FLAC__encoder_get_new_instance()
-{
-       FLAC__Encoder *encoder = (FLAC__Encoder*)malloc(sizeof(FLAC__Encoder));
-       if(encoder != 0) {
-               encoder->state = FLAC__ENCODER_UNINITIALIZED;
-               encoder->guts = 0;
-       }
        return encoder;
 }
 
-void FLAC__encoder_free_instance(FLAC__Encoder *encoder)
+void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
 {
        FLAC__ASSERT(encoder != 0);
+       FLAC__ASSERT(encoder->protected != 0);
+       FLAC__ASSERT(encoder->private != 0);
+
+       free(encoder->private);
+       free(encoder->protected);
        free(encoder);
 }
 
-FLAC__EncoderState FLAC__encoder_init(FLAC__Encoder *encoder, FLAC__EncoderWriteStatus (*write_callback)(const FLAC__Encoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data), void (*metadata_callback)(const FLAC__Encoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data), void *client_data)
+/***********************************************************************
+ *
+ * Public class methods
+ *
+ ***********************************************************************/
+
+FLAC__StreamEncoderState FLAC__stream_encoder_init(
+       FLAC__StreamEncoder *encoder,
+       bool streamable_subset,
+       bool do_mid_side_stereo,
+       bool loose_mid_side_stereo,
+       unsigned channels,
+       unsigned bits_per_sample,
+       unsigned sample_rate,
+       unsigned blocksize,
+       unsigned max_lpc_order,
+       unsigned qlp_coeff_precision,
+       bool do_qlp_coeff_prec_search,
+       bool do_exhaustive_model_search,
+       unsigned min_residual_partition_order,
+       unsigned max_residual_partition_order,
+       unsigned rice_parameter_search_dist,
+       uint64 total_samples_estimate,
+       const FLAC__StreamMetaData_SeekTable *seek_table,
+       unsigned padding,
+       FLAC__StreamEncoderWriteStatus (*write_callback)(const FLAC__StreamEncoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data),
+       void (*metadata_callback)(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data),
+       void *client_data)
 {
        unsigned i;
-       FLAC__StreamMetaData padding;
-       FLAC__StreamMetaData seek_table;
+       FLAC__StreamMetaData padding_block;
+       FLAC__StreamMetaData seek_table_block;
 
-       FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
        FLAC__ASSERT(encoder != 0);
        FLAC__ASSERT(write_callback != 0);
        FLAC__ASSERT(metadata_callback != 0);
-       FLAC__ASSERT(encoder->state == FLAC__ENCODER_UNINITIALIZED);
-       FLAC__ASSERT(encoder->guts == 0);
 
-       encoder->state = FLAC__ENCODER_OK;
+       if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
+               return encoder->protected->state = FLAC__STREAM_ENCODER_ALREADY_INITIALIZED;
+
+       encoder->protected->state = FLAC__STREAM_ENCODER_OK;
 
-       if(encoder->channels == 0 || encoder->channels > FLAC__MAX_CHANNELS)
-               return encoder->state = FLAC__ENCODER_INVALID_NUMBER_OF_CHANNELS;
+       encoder->protected->streamable_subset = streamable_subset;
+       encoder->protected->do_mid_side_stereo = do_mid_side_stereo;
+       encoder->protected->loose_mid_side_stereo = loose_mid_side_stereo;
+       encoder->protected->channels = channels;
+       encoder->protected->bits_per_sample = bits_per_sample;
+       encoder->protected->sample_rate = sample_rate;
+       encoder->protected->blocksize = blocksize;
+       encoder->protected->max_lpc_order = max_lpc_order;
+       encoder->protected->qlp_coeff_precision = qlp_coeff_precision;
+       encoder->protected->do_qlp_coeff_prec_search = do_qlp_coeff_prec_search;
+       encoder->protected->do_exhaustive_model_search = do_exhaustive_model_search;
+       encoder->protected->min_residual_partition_order = min_residual_partition_order;
+       encoder->protected->max_residual_partition_order = max_residual_partition_order;
+       encoder->protected->rice_parameter_search_dist = rice_parameter_search_dist;
+       encoder->protected->total_samples_estimate = total_samples_estimate;
+       encoder->protected->seek_table = seek_table;
+       encoder->protected->padding = padding;
 
-       if(encoder->do_mid_side_stereo && encoder->channels != 2)
-               return encoder->state = FLAC__ENCODER_MID_SIDE_CHANNELS_MISMATCH;
+       if(encoder->protected->channels == 0 || encoder->protected->channels > FLAC__MAX_CHANNELS)
+               return encoder->protected->state = FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS;
 
-       if(encoder->loose_mid_side_stereo && !encoder->do_mid_side_stereo)
-               return encoder->state = FLAC__ENCODER_ILLEGAL_MID_SIDE_FORCE;
+       if(encoder->protected->do_mid_side_stereo && encoder->protected->channels != 2)
+               return encoder->protected->state = FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH;
 
-       if(encoder->bits_per_sample >= 32)
-               encoder->do_mid_side_stereo = false; /* since we do 32-bit math, the side channel would have 33 bps and overflow */
+       if(encoder->protected->loose_mid_side_stereo && !encoder->protected->do_mid_side_stereo)
+               return encoder->protected->state = FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE;
 
-       if(encoder->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->bits_per_sample > FLAC__MAX_BITS_PER_SAMPLE)
-               return encoder->state = FLAC__ENCODER_INVALID_BITS_PER_SAMPLE;
+       if(encoder->protected->bits_per_sample >= 32)
+               encoder->protected->do_mid_side_stereo = false; /* since we do 32-bit math, the side channel would have 33 bps and overflow */
 
-       if(encoder->sample_rate == 0 || encoder->sample_rate > FLAC__MAX_SAMPLE_RATE)
-               return encoder->state = FLAC__ENCODER_INVALID_SAMPLE_RATE;
+       if(encoder->protected->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->protected->bits_per_sample > FLAC__MAX_BITS_PER_SAMPLE)
+               return encoder->protected->state = FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE;
 
-       if(encoder->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->blocksize > FLAC__MAX_BLOCK_SIZE)
-               return encoder->state = FLAC__ENCODER_INVALID_BLOCK_SIZE;
+       if(encoder->protected->sample_rate == 0 || encoder->protected->sample_rate > FLAC__MAX_SAMPLE_RATE)
+               return encoder->protected->state = FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE;
 
-       if(encoder->blocksize < encoder->max_lpc_order)
-               return encoder->state = FLAC__ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
+       if(encoder->protected->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected->blocksize > FLAC__MAX_BLOCK_SIZE)
+               return encoder->protected->state = FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE;
 
-       if(encoder->qlp_coeff_precision == 0) {
-               if(encoder->bits_per_sample < 16) {
+       if(encoder->protected->blocksize < encoder->protected->max_lpc_order)
+               return encoder->protected->state = FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
+
+       if(encoder->protected->qlp_coeff_precision == 0) {
+               if(encoder->protected->bits_per_sample < 16) {
                        /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
                        /* @@@ until then we'll make a guess */
-                       encoder->qlp_coeff_precision = max(5, 2 + encoder->bits_per_sample / 2);
+                       encoder->protected->qlp_coeff_precision = max(5, 2 + encoder->protected->bits_per_sample / 2);
                }
-               else if(encoder->bits_per_sample == 16) {
-                       if(encoder->blocksize <= 192)
-                               encoder->qlp_coeff_precision = 7;
-                       else if(encoder->blocksize <= 384)
-                               encoder->qlp_coeff_precision = 8;
-                       else if(encoder->blocksize <= 576)
-                               encoder->qlp_coeff_precision = 9;
-                       else if(encoder->blocksize <= 1152)
-                               encoder->qlp_coeff_precision = 10;
-                       else if(encoder->blocksize <= 2304)
-                               encoder->qlp_coeff_precision = 11;
-                       else if(encoder->blocksize <= 4608)
-                               encoder->qlp_coeff_precision = 12;
+               else if(encoder->protected->bits_per_sample == 16) {
+                       if(encoder->protected->blocksize <= 192)
+                               encoder->protected->qlp_coeff_precision = 7;
+                       else if(encoder->protected->blocksize <= 384)
+                               encoder->protected->qlp_coeff_precision = 8;
+                       else if(encoder->protected->blocksize <= 576)
+                               encoder->protected->qlp_coeff_precision = 9;
+                       else if(encoder->protected->blocksize <= 1152)
+                               encoder->protected->qlp_coeff_precision = 10;
+                       else if(encoder->protected->blocksize <= 2304)
+                               encoder->protected->qlp_coeff_precision = 11;
+                       else if(encoder->protected->blocksize <= 4608)
+                               encoder->protected->qlp_coeff_precision = 12;
                        else
-                               encoder->qlp_coeff_precision = 13;
+                               encoder->protected->qlp_coeff_precision = 13;
                }
                else {
-                       encoder->qlp_coeff_precision = min(13, 8*sizeof(int32) - encoder->bits_per_sample - 1);
+                       encoder->protected->qlp_coeff_precision = min(13, 8*sizeof(int32) - encoder->protected->bits_per_sample - 1);
                }
        }
-       else if(encoder->qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION || encoder->qlp_coeff_precision + encoder->bits_per_sample >= 8*sizeof(uint32) || encoder->qlp_coeff_precision >= (1u<<FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN))
-               return encoder->state = FLAC__ENCODER_INVALID_QLP_COEFF_PRECISION;
+       else if(encoder->protected->qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION || encoder->protected->qlp_coeff_precision + encoder->protected->bits_per_sample >= 8*sizeof(uint32) || encoder->protected->qlp_coeff_precision >= (1u<<FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN))
+               return encoder->protected->state = FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION;
 
-       if(encoder->streamable_subset) {
+       if(encoder->protected->streamable_subset) {
                //@@@ add check for blocksize here
-               if(encoder->bits_per_sample != 8 && encoder->bits_per_sample != 12 && encoder->bits_per_sample != 16 && encoder->bits_per_sample != 20 && encoder->bits_per_sample != 24)
-                       return encoder->state = FLAC__ENCODER_NOT_STREAMABLE;
-               if(encoder->sample_rate > 655350)
-                       return encoder->state = FLAC__ENCODER_NOT_STREAMABLE;
+               if(encoder->protected->bits_per_sample != 8 && encoder->protected->bits_per_sample != 12 && encoder->protected->bits_per_sample != 16 && encoder->protected->bits_per_sample != 20 && encoder->protected->bits_per_sample != 24)
+                       return encoder->protected->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
+               if(encoder->protected->sample_rate > 655350)
+                       return encoder->protected->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
        }
 
-       if(encoder->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
-               encoder->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
-       if(encoder->min_residual_partition_order >= encoder->max_residual_partition_order)
-               encoder->min_residual_partition_order = encoder->max_residual_partition_order;
-
-       encoder->guts = (FLAC__EncoderPrivate*)malloc(sizeof(FLAC__EncoderPrivate));
-       if(encoder->guts == 0)
-               return encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
+       if(encoder->protected->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
+               encoder->protected->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
+       if(encoder->protected->min_residual_partition_order >= encoder->protected->max_residual_partition_order)
+               encoder->protected->min_residual_partition_order = encoder->protected->max_residual_partition_order;
 
-       encoder->guts->input_capacity = 0;
-       for(i = 0; i < encoder->channels; i++) {
-               encoder->guts->integer_signal_unaligned[i] = encoder->guts->integer_signal[i] = 0;
-               encoder->guts->real_signal_unaligned[i] = encoder->guts->real_signal[i] = 0;
+       encoder->private->input_capacity = 0;
+       for(i = 0; i < encoder->protected->channels; i++) {
+               encoder->private->integer_signal_unaligned[i] = encoder->private->integer_signal[i] = 0;
+               encoder->private->real_signal_unaligned[i] = encoder->private->real_signal[i] = 0;
        }
        for(i = 0; i < 2; i++) {
-               encoder->guts->integer_signal_mid_side_unaligned[i] = encoder->guts->integer_signal_mid_side[i] = 0;
-               encoder->guts->real_signal_mid_side_unaligned[i] = encoder->guts->real_signal_mid_side[i] = 0;
+               encoder->private->integer_signal_mid_side_unaligned[i] = encoder->private->integer_signal_mid_side[i] = 0;
+               encoder->private->real_signal_mid_side_unaligned[i] = encoder->private->real_signal_mid_side[i] = 0;
        }
-       for(i = 0; i < encoder->channels; i++) {
-               encoder->guts->residual_workspace_unaligned[i][0] = encoder->guts->residual_workspace[i][0] = 0;
-               encoder->guts->residual_workspace_unaligned[i][1] = encoder->guts->residual_workspace[i][1] = 0;
-               encoder->guts->best_subframe[i] = 0;
+       for(i = 0; i < encoder->protected->channels; i++) {
+               encoder->private->residual_workspace_unaligned[i][0] = encoder->private->residual_workspace[i][0] = 0;
+               encoder->private->residual_workspace_unaligned[i][1] = encoder->private->residual_workspace[i][1] = 0;
+               encoder->private->best_subframe[i] = 0;
        }
        for(i = 0; i < 2; i++) {
-               encoder->guts->residual_workspace_mid_side_unaligned[i][0] = encoder->guts->residual_workspace_mid_side[i][0] = 0;
-               encoder->guts->residual_workspace_mid_side_unaligned[i][1] = encoder->guts->residual_workspace_mid_side[i][1] = 0;
-               encoder->guts->best_subframe_mid_side[i] = 0;
+               encoder->private->residual_workspace_mid_side_unaligned[i][0] = encoder->private->residual_workspace_mid_side[i][0] = 0;
+               encoder->private->residual_workspace_mid_side_unaligned[i][1] = encoder->private->residual_workspace_mid_side[i][1] = 0;
+               encoder->private->best_subframe_mid_side[i] = 0;
        }
-       for(i = 0; i < encoder->channels; i++) {
-               encoder->guts->subframe_workspace_ptr[i][0] = &encoder->guts->subframe_workspace[i][0];
-               encoder->guts->subframe_workspace_ptr[i][1] = &encoder->guts->subframe_workspace[i][1];
+       for(i = 0; i < encoder->protected->channels; i++) {
+               encoder->private->subframe_workspace_ptr[i][0] = &encoder->private->subframe_workspace[i][0];
+               encoder->private->subframe_workspace_ptr[i][1] = &encoder->private->subframe_workspace[i][1];
        }
        for(i = 0; i < 2; i++) {
-               encoder->guts->subframe_workspace_ptr_mid_side[i][0] = &encoder->guts->subframe_workspace_mid_side[i][0];
-               encoder->guts->subframe_workspace_ptr_mid_side[i][1] = &encoder->guts->subframe_workspace_mid_side[i][1];
+               encoder->private->subframe_workspace_ptr_mid_side[i][0] = &encoder->private->subframe_workspace_mid_side[i][0];
+               encoder->private->subframe_workspace_ptr_mid_side[i][1] = &encoder->private->subframe_workspace_mid_side[i][1];
        }
-       encoder->guts->abs_residual_unaligned = encoder->guts->abs_residual = 0;
-       encoder->guts->abs_residual_partition_sums_unaligned = encoder->guts->abs_residual_partition_sums = 0;
-       encoder->guts->raw_bits_per_partition_unaligned = encoder->guts->raw_bits_per_partition = 0;
-       encoder->guts->loose_mid_side_stereo_frames_exact = (double)encoder->sample_rate * 0.4 / (double)encoder->blocksize;
-       encoder->guts->loose_mid_side_stereo_frames = (unsigned)(encoder->guts->loose_mid_side_stereo_frames_exact + 0.5);
-       if(encoder->guts->loose_mid_side_stereo_frames == 0)
-               encoder->guts->loose_mid_side_stereo_frames = 1;
-       encoder->guts->loose_mid_side_stereo_frame_count = 0;
-       encoder->guts->current_sample_number = 0;
-       encoder->guts->current_frame_number = 0;
+       encoder->private->abs_residual_unaligned = encoder->private->abs_residual = 0;
+       encoder->private->abs_residual_partition_sums_unaligned = encoder->private->abs_residual_partition_sums = 0;
+       encoder->private->raw_bits_per_partition_unaligned = encoder->private->raw_bits_per_partition = 0;
+       encoder->private->loose_mid_side_stereo_frames_exact = (double)encoder->protected->sample_rate * 0.4 / (double)encoder->protected->blocksize;
+       encoder->private->loose_mid_side_stereo_frames = (unsigned)(encoder->private->loose_mid_side_stereo_frames_exact + 0.5);
+       if(encoder->private->loose_mid_side_stereo_frames == 0)
+               encoder->private->loose_mid_side_stereo_frames = 1;
+       encoder->private->loose_mid_side_stereo_frame_count = 0;
+       encoder->private->current_sample_number = 0;
+       encoder->private->current_frame_number = 0;
 
        /*
         * get the CPU info and set the function pointers
         */
-       FLAC__cpu_info(&encoder->guts->cpuinfo);
+       FLAC__cpu_info(&encoder->private->cpuinfo);
        /* first default to the non-asm routines */
-       encoder->guts->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
-       encoder->guts->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
-       encoder->guts->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients;
-       encoder->guts->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients;
+       encoder->private->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
+       encoder->private->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
+       encoder->private->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients;
+       encoder->private->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients;
        /* now override with asm where appropriate */
 #ifndef FLAC__NO_ASM
-       FLAC__ASSERT(encoder->guts->cpuinfo.use_asm);
+       FLAC__ASSERT(encoder->private->cpuinfo.use_asm);
 #ifdef FLAC__CPU_IA32
-       FLAC__ASSERT(encoder->guts->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
+       FLAC__ASSERT(encoder->private->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
 #ifdef FLAC__HAS_NASM
-       if(0 && encoder->guts->cpuinfo.data.ia32.sse) { /* SSE version lacks necessary resolution, plus SSE flag doesn't check for OS support */
-               if(encoder->max_lpc_order < 4)
-                       encoder->guts->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_i386_sse_lag_4;
-               else if(encoder->max_lpc_order < 8)
-                       encoder->guts->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_i386_sse_lag_8;
-               else if(encoder->max_lpc_order < 12)
-                       encoder->guts->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_i386_sse_lag_12;
+       if(0 && encoder->private->cpuinfo.data.ia32.sse) { /* SSE version lacks necessary resolution, plus SSE flag doesn't check for OS support */
+               if(encoder->protected->max_lpc_order < 4)
+                       encoder->private->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_i386_sse_lag_4;
+               else if(encoder->protected->max_lpc_order < 8)
+                       encoder->private->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_i386_sse_lag_8;
+               else if(encoder->protected->max_lpc_order < 12)
+                       encoder->private->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_i386_sse_lag_12;
                else
-                       encoder->guts->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_i386;
+                       encoder->private->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_i386;
        }
        else
-               encoder->guts->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_i386;
-       if(encoder->guts->cpuinfo.data.ia32.mmx && encoder->guts->cpuinfo.data.ia32.cmov)
-               encoder->guts->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_i386_mmx_cmov;
-       if(encoder->guts->cpuinfo.data.ia32.mmx) {
-               encoder->guts->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_i386;
-               encoder->guts->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_i386_mmx;
+               encoder->private->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_i386;
+       if(encoder->private->cpuinfo.data.ia32.mmx && encoder->private->cpuinfo.data.ia32.cmov)
+               encoder->private->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_i386_mmx_cmov;
+       if(encoder->private->cpuinfo.data.ia32.mmx) {
+               encoder->private->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_i386;
+               encoder->private->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_i386_mmx;
        }
        else {
-               encoder->guts->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_i386;
-               encoder->guts->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_i386;
+               encoder->private->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_i386;
+               encoder->private->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_i386;
        }
 #endif
 #endif
 #endif
 
-       if(encoder->bits_per_sample + FLAC__bitmath_ilog2(encoder->blocksize)+1 > 30)
-               encoder->guts->use_slow = true;
+       if(encoder->protected->bits_per_sample + FLAC__bitmath_ilog2(encoder->protected->blocksize)+1 > 30)
+               encoder->private->use_slow = true;
        else
-               encoder->guts->use_slow = false;
+               encoder->private->use_slow = false;
 
-       if(!encoder_resize_buffers_(encoder, encoder->blocksize)) {
+       if(!stream_encoder_resize_buffers_(encoder, encoder->protected->blocksize)) {
                /* the above function sets the state for us in case of an error */
-               return encoder->state;
+               return encoder->protected->state;
        }
-       FLAC__bitbuffer_init(&encoder->guts->frame);
-       encoder->guts->write_callback = write_callback;
-       encoder->guts->metadata_callback = metadata_callback;
-       encoder->guts->client_data = client_data;
+       FLAC__bitbuffer_init(&encoder->private->frame);
+       encoder->private->write_callback = write_callback;
+       encoder->private->metadata_callback = metadata_callback;
+       encoder->private->client_data = client_data;
 
        /*
         * write the stream header
         */
-       if(!FLAC__bitbuffer_clear(&encoder->guts->frame))
-               return encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
-
-       if(!FLAC__bitbuffer_write_raw_uint32(&encoder->guts->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN))
-               return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
-
-       encoder->guts->metadata.type = FLAC__METADATA_TYPE_STREAMINFO;
-       encoder->guts->metadata.is_last = (encoder->seek_table == 0 && encoder->padding == 0);
-       encoder->guts->metadata.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
-       encoder->guts->metadata.data.stream_info.min_blocksize = encoder->blocksize; /* this encoder uses the same blocksize for the whole stream */
-       encoder->guts->metadata.data.stream_info.max_blocksize = encoder->blocksize;
-       encoder->guts->metadata.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
-       encoder->guts->metadata.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
-       encoder->guts->metadata.data.stream_info.sample_rate = encoder->sample_rate;
-       encoder->guts->metadata.data.stream_info.channels = encoder->channels;
-       encoder->guts->metadata.data.stream_info.bits_per_sample = encoder->bits_per_sample;
-       encoder->guts->metadata.data.stream_info.total_samples = encoder->total_samples_estimate; /* we will replace this later with the real total */
-       memset(encoder->guts->metadata.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */
-       MD5Init(&encoder->guts->md5context);
-       if(!FLAC__add_metadata_block(&encoder->guts->metadata, &encoder->guts->frame))
-               return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
-
-       if(0 != encoder->seek_table) {
-               if(!FLAC__seek_table_is_valid(encoder->seek_table))
-                       return encoder->state = FLAC__ENCODER_INVALID_SEEK_TABLE;
-               seek_table.type = FLAC__METADATA_TYPE_SEEKTABLE;
-               seek_table.is_last = (encoder->padding == 0);
-               seek_table.length = encoder->seek_table->num_points * FLAC__STREAM_METADATA_SEEKPOINT_LEN;
-               seek_table.data.seek_table = *encoder->seek_table;
-               if(!FLAC__add_metadata_block(&seek_table, &encoder->guts->frame))
-                       return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
+
+       if(!FLAC__bitbuffer_clear(&encoder->private->frame))
+               return encoder->protected->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
+       if(!FLAC__bitbuffer_write_raw_uint32(&encoder->private->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN))
+               return encoder->protected->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
+
+       encoder->private->metadata.type = FLAC__METADATA_TYPE_STREAMINFO;
+       encoder->private->metadata.is_last = (encoder->protected->seek_table == 0 && encoder->protected->padding == 0);
+       encoder->private->metadata.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
+       encoder->private->metadata.data.stream_info.min_blocksize = encoder->protected->blocksize; /* this encoder uses the same blocksize for the whole stream */
+       encoder->private->metadata.data.stream_info.max_blocksize = encoder->protected->blocksize;
+       encoder->private->metadata.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
+       encoder->private->metadata.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
+       encoder->private->metadata.data.stream_info.sample_rate = encoder->protected->sample_rate;
+       encoder->private->metadata.data.stream_info.channels = encoder->protected->channels;
+       encoder->private->metadata.data.stream_info.bits_per_sample = encoder->protected->bits_per_sample;
+       encoder->private->metadata.data.stream_info.total_samples = encoder->protected->total_samples_estimate; /* we will replace this later with the real total */
+       memset(encoder->private->metadata.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */
+       MD5Init(&encoder->private->md5context);
+       if(!FLAC__add_metadata_block(&encoder->private->metadata, &encoder->private->frame))
+               return encoder->protected->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
+
+       if(0 != encoder->protected->seek_table) {
+               if(!FLAC__seek_table_is_valid(encoder->protected->seek_table))
+                       return encoder->protected->state = FLAC__STREAM_ENCODER_INVALID_SEEK_TABLE;
+               seek_table_block.type = FLAC__METADATA_TYPE_SEEKTABLE;
+               seek_table_block.is_last = (encoder->protected->padding == 0);
+               seek_table_block.length = encoder->protected->seek_table->num_points * FLAC__STREAM_METADATA_SEEKPOINT_LEN;
+               seek_table_block.data.seek_table = *encoder->protected->seek_table;
+               if(!FLAC__add_metadata_block(&seek_table_block, &encoder->private->frame))
+                       return encoder->protected->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
        }
 
        /* add a PADDING block if requested */
-       if(encoder->padding > 0) {
-               padding.type = FLAC__METADATA_TYPE_PADDING;
-               padding.is_last = true;
-               padding.length = encoder->padding;
-               if(!FLAC__add_metadata_block(&padding, &encoder->guts->frame))
-                       return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
+       if(encoder->protected->padding > 0) {
+               padding_block.type = FLAC__METADATA_TYPE_PADDING;
+               padding_block.is_last = true;
+               padding_block.length = encoder->protected->padding;
+               if(!FLAC__add_metadata_block(&padding_block, &encoder->private->frame))
+                       return encoder->protected->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
        }
 
-       FLAC__ASSERT(encoder->guts->frame.bits == 0); /* assert that we're byte-aligned before writing */
-       FLAC__ASSERT(encoder->guts->frame.total_consumed_bits == 0); /* assert that no reading of the buffer was done */
-       if(encoder->guts->write_callback(encoder, encoder->guts->frame.buffer, encoder->guts->frame.bytes, 0, encoder->guts->current_frame_number, encoder->guts->client_data) != FLAC__ENCODER_WRITE_OK)
-               return encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING;
+       FLAC__ASSERT(encoder->private->frame.bits == 0); /* assert that we're byte-aligned before writing */
+       FLAC__ASSERT(encoder->private->frame.total_consumed_bits == 0); /* assert that no reading of the buffer was done */
+       if(encoder->private->write_callback(encoder, encoder->private->frame.buffer, encoder->private->frame.bytes, 0, encoder->private->current_frame_number, encoder->private->client_data) != FLAC__STREAM_ENCODER_WRITE_OK)
+               return encoder->protected->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING;
 
        /* now that the metadata block is written, we can init this to an absurdly-high value... */
-       encoder->guts->metadata.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1;
+       encoder->private->metadata.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1;
        /* ... and clear this to 0 */
-       encoder->guts->metadata.data.stream_info.total_samples = 0;
+       encoder->private->metadata.data.stream_info.total_samples = 0;
 
-       return encoder->state;
+       return encoder->protected->state;
 }
 
-void FLAC__encoder_finish(FLAC__Encoder *encoder)
+void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
 {
        unsigned i, channel;
 
        FLAC__ASSERT(encoder != 0);
-       if(encoder->state == FLAC__ENCODER_UNINITIALIZED)
+       if(encoder->protected->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
                return;
-       if(encoder->guts->current_sample_number != 0) {
-               encoder->blocksize = encoder->guts->current_sample_number;
-               encoder_process_frame_(encoder, true); /* true => is last frame */
+       if(encoder->private->current_sample_number != 0) {
+               encoder->protected->blocksize = encoder->private->current_sample_number;
+               stream_encoder_process_frame_(encoder, true); /* true => is last frame */
        }
-       MD5Final(encoder->guts->metadata.data.stream_info.md5sum, &encoder->guts->md5context);
-       encoder->guts->metadata_callback(encoder, &encoder->guts->metadata, encoder->guts->client_data);
-       if(encoder->guts != 0) {
-               for(i = 0; i < encoder->channels; i++) {
-                       if(encoder->guts->integer_signal_unaligned[i] != 0) {
-                               free(encoder->guts->integer_signal_unaligned[i]);
-                               encoder->guts->integer_signal_unaligned[i] = 0;
-                       }
-                       if(encoder->guts->real_signal_unaligned[i] != 0) {
-                               free(encoder->guts->real_signal_unaligned[i]);
-                               encoder->guts->real_signal_unaligned[i] = 0;
-                       }
+       MD5Final(encoder->private->metadata.data.stream_info.md5sum, &encoder->private->md5context);
+       encoder->private->metadata_callback(encoder, &encoder->private->metadata, encoder->private->client_data);
+       for(i = 0; i < encoder->protected->channels; i++) {
+               if(encoder->private->integer_signal_unaligned[i] != 0) {
+                       free(encoder->private->integer_signal_unaligned[i]);
+                       encoder->private->integer_signal_unaligned[i] = 0;
                }
-               for(i = 0; i < 2; i++) {
-                       if(encoder->guts->integer_signal_mid_side_unaligned[i] != 0) {
-                               free(encoder->guts->integer_signal_mid_side_unaligned[i]);
-                               encoder->guts->integer_signal_mid_side_unaligned[i] = 0;
-                       }
-                       if(encoder->guts->real_signal_mid_side_unaligned[i] != 0) {
-                               free(encoder->guts->real_signal_mid_side_unaligned[i]);
-                               encoder->guts->real_signal_mid_side_unaligned[i] = 0;
-                       }
-               }
-               for(channel = 0; channel < encoder->channels; channel++) {
-                       for(i = 0; i < 2; i++) {
-                               if(encoder->guts->residual_workspace_unaligned[channel][i] != 0) {
-                                       free(encoder->guts->residual_workspace_unaligned[channel][i]);
-                                       encoder->guts->residual_workspace_unaligned[channel][i] = 0;
-                               }
-                       }
+               if(encoder->private->real_signal_unaligned[i] != 0) {
+                       free(encoder->private->real_signal_unaligned[i]);
+                       encoder->private->real_signal_unaligned[i] = 0;
                }
-               for(channel = 0; channel < 2; channel++) {
-                       for(i = 0; i < 2; i++) {
-                               if(encoder->guts->residual_workspace_mid_side_unaligned[channel][i] != 0) {
-                                       free(encoder->guts->residual_workspace_mid_side_unaligned[channel][i]);
-                                       encoder->guts->residual_workspace_mid_side_unaligned[channel][i] = 0;
-                               }
-                       }
+       }
+       for(i = 0; i < 2; i++) {
+               if(encoder->private->integer_signal_mid_side_unaligned[i] != 0) {
+                       free(encoder->private->integer_signal_mid_side_unaligned[i]);
+                       encoder->private->integer_signal_mid_side_unaligned[i] = 0;
                }
-               if(encoder->guts->abs_residual_unaligned != 0) {
-                       free(encoder->guts->abs_residual_unaligned);
-                       encoder->guts->abs_residual_unaligned = 0;
+               if(encoder->private->real_signal_mid_side_unaligned[i] != 0) {
+                       free(encoder->private->real_signal_mid_side_unaligned[i]);
+                       encoder->private->real_signal_mid_side_unaligned[i] = 0;
                }
-               if(encoder->guts->abs_residual_partition_sums_unaligned != 0) {
-                       free(encoder->guts->abs_residual_partition_sums_unaligned);
-                       encoder->guts->abs_residual_partition_sums_unaligned = 0;
+       }
+       for(channel = 0; channel < encoder->protected->channels; channel++) {
+               for(i = 0; i < 2; i++) {
+                       if(encoder->private->residual_workspace_unaligned[channel][i] != 0) {
+                               free(encoder->private->residual_workspace_unaligned[channel][i]);
+                               encoder->private->residual_workspace_unaligned[channel][i] = 0;
+                       }
                }
-               if(encoder->guts->raw_bits_per_partition_unaligned != 0) {
-                       free(encoder->guts->raw_bits_per_partition_unaligned);
-                       encoder->guts->raw_bits_per_partition_unaligned = 0;
+       }
+       for(channel = 0; channel < 2; channel++) {
+               for(i = 0; i < 2; i++) {
+                       if(encoder->private->residual_workspace_mid_side_unaligned[channel][i] != 0) {
+                               free(encoder->private->residual_workspace_mid_side_unaligned[channel][i]);
+                               encoder->private->residual_workspace_mid_side_unaligned[channel][i] = 0;
+                       }
                }
-               FLAC__bitbuffer_free(&encoder->guts->frame);
-               free(encoder->guts);
-               encoder->guts = 0;
        }
-       encoder->state = FLAC__ENCODER_UNINITIALIZED;
+       if(encoder->private->abs_residual_unaligned != 0) {
+               free(encoder->private->abs_residual_unaligned);
+               encoder->private->abs_residual_unaligned = 0;
+       }
+       if(encoder->private->abs_residual_partition_sums_unaligned != 0) {
+               free(encoder->private->abs_residual_partition_sums_unaligned);
+               encoder->private->abs_residual_partition_sums_unaligned = 0;
+       }
+       if(encoder->private->raw_bits_per_partition_unaligned != 0) {
+               free(encoder->private->raw_bits_per_partition_unaligned);
+               encoder->private->raw_bits_per_partition_unaligned = 0;
+       }
+       FLAC__bitbuffer_free(&encoder->private->frame);
+
+       encoder->protected->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
+}
+
+FLAC__StreamEncoderState FLAC__stream_encoder_state(const FLAC__StreamEncoder *encoder)
+{
+       return encoder->protected->state;
+}
+
+bool FLAC__stream_encoder_streamable_subset(const FLAC__StreamEncoder *encoder)
+{
+       return encoder->protected->streamable_subset;
+}
+
+bool FLAC__stream_encoder_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
+{
+       return encoder->protected->do_mid_side_stereo;
+}
+
+bool FLAC__stream_encoder_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
+{
+       return encoder->protected->loose_mid_side_stereo;
+}
+
+unsigned FLAC__stream_encoder_channels(const FLAC__StreamEncoder *encoder)
+{
+       return encoder->protected->channels;
+}
+
+unsigned FLAC__stream_encoder_bits_per_sample(const FLAC__StreamEncoder *encoder)
+{
+       return encoder->protected->bits_per_sample;
+}
+
+unsigned FLAC__stream_encoder_sample_rate(const FLAC__StreamEncoder *encoder)
+{
+       return encoder->protected->sample_rate;
+}
+
+unsigned FLAC__stream_encoder_blocksize(const FLAC__StreamEncoder *encoder)
+{
+       return encoder->protected->blocksize;
+}
+
+unsigned FLAC__stream_encoder_max_lpc_order(const FLAC__StreamEncoder *encoder)
+{
+       return encoder->protected->max_lpc_order;
+}
+
+unsigned FLAC__stream_encoder_qlp_coeff_precision(const FLAC__StreamEncoder *encoder)
+{
+       return encoder->protected->qlp_coeff_precision;
+}
+
+bool FLAC__stream_encoder_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder)
+{
+       return encoder->protected->do_qlp_coeff_prec_search;
+}
+
+bool FLAC__stream_encoder_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder)
+{
+       return encoder->protected->do_exhaustive_model_search;
+}
+
+unsigned FLAC__stream_encoder_min_residual_partition_order(const FLAC__StreamEncoder *encoder)
+{
+       return encoder->protected->min_residual_partition_order;
+}
+
+unsigned FLAC__stream_encoder_max_residual_partition_order(const FLAC__StreamEncoder *encoder)
+{
+       return encoder->protected->max_residual_partition_order;
 }
 
-bool FLAC__encoder_process(FLAC__Encoder *encoder, const int32 *buf[], unsigned samples)
+unsigned FLAC__stream_encoder_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
+{
+       return encoder->protected->rice_parameter_search_dist;
+}
+
+bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const int32 *buf[], unsigned samples)
 {
        unsigned i, j, channel;
        int32 x, mid, side;
-       const unsigned channels = encoder->channels, blocksize = encoder->blocksize;
+       const unsigned channels = encoder->protected->channels, blocksize = encoder->protected->blocksize;
 
        FLAC__ASSERT(encoder != 0);
-       FLAC__ASSERT(encoder->state == FLAC__ENCODER_OK);
+       FLAC__ASSERT(encoder->protected->state == FLAC__STREAM_ENCODER_OK);
 
        j = 0;
-       if(encoder->do_mid_side_stereo && channels == 2) {
+       if(encoder->protected->do_mid_side_stereo && channels == 2) {
                do {
-                       for(i = encoder->guts->current_sample_number; i < blocksize && j < samples; i++, j++) {
+                       for(i = encoder->private->current_sample_number; i < blocksize && j < samples; i++, j++) {
                                x = mid = side = buf[0][j];
-                               encoder->guts->integer_signal[0][i] = x;
-                               encoder->guts->real_signal[0][i] = (real)x;
+                               encoder->private->integer_signal[0][i] = x;
+                               encoder->private->real_signal[0][i] = (real)x;
                                x = buf[1][j];
-                               encoder->guts->integer_signal[1][i] = x;
-                               encoder->guts->real_signal[1][i] = (real)x;
+                               encoder->private->integer_signal[1][i] = x;
+                               encoder->private->real_signal[1][i] = (real)x;
                                mid += x;
                                side -= x;
                                mid >>= 1; /* NOTE: not the same as 'mid = (buf[0][j] + buf[1][j]) / 2' ! */
-                               encoder->guts->integer_signal_mid_side[1][i] = side;
-                               encoder->guts->integer_signal_mid_side[0][i] = mid;
-                               encoder->guts->real_signal_mid_side[1][i] = (real)side;
-                               encoder->guts->real_signal_mid_side[0][i] = (real)mid;
-                               encoder->guts->current_sample_number++;
+                               encoder->private->integer_signal_mid_side[1][i] = side;
+                               encoder->private->integer_signal_mid_side[0][i] = mid;
+                               encoder->private->real_signal_mid_side[1][i] = (real)side;
+                               encoder->private->real_signal_mid_side[0][i] = (real)mid;
+                               encoder->private->current_sample_number++;
                        }
                        if(i == blocksize) {
-                               if(!encoder_process_frame_(encoder, false)) /* false => not last frame */
+                               if(!stream_encoder_process_frame_(encoder, false)) /* false => not last frame */
                                        return false;
                        }
                } while(j < samples);
        }
        else {
                do {
-                       for(i = encoder->guts->current_sample_number; i < blocksize && j < samples; i++, j++) {
+                       for(i = encoder->private->current_sample_number; i < blocksize && j < samples; i++, j++) {
                                for(channel = 0; channel < channels; channel++) {
                                        x = buf[channel][j];
-                                       encoder->guts->integer_signal[channel][i] = x;
-                                       encoder->guts->real_signal[channel][i] = (real)x;
+                                       encoder->private->integer_signal[channel][i] = x;
+                                       encoder->private->real_signal[channel][i] = (real)x;
                                }
-                               encoder->guts->current_sample_number++;
+                               encoder->private->current_sample_number++;
                        }
                        if(i == blocksize) {
-                               if(!encoder_process_frame_(encoder, false)) /* false => not last frame */
+                               if(!stream_encoder_process_frame_(encoder, false)) /* false => not last frame */
                                        return false;
                        }
                } while(j < samples);
@@ -559,52 +669,52 @@ bool FLAC__encoder_process(FLAC__Encoder *encoder, const int32 *buf[], unsigned
 }
 
 /* 'samples' is channel-wide samples, e.g. for 1 second at 44100Hz, 'samples' = 44100 regardless of the number of channels */
-bool FLAC__encoder_process_interleaved(FLAC__Encoder *encoder, const int32 buf[], unsigned samples)
+bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const int32 buf[], unsigned samples)
 {
        unsigned i, j, k, channel;
        int32 x, mid, side;
-       const unsigned channels = encoder->channels, blocksize = encoder->blocksize;
+       const unsigned channels = encoder->protected->channels, blocksize = encoder->protected->blocksize;
 
        FLAC__ASSERT(encoder != 0);
-       FLAC__ASSERT(encoder->state == FLAC__ENCODER_OK);
+       FLAC__ASSERT(encoder->protected->state == FLAC__STREAM_ENCODER_OK);
 
        j = k = 0;
-       if(encoder->do_mid_side_stereo && channels == 2) {
+       if(encoder->protected->do_mid_side_stereo && channels == 2) {
                do {
-                       for(i = encoder->guts->current_sample_number; i < blocksize && j < samples; i++, j++) {
+                       for(i = encoder->private->current_sample_number; i < blocksize && j < samples; i++, j++) {
                                x = mid = side = buf[k++];
-                               encoder->guts->integer_signal[0][i] = x;
-                               encoder->guts->real_signal[0][i] = (real)x;
+                               encoder->private->integer_signal[0][i] = x;
+                               encoder->private->real_signal[0][i] = (real)x;
                                x = buf[k++];
-                               encoder->guts->integer_signal[1][i] = x;
-                               encoder->guts->real_signal[1][i] = (real)x;
+                               encoder->private->integer_signal[1][i] = x;
+                               encoder->private->real_signal[1][i] = (real)x;
                                mid += x;
                                side -= x;
                                mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
-                               encoder->guts->integer_signal_mid_side[1][i] = side;
-                               encoder->guts->integer_signal_mid_side[0][i] = mid;
-                               encoder->guts->real_signal_mid_side[1][i] = (real)side;
-                               encoder->guts->real_signal_mid_side[0][i] = (real)mid;
-                               encoder->guts->current_sample_number++;
+                               encoder->private->integer_signal_mid_side[1][i] = side;
+                               encoder->private->integer_signal_mid_side[0][i] = mid;
+                               encoder->private->real_signal_mid_side[1][i] = (real)side;
+                               encoder->private->real_signal_mid_side[0][i] = (real)mid;
+                               encoder->private->current_sample_number++;
                        }
                        if(i == blocksize) {
-                               if(!encoder_process_frame_(encoder, false)) /* false => not last frame */
+                               if(!stream_encoder_process_frame_(encoder, false)) /* false => not last frame */
                                        return false;
                        }
                } while(j < samples);
        }
        else {
                do {
-                       for(i = encoder->guts->current_sample_number; i < blocksize && j < samples; i++, j++) {
+                       for(i = encoder->private->current_sample_number; i < blocksize && j < samples; i++, j++) {
                                for(channel = 0; channel < channels; channel++) {
                                        x = buf[k++];
-                                       encoder->guts->integer_signal[channel][i] = x;
-                                       encoder->guts->real_signal[channel][i] = (real)x;
+                                       encoder->private->integer_signal[channel][i] = x;
+                                       encoder->private->real_signal[channel][i] = (real)x;
                                }
-                               encoder->guts->current_sample_number++;
+                               encoder->private->current_sample_number++;
                        }
                        if(i == blocksize) {
-                               if(!encoder_process_frame_(encoder, false)) /* false => not last frame */
+                               if(!stream_encoder_process_frame_(encoder, false)) /* false => not last frame */
                                        return false;
                        }
                } while(j < samples);
@@ -613,23 +723,77 @@ bool FLAC__encoder_process_interleaved(FLAC__Encoder *encoder, const int32 buf[]
        return true;
 }
 
-bool encoder_process_frame_(FLAC__Encoder *encoder, bool is_last_frame)
+bool stream_encoder_resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size)
+{
+       bool ok;
+       unsigned i, channel;
+
+       FLAC__ASSERT(new_size > 0);
+       FLAC__ASSERT(encoder->protected->state == FLAC__STREAM_ENCODER_OK);
+       FLAC__ASSERT(encoder->private->current_sample_number == 0);
+
+       /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
+       if(new_size <= encoder->private->input_capacity)
+               return true;
+
+       ok = true;
+       for(i = 0; ok && i < encoder->protected->channels; i++) {
+               ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->private->integer_signal_unaligned[i], &encoder->private->integer_signal[i]);
+               ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private->real_signal_unaligned[i], &encoder->private->real_signal[i]);
+       }
+       for(i = 0; ok && i < 2; i++) {
+               ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->private->integer_signal_mid_side_unaligned[i], &encoder->private->integer_signal_mid_side[i]);
+               ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private->real_signal_mid_side_unaligned[i], &encoder->private->real_signal_mid_side[i]);
+       }
+       for(channel = 0; ok && channel < encoder->protected->channels; channel++) {
+               for(i = 0; ok && i < 2; i++) {
+                       ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->private->residual_workspace_unaligned[channel][i], &encoder->private->residual_workspace[channel][i]);
+               }
+       }
+       for(channel = 0; ok && channel < 2; channel++) {
+               for(i = 0; ok && i < 2; i++) {
+                       ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->private->residual_workspace_mid_side_unaligned[channel][i], &encoder->private->residual_workspace_mid_side[channel][i]);
+               }
+       }
+       ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size, &encoder->private->abs_residual_unaligned, &encoder->private->abs_residual);
+#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
+       ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size * 2, &encoder->private->abs_residual_partition_sums_unaligned, &encoder->private->abs_residual_partition_sums);
+#endif
+#ifdef FLAC__SEARCH_FOR_ESCAPES
+       ok = ok && FLAC__memory_alloc_aligned_unsigned_array(new_size * 2, &encoder->private->raw_bits_per_partition_unaligned, &encoder->private->raw_bits_per_partition);
+#endif
+
+       if(ok)
+               encoder->private->input_capacity = new_size;
+       else
+               encoder->protected->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
+
+       return ok;
+}
+
+/***********************************************************************
+ *
+ * Private class methods
+ *
+ ***********************************************************************/
+
+bool stream_encoder_process_frame_(FLAC__StreamEncoder *encoder, bool is_last_frame)
 {
-       FLAC__ASSERT(encoder->state == FLAC__ENCODER_OK);
+       FLAC__ASSERT(encoder->protected->state == FLAC__STREAM_ENCODER_OK);
 
        /*
         * Accumulate raw signal to the MD5 signature
         */
        /* NOTE: some versions of GCC can't figure out const-ness right and will give you an 'incompatible pointer type' warning on arg 2 here: */
-       if(!FLAC__MD5Accumulate(&encoder->guts->md5context, encoder->guts->integer_signal, encoder->channels, encoder->blocksize, (encoder->bits_per_sample+7) / 8)) {
-               encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
+       if(!FLAC__MD5Accumulate(&encoder->private->md5context, encoder->private->integer_signal, encoder->protected->channels, encoder->protected->blocksize, (encoder->protected->bits_per_sample+7) / 8)) {
+               encoder->protected->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
                return false;
        }
 
        /*
         * Process the frame header and subframes into the frame bitbuffer
         */
-       if(!encoder_process_subframes_(encoder, is_last_frame)) {
+       if(!stream_encoder_process_subframes_(encoder, is_last_frame)) {
                /* the above function sets the state for us in case of an error */
                return false;
        }
@@ -637,42 +801,42 @@ bool encoder_process_frame_(FLAC__Encoder *encoder, bool is_last_frame)
        /*
         * Zero-pad the frame to a byte_boundary
         */
-       if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(&encoder->guts->frame)) {
-               encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
+       if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(&encoder->private->frame)) {
+               encoder->protected->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
                return false;
        }
 
        /*
         * CRC-16 the whole thing
         */
-       FLAC__ASSERT(encoder->guts->frame.bits == 0); /* assert that we're byte-aligned */
-       FLAC__ASSERT(encoder->guts->frame.total_consumed_bits == 0); /* assert that no reading of the buffer was done */
-       FLAC__bitbuffer_write_raw_uint32(&encoder->guts->frame, FLAC__crc16(encoder->guts->frame.buffer, encoder->guts->frame.bytes), FLAC__FRAME_FOOTER_CRC_LEN);
+       FLAC__ASSERT(encoder->private->frame.bits == 0); /* assert that we're byte-aligned */
+       FLAC__ASSERT(encoder->private->frame.total_consumed_bits == 0); /* assert that no reading of the buffer was done */
+       FLAC__bitbuffer_write_raw_uint32(&encoder->private->frame, FLAC__crc16(encoder->private->frame.buffer, encoder->private->frame.bytes), FLAC__FRAME_FOOTER_CRC_LEN);
 
        /*
         * Write it
         */
-       if(encoder->guts->write_callback(encoder, encoder->guts->frame.buffer, encoder->guts->frame.bytes, encoder->blocksize, encoder->guts->current_frame_number, encoder->guts->client_data) != FLAC__ENCODER_WRITE_OK) {
-               encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING;
+       if(encoder->private->write_callback(encoder, encoder->private->frame.buffer, encoder->private->frame.bytes, encoder->protected->blocksize, encoder->private->current_frame_number, encoder->private->client_data) != FLAC__STREAM_ENCODER_WRITE_OK) {
+               encoder->protected->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING;
                return false;
        }
 
        /*
         * Get ready for the next frame
         */
-       encoder->guts->current_sample_number = 0;
-       encoder->guts->current_frame_number++;
-       encoder->guts->metadata.data.stream_info.total_samples += (uint64)encoder->blocksize;
-       encoder->guts->metadata.data.stream_info.min_framesize = min(encoder->guts->frame.bytes, encoder->guts->metadata.data.stream_info.min_framesize);
-       encoder->guts->metadata.data.stream_info.max_framesize = max(encoder->guts->frame.bytes, encoder->guts->metadata.data.stream_info.max_framesize);
+       encoder->private->current_sample_number = 0;
+       encoder->private->current_frame_number++;
+       encoder->private->metadata.data.stream_info.total_samples += (uint64)encoder->protected->blocksize;
+       encoder->private->metadata.data.stream_info.min_framesize = min(encoder->private->frame.bytes, encoder->private->metadata.data.stream_info.min_framesize);
+       encoder->private->metadata.data.stream_info.max_framesize = max(encoder->private->frame.bytes, encoder->private->metadata.data.stream_info.max_framesize);
 
        return true;
 }
 
-bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame)
+bool stream_encoder_process_subframes_(FLAC__StreamEncoder *encoder, bool is_last_frame)
 {
        FLAC__FrameHeader frame_header;
-       unsigned channel, min_partition_order = encoder->min_residual_partition_order, max_partition_order;
+       unsigned channel, min_partition_order = encoder->protected->min_residual_partition_order, max_partition_order;
        bool do_independent, do_mid_side;
 
        /*
@@ -682,40 +846,40 @@ bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame)
                max_partition_order = 0;
        }
        else {
-               unsigned limit = 0, b = encoder->blocksize;
+               unsigned limit = 0, b = encoder->protected->blocksize;
                while(!(b & 1)) {
                        limit++;
                        b >>= 1;
                }
-               max_partition_order = min(encoder->max_residual_partition_order, limit);
+               max_partition_order = min(encoder->protected->max_residual_partition_order, limit);
        }
        min_partition_order = min(min_partition_order, max_partition_order);
 
        /*
         * Setup the frame
         */
-       if(!FLAC__bitbuffer_clear(&encoder->guts->frame)) {
-               encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
+       if(!FLAC__bitbuffer_clear(&encoder->private->frame)) {
+               encoder->protected->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
                return false;
        }
-       frame_header.blocksize = encoder->blocksize;
-       frame_header.sample_rate = encoder->sample_rate;
-       frame_header.channels = encoder->channels;
+       frame_header.blocksize = encoder->protected->blocksize;
+       frame_header.sample_rate = encoder->protected->sample_rate;
+       frame_header.channels = encoder->protected->channels;
        frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
-       frame_header.bits_per_sample = encoder->bits_per_sample;
-       frame_header.number.frame_number = encoder->guts->current_frame_number;
+       frame_header.bits_per_sample = encoder->protected->bits_per_sample;
+       frame_header.number.frame_number = encoder->private->current_frame_number;
 
        /*
         * Figure out what channel assignments to try
         */
-       if(encoder->do_mid_side_stereo) {
-               if(encoder->loose_mid_side_stereo) {
-                       if(encoder->guts->loose_mid_side_stereo_frame_count == 0) {
+       if(encoder->protected->do_mid_side_stereo) {
+               if(encoder->protected->loose_mid_side_stereo) {
+                       if(encoder->private->loose_mid_side_stereo_frame_count == 0) {
                                do_independent = true;
                                do_mid_side = true;
                        }
                        else {
-                               do_independent = (encoder->guts->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
+                               do_independent = (encoder->private->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
                                do_mid_side = !do_independent;
                        }
                }
@@ -736,19 +900,19 @@ bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame)
         */
        if(do_independent) {
                unsigned w;
-               for(channel = 0; channel < encoder->channels; channel++) {
-                       w = encoder_get_wasted_bits_(encoder->guts->integer_signal[channel], encoder->blocksize);
-                       encoder->guts->subframe_workspace[channel][0].wasted_bits = encoder->guts->subframe_workspace[channel][1].wasted_bits = w;
-                       encoder->guts->subframe_bps[channel] = encoder->bits_per_sample - w;
+               for(channel = 0; channel < encoder->protected->channels; channel++) {
+                       w = stream_encoder_get_wasted_bits_(encoder->private->integer_signal[channel], encoder->protected->blocksize);
+                       encoder->private->subframe_workspace[channel][0].wasted_bits = encoder->private->subframe_workspace[channel][1].wasted_bits = w;
+                       encoder->private->subframe_bps[channel] = encoder->protected->bits_per_sample - w;
                }
        }
        if(do_mid_side) {
                unsigned w;
-               FLAC__ASSERT(encoder->channels == 2);
+               FLAC__ASSERT(encoder->protected->channels == 2);
                for(channel = 0; channel < 2; channel++) {
-                       w = encoder_get_wasted_bits_(encoder->guts->integer_signal_mid_side[channel], encoder->blocksize);
-                       encoder->guts->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->guts->subframe_workspace_mid_side[channel][1].wasted_bits = w;
-                       encoder->guts->subframe_bps_mid_side[channel] = encoder->bits_per_sample - w + (channel==0? 0:1);
+                       w = stream_encoder_get_wasted_bits_(encoder->private->integer_signal_mid_side[channel], encoder->protected->blocksize);
+                       encoder->private->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private->subframe_workspace_mid_side[channel][1].wasted_bits = w;
+                       encoder->private->subframe_bps_mid_side[channel] = encoder->protected->bits_per_sample - w + (channel==0? 0:1);
                }
        }
 
@@ -756,8 +920,8 @@ bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame)
         * First do a normal encoding pass of each independent channel
         */
        if(do_independent) {
-               for(channel = 0; channel < encoder->channels; channel++) {
-                       if(!encoder_process_subframe_(encoder, min_partition_order, max_partition_order, false, &frame_header, encoder->guts->subframe_bps[channel], encoder->guts->integer_signal[channel], encoder->guts->real_signal[channel], encoder->guts->subframe_workspace_ptr[channel], encoder->guts->residual_workspace[channel], encoder->guts->best_subframe+channel, encoder->guts->best_subframe_bits+channel))
+               for(channel = 0; channel < encoder->protected->channels; channel++) {
+                       if(!stream_encoder_process_subframe_(encoder, min_partition_order, max_partition_order, false, &frame_header, encoder->private->subframe_bps[channel], encoder->private->integer_signal[channel], encoder->private->real_signal[channel], encoder->private->subframe_workspace_ptr[channel], encoder->private->residual_workspace[channel], encoder->private->best_subframe+channel, encoder->private->best_subframe_bits+channel))
                                return false;
                }
        }
@@ -766,10 +930,10 @@ bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame)
         * Now do mid and side channels if requested
         */
        if(do_mid_side) {
-               FLAC__ASSERT(encoder->channels == 2);
+               FLAC__ASSERT(encoder->protected->channels == 2);
 
                for(channel = 0; channel < 2; channel++) {
-                       if(!encoder_process_subframe_(encoder, min_partition_order, max_partition_order, false, &frame_header, encoder->guts->subframe_bps_mid_side[channel], encoder->guts->integer_signal_mid_side[channel], encoder->guts->real_signal_mid_side[channel], encoder->guts->subframe_workspace_ptr_mid_side[channel], encoder->guts->residual_workspace_mid_side[channel], encoder->guts->best_subframe_mid_side+channel, encoder->guts->best_subframe_bits_mid_side+channel))
+                       if(!stream_encoder_process_subframe_(encoder, min_partition_order, max_partition_order, false, &frame_header, encoder->private->subframe_bps_mid_side[channel], encoder->private->integer_signal_mid_side[channel], encoder->private->real_signal_mid_side[channel], encoder->private->subframe_workspace_ptr_mid_side[channel], encoder->private->residual_workspace_mid_side[channel], encoder->private->best_subframe_mid_side+channel, encoder->private->best_subframe_bits_mid_side+channel))
                                return false;
                }
        }
@@ -782,10 +946,10 @@ bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame)
                FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
                FLAC__ChannelAssignment channel_assignment;
 
-               FLAC__ASSERT(encoder->channels == 2);
+               FLAC__ASSERT(encoder->protected->channels == 2);
 
-               if(encoder->loose_mid_side_stereo && encoder->guts->loose_mid_side_stereo_frame_count > 0) {
-                       channel_assignment = (encoder->guts->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT? FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT : FLAC__CHANNEL_ASSIGNMENT_MID_SIDE);
+               if(encoder->protected->loose_mid_side_stereo && encoder->private->loose_mid_side_stereo_frame_count > 0) {
+                       channel_assignment = (encoder->private->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT? FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT : FLAC__CHANNEL_ASSIGNMENT_MID_SIDE);
                }
                else {
                        unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
@@ -795,10 +959,10 @@ bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame)
                        FLAC__ASSERT(do_independent && do_mid_side);
 
                        /* We have to figure out which channel assignent results in the smallest frame */
-                       bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->guts->best_subframe_bits         [0] + encoder->guts->best_subframe_bits         [1];
-                       bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE  ] = encoder->guts->best_subframe_bits         [0] + encoder->guts->best_subframe_bits_mid_side[1];
-                       bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->guts->best_subframe_bits         [1] + encoder->guts->best_subframe_bits_mid_side[1];
-                       bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE   ] = encoder->guts->best_subframe_bits_mid_side[0] + encoder->guts->best_subframe_bits_mid_side[1];
+                       bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private->best_subframe_bits         [0] + encoder->private->best_subframe_bits         [1];
+                       bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE  ] = encoder->private->best_subframe_bits         [0] + encoder->private->best_subframe_bits_mid_side[1];
+                       bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private->best_subframe_bits         [1] + encoder->private->best_subframe_bits_mid_side[1];
+                       bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE   ] = encoder->private->best_subframe_bits_mid_side[0] + encoder->private->best_subframe_bits_mid_side[1];
 
                        for(channel_assignment = 0, min_bits = bits[0], ca = 1; ca <= 3; ca++) {
                                if(bits[ca] < min_bits) {
@@ -810,27 +974,27 @@ bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame)
 
                frame_header.channel_assignment = channel_assignment;
 
-               if(!FLAC__frame_add_header(&frame_header, encoder->streamable_subset, is_last_frame, &encoder->guts->frame)) {
-                       encoder->state = FLAC__ENCODER_FRAMING_ERROR;
+               if(!FLAC__frame_add_header(&frame_header, encoder->protected->streamable_subset, is_last_frame, &encoder->private->frame)) {
+                       encoder->protected->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
                        return false;
                }
 
                switch(channel_assignment) {
                        case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
-                               left_subframe  = &encoder->guts->subframe_workspace         [0][encoder->guts->best_subframe         [0]];
-                               right_subframe = &encoder->guts->subframe_workspace         [1][encoder->guts->best_subframe         [1]];
+                               left_subframe  = &encoder->private->subframe_workspace         [0][encoder->private->best_subframe         [0]];
+                               right_subframe = &encoder->private->subframe_workspace         [1][encoder->private->best_subframe         [1]];
                                break;
                        case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
-                               left_subframe  = &encoder->guts->subframe_workspace         [0][encoder->guts->best_subframe         [0]];
-                               right_subframe = &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]];
+                               left_subframe  = &encoder->private->subframe_workspace         [0][encoder->private->best_subframe         [0]];
+                               right_subframe = &encoder->private->subframe_workspace_mid_side[1][encoder->private->best_subframe_mid_side[1]];
                                break;
                        case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
-                               left_subframe  = &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]];
-                               right_subframe = &encoder->guts->subframe_workspace         [1][encoder->guts->best_subframe         [1]];
+                               left_subframe  = &encoder->private->subframe_workspace_mid_side[1][encoder->private->best_subframe_mid_side[1]];
+                               right_subframe = &encoder->private->subframe_workspace         [1][encoder->private->best_subframe         [1]];
                                break;
                        case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
-                               left_subframe  = &encoder->guts->subframe_workspace_mid_side[0][encoder->guts->best_subframe_mid_side[0]];
-                               right_subframe = &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]];
+                               left_subframe  = &encoder->private->subframe_workspace_mid_side[0][encoder->private->best_subframe_mid_side[0]];
+                               right_subframe = &encoder->private->subframe_workspace_mid_side[1][encoder->private->best_subframe_mid_side[1]];
                                break;
                        default:
                                FLAC__ASSERT(0);
@@ -838,61 +1002,61 @@ bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame)
 
                switch(channel_assignment) {
                        case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
-                               left_bps  = encoder->guts->subframe_bps         [0];
-                               right_bps = encoder->guts->subframe_bps         [1];
+                               left_bps  = encoder->private->subframe_bps         [0];
+                               right_bps = encoder->private->subframe_bps         [1];
                                break;
                        case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
-                               left_bps  = encoder->guts->subframe_bps         [0];
-                               right_bps = encoder->guts->subframe_bps_mid_side[1];
+                               left_bps  = encoder->private->subframe_bps         [0];
+                               right_bps = encoder->private->subframe_bps_mid_side[1];
                                break;
                        case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
-                               left_bps  = encoder->guts->subframe_bps_mid_side[1];
-                               right_bps = encoder->guts->subframe_bps         [1];
+                               left_bps  = encoder->private->subframe_bps_mid_side[1];
+                               right_bps = encoder->private->subframe_bps         [1];
                                break;
                        case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
-                               left_bps  = encoder->guts->subframe_bps_mid_side[0];
-                               right_bps = encoder->guts->subframe_bps_mid_side[1];
+                               left_bps  = encoder->private->subframe_bps_mid_side[0];
+                               right_bps = encoder->private->subframe_bps_mid_side[1];
                                break;
                        default:
                                FLAC__ASSERT(0);
                }
 
                /* note that encoder_add_subframe_ sets the state for us in case of an error */
-               if(!encoder_add_subframe_(encoder, &frame_header, left_bps , left_subframe , &encoder->guts->frame))
+               if(!stream_encoder_add_subframe_(encoder, &frame_header, left_bps , left_subframe , &encoder->private->frame))
                        return false;
-               if(!encoder_add_subframe_(encoder, &frame_header, right_bps, right_subframe, &encoder->guts->frame))
+               if(!stream_encoder_add_subframe_(encoder, &frame_header, right_bps, right_subframe, &encoder->private->frame))
                        return false;
        }
        else {
-               if(!FLAC__frame_add_header(&frame_header, encoder->streamable_subset, is_last_frame, &encoder->guts->frame)) {
-                       encoder->state = FLAC__ENCODER_FRAMING_ERROR;
+               if(!FLAC__frame_add_header(&frame_header, encoder->protected->streamable_subset, is_last_frame, &encoder->private->frame)) {
+                       encoder->protected->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
                        return false;
                }
 
-               for(channel = 0; channel < encoder->channels; channel++) {
-                       if(!encoder_add_subframe_(encoder, &frame_header, encoder->guts->subframe_bps[channel], &encoder->guts->subframe_workspace[channel][encoder->guts->best_subframe[channel]], &encoder->guts->frame)) {
+               for(channel = 0; channel < encoder->protected->channels; channel++) {
+                       if(!stream_encoder_add_subframe_(encoder, &frame_header, encoder->private->subframe_bps[channel], &encoder->private->subframe_workspace[channel][encoder->private->best_subframe[channel]], &encoder->private->frame)) {
                                /* the above function sets the state for us in case of an error */
                                return false;
                        }
                }
        }
 
-       if(encoder->loose_mid_side_stereo) {
-               encoder->guts->loose_mid_side_stereo_frame_count++;
-               if(encoder->guts->loose_mid_side_stereo_frame_count >= encoder->guts->loose_mid_side_stereo_frames)
-                       encoder->guts->loose_mid_side_stereo_frame_count = 0;
+       if(encoder->protected->loose_mid_side_stereo) {
+               encoder->private->loose_mid_side_stereo_frame_count++;
+               if(encoder->private->loose_mid_side_stereo_frame_count >= encoder->private->loose_mid_side_stereo_frames)
+                       encoder->private->loose_mid_side_stereo_frame_count = 0;
        }
 
-       encoder->guts->last_channel_assignment = frame_header.channel_assignment;
+       encoder->private->last_channel_assignment = frame_header.channel_assignment;
 
        return true;
 }
 
-bool encoder_process_subframe_(FLAC__Encoder *encoder, unsigned min_partition_order, unsigned max_partition_order, bool verbatim_only, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const int32 integer_signal[], const real real_signal[], FLAC__Subframe *subframe[2], int32 *residual[2], unsigned *best_subframe, unsigned *best_bits)
+bool stream_encoder_process_subframe_(FLAC__StreamEncoder *encoder, unsigned min_partition_order, unsigned max_partition_order, bool verbatim_only, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const int32 integer_signal[], const real real_signal[], FLAC__Subframe *subframe[2], int32 *residual[2], unsigned *best_subframe, unsigned *best_bits)
 {
        real fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
        real lpc_residual_bits_per_sample;
-       real autoc[FLAC__MAX_LPC_ORDER+1]; /* WATCHOUT: the size is important even though encoder->max_lpc_order might be less; some asm routines need all the space */
+       real autoc[FLAC__MAX_LPC_ORDER+1]; /* WATCHOUT: the size is important even though encoder->protected->max_lpc_order might be less; some asm routines need all the space */
        real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER];
        real lpc_error[FLAC__MAX_LPC_ORDER];
        unsigned min_lpc_order, max_lpc_order, lpc_order;
@@ -904,14 +1068,14 @@ bool encoder_process_subframe_(FLAC__Encoder *encoder, unsigned min_partition_or
 
        /* verbatim subframe is the baseline against which we measure other compressed subframes */
        _best_subframe = 0;
-       _best_bits = encoder_evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
+       _best_bits = stream_encoder_evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
 
        if(!verbatim_only && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
                /* check for constant subframe */
-               if(encoder->guts->use_slow)
+               if(encoder->private->use_slow)
                        guess_fixed_order = FLAC__fixed_compute_best_predictor_slow(integer_signal+FLAC__MAX_FIXED_ORDER, frame_header->blocksize-FLAC__MAX_FIXED_ORDER, fixed_residual_bits_per_sample);
                else
-                       guess_fixed_order = encoder->guts->local_fixed_compute_best_predictor(integer_signal+FLAC__MAX_FIXED_ORDER, frame_header->blocksize-FLAC__MAX_FIXED_ORDER, fixed_residual_bits_per_sample);
+                       guess_fixed_order = encoder->private->local_fixed_compute_best_predictor(integer_signal+FLAC__MAX_FIXED_ORDER, frame_header->blocksize-FLAC__MAX_FIXED_ORDER, fixed_residual_bits_per_sample);
                if(fixed_residual_bits_per_sample[1] == 0.0) {
                        /* the above means integer_signal+FLAC__MAX_FIXED_ORDER is constant, now we just have to check the warmup samples */
                        unsigned i, signal_is_constant = true;
@@ -922,7 +1086,7 @@ bool encoder_process_subframe_(FLAC__Encoder *encoder, unsigned min_partition_or
                                }
                        }
                        if(signal_is_constant) {
-                               _candidate_bits = encoder_evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]);
+                               _candidate_bits = stream_encoder_evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]);
                                if(_candidate_bits < _best_bits) {
                                        _best_subframe = !_best_subframe;
                                        _best_bits = _candidate_bits;
@@ -931,7 +1095,7 @@ bool encoder_process_subframe_(FLAC__Encoder *encoder, unsigned min_partition_or
                }
                else {
                        /* encode fixed */
-                       if(encoder->do_exhaustive_model_search) {
+                       if(encoder->protected->do_exhaustive_model_search) {
                                min_fixed_order = 0;
                                max_fixed_order = FLAC__MAX_FIXED_ORDER;
                        }
@@ -947,7 +1111,7 @@ bool encoder_process_subframe_(FLAC__Encoder *encoder, unsigned min_partition_or
 #endif
                                if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
                                        rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
-                               _candidate_bits = encoder_evaluate_fixed_subframe_(integer_signal, residual[!_best_subframe], encoder->guts->abs_residual, encoder->guts->abs_residual_partition_sums, encoder->guts->raw_bits_per_partition, frame_header->blocksize, subframe_bps, fixed_order, rice_parameter, min_partition_order, max_partition_order, encoder->rice_parameter_search_dist, subframe[!_best_subframe]);
+                               _candidate_bits = stream_encoder_evaluate_fixed_subframe_(integer_signal, residual[!_best_subframe], encoder->private->abs_residual, encoder->private->abs_residual_partition_sums, encoder->private->raw_bits_per_partition, frame_header->blocksize, subframe_bps, fixed_order, rice_parameter, min_partition_order, max_partition_order, encoder->protected->rice_parameter_search_dist, subframe[!_best_subframe]);
                                if(_candidate_bits < _best_bits) {
                                        _best_subframe = !_best_subframe;
                                        _best_bits = _candidate_bits;
@@ -955,29 +1119,29 @@ bool encoder_process_subframe_(FLAC__Encoder *encoder, unsigned min_partition_or
                        }
 
                        /* encode lpc */
-                       if(encoder->max_lpc_order > 0) {
-                               if(encoder->max_lpc_order >= frame_header->blocksize)
+                       if(encoder->protected->max_lpc_order > 0) {
+                               if(encoder->protected->max_lpc_order >= frame_header->blocksize)
                                        max_lpc_order = frame_header->blocksize-1;
                                else
-                                       max_lpc_order = encoder->max_lpc_order;
+                                       max_lpc_order = encoder->protected->max_lpc_order;
                                if(max_lpc_order > 0) {
-                                       encoder->guts->local_lpc_compute_autocorrelation(real_signal, frame_header->blocksize, max_lpc_order+1, autoc);
+                                       encoder->private->local_lpc_compute_autocorrelation(real_signal, frame_header->blocksize, max_lpc_order+1, autoc);
                                        /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
                                        if(autoc[0] != 0.0) {
                                                FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, lp_coeff, lpc_error);
-                                               if(encoder->do_exhaustive_model_search) {
+                                               if(encoder->protected->do_exhaustive_model_search) {
                                                        min_lpc_order = 1;
                                                }
                                                else {
                                                        unsigned guess_lpc_order = FLAC__lpc_compute_best_order(lpc_error, max_lpc_order, frame_header->blocksize, subframe_bps);
                                                        min_lpc_order = max_lpc_order = guess_lpc_order;
                                                }
-                                               if(encoder->do_qlp_coeff_prec_search) {
+                                               if(encoder->protected->do_qlp_coeff_prec_search) {
                                                        min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
                                                        max_qlp_coeff_precision = min(32 - subframe_bps - 1, (1u<<FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN)-1);
                                                }
                                                else {
-                                                       min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->qlp_coeff_precision;
+                                                       min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected->qlp_coeff_precision;
                                                }
                                                for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
                                                        lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
@@ -990,7 +1154,7 @@ bool encoder_process_subframe_(FLAC__Encoder *encoder, unsigned min_partition_or
                                                        if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
                                                                rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
                                                        for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
-                                                               _candidate_bits = encoder_evaluate_lpc_subframe_(encoder, integer_signal, residual[!_best_subframe], encoder->guts->abs_residual, encoder->guts->abs_residual_partition_sums, encoder->guts->raw_bits_per_partition, lp_coeff[lpc_order-1], frame_header->blocksize, subframe_bps, lpc_order, qlp_coeff_precision, rice_parameter, min_partition_order, max_partition_order, encoder->rice_parameter_search_dist, subframe[!_best_subframe]);
+                                                               _candidate_bits = stream_encoder_evaluate_lpc_subframe_(encoder, integer_signal, residual[!_best_subframe], encoder->private->abs_residual, encoder->private->abs_residual_partition_sums, encoder->private->raw_bits_per_partition, lp_coeff[lpc_order-1], frame_header->blocksize, subframe_bps, lpc_order, qlp_coeff_precision, rice_parameter, min_partition_order, max_partition_order, encoder->protected->rice_parameter_search_dist, subframe[!_best_subframe]);
                                                                if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
                                                                        if(_candidate_bits < _best_bits) {
                                                                                _best_subframe = !_best_subframe;
@@ -1011,30 +1175,30 @@ bool encoder_process_subframe_(FLAC__Encoder *encoder, unsigned min_partition_or
        return true;
 }
 
-bool encoder_add_subframe_(FLAC__Encoder *encoder, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame)
+bool stream_encoder_add_subframe_(FLAC__StreamEncoder *encoder, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame)
 {
        switch(subframe->type) {
                case FLAC__SUBFRAME_TYPE_CONSTANT:
                        if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
-                               encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
+                               encoder->protected->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
                                return false;
                        }
                        break;
                case FLAC__SUBFRAME_TYPE_FIXED:
                        if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), frame_header->blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) {
-                               encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
+                               encoder->protected->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
                                return false;
                        }
                        break;
                case FLAC__SUBFRAME_TYPE_LPC:
                        if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), frame_header->blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) {
-                               encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
+                               encoder->protected->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
                                return false;
                        }
                        break;
                case FLAC__SUBFRAME_TYPE_VERBATIM:
                        if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) {
-                               encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
+                               encoder->protected->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
                                return false;
                        }
                        break;
@@ -1045,7 +1209,7 @@ bool encoder_add_subframe_(FLAC__Encoder *encoder, const FLAC__FrameHeader *fram
        return true;
 }
 
-unsigned encoder_evaluate_constant_subframe_(const int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe)
+unsigned stream_encoder_evaluate_constant_subframe_(const int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe)
 {
        subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
        subframe->data.constant.value = signal;
@@ -1053,7 +1217,7 @@ unsigned encoder_evaluate_constant_subframe_(const int32 signal, unsigned subfra
        return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe_bps;
 }
 
-unsigned encoder_evaluate_fixed_subframe_(const int32 signal[], int32 residual[], uint32 abs_residual[], uint32 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned blocksize, unsigned subframe_bps, unsigned order, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe)
+unsigned stream_encoder_evaluate_fixed_subframe_(const int32 signal[], int32 residual[], uint32 abs_residual[], uint32 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned blocksize, unsigned subframe_bps, unsigned order, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe)
 {
        unsigned i, residual_bits;
        const unsigned residual_samples = blocksize - order;
@@ -1065,7 +1229,7 @@ unsigned encoder_evaluate_fixed_subframe_(const int32 signal[], int32 residual[]
        subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
        subframe->data.fixed.residual = residual;
 
-       residual_bits = encoder_find_best_partition_order_(residual, abs_residual, abs_residual_partition_sums, raw_bits_per_partition, residual_samples, order, rice_parameter, min_partition_order, max_partition_order, rice_parameter_search_dist, &subframe->data.fixed.entropy_coding_method.data.partitioned_rice.order, subframe->data.fixed.entropy_coding_method.data.partitioned_rice.parameters, subframe->data.fixed.entropy_coding_method.data.partitioned_rice.raw_bits);
+       residual_bits = stream_encoder_find_best_partition_order_(residual, abs_residual, abs_residual_partition_sums, raw_bits_per_partition, residual_samples, order, rice_parameter, min_partition_order, max_partition_order, rice_parameter_search_dist, &subframe->data.fixed.entropy_coding_method.data.partitioned_rice.order, subframe->data.fixed.entropy_coding_method.data.partitioned_rice.parameters, subframe->data.fixed.entropy_coding_method.data.partitioned_rice.raw_bits);
 
        subframe->data.fixed.order = order;
        for(i = 0; i < order; i++)
@@ -1074,7 +1238,7 @@ unsigned encoder_evaluate_fixed_subframe_(const int32 signal[], int32 residual[]
        return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (order * subframe_bps) + residual_bits;
 }
 
-unsigned encoder_evaluate_lpc_subframe_(FLAC__Encoder *encoder, const int32 signal[], int32 residual[], uint32 abs_residual[], uint32 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], const real lp_coeff[], unsigned blocksize, unsigned subframe_bps, unsigned order, unsigned qlp_coeff_precision, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe)
+unsigned stream_encoder_evaluate_lpc_subframe_(FLAC__StreamEncoder *encoder, const int32 signal[], int32 residual[], uint32 abs_residual[], uint32 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], const real lp_coeff[], unsigned blocksize, unsigned subframe_bps, unsigned order, unsigned qlp_coeff_precision, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe)
 {
        int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
        unsigned i, residual_bits;
@@ -1086,16 +1250,16 @@ unsigned encoder_evaluate_lpc_subframe_(FLAC__Encoder *encoder, const int32 sign
                return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
 
        if(subframe_bps <= 16 && qlp_coeff_precision <= 16)
-               encoder->guts->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
+               encoder->private->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
        else
-               encoder->guts->local_lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
+               encoder->private->local_lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
 
        subframe->type = FLAC__SUBFRAME_TYPE_LPC;
 
        subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
        subframe->data.lpc.residual = residual;
 
-       residual_bits = encoder_find_best_partition_order_(residual, abs_residual, abs_residual_partition_sums, raw_bits_per_partition, residual_samples, order, rice_parameter, min_partition_order, max_partition_order, rice_parameter_search_dist, &subframe->data.lpc.entropy_coding_method.data.partitioned_rice.order, subframe->data.lpc.entropy_coding_method.data.partitioned_rice.parameters, subframe->data.lpc.entropy_coding_method.data.partitioned_rice.raw_bits);
+       residual_bits = stream_encoder_find_best_partition_order_(residual, abs_residual, abs_residual_partition_sums, raw_bits_per_partition, residual_samples, order, rice_parameter, min_partition_order, max_partition_order, rice_parameter_search_dist, &subframe->data.lpc.entropy_coding_method.data.partitioned_rice.order, subframe->data.lpc.entropy_coding_method.data.partitioned_rice.parameters, subframe->data.lpc.entropy_coding_method.data.partitioned_rice.raw_bits);
 
        subframe->data.lpc.order = order;
        subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
@@ -1107,7 +1271,7 @@ unsigned encoder_evaluate_lpc_subframe_(FLAC__Encoder *encoder, const int32 sign
        return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN + FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN + (order * (qlp_coeff_precision + subframe_bps)) + residual_bits;
 }
 
-unsigned encoder_evaluate_verbatim_subframe_(const int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe)
+unsigned stream_encoder_evaluate_verbatim_subframe_(const int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe)
 {
        subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
 
@@ -1116,7 +1280,7 @@ unsigned encoder_evaluate_verbatim_subframe_(const int32 signal[], unsigned bloc
        return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (blocksize * subframe_bps);
 }
 
-unsigned encoder_find_best_partition_order_(const int32 residual[], uint32 abs_residual[], uint32 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned residual_samples, unsigned predictor_order, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, unsigned rice_parameter_search_dist, unsigned *best_partition_order, unsigned best_parameters[], unsigned best_raw_bits[])
+unsigned stream_encoder_find_best_partition_order_(const int32 residual[], uint32 abs_residual[], uint32 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned residual_samples, unsigned predictor_order, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, unsigned rice_parameter_search_dist, unsigned *best_partition_order, unsigned best_parameters[], unsigned best_raw_bits[])
 {
        int32 r;
 #if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES)
@@ -1136,12 +1300,12 @@ unsigned encoder_find_best_partition_order_(const int32 residual[], uint32 abs_r
        }
 
 #if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES)
-       max_partition_order = encoder_precompute_partition_info_(residual, abs_residual, abs_residual_partition_sums, raw_bits_per_partition, residual_samples, predictor_order, min_partition_order, max_partition_order);
+       max_partition_order = stream_encoder_precompute_partition_info_(residual, abs_residual, abs_residual_partition_sums, raw_bits_per_partition, residual_samples, predictor_order, min_partition_order, max_partition_order);
        min_partition_order = min(min_partition_order, max_partition_order);
 
        for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
-               if(!encoder_set_partitioned_rice_(abs_residual, abs_residual_partition_sums+sum, raw_bits_per_partition+sum, residual_samples, predictor_order, rice_parameter, rice_parameter_search_dist, (unsigned)partition_order, parameters[!best_parameters_index], raw_bits[!best_parameters_index], &residual_bits)) {
-                       FLAC__ASSERT(0); /* encoder_precompute_partition_info_ should keep this from ever happening */
+               if(!stream_encoder_set_partitioned_rice_(abs_residual, abs_residual_partition_sums+sum, raw_bits_per_partition+sum, residual_samples, predictor_order, rice_parameter, rice_parameter_search_dist, (unsigned)partition_order, parameters[!best_parameters_index], raw_bits[!best_parameters_index], &residual_bits)) {
+                       FLAC__ASSERT(0); /* stream_encoder_precompute_partition_info_ should keep this from ever happening */
                }
                sum += 1u << partition_order;
                if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
@@ -1152,7 +1316,7 @@ unsigned encoder_find_best_partition_order_(const int32 residual[], uint32 abs_r
        }
 #else
        for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
-               if(!encoder_set_partitioned_rice_(abs_residual, 0, 0, residual_samples, predictor_order, rice_parameter, rice_parameter_search_dist, partition_order, parameters[!best_parameters_index], raw_bits[!best_parameters_index], &residual_bits)) {
+               if(!stream_encoder_set_partitioned_rice_(abs_residual, 0, 0, residual_samples, predictor_order, rice_parameter, rice_parameter_search_dist, partition_order, parameters[!best_parameters_index], raw_bits[!best_parameters_index], &residual_bits)) {
                        FLAC__ASSERT(best_residual_bits != 0);
                        break;
                }
@@ -1170,7 +1334,7 @@ unsigned encoder_find_best_partition_order_(const int32 residual[], uint32 abs_r
 }
 
 #if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES)
-unsigned encoder_precompute_partition_info_(const int32 residual[], uint32 abs_residual[], uint32 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned residual_samples, unsigned predictor_order, unsigned min_partition_order, unsigned max_partition_order)
+unsigned stream_encoder_precompute_partition_info_(const int32 residual[], uint32 abs_residual[], uint32 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned residual_samples, unsigned predictor_order, unsigned min_partition_order, unsigned max_partition_order)
 {
        int partition_order;
        unsigned from_partition, to_partition = 0;
@@ -1271,7 +1435,7 @@ unsigned encoder_precompute_partition_info_(const int32 residual[], uint32 abs_r
 #endif
 #define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
 
-bool encoder_set_partitioned_rice_(const uint32 abs_residual[], const uint32 abs_residual_partition_sums[], const unsigned raw_bits_per_partition[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, unsigned parameters[], unsigned raw_bits[], unsigned *bits)
+bool stream_encoder_set_partitioned_rice_(const uint32 abs_residual[], const uint32 abs_residual_partition_sums[], const unsigned raw_bits_per_partition[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, unsigned parameters[], unsigned raw_bits[], unsigned *bits)
 {
        unsigned rice_parameter, partition_bits;
 #ifndef NO_RICE_SEARCH
@@ -1450,7 +1614,7 @@ bool encoder_set_partitioned_rice_(const uint32 abs_residual[], const uint32 abs
        return true;
 }
 
-unsigned encoder_get_wasted_bits_(int32 signal[], unsigned samples)
+unsigned stream_encoder_get_wasted_bits_(int32 signal[], unsigned samples)
 {
        unsigned i, shift;
        int32 x = 0;
@@ -1473,3 +1637,4 @@ unsigned encoder_get_wasted_bits_(int32 signal[], unsigned samples)
 
        return shift;
 }
+
index a3c4e1c..c187245 100644 (file)
@@ -18,7 +18,7 @@
  */
 
 #include <stdio.h>
-#include "private/encoder_framing.h"
+#include "private/stream_encoder_framing.h"
 #include "private/crc.h"
 #include "FLAC/assert.h"