37 #ifndef SPL_arithCoder_hpp
38 #define SPL_arithCoder_hpp
44 #include <SPL/config.hpp>
57 #define SPL_ArithCoder_CodeSize32
59 #define SPL_ArithCoder_Entropy
61 #if defined(SPL_ArithCoder_CodeSize32)
62 #define SPL_ArithCoder_BigType unsigned long long
63 #define SPL_ArithCoder_NumBigBits 64
64 #define SPL_ArithCoder_CodeType unsigned long
65 #define SPL_ArithCoder_NumCodeBits 32
66 #define SPL_ArithCoder_FreqType unsigned long
67 #define SPL_ArithCoder_NumFreqBits 30
69 #define SPL_ArithCoder_BigType unsigned long
70 #define SPL_ArithCoder_NumBigBits 32
71 #define SPL_ArithCoder_CodeType unsigned short
72 #define SPL_ArithCoder_NumCodeBits 16
73 #define SPL_ArithCoder_FreqType unsigned short
74 #define SPL_ArithCoder_NumFreqBits 14
78 #define SPL_ArithCoder_DefaultMaxFreq 256
82 #define SPL_ArithCoder_long long
83 #define SPL_ArithCoder_ulong unsigned long
111 typedef SPL_ArithCoder_FreqType Freq;
114 static const int FreqBits = SPL_ArithCoder_NumFreqBits;
117 static const Freq MaxFreq = (1ULL << FreqBits) - 1;
122 typedef SPL_ArithCoder_CodeType Code;
125 typedef SPL_ArithCoder_BigType BigCode;
127 static const int BigBits = SPL_ArithCoder_NumBigBits;
128 static const int CodeBits = SPL_ArithCoder_NumCodeBits;
129 static const Code MaxCode = (1ULL << CodeBits) - 1;
130 static const Code FirstQuart = (MaxCode >> 2) + 1;
131 static const Code Half = 2 * FirstQuart;
132 static const Code ThirdQuart = 3 * FirstQuart;
151 class ArithEncoder :
public ArithCoder
168 ArithEncoder(OutputBitStream* out =
nullptr);
192 void setOutput(OutputBitStream* out);
205 OutputBitStream* getOutput()
const;
217 SPL_ArithCoder_ulong getSymCount()
const;
230 SPL_ArithCoder_ulong getBitCount()
const;
270 int encode(Freq lowFreq, Freq highFreq, Freq totalFreq);
279 static void setDebugLevel(
int debugLevel);
284 static void setDebugStream(std::ostream& out);
289 static std::ostream& getDebugStream();
295 void dump(std::ostream& out)
const;
301 ArithEncoder(
const ArithEncoder&);
305 ArithEncoder& operator=(
const ArithEncoder&);
308 static int debugLevel_;
311 static std::ostream* debugStream_;
315 int bitPlusFollow(
int bit);
318 OutputBitStream* out_;
327 unsigned long bitsOutstanding_;
330 SPL_ArithCoder_ulong bitCount_;
333 SPL_ArithCoder_ulong symCount_;
336 inline void ArithEncoder::setOutput(OutputBitStream* out)
341 inline OutputBitStream* ArithEncoder::getOutput()
const
346 inline SPL_ArithCoder_ulong ArithEncoder::getSymCount()
const
351 inline SPL_ArithCoder_ulong ArithEncoder::getBitCount()
const
353 return bitCount_ + bitsOutstanding_;
356 inline void ArithEncoder::setDebugLevel(
int debugLevel)
358 debugLevel_ = debugLevel;
361 inline void ArithEncoder::setDebugStream(std::ostream& out)
366 inline std::ostream& ArithEncoder::getDebugStream()
368 return debugStream_ ? (*debugStream_) : std::cerr;
387 class ArithDecoder :
public ArithCoder
405 ArithDecoder(InputBitStream* in =
nullptr);
429 InputBitStream* getInput()
const;
439 void setInput(InputBitStream* in);
451 SPL_ArithCoder_ulong getBitCount()
const;
462 SPL_ArithCoder_ulong getSymCount()
const;
505 int decode(Freq totalFreq, Freq& target);
522 int adjust(Freq lowFreq, Freq highFreq, Freq totalFreq);
531 static void setDebugLevel(
int debugLevel);
536 static void setDebugStream(std::ostream& out);
541 std::ostream& getDebugStream();
547 void dump(std::ostream& out)
const;
552 static int debugLevel_;
555 static std::ostream* debugStream_;
559 ArithDecoder(
const ArithDecoder&);
563 ArithDecoder& operator=(
const ArithDecoder&);
580 SPL_ArithCoder_ulong symCount_;
583 SPL_ArithCoder_ulong bitCount_;
586 inline InputBitStream* ArithDecoder::getInput()
const
591 inline SPL_ArithCoder_ulong ArithDecoder::getSymCount()
const
596 inline SPL_ArithCoder_ulong ArithDecoder::getBitCount()
const
601 inline void ArithDecoder::setDebugLevel(
int debugLevel)
603 debugLevel_ = debugLevel;
606 inline void ArithDecoder::setDebugStream(std::ostream& out)
611 inline std::ostream& ArithDecoder::getDebugStream()
613 return debugStream_ ? (*debugStream_) : std::cerr;
616 inline void ArithDecoder::setInput(InputBitStream* in)
633 class ArithCoderModel
645 ArithCoderModel(
int numSyms, ArithCoder::Freq maxFreq);
661 void lookup(
int sym, ArithCoder::Freq& lowFreq,
662 ArithCoder::Freq& highFreq, ArithCoder::Freq& totalFreq)
const;
669 void invLookup(ArithCoder::Freq targetFreq,
int& sym,
670 ArithCoder::Freq& lowFreq, ArithCoder::Freq& highFreq,
671 ArithCoder::Freq& totalFreq)
const;
677 void update(
int sym);
686 int getNumSyms()
const;
691 void setAdaptive(
bool adaptive);
696 bool getAdaptive()
const;
702 SPL_ArithCoder_ulong getSymCount(
int sym)
const;
707 SPL_ArithCoder_ulong getTotalSymCount()
const;
716 ArithCoder::Freq getTotalFreq()
const;
719 ArithCoder::Freq getMaxFreq()
const;
724 void setProbs(
const std::vector<ArithCoder::Freq>& freqs);
729 void getProbs(std::vector<ArithCoder::Freq>& freqs);
738 static void setDebugLevel(
int debugLevel);
743 static void setDebugStream(std::ostream& out);
748 static std::ostream& getDebugStream();
753 void dump(std::ostream& out)
const;
759 ArithCoderModel(
const ArithCoderModel&);
763 ArithCoderModel& operator=(
const ArithCoderModel&);
766 static int debugLevel_;
769 static std::ostream* debugStream_;
773 std::vector<int> symToIdx_;
777 std::vector<int> idxToSym_;
782 std::vector<ArithCoder::Freq> freqs_;
786 std::vector<ArithCoder::Freq> cumFreqs_;
789 std::vector<SPL_ArithCoder_ulong> symCnts_;
792 SPL_ArithCoder_ulong totalSymCnt_;
795 ArithCoder::Freq maxFreq_;
801 inline SPL_ArithCoder_ulong ArithCoderModel::getSymCount(
int sym)
const
803 return symCnts_[sym];
806 inline SPL_ArithCoder_ulong ArithCoderModel::getTotalSymCount()
const
811 inline void ArithCoderModel::setAdaptive(
bool adaptive)
813 adaptive_ = adaptive;
816 inline bool ArithCoderModel::getAdaptive()
const
822 inline ArithCoder::Freq ArithCoderModel::getMaxFreq()
const
827 inline ArithCoder::Freq ArithCoderModel::getTotalFreq()
const
832 inline int ArithCoderModel::getNumSyms()
const
834 return symToIdx_.size();
837 inline void ArithCoderModel::setDebugLevel(
int debugLevel)
839 debugLevel_ = debugLevel;
842 inline void ArithCoderModel::setDebugStream(std::ostream& out)
847 inline std::ostream& ArithCoderModel::getDebugStream()
849 return debugStream_ ? (*debugStream_) : std::cerr;
921 void setContext(
int contextId,
const std::vector<ArithCoder::Freq>&
922 symFreqs,
bool adaptive);
974 void dump(std::ostream& out)
const;
979 static int debugLevel_;
982 static std::ostream* debugStream_;
996 std::vector<ArithCoderModel*> contexts_;
1001 return (debugStream_) ? (*debugStream_) : std::cerr;
1006 return enc_.getOutput();
1011 enc_.setOutput(out);
1016 return enc_.getSymCount();
1021 return enc_.getBitCount();
1026 debugLevel_ = debugLevel;
1031 debugStream_ = &debugStream;
1036 return contexts_.size();
1109 void setContext(
int contextId,
const std::vector<ArithCoder::Freq>&
1110 symFreqs,
bool adaptive);
1162 void dump(std::ostream& out)
const;
1167 static int debugLevel_;
1170 static std::ostream* debugStream_;
1184 std::vector<ArithCoderModel*> contexts_;
1189 return debugStream_ ? (*debugStream_) : std::cerr;
1194 return dec_.getInput();
1204 return dec_.getBitCount();
1209 return dec_.getSymCount();
1214 debugLevel_ = debugLevel;
1219 debugStream_ = &debugStream;
1224 return contexts_.size();
1237 SPL_ArithCoder_ulong numOnes;
1238 SPL_ArithCoder_ulong numSyms;
1253 using Freq = ArithCoder::Freq;
1351 void getContextStats(std::vector<BinArithCoderContextStat>& stats)
const;
1358 ArithCoder::Freq totalFreq, ArithCoder::Freq maxFreq,
bool adaptive);
1380 ArithCoder::Freq& totalFreq, ArithCoder::Freq& maxFreq,
bool& adaptive);
1453 void dump(std::ostream& out)
const;
1476 #if defined(SPL_ArithCoder_Entropy)
1477 long double getEntropy()
const;
1478 void clearEntropy();
1484 static int debugLevel_;
1487 static std::ostream* debugStream_;
1501 std::vector<ArithCoderModel*> models_;
1504 SPL_ArithCoder_ulong bypassOneCnt_;
1507 SPL_ArithCoder_ulong bypassSymCnt_;
1509 #if defined(SPL_ArithCoder_Entropy)
1510 long double entropy_;
1514 #if defined(SPL_ArithCoder_Entropy)
1515 inline long double BinArithEncoder::getEntropy()
const
1519 inline void BinArithEncoder::clearEntropy()
1527 return enc_.getSymCount();
1532 return enc_.getBitCount();
1537 return enc_.getOutput();
1542 return models_.size();
1547 ArithEncoder::setDebugStream(out);
1548 debugStream_ = &out;
1553 return debugStream_ ? (*debugStream_) : std::cerr;
1558 ArithEncoder::setDebugLevel(debugLevel >> 8);
1559 debugLevel_ = debugLevel;
1564 enc_.setOutput(out);
1579 using Freq = ArithCoder::Freq;
1678 ArithCoder::Freq totalFreq, ArithCoder::Freq maxFreq,
bool adaptive);
1700 ArithCoder::Freq& totalFreq, ArithCoder::Freq& maxFreq,
bool& adaptive);
1769 void dump(std::ostream& out)
const;
1786 #if defined(SPL_ArithCoder_Entropy)
1787 long double getEntropy()
const;
1788 void clearEntropy();
1794 static int debugLevel_;
1797 static std::ostream* debugStream_;
1811 std::vector<ArithCoderModel*> models_;
1813 #if defined(SPL_ArithCoder_Entropy)
1814 long double entropy_;
1819 #if defined(SPL_ArithCoder_Entropy)
1820 inline long double BinArithDecoder::getEntropy()
const
1824 inline void BinArithDecoder::clearEntropy()
1832 return dec_.getSymCount();
1837 return dec_.getBitCount();
1842 return dec_.getInput();
1847 return models_.size();
1852 ArithDecoder::setDebugStream(out);
1853 debugStream_ = &out;
1858 return debugStream_ ? (*debugStream_) : std::cerr;
1863 ArithDecoder::setDebugLevel(debugLevel >> 8);
1864 debugLevel_ = debugLevel;
void dump(std::ostream &out) const
Dump the internal state of the encoder to the specified stream for debugging purposes.
Definition: arithCoder.cpp:861
int getNumContexts() const
Get the number of contexts.
Definition: arithCoder.hpp:1845
static void setDebugLevel(int debugLevel)
Set the debug level.
Definition: arithCoder.hpp:1556
void setInput(InputBitStream *in)
Set the bit stream from which to read encoded data.
Definition: arithCoder.hpp:1197
void setContext(int contextId, int numSyms)
Set the specified context to have the given number of symbols which are initially equiprobable...
Definition: arithCoder.cpp:866
int encodeBypass(int numSyms, int sym)
Encode the given symbol in bypass mode (i.e., a fixed probablity distribution where all symbols are e...
Definition: arithCoder.cpp:837
static void setDebugLevel(int debugLevel)
Set the debug level.
Definition: arithCoder.hpp:1212
int start()
Start a code word.
Definition: arithCoder.cpp:736
int start()
Start a code word.
Definition: arithCoder.cpp:517
~BinArithEncoder()
Destroy an arithmetic encoder.
Definition: arithCoder.cpp:510
Binary arithmetic decoder class.
Definition: arithCoder.hpp:1575
int encodeRegular(int contextId, int binVal)
Encode the specified symbol in the given context.
Definition: arithCoder.cpp:522
void dump(std::ostream &out) const
Dump the internal state of the decoder to the specified stream for debugging purposes.
Definition: arithCoder.cpp:998
static void setDebugStream(std::ostream &out)
Set the stream for debugging output.
Definition: arithCoder.hpp:1029
Definition: Arcball.hpp:48
void dump(std::ostream &out) const
Dump the internal encoder state to the specified output stream for debugging purposes.
Definition: arithCoder.cpp:572
SPL_ArithCoder_ulong getBitCount() const
Get the number of bits read so far.
Definition: arithCoder.hpp:1202
void setInput(InputBitStream *in)
Set the bit stream from which to read encoded data.
Definition: arithCoder.hpp:1867
M-ary arithmetic decoder class.
Definition: arithCoder.hpp:1047
void getContextState(int contextId, ArithCoder::Freq &oneFreq, ArithCoder::Freq &totalFreq, ArithCoder::Freq &maxFreq, bool &adaptive)
Get the symbol probabilities and adaptivity for the specified context.
Definition: arithCoder.cpp:641
int decodeBypass(int numSyms)
Decode a symbol in bypass mode (i.e., all symbols equiprobable).
Definition: arithCoder.cpp:970
int encodeBypass(int binVal)
Encode the specified symbol in bypass mode (i.e., using a fixed probability distribution with all sym...
Definition: arithCoder.cpp:546
SPL_ArithCoder_ulong getSymCount() const
Get the number of symbols encoded so far.
Definition: arithCoder.hpp:1014
static std::ostream & getDebugStream()
Get the stream for debugging output.
Definition: arithCoder.hpp:999
InputBitStream * getInput() const
Get the bit stream from which to read encoded data.
Definition: arithCoder.hpp:1840
MultiArithEncoder(int maxContexts, OutputBitStream *out=nullptr)
Create an encoder with the specified number of contexts that sends output to the given bit stream...
Definition: arithCoder.cpp:778
static std::ostream & getDebugStream()
Get the stream used for debugging output.
Definition: arithCoder.hpp:1187
int decodeRegular(int contextId)
Decode a symbol in the specified context.
Definition: arithCoder.cpp:680
int terminate()
Terminate the code word.
Definition: arithCoder.cpp:567
void dump(std::ostream &out) const
Dump the internal decoder state to the specified stream for debugging purposes.
Definition: arithCoder.cpp:741
int start()
Start a code word.
Definition: arithCoder.cpp:793
SPL_ArithCoder_ulong getSymCount() const
Get the number of symbols decoded so far.
Definition: arithCoder.hpp:1830
Output bit stream class.
Definition: bitStream.hpp:372
int getMaxContexts() const
Get the maximum number of contexts.
Definition: arithCoder.hpp:1034
static void setDebugLevel(int debugLevel)
Set the debug level.
Definition: arithCoder.hpp:1861
void getContextState(int contextId, ArithCoder::Freq &oneFreq, ArithCoder::Freq &totalFreq, ArithCoder::Freq &maxFreq, bool &adaptive)
Get the symbol probabilities and adaptivity for the specified context.
Definition: arithCoder.cpp:758
OutputBitStream * getOutput()
Get the bit stream used for output.
Definition: arithCoder.hpp:1004
M-ary arithmetic encoder class.
Definition: arithCoder.hpp:862
static void setDebugStream(std::ostream &out)
Set the stream to use for debugging output.
Definition: arithCoder.hpp:1217
SPL_ArithCoder_ulong getSymCount() const
Get the number of symbols output so far.
Definition: arithCoder.hpp:1525
int terminate()
Terminate the code word (for synchonization with the encoder).
Definition: arithCoder.cpp:731
OutputBitStream * getOutput() const
Get the bit stream to which encoded data should be written.
Definition: arithCoder.hpp:1535
~MultiArithDecoder()
Destroy the decoder.
Definition: arithCoder.cpp:911
BinArithEncoder(int numContexts, OutputBitStream *out=nullptr)
Create an arithmetic encoder with the specified number of contexts that sends output to the given bit...
Definition: arithCoder.cpp:496
static std::ostream & getDebugStream()
Get the stream used for debugging output.
Definition: arithCoder.hpp:1856
void setContextState(int contextId, ArithCoder::Freq oneFreq, ArithCoder::Freq totalFreq, ArithCoder::Freq maxFreq, bool adaptive)
Set the symbol probabilities and adaptivity for the specified context.
Definition: arithCoder.cpp:629
int decodeRegular(int contextId)
Decode a symbol using the given context.
Definition: arithCoder.cpp:931
Binary Arithmetic Coder Context Statistics Class.
Definition: arithCoder.hpp:1235
SPL_ArithCoder_ulong getBitCount() const
Get the number of bits read so far.
Definition: arithCoder.hpp:1835
int terminate()
Terminate the code word.
Definition: arithCoder.cpp:798
InputBitStream * getInput() const
Get the bit stream from which to read encoded data.
Definition: arithCoder.hpp:1192
~MultiArithEncoder()
Destroy an encoder.
Definition: arithCoder.cpp:783
Binary arithmetic encoder class.
Definition: arithCoder.hpp:1249
int getNumContexts() const
Get the number of contexts.
Definition: arithCoder.hpp:1540
static void setDebugStream(std::ostream &out)
Set the stream to use for debugging output.
Definition: arithCoder.hpp:1545
void dumpModels(std::ostream &out) const
Dump the internal encoder context state to the specified output stream for debugging purposes...
Definition: arithCoder.cpp:577
int terminate()
Terminate a code word (for synchronization with encoder).
Definition: arithCoder.cpp:926
BinArithDecoder(int numContexts, InputBitStream *in=nullptr)
Create a decoder with the specified number of contexts that receives input from the given bit stream...
Definition: arithCoder.cpp:661
SPL_ArithCoder_ulong getBitCount() const
Get the number of bits of output generated so far including bits awaiting output. ...
Definition: arithCoder.hpp:1019
~BinArithDecoder()
Destroy a decoder.
Definition: arithCoder.cpp:673
void setOutput(OutputBitStream *out)
Set the bit stream used for output.
Definition: arithCoder.hpp:1009
int decodeBypass()
Decode a symbol in bypass mode (i.e., using a fixed probability distribution with all symbols being e...
Definition: arithCoder.cpp:710
void setOutput(OutputBitStream *out)
Set the bit stream to which encoded data should be written.
Definition: arithCoder.hpp:1562
int start()
Start a code word.
Definition: arithCoder.cpp:921
void setContextState(int contextId, ArithCoder::Freq oneFreq, ArithCoder::Freq totalFreq, ArithCoder::Freq maxFreq, bool adaptive)
Set the symbol probabilities and adaptivity for the specified context.
Definition: arithCoder.cpp:746
static std::ostream & getDebugStream()
Get the stream used for debugging output.
Definition: arithCoder.hpp:1551
int encodeRegular(int contextId, int sym)
Encode the given symbol in the specified context.
Definition: arithCoder.cpp:803
SPL_ArithCoder_ulong getBitCount() const
Get the number of bits output so far.
Definition: arithCoder.hpp:1530
void setContext(int contextId, int numSyms)
Set the specified context to have the given number of symbols which are initially equiprobable...
Definition: arithCoder.cpp:1003
SPL_ArithCoder_ulong getSymCount() const
Get the number of symbols decoded so far.
Definition: arithCoder.hpp:1207
static void setDebugLevel(int debugLevel)
Set the debug level.
Definition: arithCoder.hpp:1024
int getMaxContexts() const
Get the maximum number of contexts.
Definition: arithCoder.hpp:1222
static void setDebugStream(std::ostream &out)
Set the stream to be used for debugging output.
Definition: arithCoder.hpp:1850
MultiArithDecoder(int maxContexts, InputBitStream *in=nullptr)
Create a decoder with the specified maximum number of contexts that sends output to the given bit str...
Definition: arithCoder.cpp:906