Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

LZ77 Stream

Entropy-coded streams can optionally be marked as LZ77-enabled.

// struct EntropyDecoder(num_dist: u32, lz77_allowed: bool) {

let lz77_enabled = read!(Bool);
pub let lz77_config = if lz77_enabled {
    // Prevent recursion going too deep
    assert!(lz77_allowed);

    let config = read!(struct Lz77Params(num_dist));
    // NOTE: This mutates `num_dist`
    num_dist += 1;
    Some(config)
} else {
    None
};

// ...

Where Lz77Params is defined as:

struct Lz77Params(dist_ctx: u32) {
    pub let min_symbol = read!(U32(224, 512, 4096, 8 + u(15)));
    pub let min_length = read!(U32(3, 4, 5 + u(2), 9 + u(8)));
    pub let dist_ctx = dist_ctx;
    pub let len_conf = read!(struct HybridIntegerConfig(8));
}

… and HybridIntegerConfig is defined in the corresponding chapter.

Now lz77_config contains whether LZ77 is enabled, and if so, parameters for handling LZ77 symbols. Note that num_dist is increased by one if LZ77 is enabled. The last context is used to read LZ77 distance when LZ77 length symbol is encountered during decoding.

lz77_allowed acts as a safeguard to prevent unbounded recursion; more on this in the next chapter.