The FLAC library libFLAC is a C implementation of reference encoders and decoders. By linking against libFLAC and writing a little code, it is relatively easy to add FLAC support to another program. The library is licensed under the LGPL. Complete source code of libFLAC as well as the command-line encoder and plugins is available and is a useful source of examples.
libFLAC 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, libFLAC does not use global variables and should be thread-safe.
The libFLAC interface is described in the public header files in the include/FLAC 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 src/libFLAC/, including the private header files in src/libFLAC/include/ is required.
The basic usage of libFLAC is as follows:
- The program creates an instance of a decoder or encoder using *_new().
- The program sets the parameters of the instance and callbacks for reading, writing, error reporting, and metadata reporting using *_set_*() functions.
- The program initializes the instance to validate the parameters and prepare for decoding/encoding using *_init().
- The program calls *_process_*() functions to encode or decode data, which subsequently calls the callbacks.
- The program finishes the instance with *_finish(), which flushes the input and output.
- The instance may be used again or deleted with *_delete().
For decoding, libFLAC 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 stream_decoder.h, seekable_stream_decoder.h, and file_decoder.h respectively. Typically you will choose the highest layer that your input source will support.
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.
Currently there is only one level of encoder implementation which is at the stream level (stream_encoder.h). There is currently no file encoder because seeking within a file while encoding seemed like too obscure a feature.
Structures and constants related to the format are defined in format.h.
STREAM DECODER
First we discuss the stream decoder. The instance type is FLAC__StreamDecoder. Typically the program will create a new instance by calling FLAC__stream_decoder_new(), then call FLAC__stream_decoder_set_*() functions to set the callbacks and client data, and call FLAC__stream_decoder_init(). The required callbacks are:
- 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.
- 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.
- 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.
- Error callback - This function will be called whenever an error occurs during decoding.
Once the decoder is initialized, the program will call one of several functions to stimulate the decoding process:
- FLAC__stream_decoder_process_whole_stream() - Tells the decoder to start and continue processing the stream until the read callback says FLAC__STREAM_DECODER_READ_END_OF_STREAM or FLAC__STREAM_DECODER_READ_ABORT.
- FLAC__stream_decoder_process_metadata() - Tells the decoder to start processing the stream and stop upon reaching the first audio frame.
- FLAC__stream_decoder_process_one_frame() - Tells the decoder to process one audio frame and return. The decoder must have processed all metadata first before calling this function.
- FLAC__stream_decoder_process_remaining_frames() - Tells the decoder to process all remaining frames. The decoder must have processed all metadata first but may also have processed frames with FLAC__stream_decoder_process_one_frame().
When the decoder has finished decoding (normally or through an abort), the instance is finished by calling FLAC__stream_decoder_finish(), which ensures the decoder is in the correct state and frees memory. Then the instance may be deleted with FLAC__stream_decoder_delete() or initialized again to decode another stream.
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 FLAC__stream_decoder_flush() and start feeding data from the new position through the read callback. The seekable stream decoder does just this.
SEEKABLE STREAM DECODER
The seekable stream decoder is a wrapper around the stream decoder which also provides seeking capability. The instance type is FLAC__SeekableStreamDecoder. In addition to the Read/Write/Metadata/Error callbacks of the stream decoder, the user must also provide the following:
- Seek callback - This function will be called when the decoder wants to seek to an absolute position in the stream.
- Tell callback - This function will be called when the decoder wants to know the current absolute position of the stream.
- 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.
- 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.
Seeking is exposed through the FLAC__seekable_stream_decoder_seek_absolute() 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.
The seekable stream decoder also provides MD5 signature checking. If this is turned on before initialization, FLAC__seekable_stream_decoder_finish() 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.
FILE DECODER
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 FLAC__FileDecoder. 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.
Like the seekable stream decoder, seeking is exposed through the FLAC__file_decoder_seek_absolute() 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.
The file decoder also inherits MD5 signature checking from the seekable stream decoder. If this is turned on before initialization, FLAC__file_decoder_finish() 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.
STREAM ENCODER
The stream encoder functions similarly to the stream decoder, but has fewer callbacks and more options. The instance type is FLAC__StreamEncoder. Typically the user will create a new instance by calling FLAC__stream_encoder_new(), then set the necessary parameters with FLAC__stream_encoder_set_*(), and initialize it by calling FLAC__stream_encoder_init().
Unlike the decoding process, FLAC encoding has many options that can affect the speed and compression ratio. When the user calls FLAC__stream_encoder_init() 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 user-level documentation or the formal description) but the required parameters are summarized here:
- streamable_subset - true to force the encoder to generate a Subset stream, else false.
- do_mid_side_stereo - true to try mid-side encoding on stereo input, else false. channels must be 2.
- loose_mid_side_stereo - true to do adaptive mid-side switching, else false. do_mid_side_stereo must be true.
- channels - must be <= FLAC__MAX_CHANNELS.
- bits_per_sample - do not give the encoder wider data than what you specify here or bad things will happen.
- sample_rate - must be <= FLAC__MAX_SAMPLE_RATE.
- blocksize - must be between FLAC__MIN_BLOCKSIZE and FLAC__MAX_BLOCKSIZE.
- max_lpc_order - 0 implies encoder will not try general LPC, only fixed predictors; must be <= FLAC__MAX_LPC_ORDER.
- qlp_coeff_precision - must be >= FLAC__MIN_QLP_COEFF_PRECISION, or 0 to let encoder select based on blocksize. In the current imlementation qlp_coeff_precision+bits_per_sample must be < 32.
- do_qlp_coeff_prec_search - false to use qlp_coeff_precision; true to search around qlp_coeff_precision and take best.
- do_escape_coding - true => search for escape codes in the entropy coding stage for slightly better compression.
- do_exhaustive_model_search - false to use estimated bits per residual for scoring; true to generate all and take shortest.
- min_residual_partition_order, max_residual_partition_order - 0 to estimate Rice parameter based on residual variance; > 0 to partition the residual and use parameter for each based on mean; min_residual_partition_order and max_residual_partition_order specify the min and max Rice partition order.
- rice_parameter_search_dist - 0 to try only calculated parameter k; else try all [k-rice_parameter_search_dist..k+rice_parameter_search_dist] parameters and use the best.
- total_samples_estimate - 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.
- seek_table - Optional seek table to prepend; NULL implies no seek table.
- padding - Length of PADDING block to add (goes after seek table); -1 implies do not add a PADDING block. Remember that this is length of the padding; the length of the overall PADDING block will be 4 bytes larger because of the metadata block header.
- last_metadata_is_last - The value the encoder will use for the 'is_last' flag of the last metadata block it writes. In normal usage you would set this to true, but if you will be manually inserting more metadata blocks between the time of the first write callback (when the encoder sends the fLaC header and metadata) and the time actual audio encoding starts then set this to false.
The user provides addresses for the following callbacks:
- 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.
- 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).
The call to FLAC__stream_encoder_init() currently will also immediately call the write callback with the "fLaC" signature and all the encoded metadata.
After initializing the instance, the user may feed audio data to the encoder in one of two ways:
- Channel separate, through FLAC__stream_encoder_process() - 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.
- Channel interleaved, through FLAC__stream_encoder_process_interleaved() - The user will pass a single pointer to data that is channel-interleaved (i.e. channel0_sample0, channel1_sample0, ... , channelN_sample0, channel0_sample1, ...). 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.
When the user is finished encoding data, it calls FLAC__stream_encoder_finish(), 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 FLAC__stream_encoder_delete() or initialized again to encode another stream.
MISCELLANEOUS
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.
METADATA
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.
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.
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.