FLAC can travel in Ogg instead of its native framing. The first packet holds the STREAMINFO, each later header packet a metadata block, and each audio packet exactly one FLAC frame.
First packet
| Offset | Size | Description |
|---|---|---|
| 0 | 1 | the byte 0x7F |
| 1 | 4 | the ASCII characters FLAC |
| 5 | 1 | major version of the mapping, 1 |
| 6 | 1 | minor version of the mapping, 0 |
| 7 | 2 | the number of header packets after this one, big-endian; 0 means not stated |
| 9 | 4 | the marker fLaC |
| 13 | 4 | the STREAMINFO block’s header (type 0 and length 34) |
| 17 | 34 | the STREAMINFO block |
Header packets
After the first packet, each packet holds one FLAC metadata block with its four-byte
header, such as VORBIS_COMMENT or PICTURE, and the last has the
last-block bit set. When the first packet gives a count of zero, readers go by that
bit. The blocks are exactly the native ones: PADDING,
APPLICATION,
SEEKTABLE,
VORBIS_COMMENT,
CUESHEET and
PICTURE. A SEEKTABLE’s byte offsets count from
the first frame of a native FLAC stream, which no Ogg page matches, so Ogg readers seek by
granule position instead.
Pages
- The first packet is alone on the first page, which has the BOS flag.
- The header packets follow on their own pages, and the first audio packet starts a new page.
- Pages that finish only header packets have granule position 0.
Audio packets
Each audio packet is one FLAC frame, exactly as in native FLAC. The page’s granule position is the sample number of the end of the last frame finished on it, that is the number of samples (per channel) up to that point. FLAC in Ogg is unusual: most tools write native FLAC.
The older mapping
The mapping above (version 1.0) was the first one published: FLAC 1.1.1 (2004) calls it the “first official FLAC→Ogg bitstream mapping”. The FLAC tools wrote Ogg FLAC from version 1.0.1 (2001) to 1.1.0 with an earlier, ad-hoc mapping that was never formalised or described. The FLAC project’s own mapping page says those mappings “were never formalized and have insurmountable problems”, that streams made before 1.1.1 should be decoded and re-encoded with 1.1.1 or later, and that files made before 1.1.0 cannot be seeked.
What can be said from the files and from libFLAC’s decoder: the stream starts with the
native fLaC marker, with no 0x7F FLAC header packet, and its
packets are the native FLAC stream (the marker, the metadata blocks, the frames) cut into
pieces. libFLAC reads it by passing the packets’ bytes one after another to its native
decoder, so packet boundaries carry no meaning, and its granule positions follow no stated
rule. The viewer does the same: it joins the packets, reads the metadata blocks and
decodes the frames as native FLAC, and takes the length from STREAMINFO.
Links
- FLAC to Ogg mapping – the mapping’s definition
- FLAC changelog – 1.0.1 added Ogg FLAC; 1.1.1 standardised the mapping
- FLAC format – metadata blocks and frames in full
Source: Xiph.Org, Ogg Mapping (FLAC to Ogg mapping 1.0, xiph.org/flac/ogg_mapping.html): first packet layout, the 79-byte first page, the metadata packet type range 0x01 to 0x7E and 0x81 to 0xFE, and header pages with granule position 0.