|
ADS
Algorithms and Data Structures
|
The arithmetic code (chapter 12, Statistical Coding, of Ferragina's book), exact and in integers. More...
#include "ADS/Contract.h"#include "ADS/compression/PackedBits.h"#include "ADS/numeric/Rational.h"#include <algorithm>#include <cstddef>#include <cstdint>#include <map>#include <span>#include <string>#include <string_view>#include <utility>#include <vector>#include "ADS/compression/impl/ArithmeticCoding.tpp"Go to the source code of this file.
Classes | |
| class | ADS::RangeEncoder |
| the integer arithmetic encoder of Witten, Neal and Cleary on \( w \) bits More... | |
| class | ADS::RangeDecoder |
| the decoder of a RangeEncoder on \( w \) bits More... | |
Namespaces | |
| namespace | ADS |
Functions | |
| template<class C> | |
| std::pair< Rational, Rational > | ADS::arithmeticInterval (std::span< const C > text, const std::map< C, Rational > &prob) |
| the interval \( [ l , l + s ) \) of text, as the pair \( ( l , s ) \) | |
| template<class C> | |
| std::string | ADS::arithmeticCodeExact (std::span< const C > text, const std::map< C, Rational > &prob) |
| the arithmetic code of text: the first \( \lceil \log_2( 2 / s ) \rceil \) bits of \( l + s / 2 \), a string of '0' and '1' | |
| template<class C> | |
| std::vector< C > | ADS::arithmeticDecodeExact (const std::string &bits, std::size_t n, const std::map< C, Rational > &prob) |
| the text of \( n \) symbols of the code bits | |
| template<class C> | |
| std::vector< std::uint8_t > | ADS::arithmeticEncode (std::span< const C > text, const std::map< C, std::uint64_t > &counts, int w=32) |
| the arithmetic code of text with the static model of the counts | |
| template<class C> | |
| std::vector< C > | ADS::arithmeticDecode (std::span< const std::uint8_t > code, std::size_t n, const std::map< C, std::uint64_t > &counts, int w=32) |
| the \( n \) symbols of the code with the static model of the counts | |
| std::vector< std::uint8_t > | ADS::adaptiveArithmeticEncode (std::span< const std::uint32_t > x, std::uint32_t sigma) |
| the adaptive arithmetic code of the symbols of \( x \) in \( [ 0 , \sigma ) \) | |
| std::vector< std::uint32_t > | ADS::adaptiveArithmeticDecode (std::span< const std::uint8_t > code, std::size_t n, std::uint32_t sigma) |
| the \( n \) symbols of the adaptive arithmetic code in \( [ 0 , \sigma ) \) | |
| std::vector< std::uint8_t > | ADS::adaptiveArithmeticEncode (std::string_view s) |
| the adaptive arithmetic code of the bytes of \( s \), those of adaptiveArithmeticEncode() above with \( \sigma = 256 \) | |
| std::string | ADS::adaptiveArithmeticDecode (std::span< const std::uint8_t > code, std::size_t n) |
| the \( n \) bytes of the adaptive arithmetic code, by adaptiveArithmeticDecode() above with \( \sigma = 256 \) | |
The arithmetic code (chapter 12, Statistical Coding, of Ferragina's book), exact and in integers.
A text \( S \) of \( n \) symbols, with the probabilities \( P[ \sigma ] \) and the cumulative ones \( f_\sigma = \sum_{c < \sigma} P[ c ] \), is the interval \( [ l , l + s ) \) of \( [ 0 , 1 ) \) that the symbols narrow in turn, \( s_i = s_{i - 1} P[ S[ i ] ] \) and \( l_i = l_{i - 1} + s_{i - 1} f_{S[ i ]} \), so that \( s_n = \prod_i P[ S[ i ] ] \); the code is the first \( \lceil \log_2( 2 / s_n ) \rceil \) bits of \( l_n + s_n / 2 \), a dyadic fraction inside the interval, since the truncation loses less than \( s_n / 2 \), and hence at most \( 2 + n H_0 \) bits with the empirical probabilities, \( \log_2( 1 / s_n ) \) being then \( n H_0 \) [see Entropy.h], against the \( n + n H_0 \) of the code of Huffman. arithmeticInterval(), arithmeticCodeExact() and arithmeticDecodeExact() do it in exact rationals [see Rational.h], in time that grows with the length of the numbers; they are the reference for the integer coder, and they give the numbers of the example of the same chapter.
The integer coder is that of Witten, Neal and Cleary (the range coding of the same chapter): \( [ 0 , 1 ) \) is \( [ 0 , M ) \), \( M = 2^w \), a probability is a count over a total \( T \), and the interval \( [ L , H ) \) of a symbol of cumulative counts \( [ c , c' ) \) is \( [ L + \lfloor r c / T \rfloor , L + \lfloor r c' / T \rfloor ) \), \( r = H - L \), so that the intervals of the symbols tile the current one with no gap. After each symbol the interval is expanded: inside \( [ 0 , M / 2 ) \) it emits 0, inside \( [ M / 2 , M ) \) it emits 1, inside \( [ M / 4 , 3 M / 4 ) \) it emits nothing but counts an underflow, whose bit, the opposite of the next one emitted, follows that bit; so \( r \) stays above \( M / 4 \), and a total up to \( M / 4 \) keeps every interval of a symbol nonempty. At the end, 01 or 10 (and the pending bits) pick a point of the last interval. The decoder keeps the \( w \) bits read in \( v \) and finds the symbol whose interval holds \( v \), with the same expansions; the bits past the end are zeros. With \( w = 32 \) the loss against the exact code is about \( 10^{-4} \) bits per symbol, as Witten, Neal and Cleary measured; the coders of the practice emit bytes instead of bits (the range coder of Martin), or use the asymmetric numeral systems of Duda [see Rans.h and Tans.h], which reach the same compression with a table lookup per symbol.
The model can change at each symbol, as long as the decoder changes it in the same way. The adaptive coder starts from \( P[ \sigma ] = 1 / |\Sigma| \) and, after a prefix of length \( l \) with \( l_\sigma \) occurrences of \( \sigma \), takes \( P[ \sigma ] = ( l_\sigma + 1 ) / ( l + |\Sigma| ) \), so nothing is sent in advance; the counts are halved when their total passes \( \max( 2^{16} , 2 |\Sigma| ) \), which keeps it under \( M / 4 \) and makes the model follow a text whose statistics change. The cumulative counts are in a tree of Fenwick, where an increment, a prefix sum and the search of the symbol of a count take \( O( \log |\Sigma| ) \) time; so a symbol costs \( O( \log |\Sigma| ) \) time and the code \( O( n \log |\Sigma| ) \).
The contracts [see Contract.h]: the probabilities are positive and sum to 1, the counts are positive with a total of at most \( M / 4 \), \( 2 \le w \le 32 \), the alphabet of the adaptive coder has at most \( 2^{20} \) symbols and a text only symbols of the model; the decoding of an encoding gives the text back, an audit. The functions are defined in ArithmeticCoding.tpp.