Path: blob/master/thirdparty/libwebp/src/utils/huffman_utils.h
21344 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>1718#include "src/webp/format_constants.h"19#include "src/webp/types.h"2021#ifdef __cplusplus22extern "C" {23#endif2425#define HUFFMAN_TABLE_BITS 826#define HUFFMAN_TABLE_MASK ((1 << HUFFMAN_TABLE_BITS) - 1)2728#define LENGTHS_TABLE_BITS 729#define LENGTHS_TABLE_MASK ((1 << LENGTHS_TABLE_BITS) - 1)303132// Huffman lookup table entry33typedef struct {34uint8_t bits; // number of bits used for this symbol35uint16_t value; // symbol value or table offset36} HuffmanCode;3738// long version for holding 32b values39typedef struct {40int bits; // number of bits used for this symbol,41// or an impossible value if not a literal code.42uint32_t value; // 32b packed ARGB value if literal,43// or non-literal symbol otherwise44} HuffmanCode32;4546// Contiguous memory segment of HuffmanCodes.47typedef struct HuffmanTablesSegment {48HuffmanCode* start;49// Pointer to where we are writing into the segment. Starts at 'start' and50// cannot go beyond 'start' + 'size'.51HuffmanCode* curr_table;52// Pointer to the next segment in the chain.53struct HuffmanTablesSegment* next;54int size;55} HuffmanTablesSegment;5657// Chained memory segments of HuffmanCodes.58typedef struct HuffmanTables {59HuffmanTablesSegment root;60// Currently processed segment. At first, this is 'root'.61HuffmanTablesSegment* curr_segment;62} HuffmanTables;6364// Allocates a HuffmanTables with 'size' contiguous HuffmanCodes. Returns 0 on65// memory allocation error, 1 otherwise.66WEBP_NODISCARD int VP8LHuffmanTablesAllocate(int size,67HuffmanTables* huffman_tables);68void VP8LHuffmanTablesDeallocate(HuffmanTables* const huffman_tables);6970#define HUFFMAN_PACKED_BITS 671#define HUFFMAN_PACKED_TABLE_SIZE (1u << HUFFMAN_PACKED_BITS)7273// Huffman table group.74// Includes special handling for the following cases:75// - is_trivial_literal: one common literal base for RED/BLUE/ALPHA (not GREEN)76// - is_trivial_code: only 1 code (no bit is read from bitstream)77// - use_packed_table: few enough literal symbols, so all the bit codes78// can fit into a small look-up table packed_table[]79// The common literal base, if applicable, is stored in 'literal_arb'.80typedef struct HTreeGroup HTreeGroup;81struct HTreeGroup {82HuffmanCode* htrees[HUFFMAN_CODES_PER_META_CODE];83int is_trivial_literal; // True, if huffman trees for Red, Blue & Alpha84// Symbols are trivial (have a single code).85uint32_t literal_arb; // If is_trivial_literal is true, this is the86// ARGB value of the pixel, with Green channel87// being set to zero.88int is_trivial_code; // true if is_trivial_literal with only one code89int use_packed_table; // use packed table below for short literal code90// table mapping input bits to a packed values, or escape case to literal code91HuffmanCode32 packed_table[HUFFMAN_PACKED_TABLE_SIZE];92};9394// Creates the instance of HTreeGroup with specified number of tree-groups.95WEBP_NODISCARD HTreeGroup* VP8LHtreeGroupsNew(int num_htree_groups);9697// Releases the memory allocated for HTreeGroup.98void VP8LHtreeGroupsFree(HTreeGroup* const htree_groups);99100// Builds Huffman lookup table assuming code lengths are in symbol order.101// The 'code_lengths' is pre-allocated temporary memory buffer used for creating102// the huffman table.103// Returns built table size or 0 in case of error (invalid tree or104// memory error).105WEBP_NODISCARD int VP8LBuildHuffmanTable(HuffmanTables* const root_table,106int root_bits,107const int code_lengths[],108int code_lengths_size);109110#ifdef __cplusplus111} // extern "C"112#endif113114#endif // WEBP_UTILS_HUFFMAN_UTILS_H_115116117