recp / huff

canonical huffman coding

Repository from Github https://github.comrecp/huffRepository from Github https://github.comrecp/huff

πŸ—œοΈ Huffman Coding (In Progress)

This library is designed to make Huffman coding easy to use, while providing optimized and reusable utilities. It aims to simplify the integration of Huffman coding into projects, improve maintainability, and ensure robust testing.

Huffman coding is a cornerstone of many compression algorithms and formats, widely used in:

  • πŸ–ΌοΈ Images: JPEG, PNG, WebP, GIF.
  • πŸŽ₯ Video: MPEG, H.264, HEVC (H.265).
  • 🎡 Audio: MP3, AAC, Opus.
  • πŸ“¦ Compression: ZIP, DEFLATE, Brotli.
  • πŸ”— Networking: HTTP/2, HTTP/3 (QUIC) header compression.

This library seeks to serve as a shared foundation for these and other applications, offering standardized, high-performance tools for encoding and decoding Huffman streams.

🚨 Don't use this in production until tests are ready

πŸ”§ Usage

Initializing a Huffman Table

The library provides two initialization functions:

  • huff_init_lsb() for LSB-first bitstreams.
  • huff_init_msb() for MSB-first bitstreams.
// Example: Initializing a Huffman table for LSB-first bitstreams (e.g., DEFLATE)
huff_table_t table;
uint8_t      lengths[] = {3, 3, 3, 3}; // Bit lengths for each symbol
uint16_t     symbols[] = {0, 1, 2, 3}; // Corresponding symbols
huff_init_lsb(&table, lengths, symbols, 4);

Decoding a Symbol

// LSB-first decoding
bitstream_t   bitstream = 0b10110010; // Example LSB-first bitstream
uint8_t       bit_length = 8; // Number of valid bits
uint8_t       used_bits;
uint_fast16_t symbol = huff_decode_lsb(&table, bitstream, bit_length, &used_bits);

TODO

  • lsb
  • sub tables?
  • msb
  • tests
  • build
  • documentation

About

canonical huffman coding


Languages

Language:C 97.4%Language:CMake 2.6%