Path: blob/master/3rdparty/libwebp/src/utils/huffman_utils.h
16358 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#define HUFFMAN_PACKED_BITS 646#define HUFFMAN_PACKED_TABLE_SIZE (1u << HUFFMAN_PACKED_BITS)4748// Huffman table group.49// Includes special handling for the following cases:50// - is_trivial_literal: one common literal base for RED/BLUE/ALPHA (not GREEN)51// - is_trivial_code: only 1 code (no bit is read from bitstream)52// - use_packed_table: few enough literal symbols, so all the bit codes53// can fit into a small look-up table packed_table[]54// The common literal base, if applicable, is stored in 'literal_arb'.55typedef struct HTreeGroup HTreeGroup;56struct HTreeGroup {57HuffmanCode* htrees[HUFFMAN_CODES_PER_META_CODE];58int is_trivial_literal; // True, if huffman trees for Red, Blue & Alpha59// Symbols are trivial (have a single code).60uint32_t literal_arb; // If is_trivial_literal is true, this is the61// ARGB value of the pixel, with Green channel62// being set to zero.63int is_trivial_code; // true if is_trivial_literal with only one code64int use_packed_table; // use packed table below for short literal code65// table mapping input bits to a packed values, or escape case to literal code66HuffmanCode32 packed_table[HUFFMAN_PACKED_TABLE_SIZE];67};6869// Creates the instance of HTreeGroup with specified number of tree-groups.70HTreeGroup* VP8LHtreeGroupsNew(int num_htree_groups);7172// Releases the memory allocated for HTreeGroup.73void VP8LHtreeGroupsFree(HTreeGroup* const htree_groups);7475// Builds Huffman lookup table assuming code lengths are in symbol order.76// The 'code_lengths' is pre-allocated temporary memory buffer used for creating77// the huffman table.78// Returns built table size or 0 in case of error (invalid tree or79// memory error).80int VP8LBuildHuffmanTable(HuffmanCode* const root_table, int root_bits,81const int code_lengths[], int code_lengths_size);8283#ifdef __cplusplus84} // extern "C"85#endif8687#endif // WEBP_UTILS_HUFFMAN_UTILS_H_888990