Audio is stored in one data chunk, as the
audio description says. A format
whose packets vary also needs a packet table.
Audio data chunk
| Offset | Size | Type | Name | Description |
|---|---|---|---|---|
| 0 | 4 | u32 | mEditCount | 0 at first; increased every time the audio is modified |
| 4 | … | bytes | mData | the audio, in the format from desc |
A chunk size of −1 means the size is not known, as when recording. The chunk must then be the last in the file, so its data runs to the end of the file. Apple recommends setting the true size as soon as it is known, and setting it when opening such a file to read.
Other chunks can name an edit count (peak, ovvw), and a
mismatch means they no longer describe the audio. The packet table must always match
the audio.
Audio is stored frame by frame, one sample per channel, with samples in the byte order set by the format flags.
Packet table chunk
Required when mBytesPerPacket or mFramesPerPacket is 0, and
there must be exactly one. It is otherwise optional, for priming, remainder or
valid-frame information. Its data begins with this header.
| Offset | Size | Type | Name | Description |
|---|---|---|---|---|
| 0 | 8 | s64 | mNumberPackets | the number of packets |
| 8 | 8 | s64 | mNumberValidFrames | the number of frames that actually play |
| 16 | 4 | s32 | mPrimingFrames | frames at the start that are encoder delay |
| 20 | 4 | s32 | mRemainderFrames | frames at the end that are padding |
| 24 | … | varints | table | an entry per packet, below |
What each entry holds depends on which of the two desc fields are 0:
mBytesPerPacket | mFramesPerPacket | Entry | Example |
|---|---|---|---|
| 0 | not 0 | the packet’s size in bytes | |
| not 0 | 0 | the packet’s frame count | |
| 0 | 0 | a pair: size in bytes, then frame count | Ogg Vorbis |
Variable-length integers
Each byte holds 7 bits of the number. The high bit is set when another byte follows. The most significant group of 7 bits comes first, as Apple’s examples show.
| Value | Bytes |
|---|---|
| 128 | 81 00 |
| 130 | 81 02 |
| 257 | 82 01 |
| 16,383 | FF 7F |
| 16,384 | 81 80 00 |
The table of a file never ends early: mNumberPackets entries follow the
header (for a size and frames pair, that many pairs). The chunk may be larger than the
table; nothing after the last entry is part of it.
Packet table examples
AAC, 3,074 frames. Apple’s example: 6 packets of 1,024 frames, priming
2,112, so the first two packets give no audio, the third 960 frames, the fourth and
fifth 1,024 each, and the sixth the last 66; mNumberValidFrames is
3,074 and mRemainderFrames is 1,024 − 66 = 958. The table is 6 sizes.
A constant packet size. No table is needed, so mNumberPackets
is 0 and the chunk is 24 bytes. IMA4 has 64 frames in a 34-byte packet a channel; with
5 remainder frames the header holds 0 packets, numFramesInFile − 5 valid
frames, 0 priming, and 59 (64 − 5) remainder frames. The explorer cuts the audio every
mBytesPerPacket bytes when there is no table.
From afconvert, an AAC file of 1 second at 44,100 Hz
(header and first sizes):
| Bytes | Meaning |
|---|---|
00 00 00 00 00 00 00 2E | 46 packets |
00 00 00 00 00 00 AC 44 | 44,100 valid frames |
00 00 08 40 | 2,112 priming frames |
00 00 03 7C | 892 remainder frames (46 × 1,024 = 47,104 = 2,112 + 44,100 + 892) |
06 5D 5F 4A … | the sizes: packet 0 is 6 bytes (silence), then 93, 95, 74 bytes, and so on, one byte each while under 128 |
Priming frames are the encoder’s delay; a decoder drops them from the start. The remainder frames are what the last packet decodes past the end of the audio; a decoder drops them from the end. When the valid-frame count and the packets disagree, the valid-frame count is what the file says plays. For HE-AAC the counts are in the core rate; see the layered description.
Source: Apple, Core Audio Format Specification 1.0, sections Audio Data Chunk and Packet Table Chunk (Tables 2-7 to 2-12, including the variable-length integer examples of Table 2-9 and the AAC packet table of Tables 2-11 and 2-12).