Arcode
An arithmetic coder for Rust.
About
This crate provides the an efficient implementation of an arithmetic encoder/decoder. This crate is based off the paper that describes arithmetic coding found here. This implementation features many readability and performance improvements, especially on the decoding side.
The goal of this project is not to provide an out-of-the-box compression solution. Arithmetic coding (entropy encoding) is the backbone of almost every modern day compression scheme. This crate is meant to be included in future projects that rely on an efficient entropy coder e.g. PPM, LZ77/LZ78, h265/HEVC.
Core components
There are a lot of structs available for use but for the average user there are only a few that will be used.
- Model models of the probability of symbols. Counts can be adjusted as encoding is done to improve compression.
- Encoder encodes symbols given a source model and a symbol.
- Decoder decodes symbols given a source model and a bitstream.
Examples
In the git repository there is an old_complex.rs file that does context switching on a per character basis. A simpler example can be found at new_simple.rs
Input and output bitstreams
In order for arithmetic coding to work streams need to be read a bit at a time (for decoding and for the encoders output). Because of this, BitBit is required. Wrapping whatever your input is in a buffered reader/writer should greatly improve performance.
Using bitbit to create an input stream.
use ;
use Cursor;
Source Model(s)
Depending on your application you could have one or many source models. The source model is relied on by the encoder and the decoder. If the decoder ever becomes out of phase with the encoder you will be decoding nonsense.
model::Builder
In order to make a source model you need to use the model::Builder struct.
use ;
Encode
Encoding some simple input
use BitWriter;
use ;
use ;
/// Encodes bytes and returns the compressed form
Decode
use ;
use ;
use ;
/// Decompresses the data