Lably
Skip to content

Asymmetric numeral systems

Entropy coding methods

  • 14 min read
  • 5 chapters

Related on Lably

Through Probability distribution, Finite-state machine and HTTP

Among others, ANS is used in the Facebook Zstandard compressor (also used e.g. in Linux kernel, Google Chrome browser, Android operating system, was published as RFC 8478 for MIME and HTTP), Apple LZFSE compressor, Google Draco 3D compressor (used e.g. in Pixar Universal Scene Description format) and PIK image compressor, CRAM DNA compressor from SAMtools utilities, NVIDIA nvCOMP high speed compression library, Dropbox DivANS compressor, Microsoft DirectStorage BCPack texture compressor, long-term JPEG XL and learning-based JPEG AI image compressors.

The basic idea is to encode information into a single natural number x. In the standard binary number system, we can add a bit s∈{0,1} of information to x by appending s at the end of x, which gives us x′=2x+s. For an entropy coder, this is optimal if Pr(0)=Pr(1)=1/2. ANS generalizes this process for arbitrary sets of symbols s∈S with an accompanying probability distribution (ps)s∈S. In ANS, if the information from s is appended to x to result in x′, then x′≈x⋅ps−1. Equivalently, log2⁡(x′)≈log2⁡(x)+log2⁡(1/ps), where log2⁡(x) is the number of bits of information stored in the number x, and log2⁡(1/ps) is the number of bits contained in the symbol s.

For the encoding rule, the set of natural numbers is split into disjoint subsets corresponding to different symbols – like into even and odd numbers, but with densities corresponding to the probability distribution of the symbols to encode. Then to add information from symbol s into the information already stored in the current number x, we go to number x′=C(x,s)≈x/p being the position of the x-th appearance from the s-th subset.

There are alternative ways to apply it in practice – direct mathematical formulas for encoding and decoding steps (uABS and rANS variants), or one can put the entire behavior into a table (tANS variant). Renormalization is used to prevent x going to infinity – transferring accumulated bits to or from the bitstream.

Entropy coding

Suppose a sequence of 1,000 zeros and ones would be encoded, which would take 1000 bits to store directly. However, if it is somehow known that it only contains 1 zero and 999 ones, it would be sufficient to encode the zero's position, which requires only ⌈log2⁡(1000)⌉≈10 bits here instead of the original 1000 bits.

Generally, such sequences of length n containing pn zeros and (1−p)n ones, for some probability p∈(0,1), are called combinations. Using Stirling's approximation we get their asymptotic number being

(npn)≈2nh(p) for large n and h(p)=−plog2⁡(p)−(1−p)log2⁡(1−p),

called Shannon entropy.

Hence, to choose one such sequence we need approximately nh(p) bits. It is still n bits if p=1/2, however, it can also be much smaller. For example, we need only ≈n/2 bits for p=0.11.

An entropy coder allows the encoding of a sequence of symbols using approximately the Shannon entropy bits per symbol. For example, ANS could be directly used to enumerate combinations: assign a different natural number to every sequence of symbols having fixed proportions in a nearly optimal way.

In contrast to encoding combinations, this probability distribution usually varies in data compressors. For this purpose, Shannon entropy can be seen as a weighted average: a symbol of probability p contains log2⁡(1/p) bits of information. ANS encodes information into a single natural number x, interpreted as containing log2⁡(x) bits of information. Adding information from a symbol of probability p increases this informational content to log2⁡(x)+log2⁡(1/p)=log2⁡(x/p). Hence, the new number containing both information should be x′≈x/p.

Motivating examples

Consider a source with 3 letters A, B, C, with probability 1/2, 1/4, 1/4. It is simple to construct the optimal prefix code in binary: A = 0, B = 10, C = 11. Then, a message is encoded as ABC -> 01011.

We see that an equivalent method for performing the encoding is as follows:

  • Start with number 1, and perform an operation on the number for each input letter.
  • A = multiply by 2; B = multiply by 4, add 2; C = multiply by 4, add 3.
  • Express the number in binary, then remove the first digit 1.

Consider a more general source with k letters, with rational probabilities n1/N,...,nk/N. Then performing arithmetic coding on the source requires only exact arithmetic with integers.

In general, ANS is an approximation of arithmetic coding that approximates the real probabilities r1,...,rk by rational numbers n1/N,...,nk/N with a small denominator N.

Sign in to keep reading

4 more chapters are free with a Lably account.

More in Basics