Golomb coding

Golomb coding is a lossless data compression method using a family of data compression codes invented by Solomon W. Golomb in the 1960s. Alphabets following a geometric distribution will have a Golomb code as an optimal prefix code, making Golomb coding highly suitable for situations in which the occurrence of small values in the input stream is significantly more likely than large values.

Rice coding
Rice coding (invented by Robert F. Rice) denotes using a subset of the family of Golomb codes to produce a simpler (but possibly suboptimal) prefix code. Rice used this set of codes in an adaptive coding scheme; "Rice coding" can refer either to that adaptive scheme or to using that subset of Golomb codes. Whereas a Golomb code has a tunable parameter that can be any positive integer value, Rice codes are those in which the tunable parameter is a power of two. This makes Rice codes convenient for use on a computer, since multiplication and division by 2 can be implemented more efficiently in binary arithmetic.

Rice was motivated to propose this simpler subset due to the fact that geometric distributions are often varying with time, not precisely known, or both, so selecting the seemingly optimal code might not be very advantageous.

Rice coding is used as the entropy encoding stage in a number of lossless image compression and audio data compression methods.

Construction of codes
Golomb coding uses a tunable parameter $M$ to divide an input value $x$ into two parts: $q$, the result of a division by $M$, and $r$, the remainder. The quotient is sent in unary coding, followed by the remainder in truncated binary encoding. When $$M=1$$, Golomb coding is equivalent to unary coding.

Golomb–Rice codes can be thought of as codes that indicate a number by the position of the bin ($q$), and the offset within the bin ($r$). The example figure shows the position $q$ and offset $r$ for the encoding of integer $x$ using Golomb–Rice parameter $p(0) = 0.2$, with source probabilities following a geometric distribution with $M = 3$.

Formally, the two parts are given by the following expression, where $x$ is the nonnegative integer being encoded:


 * $$q = \left \lfloor \frac{x}{M} \right \rfloor$$

and


 * $$r = x - qM$$.



Both $M$ and $q$ will be encoded using variable numbers of bits: $r$ by a unary code, and $q$ by $r$ bits for Rice code, or a choice between $b$ and $M = 3$ bits for Golomb code (i.e. $b$ is not a power of 2), with $$b = \lfloor\log_2(M)\rfloor$$. If $$r < 2^{b+1} - M$$, then use $M$ bits to encode $b$; otherwise, use $r$+1 bits to encode $b$. Clearly, $$b=\log_2(M)$$ if $r$ is a power of 2 and we can encode all values of $M$ with $r$ bits.

The integer $b$ treated by Golomb was the run length of a Bernoulli process, which has a geometric distribution starting at 0. The best choice of parameter $x$ is a function of the corresponding Bernoulli process, which is parameterized by $$p = P(x=0)$$ the probability of success in a given Bernoulli trial. $M$ is either the median of the distribution or the median ±1. It can be determined by these inequalities:
 * $$(1-p)^M + (1-p)^{M+1} \leq 1 < (1-p)^{M-1} + (1-p)^M,$$

which are solved by
 * $$M = \left\lceil -\frac{\log(2 -p)}{\log(1-p)}\right\rceil$$.

For the example with $p(0) = 0.2$:
 * $$M = \left\lceil -\frac{\log(1.8)}{\log(0.8)}\right\rceil = \left\lceil 2.634 \right\rceil = 3$$.

The Golomb code for this distribution is equivalent to the Huffman code for the same probabilities, if it were possible to compute the Huffman code for the infinite set of source values.

Use with signed integers
Golomb's scheme was designed to encode sequences of non-negative numbers. However, it is easily extended to accept sequences containing negative numbers using an overlap and interleave scheme, in which all values are reassigned to some positive number in a unique and reversible way. The sequence begins: 0, −1, 1, −2, 2, −3, 3, −4, 4, ... The n-th negative value (i.e., $M$) is mapped to the nth odd number ($-n$), and the mth positive value is mapped to the m-th even number ($2n-1$). This may be expressed mathematically as follows: a positive value $2m$ is mapped to ($$x' = 2|x| = 2x,\ x \ge 0$$), and a negative value $x$ is mapped to ($$y' = 2|y| - 1 = -2y - 1,\ y < 0$$). Such a code may be used for simplicity, even if suboptimal. Truly optimal codes for two-sided geometric distributions include multiple variants of the Golomb code, depending on the distribution parameters, including this one.

Simple algorithm
Below is the Rice–Golomb encoding, where the remainder code uses simple truncated binary encoding, also named "Rice coding" (other varying-length binary encodings, like arithmetic or Huffman encodings, are possible for the remainder codes, if the statistic distribution of remainder codes is not flat, and notably when not all possible remainders after the division are used). In this algorithm, if the M parameter is a power of 2, it becomes equivalent to the simpler Rice encoding:


 * 1) Fix the parameter M to an integer value.
 * 2) For N, the number to be encoded, find
 * 3)  quotient = q = floor(N/M)
 * 4)  remainder = r = N  modulo M
 * 5) Generate codeword
 * 6)  The code format : , where
 * 7)  Quotient code (in unary coding)
 * 8)   Write a q-length string of 1 bits (alternatively, of 0 bits)
 * 9)   Write a 0 bit (respectively, a 1 bit)
 * 10)  Remainder code (in truncated binary encoding)
 * 11)   Let $$b = \lfloor\log_2(M)\rfloor$$
 * 12)    If $$r < 2^{b+1}-M$$ code r in binary representation using b bits.
 * 13)    If $$r \ge 2^{b+1}-M$$ code the number $$r+2^{b+1}-M$$ in binary representation using b + 1 bits.

Decoding:
 * 1) Decode the unary representation of q (count the number of 1 in the beginning of the code)
 * 2) Skip the 0 delimiter
 * 3) Let $$b = \lfloor\log_2(M)\rfloor$$
 * 4) Interpret next b bits as a binary number r'. If $$r' < 2^{b+1}-M$$ holds, then the reminder $$ r = r' $$
 * 5) Otherwise interpret b + 1 bits as a binary number r', the reminder is given by $$r = r' - 2^{b+1} + M$$
 * 6) Compute $$N = q * M + r$$

Example
Set $1 − p(0) &ge; 0.45$. Thus $$b = \lfloor\log_2(10)\rfloor = 3$$. The cutoff is $$2^{b+1} - M = 16 - 10 = 6$$.

For example, with a Rice–Golomb encoding using parameter $b+1$, the decimal number 42 would first be split into $y$ = 4 and $q$ = 2, and would be encoded as qcode($r$),rcode($q$) = qcode(4),rcode(2) = 11110,010 (you don't need to encode the separating comma in the output stream, because the 0 at the end of the $r$ code is enough to say when $q$ ends and $r$ begins; both the qcode and rcode are self-delimited).

Use for run-length encoding

 * Note that $q$ and $p(0) = 0.2$ are reversed in this section compared to the use in earlier sections.

Given an alphabet of two symbols, or a set of two events, P and Q, with probabilities p and ($M = 10$) respectively, where $M = 10$, Golomb coding can be used to encode runs of zero or more P&prime;s separated by single Q&prime;s. In this application, the best setting of the parameter M is the nearest integer to $$- \frac{1}{\log_{2}p}$$. When p = 1/2, M = 1, and the Golomb code corresponds to unary ($1 – p$ P&prime;s followed by a Q is encoded as n ones followed by a zero). If a simpler code is desired, one can assign Golomb–Rice parameter $q$ (i.e., Golomb parameter $$M=2^b$$) to the integer nearest to $$- \log_2(-\log_2 p)$$; although not always the best parameter, it is usually the best Rice parameter and its compression performance is quite close to the optimal Golomb code. (Rice himself proposed using various codes for the same data to figure out which was best. A later JPL researcher proposed various methods of optimizing or estimating the code parameter. )

Consider using a Rice code with a binary portion having $r$ bits to run-length encode sequences where P has a probability $p$. If $$\mathbb{P}[\text{bit is part of }k\text{-run}]$$ is the probability that a bit will be part of an $b$-bit run ($$k-1$$ Ps and one Q) and $$(\text{compression ratio of }k\text{-run})$$ is the compression ratio of that run, then the expected compression ratio is


 * $$\begin{align}

\mathbb{E}[\text{compression ratio}] &= \sum_{k=1}^\infty (\text{compression ratio of }k\text{-run}) \cdot \mathbb{P}[\text{bit is part of }k\text{-run}] \\ &= \sum_{k=1}^\infty \frac{b+1+\lfloor 2^{-b}(k-1) \rfloor}{k} \cdot kp^{k-1} (1-p)^2 \\ &= (1-p)^2 \sum_{j=0}^\infty (b+1+j) \cdot \sum_{i=j2^b+1}^{(j+1)2^b} p^{i-1} \\ &= (1-p)^2 \sum_{j=0}^\infty (b+1+j) \cdot \left(p^{2^b j} - p^{2^{b} (j+1)}\right) \\ &= (1-p) \cdot \left (b + \sum_{m=0}^\infty p^{2^b m} \right ) \\ &= (1-p) \cdot \left(b + {\left (1-p^{2^b} \right )}^{-1}\right ) \\ \end{align}$$

Compression is often expressed in terms of $$1-\mathbb{E}[\text{compression ratio}]$$, the proportion compressed. For $$p \approx 1$$, the run-length coding approach results in compression ratios close to entropy. For example, using Rice code $$b=6$$ for $$p=0.99$$ yields $b$ compression, while the entropy limit is $p$.

Adaptive run-length Golomb–Rice encoding
When a probability distribution for integers is not known, the optimal parameter for a Golomb–Rice encoder cannot be determined. Thus, in many applications, a two-pass approach is used: first, the block of data is scanned to estimate a probability density function (PDF) for the data. The Golomb–Rice parameter is then determined from that estimated PDF. A simpler variation of that approach is to assume that the PDF belongs to a parametrized family, estimate the PDF parameters from the data, and then compute the optimal Golomb–Rice parameter. That is the approach used in most of the applications discussed below.

An alternative approach to efficiently encode integer data whose PDF is not known, or is varying, is to use a backwards-adaptive encoder. The RLGR encoder achieves that using a very simple algorithm that adjusts the Golomb–Rice parameter up or down, depending on the last encoded symbol. A decoder can follow the same rule to track the variation of the encoding parameters, so no side information needs to be transmitted, just the encoded data. Assuming a generalized Gaussian PDF, which covers a wide range of statistics seen in data such as prediction errors or transform coefficients in multimedia codecs, the RLGR encoding algorithm can perform very well in such applications.

Applications


Numerous signal codecs use a Rice code for prediction residues. In predictive algorithms, such residues tend to fall into a two-sided geometric distribution, with small residues being more frequent than large residues, and the Rice code closely approximates the Huffman code for such a distribution without the overhead of having to transmit the Huffman table. One signal that does not match a geometric distribution is a sine wave, because the differential residues create a sinusoidal signal whose values are not creating a geometric distribution (the highest and lowest residue values have similar high frequency of occurrences, only the median positive and negative residues occur less often).

Several lossless audio codecs, such as Shorten, FLAC, Apple Lossless, and MPEG-4 ALS, use a Rice code after the linear prediction step (called "adaptive FIR filter" in Apple Lossless). Rice coding is also used in the FELICS lossless image codec.

The Golomb–Rice coder is used in the entropy coding stage of Rice algorithm based lossless image codecs. One such experiment yields the compression ratio graph shown.

The JPEG-LS scheme uses Rice–Golomb to encode the prediction residuals.

The adaptive version of Golomb–Rice coding mentioned above, the RLGR encoder ,is used for encoding screen content in virtual machines in the RemoteFX component of the Microsoft Remote Desktop Protocol.