Legend

Class
Struct
Enum
Interface
Delegate
Constructor
Method
Property
Event
Field

Class: RT.ArithmeticCoding.ArithmeticCodingWriter

Summary

Implements an arithmetic coding encoder. See Remarks.

Remarks

The writer accepts a sequence of symbols as inputs via ArithmeticCodingWriter.WriteSymbol(int), encodes them and writes the encoded bytes to the specified stream. A symbol is an integer; the same integer will be returned by ArithmeticCodingReader.ReadSymbol() when the encoded stream is decoded. Symbols are encoded according to the current ArithmeticSymbolContext, which describes the relative frequencies of all symbols.

Symbols are permitted to have a frequency of zero, but it is illegal to attempt to encode a zero-frequency symbol. The symbol context does not have to remain unchanged; the context may be modified arbitrarily between calls to ArithmeticCodingWriter.WriteSymbol(int), and an entirely different context may be applied using ArithmeticCodingWriter.SetContext(ArithmeticSymbolContext). The only requirement is that identical contexts are in place before every ArithmeticCodingWriter.WriteSymbol(int) call and before its corresponding ArithmeticCodingReader.ReadSymbol() call.

This class does not offer any built-in means for the reader to detect the last symbol written. The caller must know when to stop calling ArithmeticCodingReader.ReadSymbol(). Where this cannot be deduced from context, you can dedicate an extra symbol (with a suitable frequency) to mark end of stream, or write total symbol count to the stream separately.

The reader and the writer support operation on a stream which has other data before and/or after the arithmetic-coded section. The reader's ArithmeticCodingReader.Finalize(bool) method ensures that the input stream is left with exactly the correct number of bytes consumed.

Arithmetic encoding uses fewer bits for more frequent symbols. The number of bits used per symbol is not necessarily an integer, and there is no pre-determined output bit pattern corresponding to a given symbol. Arithmetic coding is not data compression per se; it is an entropy coding algorithm. Arithmetic coding requires an accurate prediction of each symbol's probability to be supplied by the caller in order to be effective, and is only as good as the caller's modelling of the sequence of symbols being passed in.

Constructors

ArithmeticCodingWriter(Stream stream, uint[] frequencies)
Initialises an ArithmeticCodingWriter instance. See Remarks. (see also remarks)

Instance methods

void
Finalize(bool closeStream = false)
Finalizes the stream by flushing any remaining buffered data and writing the synchronization padding required by the reader. This call is mandatory; the stream will not be readable in full if this method is not called. This method does not write enough information to the stream for the reader to detect that there are no further symbols; see Remarks on ArithmeticCodingWriter for further info.
void Changes the symbol context. See Remarks. (see also remarks)
void
WriteSymbol(int symbol)
Encodes a single symbol.