After the header come the seek table, any data kept from the original file, and the compressed frames. The order depends on the version.
File layout
| Version 3980 and later | Before version 3980 |
|---|---|
| Junk (optional) | Junk (optional) |
| Descriptor | Old header, with the peak level and seek count if flagged |
| Header | WAV header, unless flag 32 is set |
| Seek table | Seek table |
| Header data (WAV header) | Bit table, below version 3810 |
| Frames | Frames |
| Terminating data | Terminating data |
| Tag | Tag |
The first frame starts at the sum of the junk, descriptor, header, seek table and header data sizes, plus one byte per frame for a bit table below version 3810.
Seek table
The table is an array of 32-bit little-endian values, normally one for each frame. FFmpeg
discards the first entry and places the first frame by the sum above. Every other entry
plus the junk length is a frame’s position in the file. In version 3980 and later the
table’s size is nSeekTableBytes; older files use the seek element count, or one
entry per frame.
Frames start on 32-bit boundaries measured from the first frame. When a position does not,
the remainder, (position − first frame) & 3, is a count of bytes to skip
after reading from the boundary below it.
Bit table
Files below version 3810 have one byte per frame after the seek table. Each byte is a count
of bits to skip at the start of its frame, combined with the byte skip above as
skip × 8 + bits. A nonzero count also adds four bytes to the previous frame’s
size.
Data from the original file
Monkey’s Audio keeps the source file’s non-audio bytes, so that decoding can restore the file exactly. The header data is everything before the audio, typically the WAV header; the terminating data is anything after the audio. With format flag 32 no WAV header is stored, and the decoder creates one.
The frames’ total size is held in two words, so files can exceed 4 GB. The history records repeated work on large files, up to version 10.65.
Frames
Every frame has nBlocksPerFrame blocks, except the last, which has
nFinalFrameBlocks. A frame’s bytes are a series of 32-bit little-endian words
that together form a big-endian stream: FFmpeg swaps each word’s bytes before decoding.
Words, and where a frame really starts
The encoder writes all the frames one after another as one stream of bytes, packs that stream into
32-bit words with the first byte in the high half, and stores each word little-endian. So in the file
every group of four bytes, counted from the start of the first frame, is the stream’s group reversed.
A frame does not start on a word: the last word of a frame also holds the first bytes of the next. The
seek table entry of a frame is the offset of the word it starts in plus the number of bytes of that
word that belong to the frame before (0 to 3). So (entry − first entry) & 3 is the
number of stream bytes to skip, and the decoder reads from the word boundary below the entry. The
file byte at the entry is therefore not necessarily the frame’s first byte.
An example from luckynight-partial.ape, frame 2: its seek entry is 319,586, 2 bytes past the
word at 319,584. Those 8 bytes of the file are B3 61 00 00 FC 00 1D 64; read as a stream
(each word reversed) they are 00 00 61 B3 64 1D 00 FC. The first two stream bytes
(00 00) end the previous frame; this frame’s CRC is the next four, 61 B3 64 1D,
which is 0x61B3641D, and the range coder’s data follows. Frame 0, which starts on a word (entry 268),
has 95 C2 4E 1F in the file for the stream bytes 1F 4E C2 95: CRC 0x1F4EC295,
then DE 98 0D 00 (stream 00 0D 98 DE: the unused byte, then the range
coder’s first byte 0x0D).
The frame data ends with a last word, written after the last frame and counted in
nAPEFrameDataBytes, that holds the last frame’s final bytes. The last frame’s size is
therefore the end of the frame data minus its entry, and a reader may need up to two bytes after
a frame’s end in files before version 3.95 (the decoder reads that far ahead; FFmpeg supplies zeros
there, and the explorer does too).
A frame in that swapped order begins:
| Size | Field | Meaning |
|---|---|---|
| 4 | CRC | Big-endian. Above version 3820, the top bit set means frame flags follow; the other 31 bits are the CRC |
| 4 | Frame flags | Only if that bit is set |
| 1 | Skipped byte | Version 3900 and later: one byte, then the range coder |
| varies | Compressed audio | See entropy coding |
Before version 3900 the frame is read through a bit reader instead, so these fields are read as bits.
Frame flags
| Value | Name | Meaning |
|---|---|---|
| 1 | APE_SPECIAL_FRAME_MONO_SILENCE / LEFT_SILENCE | A mono frame is entirely silent. In a stereo frame, the left channel (the SDK’s name for the first output channel) is silent. FFmpeg tests the mask of 3 for mono |
| 2 | APE_SPECIAL_FRAME_RIGHT_SILENCE | The second channel is silent. With bit 1 as well (value 3), a stereo frame is entirely silent: no coded values follow and every sample is 0 |
| 4 | APE_SPECIAL_FRAME_PSEUDO_STEREO | A stereo frame holds one channel only, decoded as a mono stream and copied to both outputs |
In a mono stream, FFmpeg treats either silence bit as a silent frame. In a stereo stream, it needs both.
The frame CRC
The CRC covers the decoded audio, not the compressed bytes. FFmpeg verifies it from version 3900 on, when asked: it computes an IEEE CRC-32, starting from 0xFFFFFFFF, over the little-endian bytes of each decoded sample in turn, one channel after another, using three bytes for 24-bit audio (one byte, offset by 128, for 8-bit audio). The stored value is the complement of the result shifted right by 1, so its top bit is free to flag the special codes.
The explorer checks it in every version it decodes, and found it right in every complete frame of
the test files. Up to version 3.82 (3820) the stored value is the complement without the shift, all
32 bits, and no special codes follow; from 3821 the shift and the flag bit apply (the SDK’s UnMACOld.cpp
and APEDecompress.cpp use exactly this threshold). The explorer follows it for every version.
Silent and pseudo-stereo frames are checked like any other: the CRC is of the samples, which are zeros or the copied channel. Sample values outside the bit depth’s range cannot be right; the explorer reports them.
The 64-bit “interim” form
Builds of Monkey’s Audio from 8.50 for a while encoded 24-bit files using 64-bit arithmetic in the
second stage of prediction, which differs from the normal 32-bit form when a product overflows 32 bits.
The SDK’s decoder retries a 24-bit frame that fails its CRC in this form. The sample
NoLegacy-cut.ape is one. The explorer does the same retry (and, for a frame cut short, picks
the form that gives fewer samples outside the 24-bit range).
Source: Monkey’s Audio SDK 13.46, Source/MACLib/Prepare.h (the special-frame values 1, 2 and 4), APEInfo.h (special frames above version 3820) and APEHeader.cpp; the frame layout before 3980 and the byte layout of the frame start are from FFmpeg’s ape.c and apedec.c and the SDK’s decoder, read in earlier rounds. Only the special-frame values and the version limits are compared by scripts/ape/spec-check.mjs.