|
Packit |
9c6abc |
// Copyright 2012 Google Inc. All Rights Reserved.
|
|
Packit |
9c6abc |
//
|
|
Packit |
9c6abc |
// Use of this source code is governed by a BSD-style license
|
|
Packit |
9c6abc |
// that can be found in the COPYING file in the root of the source
|
|
Packit |
9c6abc |
// tree. An additional intellectual property rights grant can be found
|
|
Packit |
9c6abc |
// in the file PATENTS. All contributing project authors may
|
|
Packit |
9c6abc |
// be found in the AUTHORS file in the root of the source tree.
|
|
Packit |
9c6abc |
// -----------------------------------------------------------------------------
|
|
Packit |
9c6abc |
//
|
|
Packit |
9c6abc |
// Utilities for building and looking up Huffman trees.
|
|
Packit |
9c6abc |
//
|
|
Packit |
9c6abc |
// Author: Urvang Joshi (urvang@google.com)
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
#ifndef WEBP_UTILS_HUFFMAN_UTILS_H_
|
|
Packit |
9c6abc |
#define WEBP_UTILS_HUFFMAN_UTILS_H_
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
#include <assert.h>
|
|
Packit |
9c6abc |
#include "src/webp/format_constants.h"
|
|
Packit |
9c6abc |
#include "src/webp/types.h"
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
#ifdef __cplusplus
|
|
Packit |
9c6abc |
extern "C" {
|
|
Packit |
9c6abc |
#endif
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
#define HUFFMAN_TABLE_BITS 8
|
|
Packit |
9c6abc |
#define HUFFMAN_TABLE_MASK ((1 << HUFFMAN_TABLE_BITS) - 1)
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
#define LENGTHS_TABLE_BITS 7
|
|
Packit |
9c6abc |
#define LENGTHS_TABLE_MASK ((1 << LENGTHS_TABLE_BITS) - 1)
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
// Huffman lookup table entry
|
|
Packit |
9c6abc |
typedef struct {
|
|
Packit |
9c6abc |
uint8_t bits; // number of bits used for this symbol
|
|
Packit |
9c6abc |
uint16_t value; // symbol value or table offset
|
|
Packit |
9c6abc |
} HuffmanCode;
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
// long version for holding 32b values
|
|
Packit |
9c6abc |
typedef struct {
|
|
Packit |
9c6abc |
int bits; // number of bits used for this symbol,
|
|
Packit |
9c6abc |
// or an impossible value if not a literal code.
|
|
Packit |
9c6abc |
uint32_t value; // 32b packed ARGB value if literal,
|
|
Packit |
9c6abc |
// or non-literal symbol otherwise
|
|
Packit |
9c6abc |
} HuffmanCode32;
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
#define HUFFMAN_PACKED_BITS 6
|
|
Packit |
9c6abc |
#define HUFFMAN_PACKED_TABLE_SIZE (1u << HUFFMAN_PACKED_BITS)
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
// Huffman table group.
|
|
Packit |
9c6abc |
// Includes special handling for the following cases:
|
|
Packit |
9c6abc |
// - is_trivial_literal: one common literal base for RED/BLUE/ALPHA (not GREEN)
|
|
Packit |
9c6abc |
// - is_trivial_code: only 1 code (no bit is read from bitstream)
|
|
Packit |
9c6abc |
// - use_packed_table: few enough literal symbols, so all the bit codes
|
|
Packit |
9c6abc |
// can fit into a small look-up table packed_table[]
|
|
Packit |
9c6abc |
// The common literal base, if applicable, is stored in 'literal_arb'.
|
|
Packit |
9c6abc |
typedef struct HTreeGroup HTreeGroup;
|
|
Packit |
9c6abc |
struct HTreeGroup {
|
|
Packit |
9c6abc |
HuffmanCode* htrees[HUFFMAN_CODES_PER_META_CODE];
|
|
Packit |
9c6abc |
int is_trivial_literal; // True, if huffman trees for Red, Blue & Alpha
|
|
Packit |
9c6abc |
// Symbols are trivial (have a single code).
|
|
Packit |
9c6abc |
uint32_t literal_arb; // If is_trivial_literal is true, this is the
|
|
Packit |
9c6abc |
// ARGB value of the pixel, with Green channel
|
|
Packit |
9c6abc |
// being set to zero.
|
|
Packit |
9c6abc |
int is_trivial_code; // true if is_trivial_literal with only one code
|
|
Packit |
9c6abc |
int use_packed_table; // use packed table below for short literal code
|
|
Packit |
9c6abc |
// table mapping input bits to a packed values, or escape case to literal code
|
|
Packit |
9c6abc |
HuffmanCode32 packed_table[HUFFMAN_PACKED_TABLE_SIZE];
|
|
Packit |
9c6abc |
};
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
// Creates the instance of HTreeGroup with specified number of tree-groups.
|
|
Packit |
9c6abc |
HTreeGroup* VP8LHtreeGroupsNew(int num_htree_groups);
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
// Releases the memory allocated for HTreeGroup.
|
|
Packit |
9c6abc |
void VP8LHtreeGroupsFree(HTreeGroup* const htree_groups);
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
// Builds Huffman lookup table assuming code lengths are in symbol order.
|
|
Packit |
9c6abc |
// The 'code_lengths' is pre-allocated temporary memory buffer used for creating
|
|
Packit |
9c6abc |
// the huffman table.
|
|
Packit |
9c6abc |
// Returns built table size or 0 in case of error (invalid tree or
|
|
Packit |
9c6abc |
// memory error).
|
|
Packit |
9c6abc |
int VP8LBuildHuffmanTable(HuffmanCode* const root_table, int root_bits,
|
|
Packit |
9c6abc |
const int code_lengths[], int code_lengths_size);
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
#ifdef __cplusplus
|
|
Packit |
9c6abc |
} // extern "C"
|
|
Packit |
9c6abc |
#endif
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
#endif // WEBP_UTILS_HUFFMAN_UTILS_H_
|