Brotli Compression Algorithm

repository·master·Indexed 12 days ago

https://github.com/google/brotli

A generic-purpose lossless compression algorithm using LZ77, Huffman coding, and 2nd order context modeling. This repository provides the C implementation, a Python module, and a command-line interface for compression and decompression. It includes tools for analyzing backward reference distance distributions and managing Brotli dictionaries.

Tokens
11K
Snippets
48
Records
56
Agent score
91%

What's inside Brotli

  1. Overview of Brotli dictionary tools

    master

    The scripts/dictionary directory contains a set of tools designed to manage Brotli dictionaries. These tools allow developers to:

    • Download the Brotli RFC.
    • Extract and validate binary dictionaries.
    • Generate dictionary derivatives, such as the constants used in the Java DictionaryData class.
  2. Analyze backward reference distance distributions with Brotli research tools

    master
    The research/ directory contains tools designed to analyze backward reference distance distributions in LZ77 compression. These tools help visualize how distances are distributed in large-window compression, which can inform more efficient encoding strategies like delta coding. The tools work by generating *.dist files containing reference data and then visualizing that data as grayscale PGM images (histograms).
  3. Understand the *.dist file format

    master

    The *.dist file format stores backward references using a flag-prefixed structure. Each entry consists of either a match length or a position-distance pair. All numbers are 32-bit integers.

    Format Structure:

    • [0| match length]: Flag byte 0 followed by a 4-byte match length.
    • [1| position| distance]: Flag byte 1 followed by a 4-byte position and a 4-byte distance.

    Standalone copy lengths (flag 0) are allowed but ignored during processing. Position-distance pairs (flag 1) are the primary data points.

  4. Configure Brotli compression quality and window size

    master

    To optimize the balance between compression density and speed, you can tune the quality and window size:

    • Quality (-q or --quality): Accepts values from 0 to 11. Higher values result in denser compression but require more time. The -Z or --best flag is a shortcut for -q 11.
    • Window Size (-w or --lgwin): Sets the LZ77 window size using a logarithmic scale. Valid values are 0 or 10-24 (default is 24). The actual window size is calculated as (2^NUM - 16). A value of 0 allows the compressor to decide the optimal value. Larger windows improve density but increase the memory required by the decoder.
    # High quality compression with specific window size
    brotli -q 11 -w 22 input.txt
  5. Use the brotli CLI for compression and decompression

    master

    The brotli command-line tool provides three primary operation modes:

    1. Compression (Default): Compresses input files. By default, source files are preserved and a .br suffix is appended to the filename.
    2. Decompression: Activated with the --decompress or -d option. It removes the suffix from the source filename to create the target file.
    3. Integrity Test: Activated with the --test or -t option. This performs decompression to verify file integrity but discards the output instead of writing to stdout.

    Helper Commands:

    • unbrotli: A shortcut for brotli --decompress.
    • brcat: A shortcut for brotli --decompress --concatenated --stdout (useful for concatenating streams).

    Input/Output Behavior:

    • If no files are provided or the file name is -, brotli reads from standard input.
    • Unless --stdout or --output is used, output filenames are derived from source filenames using a suffix (default .br).
    • Use --suffix=SUF to change the default suffix.
    # Compression
    brotli input.txt
    
    # Decompression
    brotli -d input.txt.br
    unbrotli input.txt.br
    
    # Test integrity
    brotli -t input.txt.br
    
    # Decompress to stdout
    brotli -d -c input.txt.br
  6. Install the Brotli Python module

    master

    Brotli provides a Python module that can be installed via pip. You can install the latest stable release or the latest development version directly from the GitHub repository.

    # Install the latest release
    pip install brotli
    
    # Install the tip-of-the-tree version from GitHub
    pip install --upgrade git+https://github.com/google/brotli
  7. Build Brotli from source using CMake

    master

    You can build and install Brotli using CMake by creating a build directory and configuring the installation prefix. This is useful for integrating Brotli into C++ projects.

    $ mkdir out && cd out
    $ cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=./installed ..
    $ cmake --build . --config Release --target install
  8. Develop the brotli Python module

    master

    The project provides a Makefile to manage common development tasks. You can build the module in-place, run tests, or clean build artifacts. To make the module available while allowing for live edits to the source files, use make develop to install in setuptools development mode.

    $ make          # Build the module in-place
    $ make test     # Test the module
    $ make clean    # Remove all temporary files and build output
    $ make develop  # Install the module in "development mode"
  9. Install brotli from source

    master

    To install directly from the source directory, use the provided Makefile.

    If you want to use a native Brotli installation already present on your system instead of the vendored sources, set the USE_SYSTEM_BROTLI=1 environment variable when building the wheel. This requires the pkg-config utility and the presence of the brotlicommon, brotlienc, and brotlidec components. On Fedora, ensure you have the development package installed.

    # Standard source installation
    $ make install
    
    # Install using system Brotli instead of vendored sources
    $ USE_SYSTEM_BROTLI=1 pip install brotli --no-binary brotli
    
    # Fedora dependency installation
    $ dnf install brotli brotli-devel
  10. Install Brotli via package managers

    master

    For most Linux and macOS environments, you can install the Brotli CLI and tools using standard package managers:

    • Debian/Ubuntu: Use apt.
    • macOS: Use Homebrew.

    Note that Brotli is a "stream" format and does not contain meta-information like checksums or uncompressed data length. This means the decoder will not detect if raw ranges of the compressed stream have been modified.

    # Debian-based distributions
    apt install brotli
    
    # macOS
    brew install brotli