ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Rans.h File Reference

The range variant of the asymmetric numeral systems of Duda (rANS), the entropy coder of order 0 of the fast compressors, beyond the arithmetic code of chapter 12, Statistical Coding, of Ferragina's book [see ArithmeticCoding.h]: it reaches the size of the arithmetic code, within a fraction of a percent of \( n H_0 \) [see Entropy.h], at the speed of a table of Huffman. More...

#include "ADS/Contract.h"
#include <algorithm>
#include <array>
#include <cstddef>
#include <cstdint>
#include <span>
#include <vector>
#include "ADS/compression/impl/Rans.tpp"

Go to the source code of this file.

Namespaces

namespace  ADS

Functions

std::vector< std::uint8_t > ADS::ransCompress (std::span< const std::uint8_t > in)
 the rANS code of order 0 of the bytes of in, with the frequencies and the size
std::vector< std::uint8_t > ADS::ransDecompress (std::span< const std::uint8_t > in)
 the bytes that ransCompress() coded in in
std::array< std::uint32_t, 256 > ADS::scaleFrequencies (const std::array< std::uint64_t, 256 > &f, int b)
 the frequencies \( f \) scaled to sum to \( 2^b \), each positive one kept positive

Detailed description

The range variant of the asymmetric numeral systems of Duda (rANS), the entropy coder of order 0 of the fast compressors, beyond the arithmetic code of chapter 12, Statistical Coding, of Ferragina's book [see ArithmeticCoding.h]: it reaches the size of the arithmetic code, within a fraction of a percent of \( n H_0 \) [see Entropy.h], at the speed of a table of Huffman.

The state is an integer \( x \), and a symbol \( s \) of frequency \( f_s \) and cumulative frequency \( c_s \) over a total \( M = 2^{12} \) moves it to \( C( s , x ) = M \lfloor x / f_s \rfloor + c_s + x \bmod f_s \), which grows \( x \) by a factor \( M / f_s \), i.e., by \( \log_2( M / f_s ) \) bits, the information of \( s \); the decoder inverts it, \( s \) being the symbol whose range \( [ c_s , c_s + f_s ) \) holds \( x \bmod M \) (a table of \( M \) entries) and \( x = f_s \lfloor x / M \rfloor + x \bmod M - c_s \). The state is kept in \( [ 2^{16} , 2^{32} ) \) by moving 16 bits at a time to the stream (renormalization), at most once per symbol; the decoder reads them back in the opposite order, so that the encoder works from the last symbol to the first. Unlike the arithmetic coder, which narrows an interval and emits its common bits, the state needs one multiplication per symbol and no division in the decoder, and its renormalization has no carry.

Four states take the symbols in turn, so that four chains of dependences overlap in the pipeline of the processor (the interleaving of Giesen); they share one stream. The frequencies are scaled to \( M \) with each byte of the string kept at 1 at least, which costs a fraction of a percent on a string of bytes; the tabled variant [see Tans.h] makes the state transitions a lookup and needs no multiplication, at the price of a table per alphabet built in the encoder too.

The format: the size \( n \) in 8 bytes, the 256 frequencies scaled to \( M \) in 2 bytes each, the four final states in 4 bytes each, the stream of words of 16 bits, and 8 bytes of 0; the integers are little-endian.

The contracts [see Contract.h]: ransDecompress() gets the output of ransCompress(), whose header (the frequencies sum to \( M \), the states and the stream are in range) it checks; the audit of ransCompress() is that the output decompresses to the input. The functions are defined in Rans.tpp.

See also
J. Duda, Asymmetric numeral systems: entropy coding combining speed of Huffman coding with compression rate of arithmetic coding, arXiv:1311.2540, 2013
F. Giesen, Interleaved entropy coders, arXiv:1402.3392, 2014
T. Granlund, P. L. Montgomery, Division by invariant integers using multiplication, Proc. ACM PLDI 1994, 61-72, 1994, doi:10.1145/178243.178249
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa