Introduction
FLAC is an open, royalty-free format for lossless audio compression. A FLAC file
usually takes around half the space of the same audio as WAV, and decodes back to
exactly the original samples. It is maintained by the Xiph.Org Foundation and is
specified by the IETF in RFC 9639. Its media type is audio/flac
(formerly audio/x-flac), and Apple’s type identifier is
org.xiph.flac.
FLAC is widely used for music downloads, CD rips and archives, where sound quality must be kept and file size still matters. Tags such as artist and title, and cover art, are stored inside the file.
Structure
A FLAC file starts with the four bytes fLaC. Metadata blocks follow,
each with a one-byte header (a flag marking the last block, then a 7-bit block type)
and a 24-bit big-endian length. The first block is always STREAMINFO,
which gives the sample rate, channel count, bits per sample, total number of samples
and an MD5 checksum of the decoded audio. Other blocks hold padding, a seek table,
Vorbis comment tags, a cue sheet and pictures.
The audio follows as a series of frames. Each frame starts with a 15-bit sync code and a header protected by a CRC-8, holds one subframe per channel and ends with a CRC-16 checksum. A subframe is either constant, stored verbatim, or predicted from earlier samples, with the residuals Rice-coded in partitions. The pages in the menu describe every block type, field and code.
0fLaC stream marker4 bytes4STREAMINFO block header and stream properties38 bytes42Metadata blocks seek table, tags, pictures, paddingvaries…Frames header, one subframe per channel, CRC-16repeated
Scope
This record is the FLAC stream of RFC 9639: the marker, every metadata block, every frame field, subframe type and residual coding, both CRCs and the MD5, with the extensions that live inside a FLAC file (foreign metadata, LAES and LAE Lossy payloads in APPLICATION blocks). Three neighbouring things are not part of the FLAC stream and are described on their own pages:
- FLAC in MP4 (the
fLaCsample entry and itsdfLabox) and in Ogg and Matroska belong to those containers; their viewers read the stream with the same decoder (MP4 page, Ogg mapping). - CD-Extra and
.laxfiles are a disc and file carrier that, by the specification’s own rule, must never be put inside a FLAC file; the CD-Extra page describes the disc layout, the extension file and its trailer record. The specification’s production rules, reader requirements and test cases govern programs, not bytes. No viewer here opens.lax; the FLAC viewer reads the FLAC form only.
What the viewer does with a long file. Opening a plain FLAC file reads every metadata block
(the STREAMINFO block, the seek table, the tags, the pictures) and the first frames, up to 1,024 frames or 4 MiB, whichever
comes first, and replies: the open time and its memory do not depend on the number of frames (measured, 19 November 2026,
with the file read in windows: 25 to 50 milliseconds from an hour of silence with 155,040 frames to a file of 5,000,000
frames of 16 samples, 78 MB). The rest of the file is read after the open by one streaming pass over the frames, a bounded
step at a time (at most 2,048 frames or about 60 milliseconds a step), that finds every frame and checks its header CRC-8 and
frame CRC-16, the rules that need the whole stream (the minimum block size, the seek points, the total number of samples
against STREAMINFO’s), decodes every frame once (every sample against the range of its bit depth, and the MD5 of the decoded
samples), and adds each problem to the Problems list marked “found after opening”. The pass keeps for each frame its offset, its number of
samples and three flags in typed arrays (13 bytes a frame, so the 5,000,000 frames take about 65 MB), not a tree entry; the
Frames node of the Structure tab and the Frames table are virtual: a frame’s node is made, and a table row worked out, only when
asked for, by reading again from the nearest checkpoint (one every 64 frames), so the path of a problem to its frame resolves
at any time after the frame was found. While the pass runs, the Frames node and the Frames table hold the frames found so
far (the node says “N frames found so far”, the Checksums section “Frames found: N” and, when the pass ends, the final count
and “Frames checked: N of N”); a byte of a frame that is not found yet belongs, in the tree, to the Frames node only. The
pass takes time in proportion to the number of frames and the size of the file (about 30 microseconds a frame of silence and up
to 150 of noise, or 1.3 microseconds a frame of 16 samples: an hour of silence 4.4 seconds, ten minutes of noise 4.7
seconds, four hours of silence 18 seconds, ten hours 47 seconds, the 5,000,000 small frames 6.7 seconds, in a Node process at
about 280 MB resident for the largest). The viewer starts the pass by itself after the first draw (the model offers
autostart: ['checks']), whether or not a section that shows it is on screen, and stops it when the viewer is
closed. Asking for the waveform needs every frame, so it completes the pass first, all at once, and then decodes (its byte map
is 24 bytes a frame, built then). A file that carries LAE Lossy or LAES payload blocks is still read whole at open, because those readers check
every frame against the payload. Every problem the pass finds is listed, with its true total in the Problems heading and in
pages under the list: a wrong frame CRC, a decoded sample outside the bit depth, a frame below the minimum block size, a seek
point that names no frame, a wrong sample total, a wrong MD5. The pass keeps each problem as nine bytes (its kind, the frame
and one small number) and makes the text again from the file when a page is asked for, so a million damaged frames cost
about 9 MB of list. The explorer’s limit of 256 MiB
(MAX_DECODED, the most bytes a decoded container entry may come to) does not apply to a FLAC file or to
its LAE Lossy payload: a FLAC file is read in place by range, and the LAE Lossy payload opened or saved as a file is handed
over as the ranges of the slices it is cut into, joined by the browser and never in one block in memory. It does apply to an
LAES payload opened as a file, which is joined in memory.
Not described
Known, not yet covered
- Reference: the ReplayGain 2.0 specification text itself (the Hydrogenaudio wiki) could not be read; the site refused every request (HTTP 403). The tag names and value formats on the Vorbis comment page are those that the FLAC reference code, vorbisgain, loudgain and FFmpeg agree on, and the R128 tags are from RFC 7845; what the specification says about a set that is incomplete (a gain without a peak) is not known, so the viewer shows such a set and does not report it.
- Viewer: a FLAC file that carries LAE Lossy or LAES payload blocks is still read frame by frame at open (those readers check every frame), so for such a file the open time follows the number of frames; a plain FLAC file is not (see “What the viewer does with a long file”). The problem count at the moment of opening of a plain file with more than 1,024 frames is not the final count: the problems of the later frames, a wrong sample range and a wrong MD5 arrive after the open and are added with “found after opening”.
- Viewer: until the pass over the frames has reached them, the later frames are not in the Frames node or the Frames table, and a byte picked in the hex view inside them selects the Frames node only; the waveform of a very long file builds its byte map from every frame at once (24 bytes a frame, about 120 MB for 5,000,000 frames, sent to the page).
- Viewer: the ID3v2, ID3v1 and APEv2 tags before and after the stream are read by the MP3 plugin’s readers, with the limits of those readers.
- Reference: on the window, twiddle and encoder pages the side menu marks the parent entry “LAE Lossy payload” as the current page (aria-current), although that link leads to another page; the menu has no current marker for a child of a flat entry (a sub-menu master, as the KTX pages use, would give one).
The LAE1 (LAE Lossy) payload is described and decoded in full
(LAE Lossy payload page: the WAV form and the FLAC form, container, validation, range coder, partition syntax,
dequantisation, inverse MDCT with all 2,304 table values on the window and
twiddle pages, half-band filters with their tap values, mix, output file, error table, and on
its own page the encoder-side elements the specification lists as informative; the decoder reproduces all 56 conformance outputs of the
specification bit for bit, on any JavaScript engine). The same extension in its WAV form, a RIFF/WAVE file with an LAE chunk after the audio (the 16 conformance vectors are such files, and .lae may hold either form), is opened by the WAV viewer with the same tree, tables and sounds. The viewer shows all of it whatever the size of the payload: the
syntax is laid out on demand from checkpoints of the decoder and the tables are paged with exact totals, the payload and the
16-bit FLAC layer are read from the file as they are needed, and nothing is cut off at a number of nodes, rows or bytes.
Links
Software
- FLAC reference tools – the
flacandmetaflaccommand-line encoder, decoder and tag editor (Windows, macOS, Linux) - foobar2000 – free music player and converter (Windows, macOS, mobile)
- VLC media player – free player for almost any audio or video file (Windows, macOS, Linux, mobile)
- Audacity – free audio editor that opens and exports FLAC (Windows, macOS, Linux)
- MusicBee – free music player and library manager (Windows)
- FFmpeg – command-line tool that encodes and decodes FLAC (Windows, macOS, Linux)
Developers
- libFLAC – the reference encoder and decoder libraries (C, C++)
- libsndfile – library that reads and writes FLAC (C)
- python-soundfile – audio file reading and writing, built on libsndfile (Python)
Reference
- RFC 9639 – the IETF specification of the Free Lossless Audio Codec (FLAC)