|
ADS
Algorithms and Data Structures
|
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. More...
Files | |
| file | ADS/compression/Rans.h |
| 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. | |
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 | |
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.
The definitions, the invariants and the costs of the area are in Theory of compression.
|
inlinenodiscard |
the rANS code of order 0 of the bytes of in, with the frequencies and the size
In pseudocode (the rANS of Duda with the interleaving of Giesen, beyond the book), \( x_0 , \ldots , x_3 \) the states and \( s_i \) the \( i \)-th byte, taken by the state \( x_{i \bmod 4} \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{ransCompress}( s_0 \cdots s_{n-1} ) \cr 2 & \quad f \gets \mathrm{scaleFrequencies}( \text{the counts of the } \text{bytes} , 12 );\ c \gets \text{their prefix sums} \cr 3 & \quad x_0 , \ldots , x_3 \gets 2^{16};\ W \gets \text{an empty stack} \text{ of words} \cr 4 & \quad \textbf{for}\ i \gets n - 1\ \textbf{downto}\ 0\ \textbf{do} \quad \triangleright\ \text{from the last symbol} \cr 5 & \qquad x \gets x_{i \bmod 4};\ s \gets s_i \cr 6 & \qquad \textbf{if}\ x \ge 2^{20} f_s\ \textbf{then} \ \mathrm{push}( W , x \bmod 2^{16} );\ x \gets \lfloor x / 2^{16} \rfloor \cr 7 & \qquad x_{i \bmod 4} \gets M \lfloor x / f_s \rfloor + c_s + x \bmod f_s \cr 8 & \quad \textbf{return}\ ( n , f , x_0 , \ldots , x_3 , \text{the } \text{words of } W \text{ from the top} ) \end{array}} \]
The division by \( f_s \) is a multiplication by a reciprocal (Granlund and Montgomery), exact on the states below \( 2^{32} \).
|
inlinenodiscard |
the bytes that ransCompress() coded in in
In pseudocode (the rANS of Duda, beyond the book), \( Z[ j ] \) the symbol of the slot \( j \in [ 0 , M ) \), i.e., the \( s \) with \( c_s \le j < c_s + f_s \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{ransDecompress}( y ) \cr 2 & \quad \text{read } n \text{, } f \text{ and } x_0 , \ldots , x_3; \ c \gets \text{the prefix sums of } f \cr 3 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ n - 1\ \textbf{do} \cr 4 & \qquad x \gets x_{i \bmod 4};\ j \gets x \bmod M;\ s \gets Z[ j ]; \ s_i \gets s \cr 5 & \qquad x \gets f_s \lfloor x / M \rfloor + j - c_s \cr 6 & \qquad \textbf{if}\ x < 2^{16}\ \textbf{then}\ x \gets 2^{16} x + \text{the next word of the stream} \cr 7 & \qquad x_{i \bmod 4} \gets x \cr 8 & \quad \textbf{return}\ s_0 \cdots s_{n-1} \end{array}} \]
|
inlinenodiscard |
the frequencies \( f \) scaled to sum to \( 2^b \), each positive one kept positive
It rounds each frequency, scaled by \( 2^b / N \), to the nearest integer, at least 1, and then gives the difference to \( 2^b \) to the largest one, or takes it from the largest ones, at most half of each at a time:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{scaleFrequencies}( f , b ) \cr 2 & \quad N \gets \sum_s f[ s ];\ M \gets 2^b \cr 3 & \quad \textbf{foreach}\ s\ \textbf{with}\ f[ s ] > 0\ \textbf{do} \ g[ s ] \gets \max( 1 , \lfloor ( f[ s ] M + N / 2 ) / N \rfloor ) \cr 4 & \quad \textbf{while}\ \sum_s g[ s ] \ne M\ \textbf{do} \cr 5 & \qquad s \gets \text{the symbol of the largest } g[ s ]; \ \delta \gets \sum_s g[ s ] - M \cr 6 & \qquad \textbf{if}\ \delta < 0\ \textbf{then}\ g[ s ] \gets g[ s ] - \delta\ \textbf{else}\ g[ s ] \gets g[ s ] - \max( 1 , \min( \delta , \lfloor g[ s ] / 2 \rfloor ) ) \cr 7 & \quad \textbf{return}\ g \end{array}} \]