Golomb and Rice coding

Juan Francisco Rodríguez Herrera
Vicente González Ruiz

July 10, 2017

Contents

1 Fundamentals
2 The Golomb encoder
3 The Golomb decoder
4 The Rice encoder
5 The Rice decoder

1 Fundamentals

Golomb m = 1 m = 2 m = 3 m = 4 m = 5 m = 6 m = 7 m = 8
Rice k = 0 k = 1 k = 2 k = 3










s = 0 0 00 00 000 000 000 000 0000
1 10 01 010 001 001 001 0010 0001
2 110 100 011 010 010 0100 0011 0010
3 1110 101 100 011 0110 0101 0100 0011
4 11110 1100 1010 1000 0111 0110 0101 0100
5 150 1101 1011 1001 1000 0111 0110 0101
6 160 11100 1100 1010 1001 1000 0111 0110
7 170 11101 11010 1011 1010 1001 1000 0111
8 180 111100 11011 11000 10110 10100 10010 10000

PIC

2 The Golomb encoder

  1. k log2(m).
  2. r smodm.
  3. t 2k m.
  4. Output (sdivm) using an unary code.
  5. If r < t:
    1. Output the integer encoded in the k 1 least significant bits of r using a binary code.
  6. Else:
    1. r r + t.
    2. Output the integer encoded in the k least significant bits of r using a binary code.

Example (m = 7 and s = 8)

  1. k log2(8) = 3.
  2. r 8mod7 = 1.
  3. t 23 7 = 8 7 = 1.
  4. Output 8div7 = 87 = 1 as an unary code (2 bits).
    Therefore, output 10.
  5. r = 1 t = 1.
  6. r 1 + 1 = 2.
  7. Output r = 2 using a binary code of k = 3 bits.
    Therefore, c(8) = 10010.

3 The Golomb decoder

  1. k log2(m).
  2. t 2k m.
  3. Let s the number of consecutive ones in the input (we stop when we read a 0).
  4. Let x the next k 1 bits in the input.
  5. If x < t:
    1. s s × m + x.
  6. Else:
    1. x x × 2+ next input bit.
    2. s s × m + x t.

Example (decode 10010 where m = 7)

  1. k 3.
  2. t 2k m = 23 7 = 1).
  3. s 1 because we found only one 1 in the input.
  4. x inputk1 = input2 = 01.
  5. x = 1 t = 1.
  6. x x × x × 2 + next input bit = x × 1 × 2 + 0 = 2.
  7. s s × m + x t = 1 × 7 + 2 1 = 8.

Let’s go the the lab!

  1. make golomb.
  2.                     Codec | lena boats pepers zelda Average  
    --------------------------+--------------------------------  
                            : |    :     :      :     :       :  
                       golomb | ....  ....   ....  ....    ....  
               tpt 1 | golomb | ....  ....   ....  ....    ....

    Note: this implementation of the Golomb encoding estimates the m parameter automatically.

4 The Rice encoder

  1. m 2k.
  2. Output sm using an unary code (sm + 1 bits).
  3. Output the k least significant bits of s using a binary code.

Example (k = 1 and s = 7)

  1. m 2k = 21 = 2.
  2. Output sm = 72 = 3 using an unary code of 4 bits. Therefore, output 1110.
  3. Output the k = 1 least significant bits of s = 7 using a unary code (k + 1 bits). So, output 1. Total output c(7) = 11101.

5 The Rice decoder

  1. Let s the number of consecutive ones in the input (we stop when we read a 0).
  2. Let x the next k input bits.
  3. s s × 2k + x.

Example (decode 11101 where k = 1)

  1. s 3 because we found 3 consecutive ones in the input.
  2. x next input k = 1 input bits. Therefore x 1.
  3. s s × 2k + x = 3 × 21 + 1 = 6 + 1 = 7.

Let’s go the the lab!

  1. make rice.
  2.                     Codec | lena boats pepers zelda Average  
    --------------------------+--------------------------------  
                            : |    :     :      :     :       :  
                         rice | ....  ....   ....  ....    ....  
                 tpt 1 | rice | ....  ....   ....  ....    ....

    Note: this implementation of the Rice encoding estimates the k parameter automatically.

References

[1]   S.W. Golomb. Run-Length Encodings. IEEE Transactions on Information Theory, 12:399–401, 1966.

[2]   R. F. Rice. Some Practical Universal Noiseless Coding Techniques. Technical Report 79/22, Jet Propulsion Laboratory, 1979.