2013-12-16 13:45:57 +00:00
|
|
|
/* Copyright 2013 Google Inc. All Rights Reserved.
|
|
|
|
|
2015-12-11 10:11:51 +00:00
|
|
|
Distributed under MIT license.
|
2015-11-27 10:27:11 +00:00
|
|
|
See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
|
2013-12-16 13:45:57 +00:00
|
|
|
*/
|
2013-10-11 08:26:07 +00:00
|
|
|
|
2015-03-20 14:44:15 +00:00
|
|
|
/* Utilities for building Huffman decoding tables. */
|
|
|
|
|
2013-10-11 08:26:07 +00:00
|
|
|
#ifndef BROTLI_DEC_HUFFMAN_H_
|
|
|
|
#define BROTLI_DEC_HUFFMAN_H_
|
|
|
|
|
2017-12-12 13:33:12 +00:00
|
|
|
#include "../common/platform.h"
|
2016-08-23 12:40:33 +00:00
|
|
|
#include <brotli/types.h>
|
2013-10-11 08:26:07 +00:00
|
|
|
|
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2015-08-28 13:20:24 +00:00
|
|
|
#define BROTLI_HUFFMAN_MAX_CODE_LENGTH 15
|
|
|
|
|
2016-01-22 09:19:41 +00:00
|
|
|
/* Maximum possible Huffman table size for an alphabet size of (index * 32),
|
2019-04-12 11:57:42 +00:00
|
|
|
max code length 15 and root table bits 8. This table describes table sizes
|
|
|
|
for alphabets containing up to 1152 = 36 * 32 symbols. */
|
2016-01-22 09:19:41 +00:00
|
|
|
static const uint16_t kMaxHuffmanTableSize[] = {
|
|
|
|
256, 402, 436, 468, 500, 534, 566, 598, 630, 662, 694, 726, 758, 790, 822,
|
2018-02-26 14:04:36 +00:00
|
|
|
854, 886, 920, 952, 984, 1016, 1048, 1080, 1112, 1144, 1176, 1208, 1240, 1272,
|
|
|
|
1304, 1336, 1368, 1400, 1432, 1464, 1496, 1528};
|
2016-06-03 08:51:04 +00:00
|
|
|
/* BROTLI_NUM_BLOCK_LEN_SYMBOLS == 26 */
|
2016-01-22 09:19:41 +00:00
|
|
|
#define BROTLI_HUFFMAN_MAX_SIZE_26 396
|
2016-06-03 08:51:04 +00:00
|
|
|
/* BROTLI_MAX_BLOCK_TYPE_SYMBOLS == 258 */
|
2016-01-22 09:19:41 +00:00
|
|
|
#define BROTLI_HUFFMAN_MAX_SIZE_258 632
|
2016-06-03 08:51:04 +00:00
|
|
|
/* BROTLI_MAX_CONTEXT_MAP_SYMBOLS == 272 */
|
2016-01-22 09:19:41 +00:00
|
|
|
#define BROTLI_HUFFMAN_MAX_SIZE_272 646
|
2015-08-28 13:20:24 +00:00
|
|
|
|
|
|
|
#define BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH 5
|
2015-03-20 15:13:15 +00:00
|
|
|
|
2018-09-27 11:15:46 +00:00
|
|
|
#if ((defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8_32)) && \
|
|
|
|
BROTLI_GNUC_HAS_ATTRIBUTE(aligned, 2, 7, 0))
|
|
|
|
#define BROTLI_HUFFMAN_CODE_FAST_LOAD
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(BROTLI_HUFFMAN_CODE_FAST_LOAD)
|
|
|
|
/* Do not create this struct directly - use the ConstructHuffmanCode
|
|
|
|
* constructor below! */
|
2013-10-11 08:26:07 +00:00
|
|
|
typedef struct {
|
2016-02-18 14:03:44 +00:00
|
|
|
uint8_t bits; /* number of bits used for this symbol */
|
|
|
|
uint16_t value; /* symbol value or table offset */
|
2014-02-14 14:04:23 +00:00
|
|
|
} HuffmanCode;
|
2013-10-11 08:26:07 +00:00
|
|
|
|
2018-09-27 11:15:46 +00:00
|
|
|
static BROTLI_INLINE HuffmanCode ConstructHuffmanCode(const uint8_t bits,
|
|
|
|
const uint16_t value) {
|
|
|
|
HuffmanCode h;
|
|
|
|
h.bits = bits;
|
|
|
|
h.value = value;
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Please use the following macros to optimize HuffmanCode accesses in hot
|
|
|
|
* paths.
|
|
|
|
*
|
|
|
|
* For example, assuming |table| contains a HuffmanCode pointer:
|
|
|
|
*
|
|
|
|
* BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(table);
|
|
|
|
* BROTLI_HC_ADJUST_TABLE_INDEX(table, index_into_table);
|
|
|
|
* *bits = BROTLI_HC_GET_BITS(table);
|
|
|
|
* *value = BROTLI_HC_GET_VALUE(table);
|
|
|
|
* BROTLI_HC_ADJUST_TABLE_INDEX(table, offset);
|
|
|
|
* *bits2 = BROTLI_HC_GET_BITS(table);
|
|
|
|
* *value2 = BROTLI_HC_GET_VALUE(table);
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(H)
|
|
|
|
#define BROTLI_HC_ADJUST_TABLE_INDEX(H, V) H += (V)
|
|
|
|
|
|
|
|
/* These must be given a HuffmanCode pointer! */
|
|
|
|
#define BROTLI_HC_FAST_LOAD_BITS(H) (H->bits)
|
|
|
|
#define BROTLI_HC_FAST_LOAD_VALUE(H) (H->value)
|
|
|
|
|
|
|
|
#else /* BROTLI_HUFFMAN_CODE_FAST_LOAD */
|
|
|
|
|
|
|
|
typedef BROTLI_ALIGNED(4) uint32_t HuffmanCode;
|
|
|
|
|
|
|
|
static BROTLI_INLINE HuffmanCode ConstructHuffmanCode(const uint8_t bits,
|
|
|
|
const uint16_t value) {
|
|
|
|
return ((value & 0xFFFF) << 16) | (bits & 0xFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(H) uint32_t __fastload_##H = (*H)
|
|
|
|
#define BROTLI_HC_ADJUST_TABLE_INDEX(H, V) H += (V); __fastload_##H = (*H)
|
|
|
|
|
|
|
|
/* These must be given a HuffmanCode pointer! */
|
|
|
|
#define BROTLI_HC_FAST_LOAD_BITS(H) ((__fastload_##H) & 0xFF)
|
|
|
|
#define BROTLI_HC_FAST_LOAD_VALUE(H) ((__fastload_##H) >> 16)
|
|
|
|
#endif /* BROTLI_HUFFMAN_CODE_FAST_LOAD */
|
|
|
|
|
2015-08-28 13:20:24 +00:00
|
|
|
/* Builds Huffman lookup table assuming code lengths are in symbol order. */
|
2016-04-19 14:29:10 +00:00
|
|
|
BROTLI_INTERNAL void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* root_table,
|
|
|
|
const uint8_t* const code_lengths, uint16_t* count);
|
2015-08-28 13:20:24 +00:00
|
|
|
|
2018-02-26 14:04:36 +00:00
|
|
|
/* Builds Huffman lookup table assuming code lengths are in symbol order.
|
|
|
|
Returns size of resulting table. */
|
2016-04-19 14:29:10 +00:00
|
|
|
BROTLI_INTERNAL uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table,
|
|
|
|
int root_bits, const uint16_t* const symbol_lists, uint16_t* count_arg);
|
2015-08-10 11:35:23 +00:00
|
|
|
|
2018-02-26 14:04:36 +00:00
|
|
|
/* Builds a simple Huffman table. The |num_symbols| parameter is to be
|
|
|
|
interpreted as follows: 0 means 1 symbol, 1 means 2 symbols,
|
|
|
|
2 means 3 symbols, 3 means 4 symbols with lengths [2, 2, 2, 2],
|
|
|
|
4 means 4 symbols with lengths [1, 2, 3, 3]. */
|
2016-04-19 14:29:10 +00:00
|
|
|
BROTLI_INTERNAL uint32_t BrotliBuildSimpleHuffmanTable(HuffmanCode* table,
|
|
|
|
int root_bits, uint16_t* symbols, uint32_t num_symbols);
|
2013-10-11 08:26:07 +00:00
|
|
|
|
2015-09-21 19:04:07 +00:00
|
|
|
/* Contains a collection of Huffman trees with the same alphabet size. */
|
2019-04-12 11:57:42 +00:00
|
|
|
/* alphabet_size_limit is needed due to simple codes, since
|
|
|
|
log2(alphabet_size_max) could be greater than log2(alphabet_size_limit). */
|
2015-03-20 15:13:15 +00:00
|
|
|
typedef struct {
|
|
|
|
HuffmanCode** htrees;
|
2015-08-10 11:35:23 +00:00
|
|
|
HuffmanCode* codes;
|
2019-04-12 11:57:42 +00:00
|
|
|
uint16_t alphabet_size_max;
|
|
|
|
uint16_t alphabet_size_limit;
|
2015-10-05 08:23:32 +00:00
|
|
|
uint16_t num_htrees;
|
2015-03-20 15:13:15 +00:00
|
|
|
} HuffmanTreeGroup;
|
|
|
|
|
2013-10-11 08:26:07 +00:00
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
2016-02-18 14:03:44 +00:00
|
|
|
} /* extern "C" */
|
2013-10-11 08:26:07 +00:00
|
|
|
#endif
|
|
|
|
|
2013-12-16 13:45:57 +00:00
|
|
|
#endif /* BROTLI_DEC_HUFFMAN_H_ */
|