Signal/Geometry Processing Library (SPL)  1.1.24
arithCoder.hpp
Go to the documentation of this file.
1 // Copyright (c) 2013 Michael D. Adams
2 // All rights reserved.
3 
4 // __START_OF_LICENSE__
5 //
6 // Copyright (c) 2015 Michael D. Adams
7 // All rights reserved.
8 //
9 // This file is part of the Signal Processing Library (SPL).
10 //
11 // This program is free software; you can redistribute it and/or
12 // modify it under the terms of the GNU General Public License as
13 // published by the Free Software Foundation; either version 3,
14 // or (at your option) any later version.
15 //
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public
22 // License along with this program; see the file LICENSE. If not,
23 // see <http://www.gnu.org/licenses/>.
24 //
25 // __END_OF_LICENSE__
26 
37 #ifndef SPL_arithCoder_hpp
38 #define SPL_arithCoder_hpp
39 
41 // Header files
43 
44 #include <SPL/config.hpp>
45 #include <cassert>
46 #include <fstream>
47 #include <iostream>
48 #include <vector>
49 #include <SPL/bitStream.hpp>
50 
52 //
54 
55 // In the GIC code base, the following symbol was defined.
56 // The following symbol should probably not normally be defined.
57 #define SPL_ArithCoder_CodeSize32
58 
59 #define SPL_ArithCoder_Entropy
60 
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
68 #else
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
75 #endif
76 
77 // NOTE: This probably ought to be 255 (one less than a power of 2).
78 #define SPL_ArithCoder_DefaultMaxFreq 256
79 
80 // These types need to be revisited. Maybe they should be changed.
81 // For now, they have been flagged in the code by these special names.
82 #define SPL_ArithCoder_long long
83 #define SPL_ArithCoder_ulong unsigned long
84 
85 namespace SPL {
86 
92 // Arithmetic Coder Core: Common Encoder/Decoder Class
95 
97 
106 class ArithCoder
107 {
108 public:
109 
111  typedef SPL_ArithCoder_FreqType Freq;
112 
114  static const int FreqBits = SPL_ArithCoder_NumFreqBits;
115 
117  static const Freq MaxFreq = (1ULL << FreqBits) - 1;
118 
119 protected:
120 
121  // The type for a code word.
122  typedef SPL_ArithCoder_CodeType Code;
123 
124  // A type providing sufficient dynamic range for code word computations.
125  typedef SPL_ArithCoder_BigType BigCode;
126 
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;
133 };
134 
136 
138 // M-ary Arithmetic Coder Core: Encoder Class
140 
142 
151 class ArithEncoder : public ArithCoder
152 {
153 public:
154 
156  // Constructors and destructor
158 
168  ArithEncoder(OutputBitStream* out = nullptr);
169 
176  ~ArithEncoder();
177 
179  // Set and get various state
181 
192  void setOutput(OutputBitStream* out);
193 
205  OutputBitStream* getOutput() const;
206 
217  SPL_ArithCoder_ulong getSymCount() const;
218 
230  SPL_ArithCoder_ulong getBitCount() const;
231 
233  // Encoding
235 
246  int start();
247 
257  int terminate();
258 
270  int encode(Freq lowFreq, Freq highFreq, Freq totalFreq);
271 
273  // Debugging
275 
279  static void setDebugLevel(int debugLevel);
280 
284  static void setDebugStream(std::ostream& out);
285 
289  static std::ostream& getDebugStream();
290 
295  void dump(std::ostream& out) const;
296 
297 private:
298 
299  // The copy constructor is declared but not defined in order to
300  // prevent copying.
301  ArithEncoder(const ArithEncoder&);
302 
303  // The copy constructor is declared but not defined in order to
304  // prevent copying.
305  ArithEncoder& operator=(const ArithEncoder&);
306 
307  // The debug level.
308  static int debugLevel_;
309 
310  // The stream to use for debugging output.
311  static std::ostream* debugStream_;
312 
313  inline int renorm();
314 
315  int bitPlusFollow(int bit);
316 
317  // The bit stream to which the encoder output is to be written.
318  OutputBitStream* out_;
319 
320  // The low end of the current interval.
321  Code low_;
322 
323  // The high end of the current interval.
324  Code high_;
325 
326  // The number of bits yet to be output.
327  unsigned long bitsOutstanding_;
328 
329  // The number of bits output so far.
330  SPL_ArithCoder_ulong bitCount_;
331 
332  // The number of symbols encoded so far.
333  SPL_ArithCoder_ulong symCount_;
334 };
335 
336 inline void ArithEncoder::setOutput(OutputBitStream* out)
337 {
338  out_ = out;
339 }
340 
341 inline OutputBitStream* ArithEncoder::getOutput() const
342 {
343  return out_;
344 }
345 
346 inline SPL_ArithCoder_ulong ArithEncoder::getSymCount() const
347 {
348  return symCount_;
349 }
350 
351 inline SPL_ArithCoder_ulong ArithEncoder::getBitCount() const
352 {
353  return bitCount_ + bitsOutstanding_;
354 }
355 
356 inline void ArithEncoder::setDebugLevel(int debugLevel)
357 {
358  debugLevel_ = debugLevel;
359 }
360 
361 inline void ArithEncoder::setDebugStream(std::ostream& out)
362 {
363  debugStream_ = &out;
364 }
365 
366 inline std::ostream& ArithEncoder::getDebugStream()
367 {
368  return debugStream_ ? (*debugStream_) : std::cerr;
369 }
370 
372 
374 // M-ary Arithmetic Coder Core: Decoder Class
376 
378 
387 class ArithDecoder : public ArithCoder
388 {
389 public:
390 
392  // Constructors and destructor
394 
405  ArithDecoder(InputBitStream* in = nullptr);
406 
413  ~ArithDecoder();
414 
416  // Set and get various state
418 
429  InputBitStream* getInput() const;
430 
439  void setInput(InputBitStream* in);
440 
451  SPL_ArithCoder_ulong getBitCount() const;
452 
462  SPL_ArithCoder_ulong getSymCount() const;
463 
465  // Decoding
467 
478  int start();
479 
489  int terminate();
490 
505  int decode(Freq totalFreq, Freq& target);
506 
522  int adjust(Freq lowFreq, Freq highFreq, Freq totalFreq);
523 
525  // Debugging
527 
531  static void setDebugLevel(int debugLevel);
532 
536  static void setDebugStream(std::ostream& out);
537 
541  std::ostream& getDebugStream();
542 
547  void dump(std::ostream& out) const;
548 
549 private:
550 
551  // The debug level.
552  static int debugLevel_;
553 
554  // The stream to be used for debugging output.
555  static std::ostream* debugStream_;
556 
557  // The copy constructor is declared but not defined in order to
558  // prevent copying.
559  ArithDecoder(const ArithDecoder&);
560 
561  // The copy constructor is declared but not defined in order to
562  // prevent copying.
563  ArithDecoder& operator=(const ArithDecoder&);
564 
565  inline int renorm();
566 
567  // The bit stream from which to read data to decode.
568  InputBitStream* in_;
569 
570  // The code value.
571  Code value_;
572 
573  // The low end of the current interval.
574  Code low_;
575 
576  // The high end of the current interval.
577  Code high_;
578 
579  // The number of symbols that have been decoded so far.
580  SPL_ArithCoder_ulong symCount_;
581 
582  // The number of bits that have been read so far.
583  SPL_ArithCoder_ulong bitCount_;
584 };
585 
586 inline InputBitStream* ArithDecoder::getInput() const
587 {
588  return in_;
589 }
590 
591 inline SPL_ArithCoder_ulong ArithDecoder::getSymCount() const
592 {
593  return symCount_;
594 }
595 
596 inline SPL_ArithCoder_ulong ArithDecoder::getBitCount() const
597 {
598  return bitCount_;
599 }
600 
601 inline void ArithDecoder::setDebugLevel(int debugLevel)
602 {
603  debugLevel_ = debugLevel;
604 }
605 
606 inline void ArithDecoder::setDebugStream(std::ostream& out)
607 {
608  debugStream_ = &out;
609 }
610 
611 inline std::ostream& ArithDecoder::getDebugStream()
612 {
613  return debugStream_ ? (*debugStream_) : std::cerr;
614 }
615 
616 inline void ArithDecoder::setInput(InputBitStream* in)
617 {
618  in_ = in;
619 }
620 
622 
624 // Simple Probability Model
626 
628 
633 class ArithCoderModel
634 {
635 public:
636 
638  // Constructors and destructor
640 
645  ArithCoderModel(int numSyms, ArithCoder::Freq maxFreq);
646 
650  ~ArithCoderModel();
651 
653  // Lookup and update
655 
661  void lookup(int sym, ArithCoder::Freq& lowFreq,
662  ArithCoder::Freq& highFreq, ArithCoder::Freq& totalFreq) const;
663 
669  void invLookup(ArithCoder::Freq targetFreq, int& sym,
670  ArithCoder::Freq& lowFreq, ArithCoder::Freq& highFreq,
671  ArithCoder::Freq& totalFreq) const;
672 
677  void update(int sym);
678 
680  // Set and get various state
682 
686  int getNumSyms() const;
687 
691  void setAdaptive(bool adaptive);
692 
696  bool getAdaptive() const;
697 
702  SPL_ArithCoder_ulong getSymCount(int sym) const;
703 
707  SPL_ArithCoder_ulong getTotalSymCount() const;
708 
710  // Set and get probabilities
712 
716  ArithCoder::Freq getTotalFreq() const;
717 
718  // NEW: 2015-10-23
719  ArithCoder::Freq getMaxFreq() const;
720 
724  void setProbs(const std::vector<ArithCoder::Freq>& freqs);
725 
729  void getProbs(std::vector<ArithCoder::Freq>& freqs);
730 
732  // Debugging
734 
738  static void setDebugLevel(int debugLevel);
739 
743  static void setDebugStream(std::ostream& out);
744 
748  static std::ostream& getDebugStream();
749 
753  void dump(std::ostream& out) const;
754 
755 private:
756 
757  // The copy constructor is declared but not defined in order to
758  // prevent copying.
759  ArithCoderModel(const ArithCoderModel&);
760 
761  // The copy constructor is declared but not defined in order to
762  // prevent copying.
763  ArithCoderModel& operator=(const ArithCoderModel&);
764 
765  // The debug level.
766  static int debugLevel_;
767 
768  // The stream to use for debugging output.
769  static std::ostream* debugStream_;
770 
771  // Lookup table that maps from symbols to indices.
772  // The size of the vector is the number of symbols.
773  std::vector<int> symToIdx_;
774 
775  // Lookup table that maps from indices to symbols.
776  // The size of the vector is one greater than the number of symbols.
777  std::vector<int> idxToSym_;
778 
779  // The symbol frequencies (ordered by symbol index).
780  // The size of the vector is one greater than the number of symbols.
781  // The element with index zero is always zero?
782  std::vector<ArithCoder::Freq> freqs_;
783 
784  // The cumulative frequencies.
785  // The size of the vector is one greater than the number of symbols.
786  std::vector<ArithCoder::Freq> cumFreqs_;
787 
788  // The number of times each symbol has been seen so far.
789  std::vector<SPL_ArithCoder_ulong> symCnts_;
790 
791  // The total number of symbols that have been seen so far.
792  SPL_ArithCoder_ulong totalSymCnt_;
793 
794  // The normalizing denominator for frequencies.
795  ArithCoder::Freq maxFreq_;
796 
797  // The adaptivity mode.
798  bool adaptive_;
799 };
800 
801 inline SPL_ArithCoder_ulong ArithCoderModel::getSymCount(int sym) const
802 {
803  return symCnts_[sym];
804 }
805 
806 inline SPL_ArithCoder_ulong ArithCoderModel::getTotalSymCount() const
807 {
808  return totalSymCnt_;
809 }
810 
811 inline void ArithCoderModel::setAdaptive(bool adaptive)
812 {
813  adaptive_ = adaptive;
814 }
815 
816 inline bool ArithCoderModel::getAdaptive() const
817 {
818  return adaptive_;
819 }
820 
821 // NEW: 2015-10-23
822 inline ArithCoder::Freq ArithCoderModel::getMaxFreq() const
823 {
824  return maxFreq_;
825 }
826 
827 inline ArithCoder::Freq ArithCoderModel::getTotalFreq() const
828 {
829  return cumFreqs_[0];
830 }
831 
832 inline int ArithCoderModel::getNumSyms() const
833 {
834  return symToIdx_.size();
835 }
836 
837 inline void ArithCoderModel::setDebugLevel(int debugLevel)
838 {
839  debugLevel_ = debugLevel;
840 }
841 
842 inline void ArithCoderModel::setDebugStream(std::ostream& out)
843 {
844  debugStream_ = &out;
845 }
846 
847 inline std::ostream& ArithCoderModel::getDebugStream()
848 {
849  return debugStream_ ? (*debugStream_) : std::cerr;
850 }
851 
853 
855 // M-ary Arithmetic Coder: Encoder Class
857 
863 {
864 public:
865 
867  // Constructors and destructor
869 
874  MultiArithEncoder(int maxContexts, OutputBitStream* out = nullptr);
875 
880 
882  //
884 
889 
893  void setOutput(OutputBitStream* out);
894 
898  SPL_ArithCoder_ulong getSymCount() const;
899 
904  SPL_ArithCoder_ulong getBitCount() const;
905 
909  int getMaxContexts() const;
910 
915  void setContext(int contextId, int numSyms);
916 
921  void setContext(int contextId, const std::vector<ArithCoder::Freq>&
922  symFreqs, bool adaptive);
923 
925  // Encoding
927 
933  int start();
934 
938  int terminate();
939 
943  int encodeRegular(int contextId, int sym);
944 
949  int encodeBypass(int numSyms, int sym);
950 
952  // Debugging
954 
958  static void setDebugLevel(int debugLevel);
959 
963  static void setDebugStream(std::ostream& out);
964 
968  static std::ostream& getDebugStream();
969 
974  void dump(std::ostream& out) const;
975 
976 private:
977 
978  // The debug level.
979  static int debugLevel_;
980 
981  // The stream to use for debugging output.
982  static std::ostream* debugStream_;
983 
984  // The copy constructor is declared but not defined in order to
985  // prevent copying.
987 
988  // The copy constructor is declared but not defined in order to
989  // prevent copying.
990  MultiArithEncoder& operator=(const MultiArithEncoder&);
991 
992  // The arithmetic coding engine.
993  ArithEncoder enc_;
994 
995  // The context information.
996  std::vector<ArithCoderModel*> contexts_;
997 };
998 
999 inline std::ostream& MultiArithEncoder::getDebugStream()
1000 {
1001  return (debugStream_) ? (*debugStream_) : std::cerr;
1002 }
1003 
1005 {
1006  return enc_.getOutput();
1007 }
1008 
1010 {
1011  enc_.setOutput(out);
1012 }
1013 
1014 inline SPL_ArithCoder_ulong MultiArithEncoder::getSymCount() const
1015 {
1016  return enc_.getSymCount();
1017 }
1018 
1019 inline SPL_ArithCoder_ulong MultiArithEncoder::getBitCount() const
1020 {
1021  return enc_.getBitCount();
1022 }
1023 
1024 inline void MultiArithEncoder::setDebugLevel(int debugLevel)
1025 {
1026  debugLevel_ = debugLevel;
1027 }
1028 
1029 inline void MultiArithEncoder::setDebugStream(std::ostream& debugStream)
1030 {
1031  debugStream_ = &debugStream;
1032 }
1033 
1035 {
1036  return contexts_.size();
1037 }
1038 
1040 // M-ary Arithmetic Coder: Decoder Class
1042 
1048 {
1049 public:
1050 
1052  // Constructors and destructor
1054 
1059  MultiArithDecoder(int maxContexts, InputBitStream* in = nullptr);
1060 
1065 
1067  // Set and get various state
1069 
1073  InputBitStream* getInput() const;
1074 
1078  void setInput(InputBitStream* in);
1079 
1083  SPL_ArithCoder_ulong getBitCount() const;
1084 
1088  SPL_ArithCoder_ulong getSymCount() const;
1089 
1091  // Context management
1093 
1097  int getMaxContexts() const;
1098 
1103  void setContext(int contextId, int numSyms);
1104 
1109  void setContext(int contextId, const std::vector<ArithCoder::Freq>&
1110  symFreqs, bool adaptive);
1111 
1113  // Decoding
1115 
1121  int start();
1122 
1126  int terminate();
1127 
1131  int decodeRegular(int contextId);
1132 
1137  int decodeBypass(int numSyms);
1138 
1140  // Debugging
1142 
1146  static void setDebugLevel(int debugLevel);
1147 
1151  static void setDebugStream(std::ostream& out);
1152 
1156  static std::ostream& getDebugStream();
1157 
1162  void dump(std::ostream& out) const;
1163 
1164 private:
1165 
1166  // The debug level.
1167  static int debugLevel_;
1168 
1169  // The stream to use for debugging output.
1170  static std::ostream* debugStream_;
1171 
1172  // The copy constructor is declared but not defined in order to
1173  // prevent copying.
1175 
1176  // The copy constructor is declared but not defined in order to
1177  // prevent copying.
1178  MultiArithDecoder& operator=(const MultiArithDecoder&);
1179 
1180  // The arithmetic decoding engine.
1181  ArithDecoder dec_;
1182 
1183  // The context information.
1184  std::vector<ArithCoderModel*> contexts_;
1185 };
1186 
1188 {
1189  return debugStream_ ? (*debugStream_) : std::cerr;
1190 }
1191 
1193 {
1194  return dec_.getInput();
1195 }
1196 
1198 {
1199  dec_.setInput(in);
1200 }
1201 
1202 inline SPL_ArithCoder_ulong MultiArithDecoder::getBitCount() const
1203 {
1204  return dec_.getBitCount();
1205 }
1206 
1207 inline SPL_ArithCoder_ulong MultiArithDecoder::getSymCount() const
1208 {
1209  return dec_.getSymCount();
1210 }
1211 
1212 inline void MultiArithDecoder::setDebugLevel(int debugLevel)
1213 {
1214  debugLevel_ = debugLevel;
1215 }
1216 
1217 inline void MultiArithDecoder::setDebugStream(std::ostream& debugStream)
1218 {
1219  debugStream_ = &debugStream;
1220 }
1221 
1223 {
1224  return contexts_.size();
1225 }
1226 
1228 // Binary Arithmetic Coder
1230 
1236 {
1237  SPL_ArithCoder_ulong numOnes;
1238  SPL_ArithCoder_ulong numSyms;
1239 };
1240 
1242 // Binary Arithmetic Encoder
1244 
1250 {
1251 public:
1252 
1253  using Freq = ArithCoder::Freq;
1254 
1256  // Constructors and destructor
1258 
1271  BinArithEncoder(int numContexts, OutputBitStream* out = nullptr);
1272 
1279  ~BinArithEncoder();
1280 
1282  // Set and get various state
1284 
1295  int getNumContexts() const;
1296 
1307  SPL_ArithCoder_ulong getSymCount() const;
1308 
1319  SPL_ArithCoder_ulong getBitCount() const;
1320 
1330  void setOutput(OutputBitStream* out);
1331 
1342  OutputBitStream* getOutput() const;
1343 
1345  // Context management
1347 
1351  void getContextStats(std::vector<BinArithCoderContextStat>& stats) const;
1352 
1357  void setContextState(int contextId, ArithCoder::Freq oneFreq,
1358  ArithCoder::Freq totalFreq, ArithCoder::Freq maxFreq, bool adaptive);
1359 
1379  void getContextState(int contextId, ArithCoder::Freq& oneFreq,
1380  ArithCoder::Freq& totalFreq, ArithCoder::Freq& maxFreq, bool& adaptive);
1381 
1383  // Encoding
1385 
1396  int start();
1397 
1414  int encodeRegular(int contextId, int binVal);
1415 
1431  int encodeBypass(int binVal);
1432 
1443  int terminate();
1444 
1446  // Debugging
1448 
1453  void dump(std::ostream& out) const;
1454 
1459  void dumpModels(std::ostream& out) const;
1460 
1464  static void setDebugLevel(int debugLevel);
1465 
1469  static void setDebugStream(std::ostream& out);
1470 
1474  static std::ostream& getDebugStream();
1475 
1476 #if defined(SPL_ArithCoder_Entropy)
1477  long double getEntropy() const;
1478  void clearEntropy();
1479 #endif
1480 
1481 private:
1482 
1483  // The debug level.
1484  static int debugLevel_;
1485 
1486  // The stream to use for debugging output.
1487  static std::ostream* debugStream_;
1488 
1489  // The copy constructor is declared but not defined in order to
1490  // prevent copying.
1492 
1493  // The copy constructor is declared but not defined in order to
1494  // prevent copying.
1495  BinArithEncoder& operator=(const BinArithEncoder&);
1496 
1497  // The arithmetic coding engine.
1498  ArithEncoder enc_;
1499 
1500  // The context information.
1501  std::vector<ArithCoderModel*> models_;
1502 
1503  // The number of one symbols encoded in bypass mode.
1504  SPL_ArithCoder_ulong bypassOneCnt_;
1505 
1506  // The total number of symbols encoded in bypass mode.
1507  SPL_ArithCoder_ulong bypassSymCnt_;
1508 
1509 #if defined(SPL_ArithCoder_Entropy)
1510  long double entropy_;
1511 #endif
1512 };
1513 
1514 #if defined(SPL_ArithCoder_Entropy)
1515 inline long double BinArithEncoder::getEntropy() const
1516 {
1517  return entropy_;
1518 }
1519 inline void BinArithEncoder::clearEntropy()
1520 {
1521  entropy_ = 0;
1522 }
1523 #endif
1524 
1525 inline SPL_ArithCoder_ulong BinArithEncoder::getSymCount() const
1526 {
1527  return enc_.getSymCount();
1528 }
1529 
1530 inline SPL_ArithCoder_ulong BinArithEncoder::getBitCount() const
1531 {
1532  return enc_.getBitCount();
1533 }
1534 
1536 {
1537  return enc_.getOutput();
1538 }
1539 
1541 {
1542  return models_.size();
1543 }
1544 
1545 inline void BinArithEncoder::setDebugStream(std::ostream& out)
1546 {
1547  ArithEncoder::setDebugStream(out);
1548  debugStream_ = &out;
1549 }
1550 
1551 inline std::ostream& BinArithEncoder::getDebugStream()
1552 {
1553  return debugStream_ ? (*debugStream_) : std::cerr;
1554 }
1555 
1556 inline void BinArithEncoder::setDebugLevel(int debugLevel)
1557 {
1558  ArithEncoder::setDebugLevel(debugLevel >> 8);
1559  debugLevel_ = debugLevel;
1560 }
1561 
1563 {
1564  enc_.setOutput(out);
1565 }
1566 
1568 // Binary Arithmetic Coder: Decoder Class
1570 
1576 {
1577 public:
1578 
1579  using Freq = ArithCoder::Freq;
1580 
1582  // Constructors and destructor
1584 
1597  BinArithDecoder(int numContexts, InputBitStream* in = nullptr);
1598 
1605  ~BinArithDecoder();
1606 
1608  // Set and get various state
1610 
1621  SPL_ArithCoder_ulong getSymCount() const;
1622 
1633  SPL_ArithCoder_ulong getBitCount() const;
1634 
1644  void setInput(InputBitStream* in);
1645 
1655  InputBitStream* getInput() const;
1656 
1658  // Context management
1660 
1671  int getNumContexts() const;
1672 
1677  void setContextState(int contextId, ArithCoder::Freq oneFreq,
1678  ArithCoder::Freq totalFreq, ArithCoder::Freq maxFreq, bool adaptive);
1679 
1699  void getContextState(int contextId, ArithCoder::Freq& oneFreq,
1700  ArithCoder::Freq& totalFreq, ArithCoder::Freq& maxFreq, bool& adaptive);
1701 
1703  // Decoding
1705 
1716  int start();
1717 
1728  int terminate();
1729 
1745  int decodeRegular(int contextId);
1746 
1759  int decodeBypass();
1760 
1762  // Debugging
1764 
1769  void dump(std::ostream& out) const;
1770 
1774  static void setDebugLevel(int debugLevel);
1775 
1779  static void setDebugStream(std::ostream& out);
1780 
1784  static std::ostream& getDebugStream();
1785 
1786 #if defined(SPL_ArithCoder_Entropy)
1787  long double getEntropy() const;
1788  void clearEntropy();
1789 #endif
1790 
1791 private:
1792 
1793  // The debug level.
1794  static int debugLevel_;
1795 
1796  // The stream used for debugging output.
1797  static std::ostream* debugStream_;
1798 
1799  // The copy constructor is declared but not defined in order to
1800  // prevent copying.
1802 
1803  // The copy constructor is declared but not defined in order to
1804  // prevent copying.
1805  BinArithDecoder& operator=(const BinArithDecoder&);
1806 
1807  // The arithmetic decoding engine.
1808  ArithDecoder dec_;
1809 
1810  // The context information.
1811  std::vector<ArithCoderModel*> models_;
1812 
1813 #if defined(SPL_ArithCoder_Entropy)
1814  long double entropy_;
1815 #endif
1816 
1817 };
1818 
1819 #if defined(SPL_ArithCoder_Entropy)
1820 inline long double BinArithDecoder::getEntropy() const
1821 {
1822  return entropy_;
1823 }
1824 inline void BinArithDecoder::clearEntropy()
1825 {
1826  entropy_ = 0;
1827 }
1828 #endif
1829 
1830 inline SPL_ArithCoder_ulong BinArithDecoder::getSymCount() const
1831 {
1832  return dec_.getSymCount();
1833 }
1834 
1835 inline SPL_ArithCoder_ulong BinArithDecoder::getBitCount() const
1836 {
1837  return dec_.getBitCount();
1838 }
1839 
1841 {
1842  return dec_.getInput();
1843 }
1844 
1846 {
1847  return models_.size();
1848 }
1849 
1850 inline void BinArithDecoder::setDebugStream(std::ostream& out)
1851 {
1852  ArithDecoder::setDebugStream(out);
1853  debugStream_ = &out;
1854 }
1855 
1856 inline std::ostream& BinArithDecoder::getDebugStream()
1857 {
1858  return debugStream_ ? (*debugStream_) : std::cerr;
1859 }
1860 
1861 inline void BinArithDecoder::setDebugLevel(int debugLevel)
1862 {
1863  ArithDecoder::setDebugLevel(debugLevel >> 8);
1864  debugLevel_ = debugLevel;
1865 }
1866 
1868 {
1869  dec_.setInput(in);
1870 }
1871 
1873 //
1875 
1880 }
1881 
1882 #endif
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
Input bit stream class.
Definition: bitStream.hpp:209
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
Bit Stream Classes.
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