FLAC Logo
  home  |   news  |   download  |   features  |   goals  |   format  |   id  |   comparison  |   documentation  |   developers  
  english  |   russian  
format

This is a detailed description of the FLAC format.

First, as the original developer I have to say that I am not a compression expert and I feel obligated to give credit where it is due. FLAC owes a lot to the many people who have advanced the audio compression field so freely. For instance:

  • A. J. Robinson for his work on Shorten; his code and paper are a good starting point on some of the basic methods used by FLAC. FLAC expands on the fixed predictors used in shorten.
  • S. W. Golomb and Robert F. Rice; their universal codes are used by FLAC's entropy coder.
  • N. Levinson and J. Durbin; the reference encoder uses an algorithm developed and refined by them for determining the LPC coefficients from the autocorrelation coefficients.
  • And of course, the main guy, Claude Shannon

Scope

It is a known fact that no algorithm can losslessly compress all possible input, so most compressors restrict themselves to a useful domain and try to work as well as possible within that domain. FLAC's domain is audio data. Though it can losslessly code any input, only certain kinds of input will get smaller. FLAC exploits the fact that audio data typically has a high degree of sample-to-sample correlation.

Within the audio domain, there are many possible subdomains. For example: low bitrate speech, high-bitrate multi-channel music, etc. FLAC itself does not target a specific subdomain but many of the default parameters of the reference encoder are tuned to CD-quality music data (i.e. 44.1kHz, 2 channel, 16 bits per sample). The effect of the encoding parameters on different kinds of audio data will be examined later.

Architecture

Similar to many audio coders, a FLAC encoder has the following stages:

  • Blocking. The input is broken up into many contiguous blocks. With FLAC, the blocks may vary in size. The optimal size of the block is usually affected by many factors, including the sample rate, spectral characteristics over time, etc. Though FLAC allows the block size to vary within a stream, the reference encoder uses a fixed block size.
  • Interchannel Decorrelation. In the case of stereo streams, the encoder will create mid and side signals based on the average and difference (respectively) of the left and right channels. The encoder will then pass the best form of the signal to the next stage.
  • Prediction. The block is passed through a prediction stage where the encoder tries to find a mathematical description (usually an approximate one) of the signal. This description is typically much smaller than the raw signal itself. Since the methods of prediction are known to both the encoder and decoder, only the parameters of the predictor need be included in the compressed stream. FLAC currently uses four different classes of predictors (described in the prediction section), but the format has reserved space for additional methods. FLAC allows the class of predictor to change from block to block, or even within the channels of a block.
  • Residual coding. If the predictor does not describe the signal exactly, the difference between the original signal and the predicted signal (called the error or residual signal) must be coded losslessy. If the predictor is effective, the residual signal will require fewer bits per sample than the original signal. FLAC currently uses only one method for encoding the residual (see the Residual coding section), but the format has reserved space for additional methods. FLAC allows the residual coding method to change from block to block, or even within the channels of a block.

In addition, FLAC specifies a metadata system, which allows arbitrary information about the stream to be included at the beginning of the stream.

Definitions

Many terms like "block" and "frame" are used to mean different things in differenct encoding schemes. For example, a frame in MP3 corresponds to many samples across several channels, whereas an S/PDIF frame represents just one sample for each channel. The definitions we use for FLAC follow. Note that when we talk about blocks and subblocks we are refering to the raw unencoded audio data that is the input to the encoder, and when we talk about frames and subframes, we are refering to the FLAC-encoded data.

  • Block: One or more audio samples that span several channels.
  • Subblock: One or more audio samples within a channel. So a block contains one subblock for each channel, and all subblocks contain the same number of samples.
  • Blocksize: The number of samples in any of a block's subblocks. For example, a one second block sampled at 44.1KHz has a blocksize of 44100, regardless of the number of channels.
  • Frame: A frame header plus one or more subframes.
  • Subframe: A subframe header plus one or more encoded samples from a given channel. All subframes within a frame will contain the same number of samples.

Blocking

The size used for blocking the audio data has a direct effect on the compression ratio. If the block size is too small, the resulting large number of frames mean that excess bits will be wasted on frame headers. If the block size is too large, the characteristics of the signal may vary so much that the encoder will be unable to find a good predictor. In order to simplify encoder/decoder design, FLAC imposes a minimum block size of 16 samples, and a maximum block size of 65535 samples. This range covers the optimal size for all of the audio data FLAC supports.

Currently the reference encoder uses a fixed block size, optimized on the sample rate of the input. Future versions may vary the block size depending on the characteristics of the signal.

Blocked data is passed to the predictor stage one subblock (channel) at a time. Each subblock is independently coded into a subframe, and the subframes are concatenated into a frame. Because each channel is coded separately, it means that one channel of a stereo frame may be encoded as a constant subframe, and the other an LPC subframe.

Interchannel Decorrelation

In stereo streams, in many cases there is an exploitable amount of correlation between the left and right channels. FLAC allows the frames of stereo streams to have different channel assignments, and an encoder may choose to use the best representation on a frame-by-frame basis.

  • Independent. The left and right channels are coded independently.
  • Mid-side. The left and right channels are transformed into mid and side channels. The mid channel is the midpoint (average) of the left and right signals, and the side is the difference signal (left minus right).
  • Left-side. The left channel and side channel are coded.
  • Right-side. The right channel and side channel are coded

Surprisingly, the left-side and right-side forms can be the most efficient in many frames, even though the raw number of bits per sample needed for the original signal is slightly more than that needed for independent or mid-side coding.

Prediction

FLAC uses four methods for modeling the input signal:

  • Verbatim. This is essentially a zero-order predictor of the signal. The predicted signal is zero, meaning the residual is the signal itself, and the compression is zero. This is the baseline against which the other predictors are measured. If you feed random data to the encoder, the verbatim predictor will probably be used for every subblock. Since the raw signal is not actually passed through the residual coding stage (it is added to the stream 'verbatim'), the encoding results will not be the same as a zero-order linear predictor.
  • Constant. This predictor is used whenever the subblock is pure DC ("digital silence"), i.e. a constant value throughout. The signal is run-length encoded and added to the stream.
  • Fixed linear predictor. FLAC uses a class of computationally-efficient fixed linear predictors (for a good description, see audiopak and shorten). FLAC adds a fourth-order predictor to the zero-to-third-order predictors used by Shorten. Since the predictors are fixed, the predictor order is the only parameter that needs to be stored in the compressed stream. The error signal is then passed to the residual coder.
  • FIR Linear prediction. For more accurate modeling (at a cost of slower encoding), FLAC supports up to 32nd order FIR linear prediction (again, for info on linear prediction, see audiopak and shorten). The reference encoder uses the Levinson-Durbin method for calculating the LPC coefficients from the autocorrelation coefficients, and the coefficients are quantized before computing the residual. Whereas encoders such as Shorten used a fixed quantization for the entire input, FLAC allows the quantized coefficient precision to vary from subframe to subframe. The FLAC reference encoder estimates the optimal precision to use based on the block size and dynamic range of the original signal.

Residual Coding

FLAC currently defines two similar methods for the coding of the error signal from the prediction stage. The error signal is coded using Rice codes in one of two ways: 1) the encoder estimates a single rice parameter based on the variance of the residual and Rice codes the entire residual using this parameter; 2) the residual is partitioned into several equal-length regions of contiguous samples, and each region is coded with its own Rice parameter based on the region's mean. (Note that the first method is a special case of the second method with one partition, except the Rice parameter is based on the residual variance instead of the mean.)

The FLAC format has reserved space for other coding methods. Some possiblities for volunteers would be to explore better context-modeling of the Rice parameter, or Huffman coding. See LOCO-I and pucrunch for descriptions of several universal codes.

Format

This section specifies the FLAC bitstream format. FLAC has no format version information, but it does contain reserved space in several places. Future versions of the format may use this reserved space safely without breaking the format of older streams. Older decoders may choose to abort decoding or skip data encoded with newer methods. Apart from reserved patterns, in places the format specifies invalid patterns, meaning that the patterns may never appear in any valid bitstream, in any prior, present, or future versions of the format. These invalid patterns are usually used to make the synchronization mechanism more robust.

All numbers used in a FLAC bitstream are integers; there are no floating-point representations. All numbers are big-endian coded. All numbers are unsigned unless otherwise specified.

A FLAC bitstream may be appended with ID3V1 data or prepended with ID3V2 data. FLAC has no knowledge of such data, but the reference decoder knows how to skip an ID3 tag.

Before the formal description of the stream, an overview might be helpful.

The following tables constitute a formal description of the FLAC format. Numbers in angle brackets indicate how many bits are used for a given field.

STREAM
<32> "fLaC", the FLAC stream marker in ASCII, meaning byte 0 of the stream is 0x66, followed by 0x4C 0x61 0x43
METADATA_BLOCK This is the mandatory STREAMINFO metadata block that has the basic properties of the stream
METADATA_BLOCK* Zero or more metadata blocks
FRAME+ One or more audio frames

METADATA_BLOCK
METADATA_BLOCK_HEADER A block header that specifies the type and size of the metadata block data.
METADATA_BLOCK_DATA  

METADATA_BLOCK_HEADER
<1> Last-metadata-block flag: '1' if this block is the last metadata block before the audio blocks, '0' otherwise.
<7> BLOCK_TYPE
  • 0 : STREAMINFO
  • 1 : PADDING
  • 2 : APPLICATION
  • 3 : SEEKTABLE
  • 4 : VORBIS_COMMENT
  • 5-127 : reserved
<24> Length (in bytes) of metadata to follow (does not include the size of the METADATA_BLOCK_HEADER)

METADATA_BLOCK_DATA
METADATA_BLOCK_STREAMINFO
|| METADATA_BLOCK_PADDING
|| METADATA_BLOCK_APPLICATION
|| METADATA_BLOCK_SEEKTABLE
|| METADATA_BLOCK_VORBIS_COMMENT
The block data must match the block type in the block header.

METADATA_BLOCK_STREAMINFO
<16> The minimum block size (in samples) used in the stream.
<16> The maximum block size (in samples) used in the stream. (Minimum blocksize == maximum blocksize) implies a fixed-blocksize stream.
<24> The minimum frame size (in bytes) used in the stream. May be 0 to imply the value is not known.
<24> The maximum frame size (in bytes) used in the stream. May be 0 to imply the value is not known.
<20> Sample rate in Hz. Though 20 bits are available, the maximum sample rate is limited by the structure of frame headers to 1048570Hz. Also, a value of 0 is invalid.
<3> (number of channels)-1. FLAC supports from 1 to 8 channels
<5> (bits per sample)-1. FLAC supports from 4 to 32 bits per sample. Currently the reference encoder and decoders only support up to 24 bits per sample.
<36> Total samples in stream. 'Samples' means inter-channel sample, i.e. one second of 44.1Khz audio will have 44100 samples regardless of the number of channels. A value of zero here means the number of total samples is unknown.
<128> MD5 signature of the unencoded audio data. This allows the decoder to determine if an error exists in the audio data even when the error does not result in an invalid bitstream.
NOTES
  • FLAC specifies a minimum block size of 16 and a maximum block size of 65535, meaning the bit patterns corresponding to the numbers 0-15 in the minimum blocksize and maximum blocksize fields are invalid.

METADATA_BLOCK_PADDING
<n> n '0' bits (n must be a multiple of 8)

METADATA_BLOCK_APPLICATION
<32> Registered application ID. (Visit the registration page to register an ID with FLAC.)
<n> Application data (n must be a multiple of 8)

METADATA_BLOCK_SEEKTABLE
SEEKPOINT+ One or more seek points.
NOTE
  • The number of seek points is implied by the metadata header 'length' field, i.e. equal to length / 18.

SEEKPOINT
<64> Sample number of first sample in the target frame, or 0xFFFFFFFFFFFFFFFF for a placeholder point.
<64> Offset (in bytes) from the first byte of the first frame header to the first byte of the target frame's header.
<16> Number of samples in the target frame.
NOTES
  • For placeholder points, the second and third field values are undefined.
  • Seek points within a table must be sorted in ascending order by sample number.
  • Seek points within a table must be unique by sample number, with the exception of placeholder points.
  • The previous two notes imply that there may be any number of placeholder points, but they must all occur at the end of the table.

METADATA_BLOCK_VORBIS_COMMENT
<n> The contents of a vorbis comment packet as specified here, including the vendor string. Note that the vorbis comment spec allows for on the order of 2 ^ 64 bytes of data where as the FLAC metadata block is limited to 2 ^ 24 bytes. Given the stated purpose of vorbis comments, i.e. human-readable textual information, this limit is unlikely to be restrictive. Also note that the 32-bit field lengths are little-endian coded according to the vorbis spec, as opposed to the usual big-endian coding of fixed-length integers in the rest of FLAC.

FRAME
FRAME_HEADER  
SUBFRAME+ One SUBFRAME per channel.
<?> Zero-padding to byte alignment.
FRAME_FOOTER  

FRAME_HEADER
<14> Sync code '11111111111110'
<2> Reserved:
  • 00 : mandatory value
  • 01-11 : reserved for future use
<4> Block size in inter-channel samples:
  • 0000 : get from STREAMINFO metadata block
  • 0001 : 192 samples
  • 0010-0101 : 576 * (2^(n-2)) samples, i.e. 576/1152/2304/4608
  • 0110 : get 8 bit (blocksize-1) from end of header
  • 0111 : get 16 bit (blocksize-1) from end of header
  • 1000-1111 : 256 * (2^(n-8)) samples, i.e. 256/512/1024/2048/4096/8192/16384/32768
<4> Sample rate:
  • 0000 : get from STREAMINFO metadata block
  • 0001-0011 : reserved
  • 0100 : 8kHz
  • 0101 : 16kHz
  • 0110 : 22.05kHz
  • 0111 : 24kHz
  • 1000 : 32kHz
  • 1001 : 44.1kHz
  • 1010 : 48kHz
  • 1011 : 96kHz
  • 1100 : get 8 bit sample rate (in kHz) from end of header
  • 1101 : get 16 bit sample rate (in Hz) from end of header
  • 1110 : get 16 bit sample rate (in tens of Hz) from end of header
  • 1111 : invalid, to prevent sync-fooling string of 1s
<4> Channel assignment
  • 0000-0111 : (number of independent channels)-1. when == 0001, channel 0 is the left channel and channel 1 is the right
  • 1000 : left/side stereo: channel 0 is the left channel, channel 1 is the side(difference) channel
  • 1001 : right/side stereo: channel 0 is the side(difference) channel, channel 1 is the right channel
  • 1010 : mid/side stereo: channel 0 is the mid(average) channel, channel 1 is the side(difference) channel
  • 1011-1111 : reserved
<3> Sample size in bits:
  • 000 : get from STREAMINFO metadata block
  • 001 : 8 bits per sample
  • 010 : 12 bits per sample
  • 011 : reserved
  • 100 : 16 bits per sample
  • 101 : 20 bits per sample
  • 110 : 24 bits per sample
  • 111 : reserved
<1> Zero bit padding, to prevent sync-fooling string of 1s
<?> if(variable blocksize)
   <8-56>:"UTF-8" coded sample number (decoded number is 36 bits)
else
   <8-48>:"UTF-8" coded frame number (decoded number is 31 bits)
<?> if(blocksize bits == 011x)
   8/16 bit (blocksize-1)
<?> if(sample rate bits == 11xx)
   8/16 bit sample rate
<8> CRC-8 (polynomial = x^8 + x^2 + x^1 + x^0, initialized with 0) of everything before the crc, including the sync code
NOTES
  • The blocksize bits 0000-0101 and 1000-1111 may only be used if the blocksize is fixed throughout the entire stream. Blocksize bits 0110-0111 may be used in any case but the decoder will have to pessimistically guess that it is a variable-blocksize stream. There is only one special case: the encoder may use blocksize bits 0110-0111 on the last frame of a fixed-blocksize stream, as long as the blocksize is not greater than the stream blocksize.
  • The "UTF-8" coding used for the sample/frame number is the same variable length code used to store compressed UCS-2, extended to handle larger input.

FRAME_FOOTER
<16> CRC-16 (polynomial = x^16 + x^15 + x^2 + x^0, initialized with 0) of everything before the crc, back to and including the frame header sync code

SUBFRAME
SUBFRAME_HEADER  
SUBFRAME_CONSTANT
|| SUBFRAME_FIXED
|| SUBFRAME_LPC
|| SUBFRAME_VERBATIM
The SUBFRAME_HEADER specifies which one.

SUBFRAME_HEADER
<1> Zero bit padding, to prevent sync-fooling string of 1s
<6> Subframe type:
<1+k> 'Wasted bits-per-sample' flag:
  • 0 : no wasted bits-per-sample in source subblock, k=0
  • 1 : k wasted bits-per-sample in source subblock, k-1 follows, unary coded; i.e. k=3 => 001 follows, k=7 => 0000001 follows.

SUBFRAME_CONSTANT
<n> Unencoded constant value of the subblock, n = frame's bits-per-sample.

SUBFRAME_FIXED
<n> Unencoded warm-up samples (n = frame's bits-per-sample * predictor order).
RESIDUAL Encoded residual

SUBFRAME_LPC
<n> Unencoded warm-up samples (n = frame's bits-per-sample * lpc order).
<4> (Quantized linear predictor coefficients' precision in bits)-1 (1111 = invalid).
<5> Quantized linear predictor coefficient shift needed in bits (NOTE: this number is signed two's-complement).
<n> Unencoded predictor coefficients (n = qlp coeff precision * lpc order) (NOTE: the coefficients are signed two's-complement).
RESIDUAL Encoded residual

SUBFRAME_VERBATIM
<n*i> Unencoded subblock; n = frame's bits-per-sample, i = frame's blocksize.

RESIDUAL
<2> Residual coding method:
  • 00 : partitioned rice coding
  • 01-11 : reserved
RESIDUAL_CODING_METHOD_PARTITIONED_RICE  
NOTES
  • Currently, FLAC specifies only one entropy coding method.

RESIDUAL_CODING_METHOD_PARTITIONED_RICE
<4> Partition order.
RICE_PARTITION+ There will be 2^order partitions.

RICE_PARTITION
<4(+5)> Encoding parameter:
  • 0000-1110 : Rice parameter.
  • 1111 : Escape code, meaning the partition is in unencoded binary form using n bits per sample; n follows as a 5-bit number.
<?> Encoded residual. The number of samples (n) in the partition is determined as follows:
  • if the partition order is zero, n = frame's blocksize
  • else if this is not the first partition of the subframe, n = (frame's blocksize / (2^partition order))
  • else n = (frame's blocksize / (2^partition order)) - predictor order

 Copyright (c) 2000,2001,2002 Josh Coalson