46 #ifndef _ARITH_CODEC_H_ 47 #define _ARITH_CODEC_H_ 90 static const unsigned int lut[256];
123 void EncodeSymbol(
const bool symbol,
const int context_num);
125 void EncodeUInt(
const unsigned int value,
const int bin1,
const int max_bin);
127 void EncodeSInt(
const int value,
const int bin1,
const int max_bin);
132 int ByteCount()
const;
138 void InitDecoder(
int num_bytes);
141 bool DecodeSymbol(
int context_num );
143 unsigned int DecodeUInt(
const int bin1,
const int max_bin);
145 int DecodeSInt(
const int bin1,
const int max_bin);
163 void ReadAllData(
int num_bytes);
166 inline bool InputBit();
209 Context& ctx = m_context_list[context_num];
212 const unsigned int count = m_code - m_low_code ;
213 const unsigned int range_x_prob = ( m_range* ctx.
GetScaledProb0())>>16;
214 const bool symbol = ( count >= range_x_prob );
219 m_low_code += range_x_prob;
220 m_range -= range_x_prob;
224 m_range = range_x_prob;
230 while ( m_range<=0x4000 )
232 if( ( (m_low_code+m_range-1)^m_low_code)>=0x8000 )
239 m_low_code ^= 0x4000;
245 m_low_code &= 0xFFFF;
249 m_code += InputBit();
258 const int info_ctx = (max_bin+1);
260 unsigned int value = 1;
261 while (!DecodeSymbol(bin)) {
263 if (DecodeSymbol(info_ctx)) value+=1;
264 if (bin<max_bin) bin+=1;
272 const int magnitude = DecodeUInt(bin1, max_bin);
274 if (DecodeSymbol(max_bin+2)) value=-magnitude;
275 else value=magnitude;
285 Context& ctx = m_context_list[context_num];
287 const unsigned int range_x_prob = ( m_range* ctx.
GetScaledProb0())>>16;
291 m_low_code += range_x_prob;
292 m_range -= range_x_prob;
296 m_range = range_x_prob;
302 while ( m_range <= 0x4000 )
304 if ( ( (m_low_code+m_range-1)^m_low_code)>=0x8000 )
310 m_low_code ^= 0x4000;
317 m_byteio->WriteBit( m_low_code & 0x8000);
318 for (; m_underflow > 0; m_underflow-- )
319 m_byteio->WriteBit(~m_low_code & 0x8000);
327 m_low_code &= 0xFFFF;
333 const int bin1,
const int max_bin) {
334 const int value = (the_int+1);
335 const int info_ctx = (max_bin+1);
340 while (value>max_value) {
346 bool stop = (top_bit==1);
347 EncodeSymbol(stop, bin);
350 EncodeSymbol( (value&top_bit), info_ctx);
351 if ( bin < max_bin) bin+=1;
353 EncodeSymbol(stop, bin);
358 const int bin1,
const int max_bin) {
359 EncodeUInt(std::abs(value), bin1, max_bin);
361 EncodeSymbol( (value < 0), max_bin+2 );
404 int Compress(T & in_data);
415 void Decompress(T & out_data,
const int num_bytes);
423 virtual void DoWorkCode(T & in_data) = 0;
427 virtual void DoWorkDecode(T & out_data)=0;
ArithCodec(ByteIO *p_byteio, size_t number_of_contexts)
Constructor for encoding.
Definition: arith_codec.h:434
unsigned int m_code
The present input code.
Definition: arith_codec.h:197
void InitEncoder()
Initialises the Encoder.
unsigned int GetScaledProb0() const
Returns estimate of probability of 0 (false) scaled to 2**16.
Definition: arith_codec.h:79
unsigned int DecodeUInt(const int bin1, const int max_bin)
Definition: arith_codec.h:257
Abstract binary arithmetic coding class.
Definition: arith_codec.h:374
char * m_decode_data_ptr
A pointer to the data for reading in.
Definition: arith_codec.h:188
bool DecodeSymbol(int context_num)
Decodes a symbol given a context number.
Definition: arith_codec.h:202
unsigned int m_low_code
Start of the current code range.
Definition: arith_codec.h:174
bool InputBit()
Read in a bit of data.
Definition: arith_codec.h:455
virtual ~ArithCodec()
Destructor.
Definition: arith_codec.h:393
void InitDecoder(int num_bytes)
Initialise the Decoder.
int m_underflow
Number of underflow bits.
Definition: arith_codec.h:185
int DecodeSInt(const int bin1, const int max_bin)
Definition: arith_codec.h:270
void Decompress(T &out_data, const int num_bytes)
Decompresses the bitstream and writes into the output.
Definition: arith_codec.h:449
int Compress(T &in_data)
Compresses the input and returns the number of bits written.
Definition: arith_codec.h:440
virtual void DoWorkDecode(T &out_data)=0
virtual decode-only functions
Definition: arith_codec.h:65
std::vector< Context > m_context_list
List of contexts.
Definition: arith_codec.h:148
unsigned int m_range
Length of the current code range.
Definition: arith_codec.h:177
Context()
Default Constructor.
Definition: arith_codec.h:93
void EncodeSInt(const int value, const int bin1, const int max_bin)
Definition: arith_codec.h:357
int m_input_bits_left
The index of the bit of the byte being read.
Definition: arith_codec.h:194
virtual void DoWorkCode(T &in_data)=0
Does the work of actually coding the data.
void FlushEncoder()
flushes the output of the encoder.
void EncodeUInt(const unsigned int value, const int bin1, const int max_bin)
Definition: arith_codec.h:332
Definition of class SequenceHeaderByteIO.
Definition: accessunit_byteio.h:51
int m_prob0
Definition: arith_codec.h:89
char * m_data_ptr
A point to the byte currently being read.
Definition: arith_codec.h:191
Class ByteIO - top-level class for reading/writing bytes to a stream.
Definition: byteio.h:72
void EncodeSymbol(const bool symbol, const int context_num)
encodes a symbol and writes to output
Definition: arith_codec.h:280
void Update(bool symbol)
Updates context counts.
Definition: arith_codec.h:82
Definition: arith_codec.h:95
static const unsigned int lut[256]
Definition: arith_codec.h:90
ByteIO * m_byteio
Input/output stream of Dirac-format bytes.
Definition: arith_codec.h:180
unsigned int m_scount
Definition: arith_codec.h:171