rename FLAC__Encoder to FLAC__StreamEncoder, OOPize encoder and decoder interfaces

This commit is contained in:
Josh Coalson 2001-06-13 17:59:57 +00:00
parent 244ce1114c
commit 0a15c14c6e
21 changed files with 1646 additions and 1185 deletions

View File

@ -756,24 +756,27 @@
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>
@ -786,7 +789,7 @@
</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.
@ -795,19 +798,19 @@
<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>
@ -826,25 +829,24 @@
<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>
@ -859,7 +861,7 @@
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.

View File

@ -17,11 +17,12 @@
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

View File

@ -21,11 +21,11 @@
#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

View 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

View 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);

View File

@ -17,71 +17,121 @@
* 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

View 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;
}

View 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];
}

View 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;

View 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

View 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

View File

@ -33,8 +33,6 @@ C_FILES= \
bitmath.c \
cpu.c \
crc.c \
encoder.c \
encoder_framing.c \
file_decoder.c \
fixed.c \
format.c \
@ -42,7 +40,9 @@ C_FILES= \
md5.c \
memory.c \
seek_table.c \
stream_decoder.c
stream_decoder.c \
stream_encoder.c \
stream_encoder_framing.c
NASM_FILES= \
i386/cpu_asm.s \

View File

@ -22,10 +22,28 @@
#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->guts = (FLAC__FileDecoderPrivate*)malloc(sizeof(FLAC__FileDecoderPrivate));
if(decoder->guts == 0)
return decoder->state = FLAC__FILE_DECODER_MEMORY_ALLOCATION_ERROR;
decoder->protected->state = FLAC__FILE_DECODER_OK;
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->protected->check_md5 = check_md5;
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);
}
decoder->state = FLAC__FILE_DECODER_UNINITIALIZED;
if(decoder->protected->check_md5) {
if(memcmp(decoder->private->stored_md5sum, decoder->private->computed_md5sum, 16))
md5_failed = true;
}
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;
}
}

View File

@ -16,13 +16,14 @@
# 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

View File

@ -24,10 +24,10 @@
#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

View 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"

View 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

View File

@ -22,7 +22,18 @@
#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

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View 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"