Next: Static Arithmetic Coder ( Up: Lossless Compression Previous: Lossless Compression   Contents   Index

# Adaptive Arithmetic Coder ( A0Coder )

Definition

An instance C of A0Coder is an adaptive arithmetic coder of order zero (which explains the ``0'' in the class name). When the coder encodes a stream it counts the frequencies of the characters that have occured so far in order to obtain a model of the probabilities for the future characters. The model is called order zero because it does not take into account that the probability of a character c may depend on the characters that precede it. (E.g., in an English text the probability that the next character is a ``u'' is very high if the current character is a ``q''. The PPMIICoder in Section PPMIICoder takes this into account.) The advantage of arithmetic coding [92] is that a character in the source stream can be encoded by a fractional number of bits in the target stream, which leads to good results in practice.
The coder is called adaptive because the model evolves gradually while the coder scans its input. In contrast to that a static coder reads the whole input once before it generates a model. During the actual encoding (in a second scan) the model remains fixed. The adaptive coder resets and/or scales its model when the number of characters read since the last reset/scaling exceeds a certain threshold. Both thresholds can be controlled by the user.

#include < LEDA/coding/arithmetic_coding.h >

Creation

 A0Coder C(streambuf* src_stream = 0, streambuf* tgt_stream = 0, bool own_streams = false) creates an instance C which uses the given source and target streams. If own_streams is set, then C is responsible for the destruction of the streams, otherwise the pointers src_stream and tgt_stream must be valid during the life-time of C. A0Coder C(const char* src_file_name, const char* tgt_file_name) creates an instance C which uses file-streams for input and output.

Operations

Standard Operations

 void C.encode() encodes the source stream and writes the output to the target stream. void C.decode() decodes the source stream and writes the output to the target stream. uint32 C.encode_memory_chunk(const char* in_buf, uint32 in_len, char* out_buf, uint32 out_len) encodes the memory chunk starting at in_buf with size in_len into the buffer starting at out_buf with size out_len. The function returns actual length of the encoded chunk which may be smaller than out_len. If the output buffer is too small for the encoded data the failure flag will be set (see below). uint32 C.decode_memory_chunk(const char* in_buf, uint32 in_len, char* out_buf, uint32 out_len) decodes a memory chunk. The meaning of the parameters and the return value is the same as in the previous function. streambuf* C.get_src_stream() returns the current source stream. void C.set_src_stream(streambuf* src_stream, bool own_stream = false) sets the source stream (cf. constructor). void C.set_src_file(const char* file_name) sets a file as source stream. streambuf* C.get_tgt_stream() returns the current target stream. void C.set_tgt_stream(streambuf* tgt_stream, bool own_Stream = false) sets the target stream (cf. constructor). void C.set_tgt_file(const char* file_name) sets a file as target stream. void C.reset(bool keep_thresholds = true) puts C in the same state as the default constructor. If keep_thresholds is false the thresholds are set to their default values. bool C.failed() returns true if an error occured. bool C.finished() returns true if the coding is finished. string C.get_description() provides a description for C.