Signal/Geometry Processing Library (SPL) 1.1.24 |

mCoder.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
34 // Defining this symbol will cause all symbols to be processed in
35 // bypass mode.
36 // This symbol is intended for testing purposes only and SHOULD NOT
37 // normally be defined.
38 //#define SPL_MCoder_BypassAll
39
40 #ifndef SPL_mCoder_hpp
41 #define SPL_mCoder_hpp
42
44 // Header files
46
47 #include <SPL/config.hpp>
48 #include <vector>
49 #include <iostream>
50 #include <SPL/bitStream.hpp>
51
53 //
55
56 namespace SPL {
57
63 // M-Coder: Common Encoder/Decoder Information Class
66
67 class MCoder
68 {
69 protected:
70
71 struct Context;
72
73 // Various lookup tables.
74 static const unsigned char rangeTabLPS[64][4];
75 static const unsigned char transIdxLPS[64];
76 static const unsigned char transIdxMPS[64];
77 };
78
79 // Per-context state information
80 struct MCoder::Context {
81 // The most probable symbol (MPS).
82 unsigned char valMPS_;
83
84 // The state index.
85 unsigned char pStateIdx_;
86 };
87
89 // M-Coder Encoder Class
91
96 class MEncoder : public MCoder
97 {
98 public:
99
101 // Constructors and destructor
103
108 MEncoder(int numContexts = 0, OutputBitStream* out = 0);
109
113 ~MEncoder();
114
116 // Set and get various state
118
122 void setNumContexts(int numContexts);
123
127 int getNumContexts() const;
128
132 void clearContexts();
133
137 void setOutput(OutputBitStream* out);
138
142 OutputBitStream* getOutput() const;
143
147 long getSymCount() const;
148
153 long getBitCount() const;
154
156 // Encoding
158
162 void start();
163
167 int terminate();
168
174 int encodeRegular(int contextId, int binVal);
175
180 int encodeBypass(int binVal);
181
183 // Debugging
185
189 static void setDebugLevel(int debugLevel);
190
194 static void setDebugStream(std::ostream& debugStream);
195
199 static std::ostream& getDebugStream();
200
204 void dump(std::ostream& out) const;
205
206 private:
207
208 static int debugLevel_;
209 static std::ostream* debugStream_;
210
211 int encodeTerminate(int binVal);
212 int renormE();
213 int putBit(int b);
214 int encodeFlush();
215 int writeBits(int v, int n);
216
217 // The copy constructor is declared but not defined in order to
218 // prevent copying.
219 MEncoder(const MEncoder&);
220
221 // The copy assignment operator is declared but not defined in order to
222 // prevent copy assignment.
223 MEncoder& operator=(const MEncoder&);
224
225 // The bit stream for output.
226 OutputBitStream* out_;
227
228 // The per-context state information.
229 std::vector<Context> contexts_;
230
231 // Have any bits been coded yet?
232 unsigned char firstBitFlag_;
233
234 // Lower bound of current interval.
235 unsigned short codILow_;
236
237 // Size of current interval.
238 unsigned short codIRange_;
239
240 // The number of bits to follow the next bit of output.
241 unsigned long bitsOutstanding_;
242
243 // The number of symbols output.
244 long symCount_;
245
246 // The number of bits output so far.
247 long bitCount_;
248
249 // Has a code word been started?
250 bool initialized_;
251
252 };
253
254 inline long MEncoder::getSymCount() const
255 {
256 return symCount_;
257 }
258
259 inline long MEncoder::getBitCount() const
260 {
261 return bitCount_ + bitsOutstanding_;
262 }
263
264 inline int MEncoder::getNumContexts() const {
265 return contexts_.size();
266 }
267
268 inline OutputBitStream* MEncoder::getOutput() const {
269 return out_;
270 }
271
272 inline void MEncoder::setOutput(OutputBitStream* out) {
273 out_ = out;
274 }
275
277 // M-Coder Decoder Class
279
284 class MDecoder : public MCoder {
285 public:
286
288 // Constructors and destructor
290
295 MDecoder(int numContexts = 0, InputBitStream* in = 0);
296
300 ~MDecoder();
301
303 // Set and get various state
305
309 void setNumContexts(int numContexts);
310
314 int getNumContexts() const;
315
320 void setInput(InputBitStream* in);
321
326 InputBitStream* getInput() const;
327
331 void clearContexts();
332
336 long getBitCount() const;
337
341 long getSymCount() const;
342
344 // Decoding
346
353 int start();
354
358 int terminate();
359
363 int decodeRegular(int contextId);
364
369 int decodeBypass();
370
372 // Debugging
374
379 void dump(std::ostream& out) const;
380
384 static void setDebugLevel(int debugLevel);
385
389 static void setDebugStream(std::ostream& debugStream);
390
394 static std::ostream& getDebugStream();
395
396 private:
397
398 static int debugLevel_;
399 static std::ostream* debugStream_;
400 int decodeTerminate();
401 int readBits(int n);
402 int renormD();
403
404 // The copy constructor is declared but not defined in order to
405 // prevent copying.
406 MDecoder(const MDecoder&);
407
408 // The copy assignment operator is declared but not defined in order to
409 // prevent copy assignment.
410 MDecoder& operator=(const MDecoder&);
411
412 // The bit stream for input.
413 InputBitStream* in_;
414
415 // The current code range.
416 unsigned short codIRange_;
417
418 // The current code offset.
419 unsigned short codIOffset_;
420
421 // The per-context state information.
422 std::vector<Context> contexts_;
423
424 // The number of bits read so far.
425 unsigned long bitCount_;
426
427 // The number of symbols decoded so far.
428 unsigned long symCount_;
429
430 // Has a code word been started?
431 bool initialized_;
432
433 };
434
435 inline long MDecoder::getBitCount() const {
436 return bitCount_;
437 }
438
439 inline long MDecoder::getSymCount() const {
440 return symCount_;
441 }
442
443 inline int MDecoder::getNumContexts() const {
444 return contexts_.size();
445 }
446
447 inline InputBitStream* MDecoder::getInput() const {
448 return in_;
449 }
450
451 inline void MDecoder::setInput(InputBitStream* in) {
452 in_ = in;
453 }
454
456 //
458
463 }
464
465 #endif
SPL::MEncoder::setDebugLevel
static void setDebugLevel(int debugLevel)
Set the debug level.
Definition: mCoder.cpp:385
SPL::MEncoder::getDebugStream
static std::ostream & getDebugStream()
Get the stream used for debugging output.
Definition: mCoder.cpp:395
SPL::MDecoder::setDebugLevel
static void setDebugLevel(int debugLevel)
Set the debug level.
Definition: mCoder.cpp:620
SPL::MDecoder::decodeRegular
int decodeRegular(int contextId)
Decode a symbol in the specified context.
Definition: mCoder.cpp:475
SPL::MDecoder::getInput
InputBitStream * getInput() const
Get the input bit stream (i.e., the bit stream from which encoded data is to be read).
Definition: mCoder.hpp:447
SPL::MEncoder::clearContexts
void clearContexts()
Clear the state of all of the contexts.
Definition: mCoder.cpp:179
SPL::MEncoder::dump
void dump(std::ostream &out) const
Dump the internal state of the encoder for debugging.
Definition: mCoder.cpp:374
SPL::MDecoder::start
int start()
Prepare to decode an arithmetic code word.
Definition: mCoder.cpp:456
SPL
Definition: Arcball.hpp:48
SPL::MDecoder::decodeBypass
int decodeBypass()
Decode a symbol in bypass mode (i.e., assuming both symbols are equiprobable).
Definition: mCoder.cpp:512
SPL::MEncoder
The M-Coder (binary) arithmetic encoder class.
Definition: mCoder.hpp:96
SPL::MDecoder::terminate
int terminate()
Terminate the arithmetic code word.
Definition: mCoder.cpp:541
SPL::MEncoder::encodeBypass
int encodeBypass(int binVal)
Encode a symbol in bypass mode (i.e., assuming that both symbols are equiprobable).
Definition: mCoder.cpp:235
SPL::MEncoder::setDebugStream
static void setDebugStream(std::ostream &debugStream)
Set the stream for debugging output.
Definition: mCoder.cpp:390
SPL::MDecoder::getBitCount
long getBitCount() const
Get the number of bits read so far.
Definition: mCoder.hpp:435
SPL::MEncoder::setNumContexts
void setNumContexts(int numContexts)
Set the number of contexts.
Definition: mCoder.cpp:172
SPL::MDecoder::setDebugStream
static void setDebugStream(std::ostream &debugStream)
Set the stream to use for debugging output.
Definition: mCoder.cpp:625
SPL::OutputBitStream
Output bit stream class.
Definition: bitStream.hpp:372
SPL::MDecoder
The M-Coder (binary) arithmetic decoder class.
Definition: mCoder.hpp:284
SPL::MDecoder::~MDecoder
~MDecoder()
Destroy a decoder.
Definition: mCoder.cpp:433
SPL::MDecoder::getSymCount
long getSymCount() const
Get the number of symbols decoded so far.
Definition: mCoder.hpp:439
SPL::MEncoder::setOutput
void setOutput(OutputBitStream *out)
Set the bit stream to use for output.
Definition: mCoder.hpp:272
SPL::MDecoder::getNumContexts
int getNumContexts() const
Get the number of contexts.
Definition: mCoder.hpp:443
SPL::MEncoder::getOutput
OutputBitStream * getOutput() const
Get the bit stream being used for output.
Definition: mCoder.hpp:268
SPL::MDecoder::setNumContexts
void setNumContexts(int numContexts)
Set the number of contexts.
Definition: mCoder.cpp:441
SPL::MDecoder::setInput
void setInput(InputBitStream *in)
Set the input bit stream (i.e., the bit stream from which encoded data is to be read).
Definition: mCoder.hpp:451
SPL::MEncoder::~MEncoder
~MEncoder()
Destroy an encoder.
Definition: mCoder.cpp:164
SPL::MEncoder::getSymCount
long getSymCount() const
Get the number of symbols that have been encoded so far.
Definition: mCoder.hpp:254
SPL::MEncoder::encodeRegular
int encodeRegular(int contextId, int binVal)
Encode a symbol in the specified context.
Definition: mCoder.cpp:202
SPL::MEncoder::MEncoder
MEncoder(int numContexts=0, OutputBitStream *out=0)
Create an encoder with a specified number of contexts that sends output to a given bit stream...
Definition: mCoder.cpp:149
SPL::MEncoder::getNumContexts
int getNumContexts() const
Get the number of contexts.
Definition: mCoder.hpp:264
SPL::MDecoder::dump
void dump(std::ostream &out) const
Dump the internal state information for the decoder to a stream (for debugging).
Definition: mCoder.cpp:608
SPL::InputBitStream
Input bit stream class.
Definition: bitStream.hpp:209
SPL::MDecoder::clearContexts
void clearContexts()
Clear the state of all of the contexts.
Definition: mCoder.cpp:448
SPL::MEncoder::getBitCount
long getBitCount() const
Get the number of bits (of encoded data) that have been output to the underlying bit stream so far...
Definition: mCoder.hpp:259
bitStream.hpp
Bit Stream Classes.
SPL::MDecoder::MDecoder
MDecoder(int numContexts=0, InputBitStream *in=0)
Create a decoder with the specified number of contexts that reads input from the given bit stream...
Definition: mCoder.cpp:422
SPL::MEncoder::start
void start()
Start the arithmetic code word.
Definition: mCoder.cpp:187
SPL::MEncoder::terminate
int terminate()
Terminate the arithmetic code word.
Definition: mCoder.cpp:268
SPL::MDecoder::getDebugStream
static std::ostream & getDebugStream()
Get the stream used for debugging output.
Definition: mCoder.cpp:630