Free Lossless Audio Codec (FLAC)
Extracted elements (30)
FLAC has no format version field; instead, reserved space is included in multiple locations. Future format versions MAY use reserved space safely. Forbidden patterns are listed explicitly so decoders can detect invalid data. This approach was chosen to allow forward-compatible extension without breaking older decoders.
Frame headers use lookup tables for the most common sample rates, block sizes, channel counts, and bit depths to keep overhead small. When a value is not in the lookup table, the frame header redirects the decoder to an appended field or the streaminfo block. Relying on streaminfo makes a stream non-streamable.
Mid-side stereo decorrelation exploits inter-channel correlation in stereo files. The side channel always requires one extra bit of depth due to the subtraction, but the mid channel does not because the right-shift is lossless: any odd mid sample is always accompanied by an odd side sample, allowing the lost LSB to be recovered.
Subframes (one per channel) are encoded serially within a frame, not channel-interleaved. This reduces decoder complexity at the cost of larger decode buffers, and simplifies predictor and residual coding per channel.
Appendix C enumerates known interoperability hazards: features outside the streamable subset, variable block size handling, 5-bit Rice parameters, Rice escape codes, uncommon block sizes and bit depths, multi-channel audio, uncommon sample rates, and changing audio properties mid-stream. Older decoders may not support these features.
FLAC can be mapped into Ogg, Matroska, and ISO Base Media File Format (MP4) containers. Each container mapping is defined separately in Section 10. The streamable subset is not required for containerized FLAC since containers handle seeking and framing.
Seek table byte offsets are relative to an unmuxed FLAC stream and MUST NOT be updated when muxing into a container. The seek table is generally unusable for seeking in containerized FLAC because containers interleave other data; containers typically provide their own seeking mechanisms.
All samples encoded to and decoded from the FLAC format MUST be in a signed representation. Unsigned audio must be converted (typically by subtracting half the unsigned range) before encoding.
Certain bit patterns are forbidden in any FLAC bitstream: metadata block type 127, streaminfo min/max block sizes below 16, sample rate bits 0b1111, uncommon block size value 65536, LPC coefficient precision bits 0b1111, and negative LPC right shift values.
The blocking strategy bit in frame headers MUST NOT change during the audio stream. Each frame MUST start on a byte boundary and begin with the 15-bit sync code 0b111111111111100.
The streaminfo metadata block MUST be the first metadata block in every FLAC stream. There MUST be no more than one streaminfo metadata block per stream.
To qualify as a FLAC streamable subset stream: sample rate and bit depth MUST be self-contained in each frame header (not referencing streaminfo); block size MUST NOT exceed 16384 samples (4608 for audio ≤48 kHz); LPC predictor order MUST NOT exceed 12 for audio ≤48 kHz; Rice partition order MUST NOT exceed 8; and channel ordering MUST use only the predefined layouts in Section 9.1.3.
A FLAC bitstream begins with the 4-byte fLaC marker (0x664C6143), followed by a mandatory streaminfo metadata block, optional additional metadata blocks, and then audio frames. FLAC supports 127 metadata block types; currently 7 are defined.
Each audio frame starts with the 15-bit sync code 0b111111111111100, followed by a blocking strategy bit (0=fixed, 1=variable block size), 4 block-size bits, 4 sample-rate bits, 4 channel bits, 3 bit-depth bits, 1 reserved bit, a coded number, optional uncommon block size, optional uncommon sample rate, and an 8-bit CRC.
FLAC defines four subframe types: Constant (single value for DC/silence), Verbatim (raw samples, no predictor), Fixed predictor (one of 5 predefined LPC predictors, no stored coefficients), and Linear predictor (general LPC with stored coefficients, precision bits, and a right-shift value). Each subframe has its own header identifying the type and predictor order.
The coded residual is stored as Rice-coded partitions. Signed residual samples are zigzag-encoded to unsigned folded values before Rice coding. The partition order (exponent of 2 giving the partition count) MUST produce partitions whose sample count is a whole number. Rice parameters are encoded using either 4-bit or 5-bit parameter fields.
The linear predictor subframe stores: predictor order, coefficient precision (4 bits; 0b1111 is forbidden), prediction right shift (5 bits; negative values are forbidden), s(precision) coefficients, and Rice-coded residual. A prediction is computed as sum(coeff[i] * sample[i]) >> right_shift.
The picture metadata block embeds image data or a URI, derived from ID3v2 APIC frames. It contains u(32) picture type, u(32)+data media type, u(32)+data description (UTF-8), u(32) width, u(32) height, u(32) color depth, u(32) color count, u(32)+data picture data. Width, height, color depth, and color count are informational only; applications MUST NOT use them for decoding.
The seek table metadata block stores zero or more 18-byte seek points. Each seek point contains a u(64) first-sample number (0xFFFFFFFFFFFFFFFF for placeholder), u(64) byte offset from the first frame header, and u(16) sample count for the target frame. Seek points MUST be sorted ascending by sample number and MUST be unique by sample number (placeholders excepted).
The Vorbis comment metadata block stores human-readable UTF-8 metadata as a vendor string followed by name=value fields. Field lengths and count use little-endian 32-bit integers, in contrast to FLAC's usual big-endian convention. Field names MUST consist only of printable ASCII (U+0020–U+007E excluding '=') and MUST be evaluated case-insensitively.
The WAVEFORMATEXTENSIBLE_CHANNEL_MASK Vorbis comment field overrides default channel assignments using 18-bit flag positions (front left through top rear right). A value of 0x0 indicates channels do not correlate with speaker positions. Files using this field are not part of the streamable subset.
This document creates the IANA 'FLAC Application Metadata Block IDs' registry for 32-bit application identifiers used in the Application metadata block (Section 8.4). Registration policy is First Come First Served.
This document registers the media type audio/flac with IANA for standalone FLAC files. Parameters include the rate (sample rate) and channels parameters.
A decoder that blindly accepts maximum frame size, maximum block size, channel count, bit depth, and total sample count from the streaminfo metadata block without performing bounds checks during audio frame decoding could be vulnerable to buffer overflows. Decoders SHOULD validate these fields against actual frame data.
When a picture metadata block contains a URI, applications MUST obtain explicit user approval before retrieving images via remote protocols or local images not in the same directory as the FLAC file. Applications MAY reject URI processing for security or privacy reasons.
Block size bits (4 bits) encode common sizes via lookup table: 0b0001=192, 0b0010–0b0101=144*2^v, 0b1000–0b1111=2^v. Values 0b0110 and 0b0111 signal an uncommon block size stored as 8-bit or 16-bit after the coded number (value is block_size-1).
Channel bits (4 bits) encode both channel count (0b0000–0b0111 for 1–8 independent channels with defined speaker assignments) and stereo decorrelation mode: 0b1000=left-side, 0b1001=side-right, 0b1010=mid-side. Values 0b1011–0b1111 are reserved.
Each metadata block header is 4 bytes: 1 bit last-block flag, 7 bits block type (0–126; 127 is forbidden), and 24 bits of block body size in bytes coded big-endian.
The coded number (frame or sample number) is encoded using a UTF-8-like variable-length scheme extended to a maximum of 36 bits unencoded / 7 bytes encoded (per Table 18). Frame numbers are limited to 31 bits / 6 bytes encoded. Standard UTF-8 encoders and decoders cannot handle these extended codes.
The streaminfo metadata block encodes: u(16) min block size, u(16) max block size, u(24) min frame size, u(24) max frame size, u(20) sample rate, u(3) channels-1, u(5) bits_per_sample-1, u(36) total interchannel samples, u(128) MD5 checksum of raw PCM. Min and max block sizes MUST be in the range 16–65535.