<LI><A HREF="#flac">flac</A> - the usage of the command-line file encoder/decoder <B><TT>flac</TT></B>.</LI>
<LI><A HREF="#metaflac">metaflac</A> - the usage of the command-line FLAC metadata editor <B><TT>metaflac</TT></B>.</LI>
<LI><A HREF="#plugins">plugins</A> - documentation for the various input plugins.</LI>
- <LI><A HREF="#libflac">libFLAC API</A> - for developers who want to add FLAC support to their programs.</LI>
- <LI><A HREF="#libflacpp">libFLAC++ API</A> - the documentation for object layer around libFLAC.</LI>
+ <LI><A HREF="api/index.html">libFLAC and libFLAC++ APIs</A> - for developers who want to add FLAC support to their programs.</LI>
<LI><A HREF="#bugs">bugs</A> - known bugs.</LI>
<LI><A HREF="#monkey">How to add FLAC support to the Monkey's Audio GUI</A></LI>
</UL>
</TABLE>
-<TABLE WIDTH="100%" CELLPADDING="5" CELLSPACING="5" BORDER="0">
-<TR><TD>
- <TABLE WIDTH="100%" CELLPADDING="0" CELLSPACING="0" BORDER="0"><TR BGCOLOR="#000000"><TD><IMG SRC="images/1x1.gif" WIDTH="1" HEIGHT="1" ALT=""></TD></TR></TABLE>
- <TABLE CELLSPACING="0" CELLPADDING="3" WIDTH="100%" BORDER="0" BGCOLOR="#D3D4C5">
- <TR><TD><FONT FACE="Lucida,Verdana,Helvetica,Arial">
- <A NAME="libflac"><B><FONT SIZE="+2">libFLAC</FONT></B>
- </FONT></TD></TR>
- </TABLE>
- <TABLE WIDTH="100%" CELLPADDING="0" CELLSPACING="0" BORDER="0"><TR BGCOLOR="#000000"><TD><IMG SRC="images/1x1.gif" WIDTH="1" HEIGHT="1" ALT=""></TD></TR></TABLE>
- <TABLE CELLSPACING="0" CELLPADDING="3" WIDTH="100%" BORDER="0" BGCOLOR="#EEEED4">
- <TR><TD><FONT FACE="Lucida,Verdana,Helvetica,Arial">
- <P>
- The FLAC library <B><TT>libFLAC</TT></B> is a C implementation of reference encoders and decoders, and a metadata interface. By linking against <B><TT>libFLAC</TT></B> and writing a little code, it is relatively easy to add FLAC support to another program. The library is licensed under the <A HREF="http://www.gnu.org/copyleft/lesser.html">LGPL</A>. Complete source code of <B><TT>libFLAC</TT></B> as well as the command-line encoder and plugins is available and is a useful source of examples.
- </P>
- <P>
- There is also a C++ object wrapper around <B><TT>libFLAC</TT></B> called <B><TT>libFLAC++</TT></B>; see the documentation <A HREF="#libflacpp">below</A>.
- </P>
- <P>
- <B><TT>libFLAC</TT></B> usually only requires the standard C library and C math library. In particular, threading is not used so there is no dependency on a thread library. However, <B><TT>libFLAC</TT></B> does not use global variables and should be thread-safe.
- </P>
- <P>
- The <B><TT>libFLAC</TT></B> interface is described in the public header files in the <TT>include/FLAC/</TT> directory. The public headers and the compiled library are all that is needed to compile and link against the library. Note that none of the code in <TT>src/libFLAC/</TT>, including the private header files in <TT>src/libFLAC/include/</TT> is required.
- </P>
- <P>
- Aside from encoders and decoders, <B><TT>libFLAC</TT></B> provides a powerful metadata interface for manipulating metadata in FLAC files. It allows the user to add, delete, and modify FLAC metadata blocks and it can automatically take advantage of PADDING blocks to avoid rewriting the entire FLAC file when changing the size of the metadata. The documentation for the metadata interface is currently being rewritten but there are extensive usage comments in the header file <TT>include/FLAC/metadata.h</TT>.
- </P>
- <P>
- The basic usage of a <B><TT>libFLAC</TT></B> encoder or decoder is as follows:
- <OL>
- <LI>The program creates an instance of a decoder or encoder using <TT>*_new()</TT>.</LI>
- <LI>The program sets the parameters of the instance and callbacks for reading, writing, error reporting, and metadata reporting using <TT>*_set_*()</TT> functions.</LI>
- <LI>The program initializes the instance to validate the parameters and prepare for decoding/encoding using <TT>*_init()</TT>.</LI>
- <LI>The program calls <TT>*_process_*()</TT> functions to encode or decode data, which subsequently calls the callbacks.</LI>
- <LI>The program finishes the instance with <TT>*_finish()</TT>, which flushes the input and output and resets the encoder/decoder to the unitialized state.</LI>
- <LI>The instance may be used again or deleted with <TT>*_delete()</TT>.</LI>
- </OL>
- </P>
- <P>
- For decoding, <B><TT>libFLAC</TT></B> provides three layers of access. The lowest layer is non-seekable stream-level decoding, the next is seekable stream-level decoding, and the highest layer is file-level decoding. The interfaces are described in <TT>stream_decoder.h</TT>, <TT>seekable_stream_decoder.h</TT>, and <TT>file_decoder.h</TT> respectively. Typically you will choose the highest layer that your input source will support.
- </P>
- <P>
- The stream decoder relies on callbacks for all input and output and has no provisions for seeking. The seekable stream decoder wraps the stream decoder and exposes functions for seeking. However, you must provide extra callbacks for seek-related operations on your stream, like seek and tell. The file decoder wraps the seekable stream decoder and supplies most of the callbacks internally, simplifying the processing of standard files.
- </P>
- <P>
- Currently there is only one level of encoder implementation which is at the stream level (<TT>stream_encoder.h</TT>). There is 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_new()</TT>, then call <TT>FLAC__stream_decoder_set_*()</TT> functions to set the callbacks and client data, and call <TT>FLAC__stream_decoder_init()</TT>. The required callbacks are:
- <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__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 (i.e. write callback). The metadata block that is passed in must not be modified, and it doesn't live beyond the callback, so you should make a copy of it with <TT>FLAC__metadata_object_clone()</TT> if you will need it elsewhere. Since metadata blocks can potentially be large, by default the decoder only calls the metadata callback for the STREAMINFO block; you can instruct the decoder to pass or filter other blocks with <TT>FLAC__stream_decoder_set_metadata_*()</TT> calls.</LI>
- <LI>Error callback - This function will be called whenever an error occurs during decoding.</LI>
- </UL>
- </P>
- <P>
- Once the decoder is initialized, your program will call one of several functions to start the decoding process:
- <UL>
- <LI><B><TT>FLAC__stream_decoder_process_whole_stream()</TT></B> - Tells the decoder to start and continue processing the stream until the read callback says <TT>FLAC__STREAM_DECODER_READ_END_OF_STREAM</TT> or <TT>FLAC__STREAM_DECODER_READ_ABORT</TT>.</LI>
- <LI><B><TT>FLAC__stream_decoder_process_metadata()</TT></B> - Tells the decoder to start processing the stream and stop upon reaching the first audio frame.</LI>
- <LI><B><TT>FLAC__stream_decoder_process_one_frame()</TT></B> - Tells the decoder to process one audio frame and return. The decoder must have processed all metadata first before calling this function.</LI>
- <LI><B><TT>FLAC__stream_decoder_process_remaining_frames()</TT></B> - Tells the decoder to process all remaining frames. The decoder must have processed all metadata first but may also have processed frames with <TT>FLAC__stream_decoder_process_one_frame()</TT>.</LI>
- </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. 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 seekable stream decoder does just this.
- </P>
- <P>
- <B>SEEKABLE STREAM DECODER</B>
- </P>
- <P>
- The seekable stream decoder is a wrapper around the stream decoder which also provides seeking capability. The instance type is <TT>FLAC__SeekableStreamDecoder</TT>. In addition to the Read/Write/Metadata/Error callbacks of the stream decoder, the user must also provide the following:
- <UL>
- <LI>Seek callback - This function will be called when the decoder wants to seek to an absolute position in the stream.</LI>
- <LI>Tell callback - This function will be called when the decoder wants to know the current absolute position of the stream.</LI>
- <LI>Length callback - This function will be called when the decoder wants to know length of the stream. The seeking algorithm currently requires that the overall stream length be known.</LI>
- <LI>EOF callback - This function will be called when the decoder wants to know if it is at the end of the stream. This could be determined from the tell and length callbacks but it may be more expensive that way.</LI>
- </UL>
- </P>
- <P>
- Seeking is exposed through the <TT>FLAC__seekable_stream_decoder_seek_absolute()</TT> method. At any point after the seekable stream decoder has been initialized, the user can call this function to seek to an exact sample within the stream. Subsequently, the first time the write callback is called it will contain a (possibly partial) block starting at that sample.
- </P>
- <P>
- The seekable stream decoder also provides MD5 signature checking. If this is turned on before initialization, <TT>FLAC__seekable_stream_decoder_finish()</TT> will report when the decoded MD5 signature does not match the one stored in the STREAMINFO block. MD5 checking is automatically turned off if there is no signature in the STREAMINFO block or when a seek is attempted.
- </P>
- <P>
- <B>FILE DECODER</B>
- </P>
- <P>
- The file decoder is a trivial wrapper around the seekable stream decoder meant to simplfy the process of decoding from a standard file. The instance type is <TT>FLAC__FileDecoder</TT>. The file decoder supplies all but the Write/Metadata/Error callbacks. The user needs only to provide the path to the file and the file decoder handles the rest.
- </P>
- <P>
- Like the seekable stream decoder, seeking is exposed through the <TT>FLAC__file_decoder_seek_absolute()</TT> method. At any point after the file decoder has been initialized, the user 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>
- The file decoder also inherits MD5 signature checking from the seekable stream decoder. If this is turned on before initialization, <TT>FLAC__file_decoder_finish()</TT> will report when the decoded MD5 signature does not match the one stored in the STREAMINFO block. MD5 checking is automatically turned off if there is no signature in the STREAMINFO block or when a seek is attempted.
- </P>
- <P>
- <B>STREAM ENCODER</B>
- </P>
- <P>
- 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 user will create a new instance by calling <TT>FLAC__stream_encoder_new()</TT>, then set the necessary parameters with <TT>FLAC__stream_encoder_set_*()</TT>, and 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. When the user calls <TT>FLAC__stream_encoder_init()</TT> the encoder will validate the values, so you should make sure to check the returned state to see that it is FLAC__STREAM_ENCODER_OK. When setting these parameters you should have some basic knowledge of the format (see the <A HREF="#format">user-level documentation</A> or the <A HREF="format.html">formal description</A>) but the required parameters are summarized here:
- <UL>
- <LI><B><TT>streamable_subset</TT></B> - true to force the encoder to generate a <A HREF="format.html#subset">Subset stream</A>, else false.</LI>
- <LI><B><TT>do_mid_side_stereo</TT></B> - true to try mid-side encoding on stereo input, else false. <TT>channels</TT> must be 2.</LI>
- <LI><B><TT>loose_mid_side_stereo</TT></B> - true to do adaptive mid-side switching, else false. <TT>do_mid_side_stereo</TT> must be true.</LI>
- <LI><B><TT>channels</TT></B> - must be <= <TT>FLAC__MAX_CHANNELS</TT>.</LI>
- <LI><B><TT>bits_per_sample</TT></B> - do not give the encoder wider data than what you specify here or bad things will happen.</LI>
- <LI><B><TT>sample_rate</TT></B> - must be <= <TT>FLAC__MAX_SAMPLE_RATE</TT>.</LI>
- <LI><B><TT>blocksize</TT></B> - must be between <TT>FLAC__MIN_BLOCKSIZE</TT> and <TT>FLAC__MAX_BLOCKSIZE</TT>.</LI>
- <LI><B><TT>max_lpc_order</TT></B> - 0 implies encoder will not try general LPC, only fixed predictors; must be <= <TT>FLAC__MAX_LPC_ORDER</TT>.</LI>
- <LI><B><TT>qlp_coeff_precision</TT></B> - must be >= <TT>FLAC__MIN_QLP_COEFF_PRECISION</TT>, or 0 to let encoder select based on blocksize. In the current imlementation <TT>qlp_coeff_precision</TT>+<TT>bits_per_sample</TT> must be < 32.</LI>
- <LI><B><TT>do_qlp_coeff_prec_search</TT></B> - false to use <TT>qlp_coeff_precision</TT>; true to search around <TT>qlp_coeff_precision</TT> and take best.</LI>
- <LI><B><TT>do_escape_coding</TT></B> - true => search for escape codes in the entropy coding stage for slightly better compression.</LI>
- <LI><B><TT>do_exhaustive_model_search</TT></B> - false to use estimated bits per residual for scoring; true to generate all and take shortest.</LI>
- <LI><B><TT>min_residual_partition_order</TT></B>, <B><TT>max_residual_partition_order</TT></B> - 0 to estimate Rice parameter based on residual variance; > 0 to partition the residual and use parameter for each based on mean; <TT>min_residual_partition_order</TT> and <TT>max_residual_partition_order</TT> specify the min and max Rice partition order.</LI>
- <LI><B><TT>rice_parameter_search_dist</TT></B> - 0 to try only calculated parameter k; else try all [k-<TT>rice_parameter_search_dist</TT>..k+<TT>rice_parameter_search_dist</TT>] parameters and use the best.</LI>
- <LI><B><TT>total_samples_estimate</TT></B> - May be set to 0 if unknown. Otherwise, set this to the number of samples to be encoded. This will allow the STREAMINFO block to be more accurate during the first pass in the event that the encoder can't seek back to the beginning of the output file to write the updated STREAMINFO block.</LI>
- <LI><B><TT>metadata</TT></B> - Optional array of pointers to metadata blocks to be written; NULL implies no metadata. The STREAMINFO block is always written automatically and must not be present in the array of pointers.</LI>
- </UL>
- </P>
- <P>
- The user provides 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__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 user may feed audio data to the encoder in one of two ways:
- <UL>
- <LI>Channel separate, through <B><TT>FLAC__stream_encoder_process()</TT></B> - The user 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 user 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 user 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 final 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>
- </P>
- <P>
- It should be noted that any time an array of pointers to audio data is passed, the channel order currently only has meaning for stereo streams. Channel 0 corresponds to the left channel and channel 1 corresponds to the right channel.
- </P>
- <P>
- <B>METADATA</B>
- </P>
- <P>
- For programs that write their own metadata, but that do not know the actual metadata until after encoding, 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 metadata is known, the program can write a slightly larger padding block, then split it after encoding.
- </P>
- <P>
- Make sure you understand how lengths are calculated. All FLAC metadata blocks have a 4 byte header which contains the type and length. This length does not include the 4 bytes of the header. See the <A HREF="format.html#metadata_block">format page</A> for the specification of metadata blocks and their lengths.
- </P>
- </FONT>
- </TD></TR>
- </TABLE>
- <TABLE WIDTH="100%" CELLPADDING="0" CELLSPACING="0" BORDER="0"><TR BGCOLOR="#000000"><TD><IMG SRC="images/1x1.gif" WIDTH="1" HEIGHT="1" ALT=""></TD></TR></TABLE>
-</TD></TR>
-</TABLE>
-
-
-<TABLE WIDTH="100%" CELLPADDING="5" CELLSPACING="5" BORDER="0">
-<TR><TD>
- <TABLE WIDTH="100%" CELLPADDING="0" CELLSPACING="0" BORDER="0"><TR BGCOLOR="#000000"><TD><IMG SRC="images/1x1.gif" WIDTH="1" HEIGHT="1" ALT=""></TD></TR></TABLE>
- <TABLE CELLSPACING="0" CELLPADDING="3" WIDTH="100%" BORDER="0" BGCOLOR="#D3D4C5">
- <TR><TD><FONT FACE="Lucida,Verdana,Helvetica,Arial">
- <A NAME="libflacpp"><B><FONT SIZE="+2">libFLAC++</FONT></B>
- </FONT></TD></TR>
- </TABLE>
- <TABLE WIDTH="100%" CELLPADDING="0" CELLSPACING="0" BORDER="0"><TR BGCOLOR="#000000"><TD><IMG SRC="images/1x1.gif" WIDTH="1" HEIGHT="1" ALT=""></TD></TR></TABLE>
- <TABLE CELLSPACING="0" CELLPADDING="3" WIDTH="100%" BORDER="0" BGCOLOR="#EEEED4">
- <TR><TD><FONT FACE="Lucida,Verdana,Helvetica,Arial">
- <P>
- <B><TT>libFLAC++</TT></B> is a C++ object wrapper around <B><TT>libFLAC</TT></B>. It provides classed for the encoders and decoders as well as the metadata interface.
- </P>
- <P>
- The documentation for <B><TT>libFLAC++</TT></B> is currently being rewritten. As a wrapper it is actually quite simple. The method names and semantics generally follow those in the C layer and comments in the header files specify where there are differences.
- </P>
- </FONT>
- </TD></TR>
- </TABLE>
- <TABLE WIDTH="100%" CELLPADDING="0" CELLSPACING="0" BORDER="0"><TR BGCOLOR="#000000"><TD><IMG SRC="images/1x1.gif" WIDTH="1" HEIGHT="1" ALT=""></TD></TR></TABLE>
-</TD></TR>
-</TABLE>
-
-
<TABLE WIDTH="100%" CELLPADDING="5" CELLSPACING="5" BORDER="0">
<TR><TD>
<TABLE WIDTH="100%" CELLPADDING="0" CELLSPACING="0" BORDER="0"><TR BGCOLOR="#000000"><TD><IMG SRC="images/1x1.gif" WIDTH="1" HEIGHT="1" ALT=""></TD></TR></TABLE>