pr0h0
Compression Released

fm4

A lossless context-mixing compressor for text, code, binaries, images, audio and video. It beats xz, FLAC and JPEG XL on size, with byte-identical C, native and WebAssembly builds.

fm4 is a lossless compressor that stores a whole file as one number. A model predicts every bit, and an arithmetic coder narrows an interval inside [0, 1) by those predictions; any number in the final interval identifies the file.

It started as an experiment in encoding files as plain fractions N / D. That can never compress: measured on real data, the ratio was always 1.00x. Letting a learned model choose how wide each interval is turned the idea into context-mixing arithmetic coding, which does compress. fm4 is the fourth format the project went through (the full story).

The model mixes many predictions per bit: order-n contexts, match models, word and column contexts, an x86 filter for machine code, pixel predictors for images, least-squares filters for audio and the previous frame for video. Small neural mixers combine them and retrain after every bit.

make c
c/fm4 enc 4 bytes notes.txt notes.fm4     # compress at level 4 (0-5)
c/fm4 dec notes.fm4 notes.out
c/fm4 a 4 project.fma src/ docs/          # solid archive of files and directories

There are three implementations: a C reference, a BPL3 port built natively, and the same port compiled to WebAssembly for browsers and Node. All three write byte-identical files on Linux, macOS and Windows.

Key features

  • Strong compression: at level 4, about 30% smaller than xz -9e on source code, 26% on x86-64 binaries, 5% smaller than JPEG XL lossless on photos, and 6-45% smaller than flac -8 on audio
  • Modes for each kind of data: general bytes, raw RGB images, 16-bit PCM audio (1-8 channels) and raw RGB video
  • Archives: solid archives of files and directories, with WAV, BMP and PPM data routed to the audio and image models and already-compressed files stored
  • Parallel: large inputs are cut into segments coded on all cores, with output that doesn't depend on the thread count
  • Larger than RAM: inputs are memory-mapped and output goes through temporary files, so a 3 GB file codes under a 1 GB memory limit
  • Browser build: a WebAssembly module with a Web Worker pool and a small JS API
  • Hardened decoder: header and data CRCs, bounded allocation, strict parameter checks, fuzzed under ASan, UBSan and MSan

Architecture

  • Coder: a 32-bit binary arithmetic coder with 12-bit probabilities; every byte is coded as 8 bits.
  • Model: context tables of 64-byte buckets (one cache line per context per 4 bits) holding adaptive counters and bit histories, plus match models, mixers chosen by context, and APM/SSE refinement stages. Levels 0-5 add models.
  • Media: images code the error of a blended pixel predictor (MED, gradients, cross-channel and online least squares); audio predicts samples with least-squares filters over its own and the neighbouring channel; video adds the previous frame.
  • Containers: a 36-byte stream header with CRC-32s of the header and the data; segmented files and archives are lists of such streams.
  • Implementations: c/fm4.c is the reference and the spec. The BPL3 port compiles to native code and to WebAssembly, with a JS host that runs segments in Web Workers.

Technical challenges

  • Byte-identical output across C, BPL and WebAssembly, even though the least-squares predictors use floating point. It holds because fused multiply-add is disabled and only sqrt and floor come from libm, both exactly defined by IEEE 754. Locked output hashes catch any drift on Linux, macOS and Windows.
  • Speed: the model is compiled once per level and mode with its feature switches as constants, which made level 1 about 35% faster with unchanged output. Segments spread large inputs across cores: 256 MB went from 158 s to 62 s.
  • Untrusted input: a decoder must never crash, hang or allocate without bound on a forged file. Lengths are checked against the header CRC and a payload bound before allocation, only the parameter sets the encoder writes are accepted, and libFuzzer harnesses cover single streams and segmented files.
  • Keeping a port exact: matching the C build bit for bit found several BPL3 compiler bugs, and the BPL build now runs within about 2% of the C build.

Outcomes

  • Smaller than xz, zstd, PNG, JPEG XL and FLAC on every file in its benchmarks, at 0.3-2.7 MB/s per core
  • Open source under the Apache 2.0 license, with CI on Linux, macOS and Windows plus sanitizer and fuzzing jobs
  • v0.1.0 released, with format 1 frozen: future versions keep reading every file it writes

Updates

  1. fm4 is public

    Open-sourced fm4, a lossless context-mixing compressor for text, code, binaries, images, audio and video, under the Apache-2.0 license.

    At level 4 it comes out about 30% smaller than xz on source code, 5% smaller than JPEG XL on photos and 6-45% smaller than FLAC on audio. The trade-off is speed: 0.3-2.7 MB/s per core. There are three implementations, C, native BPL3 and WebAssembly, and all three write byte-identical files. The linked post tells how it grew out of an attempt to store files as plain fractions.