| 1 |
/* Copyright 2010 Google Inc. All Rights Reserved. |
| 2 |
|
| 3 |
Distributed under MIT license. |
| 4 |
See file LICENSE for detail or copy at https://opensource.org/licenses/MIT |
| 5 |
*/ |
| 6 |
|
| 7 |
/* Entropy encoding (Huffman) utilities. */ |
| 8 |
|
| 9 |
#ifndef BROTLI_ENC_ENTROPY_ENCODE_H_ |
| 10 |
#define BROTLI_ENC_ENTROPY_ENCODE_H_ |
| 11 |
|
| 12 |
#include "../common/platform.h" |
| 13 |
|
| 14 |
#if defined(__cplusplus) || defined(c_plusplus) |
| 15 |
extern "C" { |
| 16 |
#endif |
| 17 |
|
| 18 |
/* A node of a Huffman tree. */ |
| 19 |
typedef struct HuffmanTree { |
| 20 |
uint32_t total_count_; |
| 21 |
int16_t index_left_; |
| 22 |
int16_t index_right_or_value_; |
| 23 |
} HuffmanTree; |
| 24 |
|
| 25 |
static BROTLI_INLINE void InitHuffmanTree(HuffmanTree* self, uint32_t count, |
| 26 |
int16_t left, int16_t right) { |
| 27 |
self->total_count_ = count; |
| 28 |
self->index_left_ = left; |
| 29 |
self->index_right_or_value_ = right; |
| 30 |
} |
| 31 |
|
| 32 |
/* Returns 1 is assignment of depths succeeded, otherwise 0. */ |
| 33 |
BROTLI_INTERNAL BROTLI_BOOL BrotliSetDepth( |
| 34 |
int p, HuffmanTree* pool, uint8_t* depth, int max_depth); |
| 35 |
|
| 36 |
/* This function will create a Huffman tree. |
| 37 |
|
| 38 |
The (data,length) contains the population counts. |
| 39 |
The tree_limit is the maximum bit depth of the Huffman codes. |
| 40 |
|
| 41 |
The depth contains the tree, i.e., how many bits are used for |
| 42 |
the symbol. |
| 43 |
|
| 44 |
The actual Huffman tree is constructed in the tree[] array, which has to |
| 45 |
be at least 2 * length + 1 long. |
| 46 |
|
| 47 |
See http://en.wikipedia.org/wiki/Huffman_coding */ |
| 48 |
BROTLI_INTERNAL void BrotliCreateHuffmanTree(const uint32_t* data, |
| 49 |
const size_t length, |
| 50 |
const int tree_limit, |
| 51 |
HuffmanTree* tree, |
| 52 |
uint8_t* depth); |
| 53 |
|
| 54 |
/* Change the population counts in a way that the consequent |
| 55 |
Huffman tree compression, especially its RLE-part will be more |
| 56 |
likely to compress this data more efficiently. |
| 57 |
|
| 58 |
length contains the size of the histogram. |
| 59 |
counts contains the population counts. |
| 60 |
good_for_rle is a buffer of at least length size */ |
| 61 |
BROTLI_INTERNAL void BrotliOptimizeHuffmanCountsForRle( |
| 62 |
size_t length, uint32_t* counts, uint8_t* good_for_rle); |
| 63 |
|
| 64 |
/* Write a Huffman tree from bit depths into the bit-stream representation |
| 65 |
of a Huffman tree. The generated Huffman tree is to be compressed once |
| 66 |
more using a Huffman tree */ |
| 67 |
BROTLI_INTERNAL void BrotliWriteHuffmanTree(const uint8_t* depth, |
| 68 |
size_t length, |
| 69 |
size_t* tree_size, |
| 70 |
uint8_t* tree, |
| 71 |
uint8_t* extra_bits_data); |
| 72 |
|
| 73 |
/* Get the actual bit values for a tree of bit depths. */ |
| 74 |
BROTLI_INTERNAL void BrotliConvertBitDepthsToSymbols(const uint8_t* depth, |
| 75 |
size_t len, |
| 76 |
uint16_t* bits); |
| 77 |
|
| 78 |
BROTLI_INTERNAL extern BROTLI_MODEL("small") const size_t kBrotliShellGaps[6]; |
| 79 |
/* Input size optimized Shell sort. */ |
| 80 |
typedef BROTLI_BOOL (*HuffmanTreeComparator)( |
| 81 |
const HuffmanTree*, const HuffmanTree*); |
| 82 |
static BROTLI_INLINE void SortHuffmanTreeItems(HuffmanTree* items, |
| 83 |
const size_t n, HuffmanTreeComparator comparator) { |
| 84 |
if (n < 13) { |
| 85 |
/* Insertion sort. */ |
| 86 |
size_t i; |
| 87 |
for (i = 1; i < n; ++i) { |
| 88 |
HuffmanTree tmp = items[i]; |
| 89 |
size_t k = i; |
| 90 |
size_t j = i - 1; |
| 91 |
while (comparator(&tmp, &items[j])) { |
| 92 |
items[k] = items[j]; |
| 93 |
k = j; |
| 94 |
if (!j--) break; |
| 95 |
} |
| 96 |
items[k] = tmp; |
| 97 |
} |
| 98 |
return; |
| 99 |
} else { |
| 100 |
/* Shell sort. */ |
| 101 |
int g = n < 57 ? 2 : 0; |
| 102 |
for (; g < 6; ++g) { |
| 103 |
size_t gap = kBrotliShellGaps[g]; |
| 104 |
size_t i; |
| 105 |
for (i = gap; i < n; ++i) { |
| 106 |
size_t j = i; |
| 107 |
HuffmanTree tmp = items[i]; |
| 108 |
for (; j >= gap && comparator(&tmp, &items[j - gap]); j -= gap) { |
| 109 |
items[j] = items[j - gap]; |
| 110 |
} |
| 111 |
items[j] = tmp; |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
#if defined(__cplusplus) || defined(c_plusplus) |
| 118 |
} /* extern "C" */ |
| 119 |
#endif |
| 120 |
|
| 121 |
#endif /* BROTLI_ENC_ENTROPY_ENCODE_H_ */ |