Path: blob/master/thirdparty/libwebp/src/utils/huffman_utils.h
9912 views
// Copyright 2012 Google Inc. All Rights Reserved.1//2// Use of this source code is governed by a BSD-style license3// that can be found in the COPYING file in the root of the source4// tree. An additional intellectual property rights grant can be found5// in the file PATENTS. All contributing project authors may6// be found in the AUTHORS file in the root of the source tree.7// -----------------------------------------------------------------------------8//9// Utilities for building and looking up Huffman trees.10//11// Author: Urvang Joshi ([email protected])1213#ifndef WEBP_UTILS_HUFFMAN_UTILS_H_14#define WEBP_UTILS_HUFFMAN_UTILS_H_1516#include <assert.h>17#include "src/webp/format_constants.h"18#include "src/webp/types.h"1920#ifdef __cplusplus21extern "C" {22#endif2324#define HUFFMAN_TABLE_BITS 825#define HUFFMAN_TABLE_MASK ((1 << HUFFMAN_TABLE_BITS) - 1)2627#define LENGTHS_TABLE_BITS 728#define LENGTHS_TABLE_MASK ((1 << LENGTHS_TABLE_BITS) - 1)293031// Huffman lookup table entry32typedef struct {33uint8_t bits; // number of bits used for this symbol34uint16_t value; // symbol value or table offset35} HuffmanCode;3637// long version for holding 32b values38typedef struct {39int bits; // number of bits used for this symbol,40// or an impossible value if not a literal code.41uint32_t value; // 32b packed ARGB value if literal,42// or non-literal symbol otherwise43} HuffmanCode32;4445// Contiguous memory segment of HuffmanCodes.46typedef struct HuffmanTablesSegment {47HuffmanCode* start;48// Pointer to where we are writing into the segment. Starts at 'start' and49// cannot go beyond 'start' + 'size'.50HuffmanCode* curr_table;51// Pointer to the next segment in the chain.52struct HuffmanTablesSegment* next;53int size;54} HuffmanTablesSegment;5556// Chained memory segments of HuffmanCodes.57typedef struct HuffmanTables {58HuffmanTablesSegment root;59// Currently processed segment. At first, this is 'root'.60HuffmanTablesSegment* curr_segment;61} HuffmanTables;6263// Allocates a HuffmanTables with 'size' contiguous HuffmanCodes. Returns 0 on64// memory allocation error, 1 otherwise.65WEBP_NODISCARD int VP8LHuffmanTablesAllocate(int size,66HuffmanTables* huffman_tables);67void VP8LHuffmanTablesDeallocate(HuffmanTables* const huffman_tables);6869#define HUFFMAN_PACKED_BITS 670#define HUFFMAN_PACKED_TABLE_SIZE (1u << HUFFMAN_PACKED_BITS)7172// Huffman table group.73// Includes special handling for the following cases:74// - is_trivial_literal: one common literal base for RED/BLUE/ALPHA (not GREEN)75// - is_trivial_code: only 1 code (no bit is read from bitstream)76// - use_packed_table: few enough literal symbols, so all the bit codes77// can fit into a small look-up table packed_table[]78// The common literal base, if applicable, is stored in 'literal_arb'.79typedef struct HTreeGroup HTreeGroup;80struct HTreeGroup {81HuffmanCode* htrees[HUFFMAN_CODES_PER_META_CODE];82int is_trivial_literal; // True, if huffman trees for Red, Blue & Alpha83// Symbols are trivial (have a single code).84uint32_t literal_arb; // If is_trivial_literal is true, this is the85// ARGB value of the pixel, with Green channel86// being set to zero.87int is_trivial_code; // true if is_trivial_literal with only one code88int use_packed_table; // use packed table below for short literal code89// table mapping input bits to a packed values, or escape case to literal code90HuffmanCode32 packed_table[HUFFMAN_PACKED_TABLE_SIZE];91};9293// Creates the instance of HTreeGroup with specified number of tree-groups.94WEBP_NODISCARD HTreeGroup* VP8LHtreeGroupsNew(int num_htree_groups);9596// Releases the memory allocated for HTreeGroup.97void VP8LHtreeGroupsFree(HTreeGroup* const htree_groups);9899// Builds Huffman lookup table assuming code lengths are in symbol order.100// The 'code_lengths' is pre-allocated temporary memory buffer used for creating101// the huffman table.102// Returns built table size or 0 in case of error (invalid tree or103// memory error).104WEBP_NODISCARD int VP8LBuildHuffmanTable(HuffmanTables* const root_table,105int root_bits,106const int code_lengths[],107int code_lengths_size);108109#ifdef __cplusplus110} // extern "C"111#endif112113#endif // WEBP_UTILS_HUFFMAN_UTILS_H_114115116