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;
898 SPL_ArithCoder_ulong getSymCount()
const;
904 SPL_ArithCoder_ulong getBitCount()
const;
909 int getMaxContexts()
const;
915 void setContext(
int contextId,
int numSyms);
921 void setContext(
int contextId,
const std::vector<ArithCoder::Freq>&
922 symFreqs,
bool adaptive);
943 int encodeRegular(
int contextId,
int sym);
949 int encodeBypass(
int numSyms,
int sym);
958 static void setDebugLevel(
int debugLevel);
963 static void setDebugStream(std::ostream& out);
968 static std::ostream& getDebugStream();
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;
1011 enc_.setOutput(out);
1016 return enc_.getSymCount();
1021 return enc_.getBitCount();
1026 debugLevel_ = debugLevel;
1031 debugStream_ = &debugStream;
1036 return contexts_.size();
1083 SPL_ArithCoder_ulong getBitCount()
const;
1088 SPL_ArithCoder_ulong getSymCount()
const;
1097 int getMaxContexts()
const;
1103 void setContext(
int contextId,
int numSyms);
1109 void setContext(
int contextId,
const std::vector<ArithCoder::Freq>&
1110 symFreqs,
bool adaptive);
1131 int decodeRegular(
int contextId);
1137 int decodeBypass(
int numSyms);
1146 static void setDebugLevel(
int debugLevel);
1151 static void setDebugStream(std::ostream& out);
1156 static std::ostream& getDebugStream();
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;
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;
1295 int getNumContexts()
const;
1307 SPL_ArithCoder_ulong getSymCount()
const;
1319 SPL_ArithCoder_ulong getBitCount()
const;
1351 void getContextStats(std::vector<BinArithCoderContextStat>& stats)
const;
1357 void setContextState(
int contextId, ArithCoder::Freq oneFreq,
1358 ArithCoder::Freq totalFreq, ArithCoder::Freq maxFreq,
bool adaptive);
1379 void getContextState(
int contextId, ArithCoder::Freq& oneFreq,
1380 ArithCoder::Freq& totalFreq, ArithCoder::Freq& maxFreq,
bool& adaptive);
1414 int encodeRegular(
int contextId,
int binVal);
1431 int encodeBypass(
int binVal);
1453 void dump(std::ostream& out)
const;
1459 void dumpModels(std::ostream& out)
const;
1464 static void setDebugLevel(
int debugLevel);
1469 static void setDebugStream(std::ostream& out);
1474 static std::ostream& getDebugStream();
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();
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;
1621 SPL_ArithCoder_ulong getSymCount()
const;
1633 SPL_ArithCoder_ulong getBitCount()
const;
1671 int getNumContexts()
const;
1677 void setContextState(
int contextId, ArithCoder::Freq oneFreq,
1678 ArithCoder::Freq totalFreq, ArithCoder::Freq maxFreq,
bool adaptive);
1699 void getContextState(
int contextId, ArithCoder::Freq& oneFreq,
1700 ArithCoder::Freq& totalFreq, ArithCoder::Freq& maxFreq,
bool& adaptive);
1745 int decodeRegular(
int contextId);
1769 void dump(std::ostream& out)
const;
1774 static void setDebugLevel(
int debugLevel);
1779 static void setDebugStream(std::ostream& out);
1784 static std::ostream& getDebugStream();
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();
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;
SPL_ArithCoder_ulong getSymCount() const
Get the number of symbols output so far.
Definition: include/SPL/arithCoder.hpp:1525
static void setDebugLevel(int debugLevel)
Set the debug level.
Definition: include/SPL/arithCoder.hpp:1556
void setInput(InputBitStream *in)
Set the bit stream from which to read encoded data.
Definition: include/SPL/arithCoder.hpp:1197
SPL_ArithCoder_ulong getBitCount() const
Get the number of bits of output generated so far including bits awaiting output. ...
Definition: include/SPL/arithCoder.hpp:1019
static void setDebugLevel(int debugLevel)
Set the debug level.
Definition: include/SPL/arithCoder.hpp:1212
Binary arithmetic decoder class.
Definition: include/SPL/arithCoder.hpp:1575
static void setDebugStream(std::ostream &out)
Set the stream for debugging output.
Definition: include/SPL/arithCoder.hpp:1029
Definition: Arcball.hpp:48
void setInput(InputBitStream *in)
Set the bit stream from which to read encoded data.
Definition: include/SPL/arithCoder.hpp:1867
M-ary arithmetic decoder class.
Definition: include/SPL/arithCoder.hpp:1047
static std::ostream & getDebugStream()
Get the stream for debugging output.
Definition: include/SPL/arithCoder.hpp:999
static std::ostream & getDebugStream()
Get the stream used for debugging output.
Definition: include/SPL/arithCoder.hpp:1187
std::ostream * getOutput() const
Get the output (character) stream associated with the bit stream.
Definition: bitStream.cpp:188
SPL_ArithCoder_ulong getBitCount() const
Get the number of bits read so far.
Definition: include/SPL/arithCoder.hpp:1202
int getNumContexts() const
Get the number of contexts.
Definition: include/SPL/arithCoder.hpp:1540
Output bit stream class.
Definition: bitStream.hpp:372
static void setDebugLevel(int debugLevel)
Set the debug level.
Definition: include/SPL/arithCoder.hpp:1861
OutputBitStream * getOutput()
Get the bit stream used for output.
Definition: include/SPL/arithCoder.hpp:1004
InputBitStream * getInput() const
Get the bit stream from which to read encoded data.
Definition: include/SPL/arithCoder.hpp:1192
M-ary arithmetic encoder class.
Definition: include/SPL/arithCoder.hpp:862
static void setDebugStream(std::ostream &out)
Set the stream to use for debugging output.
Definition: include/SPL/arithCoder.hpp:1217
SPL_ArithCoder_ulong getBitCount() const
Get the number of bits output so far.
Definition: include/SPL/arithCoder.hpp:1530
SPL_ArithCoder_ulong getSymCount() const
Get the number of symbols encoded so far.
Definition: include/SPL/arithCoder.hpp:1014
SPL_ArithCoder_ulong getBitCount() const
Get the number of bits read so far.
Definition: include/SPL/arithCoder.hpp:1835
SPL_ArithCoder_ulong getSymCount() const
Get the number of symbols decoded so far.
Definition: include/SPL/arithCoder.hpp:1830
static std::ostream & getDebugStream()
Get the stream used for debugging output.
Definition: include/SPL/arithCoder.hpp:1856
InputBitStream * getInput() const
Get the bit stream from which to read encoded data.
Definition: include/SPL/arithCoder.hpp:1840
int getMaxContexts() const
Get the maximum number of contexts.
Definition: include/SPL/arithCoder.hpp:1034
Binary Arithmetic Coder Context Statistics Class.
Definition: include/SPL/arithCoder.hpp:1235
SPL_ArithCoder_ulong getSymCount() const
Get the number of symbols decoded so far.
Definition: include/SPL/arithCoder.hpp:1207
Binary arithmetic encoder class.
Definition: include/SPL/arithCoder.hpp:1249
static void setDebugStream(std::ostream &out)
Set the stream to use for debugging output.
Definition: include/SPL/arithCoder.hpp:1545
int getMaxContexts() const
Get the maximum number of contexts.
Definition: include/SPL/arithCoder.hpp:1222
OutputBitStream * getOutput() const
Get the bit stream to which encoded data should be written.
Definition: include/SPL/arithCoder.hpp:1535
void setOutput(OutputBitStream *out)
Set the bit stream used for output.
Definition: include/SPL/arithCoder.hpp:1009
void setOutput(OutputBitStream *out)
Set the bit stream to which encoded data should be written.
Definition: include/SPL/arithCoder.hpp:1562
int getNumContexts() const
Get the number of contexts.
Definition: include/SPL/arithCoder.hpp:1845
static std::ostream & getDebugStream()
Get the stream used for debugging output.
Definition: include/SPL/arithCoder.hpp:1551
static void setDebugLevel(int debugLevel)
Set the debug level.
Definition: include/SPL/arithCoder.hpp:1024
static void setDebugStream(std::ostream &out)
Set the stream to be used for debugging output.
Definition: include/SPL/arithCoder.hpp:1850