ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ArithmeticCoding.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------ File ArithmeticCoding.h -------------------------*/
3/*--------------------------------------------------------------------------*/
89/*--------------------------------------------------------------------------*/
90/*----------------------------- DEFINITIONS --------------------------------*/
91/*--------------------------------------------------------------------------*/
92
93#ifndef __ADS_ArithmeticCoding
94 #define __ADS_ArithmeticCoding
95/* self-identification: #endif constant */
96
97/*--------------------------------------------------------------------------*/
98/*------------------------------ INCLUDES ----------------------------------*/
99/*--------------------------------------------------------------------------*/
100
101#include "ADS/Contract.h"
103#include "ADS/numeric/Rational.h"
104
105#include <algorithm>
106#include <cstddef>
107#include <cstdint>
108#include <map>
109#include <span>
110#include <string>
111#include <string_view>
112#include <utility>
113#include <vector>
114
115/*--------------------------------------------------------------------------*/
116/*--------------------------- NAMESPACE ------------------------------------*/
117/*--------------------------------------------------------------------------*/
118
119namespace ADS
120{
121
122/*--------------------------------------------------------------------------*/
123/*----------------------- THE EXACT ARITHMETIC CODE ------------------------*/
124/*--------------------------------------------------------------------------*/
125
142template< class C >
143[[nodiscard]] std::pair< Rational , Rational >
144arithmeticInterval( std::span< const C > text ,
145 const std::map< C , Rational > & prob );
146
169template< class C >
170[[nodiscard]] std::string
171arithmeticCodeExact( std::span< const C > text ,
172 const std::map< C , Rational > & prob );
173
191template< class C >
192[[nodiscard]] std::vector< C >
193arithmeticDecodeExact( const std::string & bits , std::size_t n ,
194 const std::map< C , Rational > & prob );
195
196/*--------------------------------------------------------------------------*/
197/*--------------------------- CLASS RangeEncoder ---------------------------*/
198/*--------------------------------------------------------------------------*/
199
203{
204
205/*--------------------------------------------------------------------------*/
206 public:
210 explicit RangeEncoder( int w = 32 );
211
213 [[nodiscard]] std::uint64_t maxTotal( void ) const { return( f_M / 4 ); }
214
240 void encode( std::uint64_t low , std::uint64_t high , std::uint64_t total );
241
246 [[nodiscard]] std::vector< std::uint8_t > finish( void );
247
249 [[nodiscard]] std::size_t bits( void ) const { return( f_out.size() ); }
250
251/*--------------------------------------------------------------------------*/
252 private:
254 void emit( bool b );
255
256 std::uint64_t f_M;
257 std::uint64_t f_low = 0;
258 std::uint64_t f_high;
259 std::size_t f_pending = 0;
260 PackedBitWriter f_out;
261
262 }; // end( class RangeEncoder )
263
264/*--------------------------------------------------------------------------*/
265/*--------------------------- CLASS RangeDecoder ---------------------------*/
266/*--------------------------------------------------------------------------*/
267
270{
271
272/*--------------------------------------------------------------------------*/
273 public:
276 explicit RangeDecoder( std::span< const std::uint8_t > bytes , int w = 32 );
277
279 [[nodiscard]] std::uint64_t maxTotal( void ) const { return( f_M / 4 ); }
280
295 [[nodiscard]] std::uint64_t target( std::uint64_t total ) const;
296
322 void consume( std::uint64_t low , std::uint64_t high , std::uint64_t total );
323
324/*--------------------------------------------------------------------------*/
325 private:
326 std::uint64_t f_M;
327 std::uint64_t f_low = 0;
328 std::uint64_t f_high;
329 std::uint64_t f_v = 0;
330 PackedBitReader f_in;
331
332 }; // end( class RangeDecoder )
333
334/*--------------------------------------------------------------------------*/
335/*---------------------- THE INTEGER ARITHMETIC CODE -----------------------*/
336/*--------------------------------------------------------------------------*/
337
359template< class C >
360[[nodiscard]] std::vector< std::uint8_t >
361arithmeticEncode( std::span< const C > text ,
362 const std::map< C , std::uint64_t > & counts , int w = 32 );
363
381template< class C >
382[[nodiscard]] std::vector< C >
383arithmeticDecode( std::span< const std::uint8_t > code , std::size_t n ,
384 const std::map< C , std::uint64_t > & counts , int w = 32 );
385
411[[nodiscard]] inline std::vector< std::uint8_t >
412adaptiveArithmeticEncode( std::span< const std::uint32_t > x ,
413 std::uint32_t sigma );
414
437[[nodiscard]] inline std::vector< std::uint32_t >
438adaptiveArithmeticDecode( std::span< const std::uint8_t > code ,
439 std::size_t n ,
440 std::uint32_t sigma );
441
446[[nodiscard]] inline std::vector< std::uint8_t >
447adaptiveArithmeticEncode( std::string_view s );
448
451[[nodiscard]] inline std::string
452adaptiveArithmeticDecode( std::span< const std::uint8_t > code ,
453 std::size_t n );
454
455/*--------------------------------------------------------------------------*/
456
457} // end( namespace ADS )
458
459/*--------------------------------------------------------------------------*/
460/*-------------------------- IMPLEMENTATION --------------------------------*/
461/*--------------------------------------------------------------------------*/
462
463#include "ADS/compression/impl/ArithmeticCoding.tpp"
464
465/*--------------------------------------------------------------------------*/
466
467#endif /* ArithmeticCoding.h included */
468
469/*--------------------------------------------------------------------------*/
470/*---------------------- End File ArithmeticCoding.h -----------------------*/
471/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The bits of the compressors of this folder, packed in bytes, since their input and their output are s...
The exact rational numbers of Knuth (section 4.5.1 of The Art of Computer Programming),...
reads the bits of a sequence of bytes in the order of a PackedBitWriter, and zeros past its end
Definition PackedBits.h:108
a growing sequence of bits, packed in bytes, the first bit in the most significant bit of the first b...
Definition PackedBits.h:70
std::uint64_t maxTotal(void) const
the largest total of the counts,
Definition ArithmeticCoding.h:279
void consume(std::uint64_t low, std::uint64_t high, std::uint64_t total)
narrows the interval to the symbol of cumulative counts out of total, as the encoder did
RangeDecoder(std::span< const std::uint8_t > bytes, int w=32)
the decoder of the code bytes, which must outlive it
std::uint64_t target(std::uint64_t total) const
the cumulative count of the next symbol: it is the one whose counts hold it
std::uint64_t maxTotal(void) const
the largest total of the counts,
Definition ArithmeticCoding.h:213
std::vector< std::uint8_t > finish(void)
the bits that end the code, and the code
std::size_t bits(void) const
the number of bits emitted
Definition ArithmeticCoding.h:249
void encode(std::uint64_t low, std::uint64_t high, std::uint64_t total)
narrows the interval to the symbol of cumulative counts out of total
RangeEncoder(int w=32)
the encoder on bits, with the interval ,
std::vector< std::uint32_t > adaptiveArithmeticDecode(std::span< const std::uint8_t > code, std::size_t n, std::uint32_t sigma)
the symbols of the adaptive arithmetic code in
std::string arithmeticCodeExact(std::span< const C > text, const std::map< C, Rational > &prob)
the arithmetic code of text: the first bits of , a string of '0' and '1'
std::vector< std::uint8_t > adaptiveArithmeticEncode(std::span< const std::uint32_t > x, std::uint32_t sigma)
the adaptive arithmetic code of the symbols of in
std::pair< Rational, Rational > arithmeticInterval(std::span< const C > text, const std::map< C, Rational > &prob)
the interval of text, as the pair
std::vector< C > arithmeticDecode(std::span< const std::uint8_t > code, std::size_t n, const std::map< C, std::uint64_t > &counts, int w=32)
the symbols of the code with the static model of the counts
std::vector< std::uint8_t > 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
std::vector< C > arithmeticDecodeExact(const std::string &bits, std::size_t n, const std::map< C, Rational > &prob)
the text of symbols of the code bits
Definition ADT.h:96