From 1276f3ee963fd15c45b7412410fc716498e1baa6 Mon Sep 17 00:00:00 2001 From: Josh Coalson Date: Mon, 11 Jun 2001 23:45:27 +0000 Subject: [PATCH] add section for libFLAC --- doc/documentation.html | 160 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 154 insertions(+), 6 deletions(-) diff --git a/doc/documentation.html b/doc/documentation.html index 8515182..5b480e6 100644 --- a/doc/documentation.html +++ b/doc/documentation.html @@ -62,7 +62,15 @@ +

- This page describes the user-level view of the FLAC format (for a more detailed explanation see the format page). It also contains the user documentation for flac, which is the command-line file encoder/decoder, metaflac, the FLAC metadata editor, and the input plugins. + This page is broken up into the following sections: +

    +
  • format - the user-level view of the FLAC format (for a more detailed explanation see the format page).
  • +
  • flac - the usage of the command-line file encoder/decoder flac.
  • +
  • metaflac - the usage of the command-line FLAC metadata editor metaflac.
  • +
  • plugins - documentation for the various input plugins.
  • +
  • libFLAC API - for developers who want to add FLAC support to their programs.
  • +
  • bugs - known bugs.
  • +

Keep in mind that the online version of this document will always apply to the latest release. For older releases, check the documentation included with the release package. @@ -80,7 +88,7 @@
- format + format
@@ -652,7 +660,7 @@
- metaflac + metaflac
@@ -685,7 +693,7 @@
- xmms plugin + xmms plugin
@@ -707,7 +715,7 @@
- winamp plugin + winamp plugin
@@ -729,7 +737,147 @@
+
- known bugs + libFLAC +
+
+ + +
+

+ 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: +

    +
  1. The program creates an instance of a decoder or encoder.
  2. +
  3. The program initialized the instance and provides libFLAC with callbacks for reading, writing, error reporting, and metadata reporting.
  4. +
  5. The program calls libFLAC to encode or decode data, which subsequently calls the callbacks.
  6. +
  7. The program finalizes the instance, which flushes the input and output.
  8. +
+

+

+ For decoding, libFLAC 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 stream_decoder.h and file_decoder.h 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 (encoder.h). 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_get_new_instance(), then call FLAC__stream_decoder_init() with the addresses of the required callbacks. The program can also supply a client_data pointer to FLAC__stream_decoder_init() which will be included when calling the callbacks. +

    +
  • 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[].
  • +
  • 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 is encountered.
  • +
+

+

+ 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. +

+

+ 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 file decoder does just this. +

+

+ FILE DECODER +

+

+ The file decoder is a wrapper around the stream decoder meant to simplfy the process of decoding from a file. The instance type is FLAC__FileDecoder. 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. +

+

+ Since the file decoder manages the input automatically, it also can provide seeking. This is exposed through the FLAC__file_decoder_seek_absolute() 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. +

+

+ ENCODER +

+

+ The encoder functions similarly to the stream decoder. Currently there is no file encoder but some of the code from flac may be incorporated to do this. The instance type is FLAC__Encoder. Typically the program will create a new instance by calling FLAC__encoder_get_new_instance(). Once the instance is created, but before initialization with FLAC__encoder_init(), the program should set the required encoding parameters directly. +

+

+ 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 FLAC__encoder_init(), they are written directly to encoder instance public variables by the program. When the program calls FLAC__encoder_init() the encoder will validate the values. 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_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 - Size of PADDING block to add (goes after seek table); 0 implies do not add a PADDING block.
  • +
+ Note that these parameters must be set before FLAC__encoder_init() and must not be changed anytime thereafter. +

+

+ After setting the parameters the program should call FLAC__encoder_init() to validate them and register 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__encoder_init() currently will also immediately call the write callback with the "fLaC" signature and all the encoded metadata. +

+

+ After initializing the instance, the program may feed audio data to the encoder in one of two ways: +

    +
  • Channel separate, through FLAC__encoder_process() - 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.
  • +
  • Channel interleaved, through FLAC__encoder_process_interleaved() - The program 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 program is finished encoding data, it calls FLAC__encoder_finish(), which causes the encoder to encode any data still in its input pipe, and call the metadata callback with the correct encoding statistics. +

+

+ 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__Encoder 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. +

+
+
+
+

+ + + +
+
+ +
+ known bugs
-- 2.7.4