Introduction
APE files are made by Monkey’s Audio, a lossless audio compressor written by Matthew T. Ashland and first released in 2000. It usually compresses a little better than FLAC, at the cost of slower encoding and decoding, and is popular for CD rips on Windows.
Monkey’s Audio also gave its name to the APE tag format, which other formats such as WavPack and Musepack use for their tags.
Structure
An APE file starts with the four bytes MAC (with a trailing space),
although the format tolerates other data, such as an ID3v2 tag, before them. Files from
version 3.98 on follow this with a descriptor and a header, and older files with a single
32-byte header. Together these give the format version, the compression level, the
number of frames and blocks, the channel count, bit depth and sample rate. A
seek table lists where each frame starts, and
the compressed frames make up the rest of the file. Tags sit at the end, usually as an
APEv2 tag beginning APETAGEX.
Numbers are little-endian. Monkey’s Audio has no written specification: the pages in the
menu describe it from the SDK’s source code (version 13.46 of the program, read for
understanding) and from FFmpeg’s decoder, and were checked by decoding every sample
file found, bit for bit, against FFmpeg (see Sample files below). They say where something
could not be confirmed.
0MAC descriptor and headervaries…Seek table frame positionsvaries…Frames compressed audiorepeatedendAPEv2 tag optional, APETAGEXvaries
How a frame is decoded
- Find the frame in the seek table, take its words reversed (frames), skip the bytes of the previous frame.
- Read the CRC (and the special codes if its bit 31 is set).
- Decode a value for every channel of every block with the entropy coder the version asks for: a Rice code before 3.90, a range coder after.
- Run each channel’s values through the level’s NN filters (16 taps for normal, 64 for high, 32 and 256 for extra high, 16, 256 and 1,280 for insane).
- Run the second-stage predictor, which for stereo also looks at the other channel.
- Turn the (side, mid) pair back into left and right, check the CRC of the samples, and (for the original file) write them as 8, 16 or 24-bit little-endian PCM between the stored WAV header and trailer.
Every frame starts from nothing: filters, predictors and Rice states are reset, so frames can be decoded in any order, which is how the explorer decodes a long file in parts. Nothing is cut for length: the waveform, the exact Peak and RMS levels, Play and the Checks (every frame’s CRC, the descriptor’s MD5) cover the whole file, opening a file of hours costs one pass of decoding for the levels, and the memory held does not grow with the length. The waveform and the bytes are linked both ways (a click on the waveform selects its frame’s bytes, a frame picked in the tree moves the playhead), and the “Original file” entry rebuilds the file that was compressed (the stored or built header, the samples as that file laid them out, the trailer), in pieces, so a long file has no size limit.
Image link files (.apl)
A .apl file is not audio. It is text that names an .ape image of a whole CD and a range
of its blocks, so a player can show one track as a file of its own:
[Monkey's Audio Image Link File]
Image File=album.ape
Start Block=0
Finish Block=1234567
The SDK reads the first 4,096 bytes, finds the four labels anywhere in them (comparing without regard to case) and takes the image file as the text to the end of its line; a path without a directory is taken to sit next to the link file. The range is in blocks (samples of every channel). An APE tag may follow the text. The explorer shows the lines, the range and the tag.
Sample files
The pages were checked against these files, from FFmpeg’s test suite (all of the same 60-second clip of music, cut at 328,000 bytes, or 1 MiB, or 100,000 bytes, so only the first frames are whole): versions 3.80, 3.88, 3.89 beta 1, 3.91 beta 1, 3.92 beta 2 and 3.94 beta 1 at normal and extra high, a 3.99 file at normal, and a 24-bit 3.99 file at high. Every complete frame decodes to the same samples as FFmpeg and has the CRC it should. For the other levels and bit depths a small encoder was written from the same description and used to make 8, 16 and 24-bit mono and stereo files at all five levels, with silent and pseudo-stereo frames, tags, a stored WAV header, a different sample rate and files of 3 to 8 channels; FFmpeg decodes every one of them (up to stereo) to the same samples. Further checks:
- Whole files from real encoders (samples.mplayerhq.hu and FFmpeg’s bug reports): a 60-second 3.99 file at
normal with the MD5 in its descriptor, a 15-second 3.97 file whose stored WAV header carries a
PADchunk, a 244 MB image of a CD in version 3.97 at high, 3.99 files at normal, high and insane (level 5000, 30 seconds), and one whose header claims 4,194,341 frames while its seek table has 37 entries. The decoder equals FFmpeg sample for sample on all of them (the CD image: its first 12 frames), and the whole rebuilt original file equals the SDK’s decoder output on the 3.99 and 3.97 files above and two of the bug-report files; the SDK’s full verify (mac -V, which also checks the MD5) passes on every one but the damaged one and the CD image (the SDK stops on it with its error 1008; its first 12 frames decode with right CRCs). The MD5 the explorer computes (the stored WAV header, the frames, the trailer, the 24-byte header and the seek table, in that order) equals the stored one on the 3.99 file, so that order is the SDK writer’s. - The SDK as encoder and decoder (the 13.46 sources, built here, BSD licence): files of 32-bit integers and floats, 3 and 6 channels, 16-bit 6-channel files, files made from AIFF, Wave64, CAF and SND sources (big-endian, signed 8-bit among them), and the overflow files described above. For every one the explorer’s samples and its rebuilt original file equal what the SDK’s decoder writes, byte for byte (more than 250 files compared in the test).
Not described
Monkey’s Audio has no written specification. These pages follow its SDK source and FFmpeg’s decoder. Every part of the format that the SDK source defines is described here and shown by the explorer; what remains below is only unknown.
Unknown
- Reference and viewer, versions 3.00 to 3.79: whether files of these versions set flags the SDK source does not use is unknown.
- Reference: version 4.11 files: the SDK decodes exactly the file version 4110 as 3.99 (its source says a few users mailed such files and it does not know their origin), and the explorer does the same (4000 or 4999, which the SDK refuses, are flagged). Whether anything else differs in 4.11 files is unknown.
- Reference: the 64-bit “interim” form is exact only while the sums stay below 253, which any real audio does; a file built to overflow that would decode wrongly.
Scope statements, not gaps: there is no compression level 6000 or “extra insane” in the SDK (13.46), whose levels are 1000 to 5000, so a header with another level is reported and not decoded; more than two channels, the file MD5 order and the files made from AIFF, Wave64, CAF or SND sources were checked against the SDK’s own encoder and decoder (see Sample files).
Links
Software
- Monkey’s Audio – the original encoder and decoder, from the format’s author (Windows)
- 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)
- FFmpeg – command-line tool that decodes APE, but cannot write it (Windows, macOS, Linux)
Developers
- Monkey’s Audio SDK – encoder and decoder source code from the author (C++)