multi_compress 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +13 -0
- data/LICENSE.txt +21 -0
- data/README.md +116 -0
- data/ext/multi_compress/extconf.rb +171 -0
- data/ext/multi_compress/multi_compress.c +1534 -0
- data/ext/multi_compress/vendor/brotli/c/common/constants.c +15 -0
- data/ext/multi_compress/vendor/brotli/c/common/constants.h +201 -0
- data/ext/multi_compress/vendor/brotli/c/common/context.c +156 -0
- data/ext/multi_compress/vendor/brotli/c/common/context.h +113 -0
- data/ext/multi_compress/vendor/brotli/c/common/dictionary.c +5916 -0
- data/ext/multi_compress/vendor/brotli/c/common/dictionary.h +64 -0
- data/ext/multi_compress/vendor/brotli/c/common/platform.c +23 -0
- data/ext/multi_compress/vendor/brotli/c/common/platform.h +541 -0
- data/ext/multi_compress/vendor/brotli/c/common/shared_dictionary.c +521 -0
- data/ext/multi_compress/vendor/brotli/c/common/shared_dictionary_internal.h +75 -0
- data/ext/multi_compress/vendor/brotli/c/common/transform.c +291 -0
- data/ext/multi_compress/vendor/brotli/c/common/transform.h +85 -0
- data/ext/multi_compress/vendor/brotli/c/common/version.h +51 -0
- data/ext/multi_compress/vendor/brotli/c/dec/bit_reader.c +78 -0
- data/ext/multi_compress/vendor/brotli/c/dec/bit_reader.h +423 -0
- data/ext/multi_compress/vendor/brotli/c/dec/decode.c +2875 -0
- data/ext/multi_compress/vendor/brotli/c/dec/huffman.c +342 -0
- data/ext/multi_compress/vendor/brotli/c/dec/huffman.h +122 -0
- data/ext/multi_compress/vendor/brotli/c/dec/prefix.h +733 -0
- data/ext/multi_compress/vendor/brotli/c/dec/state.c +183 -0
- data/ext/multi_compress/vendor/brotli/c/dec/state.h +400 -0
- data/ext/multi_compress/vendor/brotli/c/enc/backward_references.c +207 -0
- data/ext/multi_compress/vendor/brotli/c/enc/backward_references.h +40 -0
- data/ext/multi_compress/vendor/brotli/c/enc/backward_references_hq.c +939 -0
- data/ext/multi_compress/vendor/brotli/c/enc/backward_references_hq.h +96 -0
- data/ext/multi_compress/vendor/brotli/c/enc/backward_references_inc.h +189 -0
- data/ext/multi_compress/vendor/brotli/c/enc/bit_cost.c +36 -0
- data/ext/multi_compress/vendor/brotli/c/enc/bit_cost.h +64 -0
- data/ext/multi_compress/vendor/brotli/c/enc/bit_cost_inc.h +127 -0
- data/ext/multi_compress/vendor/brotli/c/enc/block_encoder_inc.h +34 -0
- data/ext/multi_compress/vendor/brotli/c/enc/block_splitter.c +217 -0
- data/ext/multi_compress/vendor/brotli/c/enc/block_splitter.h +52 -0
- data/ext/multi_compress/vendor/brotli/c/enc/block_splitter_inc.h +481 -0
- data/ext/multi_compress/vendor/brotli/c/enc/brotli_bit_stream.c +1336 -0
- data/ext/multi_compress/vendor/brotli/c/enc/brotli_bit_stream.h +89 -0
- data/ext/multi_compress/vendor/brotli/c/enc/cluster.c +57 -0
- data/ext/multi_compress/vendor/brotli/c/enc/cluster.h +49 -0
- data/ext/multi_compress/vendor/brotli/c/enc/cluster_inc.h +325 -0
- data/ext/multi_compress/vendor/brotli/c/enc/command.c +28 -0
- data/ext/multi_compress/vendor/brotli/c/enc/command.h +191 -0
- data/ext/multi_compress/vendor/brotli/c/enc/compound_dictionary.c +207 -0
- data/ext/multi_compress/vendor/brotli/c/enc/compound_dictionary.h +74 -0
- data/ext/multi_compress/vendor/brotli/c/enc/compress_fragment.c +800 -0
- data/ext/multi_compress/vendor/brotli/c/enc/compress_fragment.h +86 -0
- data/ext/multi_compress/vendor/brotli/c/enc/compress_fragment_two_pass.c +657 -0
- data/ext/multi_compress/vendor/brotli/c/enc/compress_fragment_two_pass.h +72 -0
- data/ext/multi_compress/vendor/brotli/c/enc/dictionary_hash.c +1848 -0
- data/ext/multi_compress/vendor/brotli/c/enc/dictionary_hash.h +25 -0
- data/ext/multi_compress/vendor/brotli/c/enc/encode.c +1996 -0
- data/ext/multi_compress/vendor/brotli/c/enc/encoder_dict.c +640 -0
- data/ext/multi_compress/vendor/brotli/c/enc/encoder_dict.h +157 -0
- data/ext/multi_compress/vendor/brotli/c/enc/entropy_encode.c +504 -0
- data/ext/multi_compress/vendor/brotli/c/enc/entropy_encode.h +123 -0
- data/ext/multi_compress/vendor/brotli/c/enc/entropy_encode_static.h +542 -0
- data/ext/multi_compress/vendor/brotli/c/enc/fast_log.c +105 -0
- data/ext/multi_compress/vendor/brotli/c/enc/fast_log.h +67 -0
- data/ext/multi_compress/vendor/brotli/c/enc/find_match_length.h +72 -0
- data/ext/multi_compress/vendor/brotli/c/enc/hash.h +728 -0
- data/ext/multi_compress/vendor/brotli/c/enc/hash_composite_inc.h +140 -0
- data/ext/multi_compress/vendor/brotli/c/enc/hash_forgetful_chain_inc.h +295 -0
- data/ext/multi_compress/vendor/brotli/c/enc/hash_longest_match64_inc.h +262 -0
- data/ext/multi_compress/vendor/brotli/c/enc/hash_longest_match_inc.h +258 -0
- data/ext/multi_compress/vendor/brotli/c/enc/hash_longest_match_quickly_inc.h +266 -0
- data/ext/multi_compress/vendor/brotli/c/enc/hash_rolling_inc.h +212 -0
- data/ext/multi_compress/vendor/brotli/c/enc/hash_to_binary_tree_inc.h +330 -0
- data/ext/multi_compress/vendor/brotli/c/enc/histogram.c +100 -0
- data/ext/multi_compress/vendor/brotli/c/enc/histogram.h +64 -0
- data/ext/multi_compress/vendor/brotli/c/enc/histogram_inc.h +51 -0
- data/ext/multi_compress/vendor/brotli/c/enc/literal_cost.c +180 -0
- data/ext/multi_compress/vendor/brotli/c/enc/literal_cost.h +32 -0
- data/ext/multi_compress/vendor/brotli/c/enc/memory.c +194 -0
- data/ext/multi_compress/vendor/brotli/c/enc/memory.h +131 -0
- data/ext/multi_compress/vendor/brotli/c/enc/metablock.c +677 -0
- data/ext/multi_compress/vendor/brotli/c/enc/metablock.h +106 -0
- data/ext/multi_compress/vendor/brotli/c/enc/metablock_inc.h +185 -0
- data/ext/multi_compress/vendor/brotli/c/enc/params.h +47 -0
- data/ext/multi_compress/vendor/brotli/c/enc/prefix.h +54 -0
- data/ext/multi_compress/vendor/brotli/c/enc/quality.h +202 -0
- data/ext/multi_compress/vendor/brotli/c/enc/ringbuffer.h +168 -0
- data/ext/multi_compress/vendor/brotli/c/enc/state.h +104 -0
- data/ext/multi_compress/vendor/brotli/c/enc/static_dict.c +542 -0
- data/ext/multi_compress/vendor/brotli/c/enc/static_dict.h +41 -0
- data/ext/multi_compress/vendor/brotli/c/enc/static_dict_lut.h +5866 -0
- data/ext/multi_compress/vendor/brotli/c/enc/utf8_util.c +85 -0
- data/ext/multi_compress/vendor/brotli/c/enc/utf8_util.h +33 -0
- data/ext/multi_compress/vendor/brotli/c/enc/write_bits.h +88 -0
- data/ext/multi_compress/vendor/brotli/c/include/brotli/decode.h +409 -0
- data/ext/multi_compress/vendor/brotli/c/include/brotli/encode.h +501 -0
- data/ext/multi_compress/vendor/brotli/c/include/brotli/port.h +305 -0
- data/ext/multi_compress/vendor/brotli/c/include/brotli/shared_dictionary.h +100 -0
- data/ext/multi_compress/vendor/brotli/c/include/brotli/types.h +83 -0
- data/ext/multi_compress/vendor/lz4/lib/LICENSE +24 -0
- data/ext/multi_compress/vendor/lz4/lib/Makefile +244 -0
- data/ext/multi_compress/vendor/lz4/lib/README.md +193 -0
- data/ext/multi_compress/vendor/lz4/lib/dll/example/Makefile +63 -0
- data/ext/multi_compress/vendor/lz4/lib/dll/example/README.md +69 -0
- data/ext/multi_compress/vendor/lz4/lib/dll/example/fullbench-dll.sln +25 -0
- data/ext/multi_compress/vendor/lz4/lib/dll/example/fullbench-dll.vcxproj +182 -0
- data/ext/multi_compress/vendor/lz4/lib/liblz4-dll.rc.in +35 -0
- data/ext/multi_compress/vendor/lz4/lib/liblz4.pc.in +14 -0
- data/ext/multi_compress/vendor/lz4/lib/lz4.c +2829 -0
- data/ext/multi_compress/vendor/lz4/lib/lz4.h +884 -0
- data/ext/multi_compress/vendor/lz4/lib/lz4file.c +341 -0
- data/ext/multi_compress/vendor/lz4/lib/lz4file.h +93 -0
- data/ext/multi_compress/vendor/lz4/lib/lz4frame.c +2136 -0
- data/ext/multi_compress/vendor/lz4/lib/lz4frame.h +751 -0
- data/ext/multi_compress/vendor/lz4/lib/lz4frame_static.h +47 -0
- data/ext/multi_compress/vendor/lz4/lib/lz4hc.c +2192 -0
- data/ext/multi_compress/vendor/lz4/lib/lz4hc.h +414 -0
- data/ext/multi_compress/vendor/lz4/lib/xxhash.c +1030 -0
- data/ext/multi_compress/vendor/lz4/lib/xxhash.h +328 -0
- data/ext/multi_compress/vendor/zstd/lib/BUCK +232 -0
- data/ext/multi_compress/vendor/zstd/lib/Makefile +369 -0
- data/ext/multi_compress/vendor/zstd/lib/README.md +237 -0
- data/ext/multi_compress/vendor/zstd/lib/common/allocations.h +55 -0
- data/ext/multi_compress/vendor/zstd/lib/common/bits.h +200 -0
- data/ext/multi_compress/vendor/zstd/lib/common/bitstream.h +457 -0
- data/ext/multi_compress/vendor/zstd/lib/common/compiler.h +450 -0
- data/ext/multi_compress/vendor/zstd/lib/common/cpu.h +249 -0
- data/ext/multi_compress/vendor/zstd/lib/common/debug.c +30 -0
- data/ext/multi_compress/vendor/zstd/lib/common/debug.h +116 -0
- data/ext/multi_compress/vendor/zstd/lib/common/entropy_common.c +340 -0
- data/ext/multi_compress/vendor/zstd/lib/common/error_private.c +63 -0
- data/ext/multi_compress/vendor/zstd/lib/common/error_private.h +168 -0
- data/ext/multi_compress/vendor/zstd/lib/common/fse.h +640 -0
- data/ext/multi_compress/vendor/zstd/lib/common/fse_decompress.c +313 -0
- data/ext/multi_compress/vendor/zstd/lib/common/huf.h +286 -0
- data/ext/multi_compress/vendor/zstd/lib/common/mem.h +426 -0
- data/ext/multi_compress/vendor/zstd/lib/common/pool.c +371 -0
- data/ext/multi_compress/vendor/zstd/lib/common/pool.h +90 -0
- data/ext/multi_compress/vendor/zstd/lib/common/portability_macros.h +158 -0
- data/ext/multi_compress/vendor/zstd/lib/common/threading.c +182 -0
- data/ext/multi_compress/vendor/zstd/lib/common/threading.h +150 -0
- data/ext/multi_compress/vendor/zstd/lib/common/xxhash.c +18 -0
- data/ext/multi_compress/vendor/zstd/lib/common/xxhash.h +7020 -0
- data/ext/multi_compress/vendor/zstd/lib/common/zstd_common.c +48 -0
- data/ext/multi_compress/vendor/zstd/lib/common/zstd_deps.h +111 -0
- data/ext/multi_compress/vendor/zstd/lib/common/zstd_internal.h +392 -0
- data/ext/multi_compress/vendor/zstd/lib/common/zstd_trace.h +163 -0
- data/ext/multi_compress/vendor/zstd/lib/compress/clevels.h +134 -0
- data/ext/multi_compress/vendor/zstd/lib/compress/fse_compress.c +625 -0
- data/ext/multi_compress/vendor/zstd/lib/compress/hist.c +181 -0
- data/ext/multi_compress/vendor/zstd/lib/compress/hist.h +75 -0
- data/ext/multi_compress/vendor/zstd/lib/compress/huf_compress.c +1464 -0
- data/ext/multi_compress/vendor/zstd/lib/compress/zstd_compress.c +7153 -0
- data/ext/multi_compress/vendor/zstd/lib/compress/zstd_compress_internal.h +1534 -0
- data/ext/multi_compress/vendor/zstd/lib/compress/zstd_compress_literals.c +235 -0
- data/ext/multi_compress/vendor/zstd/lib/compress/zstd_compress_literals.h +39 -0
- data/ext/multi_compress/vendor/zstd/lib/compress/zstd_compress_sequences.c +442 -0
- data/ext/multi_compress/vendor/zstd/lib/compress/zstd_compress_sequences.h +54 -0
- data/ext/multi_compress/vendor/zstd/lib/compress/zstd_compress_superblock.c +688 -0
- data/ext/multi_compress/vendor/zstd/lib/compress/zstd_compress_superblock.h +32 -0
- data/ext/multi_compress/vendor/zstd/lib/compress/zstd_cwksp.h +748 -0
- data/ext/multi_compress/vendor/zstd/lib/compress/zstd_double_fast.c +770 -0
- data/ext/multi_compress/vendor/zstd/lib/compress/zstd_double_fast.h +50 -0
- data/ext/multi_compress/vendor/zstd/lib/compress/zstd_fast.c +968 -0
- data/ext/multi_compress/vendor/zstd/lib/compress/zstd_fast.h +38 -0
- data/ext/multi_compress/vendor/zstd/lib/compress/zstd_lazy.c +2199 -0
- data/ext/multi_compress/vendor/zstd/lib/compress/zstd_lazy.h +202 -0
- data/ext/multi_compress/vendor/zstd/lib/compress/zstd_ldm.c +730 -0
- data/ext/multi_compress/vendor/zstd/lib/compress/zstd_ldm.h +117 -0
- data/ext/multi_compress/vendor/zstd/lib/compress/zstd_ldm_geartab.h +106 -0
- data/ext/multi_compress/vendor/zstd/lib/compress/zstd_opt.c +1576 -0
- data/ext/multi_compress/vendor/zstd/lib/compress/zstd_opt.h +80 -0
- data/ext/multi_compress/vendor/zstd/lib/compress/zstdmt_compress.c +1882 -0
- data/ext/multi_compress/vendor/zstd/lib/compress/zstdmt_compress.h +113 -0
- data/ext/multi_compress/vendor/zstd/lib/decompress/huf_decompress.c +1944 -0
- data/ext/multi_compress/vendor/zstd/lib/decompress/huf_decompress_amd64.S +595 -0
- data/ext/multi_compress/vendor/zstd/lib/decompress/zstd_ddict.c +244 -0
- data/ext/multi_compress/vendor/zstd/lib/decompress/zstd_ddict.h +44 -0
- data/ext/multi_compress/vendor/zstd/lib/decompress/zstd_decompress.c +2407 -0
- data/ext/multi_compress/vendor/zstd/lib/decompress/zstd_decompress_block.c +2215 -0
- data/ext/multi_compress/vendor/zstd/lib/decompress/zstd_decompress_block.h +73 -0
- data/ext/multi_compress/vendor/zstd/lib/decompress/zstd_decompress_internal.h +240 -0
- data/ext/multi_compress/vendor/zstd/lib/deprecated/zbuff.h +214 -0
- data/ext/multi_compress/vendor/zstd/lib/deprecated/zbuff_common.c +26 -0
- data/ext/multi_compress/vendor/zstd/lib/deprecated/zbuff_compress.c +167 -0
- data/ext/multi_compress/vendor/zstd/lib/deprecated/zbuff_decompress.c +77 -0
- data/ext/multi_compress/vendor/zstd/lib/dictBuilder/cover.c +1261 -0
- data/ext/multi_compress/vendor/zstd/lib/dictBuilder/cover.h +152 -0
- data/ext/multi_compress/vendor/zstd/lib/dictBuilder/divsufsort.c +1913 -0
- data/ext/multi_compress/vendor/zstd/lib/dictBuilder/divsufsort.h +67 -0
- data/ext/multi_compress/vendor/zstd/lib/dictBuilder/fastcover.c +766 -0
- data/ext/multi_compress/vendor/zstd/lib/dictBuilder/zdict.c +1133 -0
- data/ext/multi_compress/vendor/zstd/lib/dll/example/Makefile +48 -0
- data/ext/multi_compress/vendor/zstd/lib/dll/example/README.md +63 -0
- data/ext/multi_compress/vendor/zstd/lib/dll/example/build_package.bat +20 -0
- data/ext/multi_compress/vendor/zstd/lib/dll/example/fullbench-dll.sln +25 -0
- data/ext/multi_compress/vendor/zstd/lib/dll/example/fullbench-dll.vcxproj +181 -0
- data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_legacy.h +452 -0
- data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v01.c +2127 -0
- data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v01.h +94 -0
- data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v02.c +3465 -0
- data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v02.h +93 -0
- data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v03.c +3105 -0
- data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v03.h +93 -0
- data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v04.c +3598 -0
- data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v04.h +142 -0
- data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v05.c +4005 -0
- data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v05.h +162 -0
- data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v06.c +4106 -0
- data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v06.h +172 -0
- data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v07.c +4490 -0
- data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v07.h +187 -0
- data/ext/multi_compress/vendor/zstd/lib/libzstd.mk +237 -0
- data/ext/multi_compress/vendor/zstd/lib/libzstd.pc.in +16 -0
- data/ext/multi_compress/vendor/zstd/lib/module.modulemap +35 -0
- data/ext/multi_compress/vendor/zstd/lib/zdict.h +474 -0
- data/ext/multi_compress/vendor/zstd/lib/zstd.h +3089 -0
- data/ext/multi_compress/vendor/zstd/lib/zstd_errors.h +114 -0
- data/lib/multi_compress/version.rb +5 -0
- data/lib/multi_compress.rb +329 -0
- metadata +322 -0
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/* Copyright 2017 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
|
+
#ifndef BROTLI_ENC_ENCODER_DICT_H_
|
|
8
|
+
#define BROTLI_ENC_ENCODER_DICT_H_
|
|
9
|
+
|
|
10
|
+
#include <brotli/shared_dictionary.h>
|
|
11
|
+
#include <brotli/types.h>
|
|
12
|
+
|
|
13
|
+
#include "../common/dictionary.h"
|
|
14
|
+
#include "../common/platform.h"
|
|
15
|
+
#include "compound_dictionary.h"
|
|
16
|
+
#include "memory.h"
|
|
17
|
+
#include "static_dict_lut.h"
|
|
18
|
+
|
|
19
|
+
#if defined(__cplusplus) || defined(c_plusplus)
|
|
20
|
+
extern "C" {
|
|
21
|
+
#endif
|
|
22
|
+
|
|
23
|
+
/*
|
|
24
|
+
Dictionary hierarchy for Encoder:
|
|
25
|
+
-SharedEncoderDictionary
|
|
26
|
+
--CompoundDictionary
|
|
27
|
+
---PreparedDictionary [up to 15x]
|
|
28
|
+
= prefix dictionary with precomputed hashes
|
|
29
|
+
--ContextualEncoderDictionary
|
|
30
|
+
---BrotliEncoderDictionary [up to 64x]
|
|
31
|
+
= for each context, precomputed static dictionary with words + transforms
|
|
32
|
+
|
|
33
|
+
Dictionary hiearchy from common: similar, but without precomputed hashes
|
|
34
|
+
-BrotliSharedDictionary
|
|
35
|
+
--BrotliDictionary [up to 64x]
|
|
36
|
+
--BrotliTransforms [up to 64x]
|
|
37
|
+
--const uint8_t* prefix [up to 15x]: compound dictionaries
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
typedef struct BrotliTrieNode {
|
|
41
|
+
uint8_t single; /* if 1, sub is a single node for c instead of 256 */
|
|
42
|
+
uint8_t c;
|
|
43
|
+
uint8_t len_; /* untransformed length */
|
|
44
|
+
uint32_t idx_; /* word index + num words * transform index */
|
|
45
|
+
uint32_t sub; /* index of sub node(s) in the pool */
|
|
46
|
+
} BrotliTrieNode;
|
|
47
|
+
|
|
48
|
+
typedef struct BrotliTrie {
|
|
49
|
+
BrotliTrieNode* pool;
|
|
50
|
+
size_t pool_capacity;
|
|
51
|
+
size_t pool_size;
|
|
52
|
+
BrotliTrieNode root;
|
|
53
|
+
} BrotliTrie;
|
|
54
|
+
|
|
55
|
+
#if defined(BROTLI_EXPERIMENTAL)
|
|
56
|
+
BROTLI_INTERNAL const BrotliTrieNode* BrotliTrieSub(const BrotliTrie* trie,
|
|
57
|
+
const BrotliTrieNode* node, uint8_t c);
|
|
58
|
+
#endif /* BROTLI_EXPERIMENTAL */
|
|
59
|
+
|
|
60
|
+
/* Dictionary data (words and transforms) for 1 possible context */
|
|
61
|
+
typedef struct BrotliEncoderDictionary {
|
|
62
|
+
const BrotliDictionary* words;
|
|
63
|
+
uint32_t num_transforms;
|
|
64
|
+
|
|
65
|
+
/* cut off for fast encoder */
|
|
66
|
+
uint32_t cutoffTransformsCount;
|
|
67
|
+
uint64_t cutoffTransforms;
|
|
68
|
+
|
|
69
|
+
/* from dictionary_hash.h, for fast encoder */
|
|
70
|
+
const uint16_t* hash_table_words;
|
|
71
|
+
const uint8_t* hash_table_lengths;
|
|
72
|
+
|
|
73
|
+
/* from static_dict_lut.h, for slow encoder */
|
|
74
|
+
const uint16_t* buckets;
|
|
75
|
+
const DictWord* dict_words;
|
|
76
|
+
/* Heavy version, for use by slow encoder when there are custom transforms.
|
|
77
|
+
Contains every possible transformed dictionary word in a trie. It encodes
|
|
78
|
+
about as fast as the non-heavy encoder but consumes a lot of memory and
|
|
79
|
+
takes time to build. */
|
|
80
|
+
BrotliTrie trie;
|
|
81
|
+
BROTLI_BOOL has_words_heavy;
|
|
82
|
+
|
|
83
|
+
/* Reference to other dictionaries. */
|
|
84
|
+
const struct ContextualEncoderDictionary* parent;
|
|
85
|
+
|
|
86
|
+
/* Allocated memory, used only when not using the Brotli defaults */
|
|
87
|
+
uint16_t* hash_table_data_words_;
|
|
88
|
+
uint8_t* hash_table_data_lengths_;
|
|
89
|
+
size_t buckets_alloc_size_;
|
|
90
|
+
uint16_t* buckets_data_;
|
|
91
|
+
size_t dict_words_alloc_size_;
|
|
92
|
+
DictWord* dict_words_data_;
|
|
93
|
+
BrotliDictionary* words_instance_;
|
|
94
|
+
} BrotliEncoderDictionary;
|
|
95
|
+
|
|
96
|
+
/* Dictionary data for all 64 contexts */
|
|
97
|
+
typedef struct ContextualEncoderDictionary {
|
|
98
|
+
BROTLI_BOOL context_based;
|
|
99
|
+
uint8_t num_dictionaries;
|
|
100
|
+
uint8_t context_map[SHARED_BROTLI_NUM_DICTIONARY_CONTEXTS];
|
|
101
|
+
const BrotliEncoderDictionary* dict[SHARED_BROTLI_NUM_DICTIONARY_CONTEXTS];
|
|
102
|
+
|
|
103
|
+
/* If num_instances_ is 1, instance_ is used, else dynamic allocation with
|
|
104
|
+
instances_ is used. */
|
|
105
|
+
size_t num_instances_;
|
|
106
|
+
BrotliEncoderDictionary instance_;
|
|
107
|
+
BrotliEncoderDictionary* instances_;
|
|
108
|
+
} ContextualEncoderDictionary;
|
|
109
|
+
|
|
110
|
+
typedef struct SharedEncoderDictionary {
|
|
111
|
+
/* Magic value to distinguish this struct from PreparedDictionary for
|
|
112
|
+
certain external usages. */
|
|
113
|
+
uint32_t magic;
|
|
114
|
+
|
|
115
|
+
/* LZ77 prefix, compound dictionary */
|
|
116
|
+
CompoundDictionary compound;
|
|
117
|
+
|
|
118
|
+
/* Custom static dictionary (optionally context-based) */
|
|
119
|
+
ContextualEncoderDictionary contextual;
|
|
120
|
+
|
|
121
|
+
/* The maximum quality the dictionary was computed for */
|
|
122
|
+
int max_quality;
|
|
123
|
+
} SharedEncoderDictionary;
|
|
124
|
+
|
|
125
|
+
typedef struct ManagedDictionary {
|
|
126
|
+
uint32_t magic;
|
|
127
|
+
MemoryManager memory_manager_;
|
|
128
|
+
uint32_t* dictionary;
|
|
129
|
+
} ManagedDictionary;
|
|
130
|
+
|
|
131
|
+
/* Initializes to the brotli built-in dictionary */
|
|
132
|
+
BROTLI_INTERNAL void BrotliInitSharedEncoderDictionary(
|
|
133
|
+
SharedEncoderDictionary* dict);
|
|
134
|
+
|
|
135
|
+
#if defined(BROTLI_EXPERIMENTAL)
|
|
136
|
+
/* Initializes to shared dictionary that will be parsed from
|
|
137
|
+
encoded_dict. Requires that you keep the encoded_dict buffer
|
|
138
|
+
around, parts of data will point to it. */
|
|
139
|
+
BROTLI_INTERNAL BROTLI_BOOL BrotliInitCustomSharedEncoderDictionary(
|
|
140
|
+
MemoryManager* m, const uint8_t* encoded_dict, size_t size,
|
|
141
|
+
int quality, SharedEncoderDictionary* dict);
|
|
142
|
+
#endif /* BROTLI_EXPERIMENTAL */
|
|
143
|
+
|
|
144
|
+
BROTLI_INTERNAL void BrotliCleanupSharedEncoderDictionary(
|
|
145
|
+
MemoryManager* m, SharedEncoderDictionary* dict);
|
|
146
|
+
|
|
147
|
+
BROTLI_INTERNAL ManagedDictionary* BrotliCreateManagedDictionary(
|
|
148
|
+
brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque);
|
|
149
|
+
|
|
150
|
+
BROTLI_INTERNAL void BrotliDestroyManagedDictionary(
|
|
151
|
+
ManagedDictionary* dictionary);
|
|
152
|
+
|
|
153
|
+
#if defined(__cplusplus) || defined(c_plusplus)
|
|
154
|
+
} /* extern "C" */
|
|
155
|
+
#endif
|
|
156
|
+
|
|
157
|
+
#endif /* BROTLI_ENC_ENCODER_DICT_H_ */
|
|
@@ -0,0 +1,504 @@
|
|
|
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
|
+
#include "entropy_encode.h"
|
|
10
|
+
|
|
11
|
+
#include <string.h> /* memset */
|
|
12
|
+
|
|
13
|
+
#include <brotli/types.h>
|
|
14
|
+
|
|
15
|
+
#include "../common/constants.h"
|
|
16
|
+
#include "../common/platform.h"
|
|
17
|
+
|
|
18
|
+
#if defined(__cplusplus) || defined(c_plusplus)
|
|
19
|
+
extern "C" {
|
|
20
|
+
#endif
|
|
21
|
+
|
|
22
|
+
const size_t kBrotliShellGaps[] = {132, 57, 23, 10, 4, 1};
|
|
23
|
+
|
|
24
|
+
BROTLI_BOOL BrotliSetDepth(
|
|
25
|
+
int p0, HuffmanTree* pool, uint8_t* depth, int max_depth) {
|
|
26
|
+
int stack[16];
|
|
27
|
+
int level = 0;
|
|
28
|
+
int p = p0;
|
|
29
|
+
BROTLI_DCHECK(max_depth <= 15);
|
|
30
|
+
stack[0] = -1;
|
|
31
|
+
while (BROTLI_TRUE) {
|
|
32
|
+
if (pool[p].index_left_ >= 0) {
|
|
33
|
+
level++;
|
|
34
|
+
if (level > max_depth) return BROTLI_FALSE;
|
|
35
|
+
stack[level] = pool[p].index_right_or_value_;
|
|
36
|
+
p = pool[p].index_left_;
|
|
37
|
+
continue;
|
|
38
|
+
} else {
|
|
39
|
+
depth[pool[p].index_right_or_value_] = (uint8_t)level;
|
|
40
|
+
}
|
|
41
|
+
while (level >= 0 && stack[level] == -1) level--;
|
|
42
|
+
if (level < 0) return BROTLI_TRUE;
|
|
43
|
+
p = stack[level];
|
|
44
|
+
stack[level] = -1;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/* Sort the root nodes, least popular first. */
|
|
49
|
+
static BROTLI_INLINE BROTLI_BOOL SortHuffmanTree(
|
|
50
|
+
const HuffmanTree* v0, const HuffmanTree* v1) {
|
|
51
|
+
if (v0->total_count_ != v1->total_count_) {
|
|
52
|
+
return TO_BROTLI_BOOL(v0->total_count_ < v1->total_count_);
|
|
53
|
+
}
|
|
54
|
+
return TO_BROTLI_BOOL(v0->index_right_or_value_ > v1->index_right_or_value_);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/* This function will create a Huffman tree.
|
|
58
|
+
|
|
59
|
+
The catch here is that the tree cannot be arbitrarily deep.
|
|
60
|
+
Brotli specifies a maximum depth of 15 bits for "code trees"
|
|
61
|
+
and 7 bits for "code length code trees."
|
|
62
|
+
|
|
63
|
+
count_limit is the value that is to be faked as the minimum value
|
|
64
|
+
and this minimum value is raised until the tree matches the
|
|
65
|
+
maximum length requirement.
|
|
66
|
+
|
|
67
|
+
This algorithm is not of excellent performance for very long data blocks,
|
|
68
|
+
especially when population counts are longer than 2**tree_limit, but
|
|
69
|
+
we are not planning to use this with extremely long blocks.
|
|
70
|
+
|
|
71
|
+
See http://en.wikipedia.org/wiki/Huffman_coding */
|
|
72
|
+
void BrotliCreateHuffmanTree(const uint32_t* data,
|
|
73
|
+
const size_t length,
|
|
74
|
+
const int tree_limit,
|
|
75
|
+
HuffmanTree* tree,
|
|
76
|
+
uint8_t* depth) {
|
|
77
|
+
uint32_t count_limit;
|
|
78
|
+
HuffmanTree sentinel;
|
|
79
|
+
InitHuffmanTree(&sentinel, BROTLI_UINT32_MAX, -1, -1);
|
|
80
|
+
/* For block sizes below 64 kB, we never need to do a second iteration
|
|
81
|
+
of this loop. Probably all of our block sizes will be smaller than
|
|
82
|
+
that, so this loop is mostly of academic interest. If we actually
|
|
83
|
+
would need this, we would be better off with the Katajainen algorithm. */
|
|
84
|
+
for (count_limit = 1; ; count_limit *= 2) {
|
|
85
|
+
size_t n = 0;
|
|
86
|
+
size_t i;
|
|
87
|
+
size_t j;
|
|
88
|
+
size_t k;
|
|
89
|
+
for (i = length; i != 0;) {
|
|
90
|
+
--i;
|
|
91
|
+
if (data[i]) {
|
|
92
|
+
const uint32_t count = BROTLI_MAX(uint32_t, data[i], count_limit);
|
|
93
|
+
InitHuffmanTree(&tree[n++], count, -1, (int16_t)i);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (n == 1) {
|
|
98
|
+
depth[tree[0].index_right_or_value_] = 1; /* Only one element. */
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
SortHuffmanTreeItems(tree, n, SortHuffmanTree);
|
|
103
|
+
|
|
104
|
+
/* The nodes are:
|
|
105
|
+
[0, n): the sorted leaf nodes that we start with.
|
|
106
|
+
[n]: we add a sentinel here.
|
|
107
|
+
[n + 1, 2n): new parent nodes are added here, starting from
|
|
108
|
+
(n+1). These are naturally in ascending order.
|
|
109
|
+
[2n]: we add a sentinel at the end as well.
|
|
110
|
+
There will be (2n+1) elements at the end. */
|
|
111
|
+
tree[n] = sentinel;
|
|
112
|
+
tree[n + 1] = sentinel;
|
|
113
|
+
|
|
114
|
+
i = 0; /* Points to the next leaf node. */
|
|
115
|
+
j = n + 1; /* Points to the next non-leaf node. */
|
|
116
|
+
for (k = n - 1; k != 0; --k) {
|
|
117
|
+
size_t left, right;
|
|
118
|
+
if (tree[i].total_count_ <= tree[j].total_count_) {
|
|
119
|
+
left = i;
|
|
120
|
+
++i;
|
|
121
|
+
} else {
|
|
122
|
+
left = j;
|
|
123
|
+
++j;
|
|
124
|
+
}
|
|
125
|
+
if (tree[i].total_count_ <= tree[j].total_count_) {
|
|
126
|
+
right = i;
|
|
127
|
+
++i;
|
|
128
|
+
} else {
|
|
129
|
+
right = j;
|
|
130
|
+
++j;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
{
|
|
134
|
+
/* The sentinel node becomes the parent node. */
|
|
135
|
+
size_t j_end = 2 * n - k;
|
|
136
|
+
tree[j_end].total_count_ =
|
|
137
|
+
tree[left].total_count_ + tree[right].total_count_;
|
|
138
|
+
tree[j_end].index_left_ = (int16_t)left;
|
|
139
|
+
tree[j_end].index_right_or_value_ = (int16_t)right;
|
|
140
|
+
|
|
141
|
+
/* Add back the last sentinel node. */
|
|
142
|
+
tree[j_end + 1] = sentinel;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
if (BrotliSetDepth((int)(2 * n - 1), &tree[0], depth, tree_limit)) {
|
|
146
|
+
/* We need to pack the Huffman tree in tree_limit bits. If this was not
|
|
147
|
+
successful, add fake entities to the lowest values and retry. */
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
static void Reverse(uint8_t* v, size_t start, size_t end) {
|
|
154
|
+
--end;
|
|
155
|
+
while (start < end) {
|
|
156
|
+
uint8_t tmp = v[start];
|
|
157
|
+
v[start] = v[end];
|
|
158
|
+
v[end] = tmp;
|
|
159
|
+
++start;
|
|
160
|
+
--end;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
static void BrotliWriteHuffmanTreeRepetitions(
|
|
165
|
+
const uint8_t previous_value,
|
|
166
|
+
const uint8_t value,
|
|
167
|
+
size_t repetitions,
|
|
168
|
+
size_t* tree_size,
|
|
169
|
+
uint8_t* tree,
|
|
170
|
+
uint8_t* extra_bits_data) {
|
|
171
|
+
BROTLI_DCHECK(repetitions > 0);
|
|
172
|
+
if (previous_value != value) {
|
|
173
|
+
tree[*tree_size] = value;
|
|
174
|
+
extra_bits_data[*tree_size] = 0;
|
|
175
|
+
++(*tree_size);
|
|
176
|
+
--repetitions;
|
|
177
|
+
}
|
|
178
|
+
if (repetitions == 7) {
|
|
179
|
+
tree[*tree_size] = value;
|
|
180
|
+
extra_bits_data[*tree_size] = 0;
|
|
181
|
+
++(*tree_size);
|
|
182
|
+
--repetitions;
|
|
183
|
+
}
|
|
184
|
+
if (repetitions < 3) {
|
|
185
|
+
size_t i;
|
|
186
|
+
for (i = 0; i < repetitions; ++i) {
|
|
187
|
+
tree[*tree_size] = value;
|
|
188
|
+
extra_bits_data[*tree_size] = 0;
|
|
189
|
+
++(*tree_size);
|
|
190
|
+
}
|
|
191
|
+
} else {
|
|
192
|
+
size_t start = *tree_size;
|
|
193
|
+
repetitions -= 3;
|
|
194
|
+
while (BROTLI_TRUE) {
|
|
195
|
+
tree[*tree_size] = BROTLI_REPEAT_PREVIOUS_CODE_LENGTH;
|
|
196
|
+
extra_bits_data[*tree_size] = repetitions & 0x3;
|
|
197
|
+
++(*tree_size);
|
|
198
|
+
repetitions >>= 2;
|
|
199
|
+
if (repetitions == 0) {
|
|
200
|
+
break;
|
|
201
|
+
}
|
|
202
|
+
--repetitions;
|
|
203
|
+
}
|
|
204
|
+
Reverse(tree, start, *tree_size);
|
|
205
|
+
Reverse(extra_bits_data, start, *tree_size);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
static void BrotliWriteHuffmanTreeRepetitionsZeros(
|
|
210
|
+
size_t repetitions,
|
|
211
|
+
size_t* tree_size,
|
|
212
|
+
uint8_t* tree,
|
|
213
|
+
uint8_t* extra_bits_data) {
|
|
214
|
+
if (repetitions == 11) {
|
|
215
|
+
tree[*tree_size] = 0;
|
|
216
|
+
extra_bits_data[*tree_size] = 0;
|
|
217
|
+
++(*tree_size);
|
|
218
|
+
--repetitions;
|
|
219
|
+
}
|
|
220
|
+
if (repetitions < 3) {
|
|
221
|
+
size_t i;
|
|
222
|
+
for (i = 0; i < repetitions; ++i) {
|
|
223
|
+
tree[*tree_size] = 0;
|
|
224
|
+
extra_bits_data[*tree_size] = 0;
|
|
225
|
+
++(*tree_size);
|
|
226
|
+
}
|
|
227
|
+
} else {
|
|
228
|
+
size_t start = *tree_size;
|
|
229
|
+
repetitions -= 3;
|
|
230
|
+
while (BROTLI_TRUE) {
|
|
231
|
+
tree[*tree_size] = BROTLI_REPEAT_ZERO_CODE_LENGTH;
|
|
232
|
+
extra_bits_data[*tree_size] = repetitions & 0x7;
|
|
233
|
+
++(*tree_size);
|
|
234
|
+
repetitions >>= 3;
|
|
235
|
+
if (repetitions == 0) {
|
|
236
|
+
break;
|
|
237
|
+
}
|
|
238
|
+
--repetitions;
|
|
239
|
+
}
|
|
240
|
+
Reverse(tree, start, *tree_size);
|
|
241
|
+
Reverse(extra_bits_data, start, *tree_size);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
void BrotliOptimizeHuffmanCountsForRle(size_t length, uint32_t* counts,
|
|
246
|
+
uint8_t* good_for_rle) {
|
|
247
|
+
size_t nonzero_count = 0;
|
|
248
|
+
size_t stride;
|
|
249
|
+
size_t limit;
|
|
250
|
+
size_t sum;
|
|
251
|
+
const size_t streak_limit = 1240;
|
|
252
|
+
/* Let's make the Huffman code more compatible with RLE encoding. */
|
|
253
|
+
size_t i;
|
|
254
|
+
for (i = 0; i < length; i++) {
|
|
255
|
+
if (counts[i]) {
|
|
256
|
+
++nonzero_count;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
if (nonzero_count < 16) {
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
while (length != 0 && counts[length - 1] == 0) {
|
|
263
|
+
--length;
|
|
264
|
+
}
|
|
265
|
+
if (length == 0) {
|
|
266
|
+
return; /* All zeros. */
|
|
267
|
+
}
|
|
268
|
+
/* Now counts[0..length - 1] does not have trailing zeros. */
|
|
269
|
+
{
|
|
270
|
+
size_t nonzeros = 0;
|
|
271
|
+
uint32_t smallest_nonzero = 1 << 30;
|
|
272
|
+
for (i = 0; i < length; ++i) {
|
|
273
|
+
if (counts[i] != 0) {
|
|
274
|
+
++nonzeros;
|
|
275
|
+
if (smallest_nonzero > counts[i]) {
|
|
276
|
+
smallest_nonzero = counts[i];
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
if (nonzeros < 5) {
|
|
281
|
+
/* Small histogram will model it well. */
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
if (smallest_nonzero < 4) {
|
|
285
|
+
size_t zeros = length - nonzeros;
|
|
286
|
+
if (zeros < 6) {
|
|
287
|
+
for (i = 1; i < length - 1; ++i) {
|
|
288
|
+
if (counts[i - 1] != 0 && counts[i] == 0 && counts[i + 1] != 0) {
|
|
289
|
+
counts[i] = 1;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
if (nonzeros < 28) {
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
/* 2) Let's mark all population counts that already can be encoded
|
|
299
|
+
with an RLE code. */
|
|
300
|
+
memset(good_for_rle, 0, length);
|
|
301
|
+
{
|
|
302
|
+
/* Let's not spoil any of the existing good RLE codes.
|
|
303
|
+
Mark any seq of 0's that is longer as 5 as a good_for_rle.
|
|
304
|
+
Mark any seq of non-0's that is longer as 7 as a good_for_rle. */
|
|
305
|
+
uint32_t symbol = counts[0];
|
|
306
|
+
size_t step = 0;
|
|
307
|
+
for (i = 0; i <= length; ++i) {
|
|
308
|
+
if (i == length || counts[i] != symbol) {
|
|
309
|
+
if ((symbol == 0 && step >= 5) ||
|
|
310
|
+
(symbol != 0 && step >= 7)) {
|
|
311
|
+
size_t k;
|
|
312
|
+
for (k = 0; k < step; ++k) {
|
|
313
|
+
good_for_rle[i - k - 1] = 1;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
step = 1;
|
|
317
|
+
if (i != length) {
|
|
318
|
+
symbol = counts[i];
|
|
319
|
+
}
|
|
320
|
+
} else {
|
|
321
|
+
++step;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
/* 3) Let's replace those population counts that lead to more RLE codes.
|
|
326
|
+
Math here is in 24.8 fixed point representation. */
|
|
327
|
+
stride = 0;
|
|
328
|
+
limit = 256 * (counts[0] + counts[1] + counts[2]) / 3 + 420;
|
|
329
|
+
sum = 0;
|
|
330
|
+
for (i = 0; i <= length; ++i) {
|
|
331
|
+
if (i == length || good_for_rle[i] ||
|
|
332
|
+
(i != 0 && good_for_rle[i - 1]) ||
|
|
333
|
+
(256 * counts[i] - limit + streak_limit) >= 2 * streak_limit) {
|
|
334
|
+
if (stride >= 4 || (stride >= 3 && sum == 0)) {
|
|
335
|
+
size_t k;
|
|
336
|
+
/* The stride must end, collapse what we have, if we have enough (4). */
|
|
337
|
+
size_t count = (sum + stride / 2) / stride;
|
|
338
|
+
if (count == 0) {
|
|
339
|
+
count = 1;
|
|
340
|
+
}
|
|
341
|
+
if (sum == 0) {
|
|
342
|
+
/* Don't make an all zeros stride to be upgraded to ones. */
|
|
343
|
+
count = 0;
|
|
344
|
+
}
|
|
345
|
+
for (k = 0; k < stride; ++k) {
|
|
346
|
+
/* We don't want to change value at counts[i],
|
|
347
|
+
that is already belonging to the next stride. Thus - 1. */
|
|
348
|
+
counts[i - k - 1] = (uint32_t)count;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
stride = 0;
|
|
352
|
+
sum = 0;
|
|
353
|
+
if (i < length - 2) {
|
|
354
|
+
/* All interesting strides have a count of at least 4, */
|
|
355
|
+
/* at least when non-zeros. */
|
|
356
|
+
limit = 256 * (counts[i] + counts[i + 1] + counts[i + 2]) / 3 + 420;
|
|
357
|
+
} else if (i < length) {
|
|
358
|
+
limit = 256 * counts[i];
|
|
359
|
+
} else {
|
|
360
|
+
limit = 0;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
++stride;
|
|
364
|
+
if (i != length) {
|
|
365
|
+
sum += counts[i];
|
|
366
|
+
if (stride >= 4) {
|
|
367
|
+
limit = (256 * sum + stride / 2) / stride;
|
|
368
|
+
}
|
|
369
|
+
if (stride == 4) {
|
|
370
|
+
limit += 120;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
static void DecideOverRleUse(const uint8_t* depth, const size_t length,
|
|
377
|
+
BROTLI_BOOL* use_rle_for_non_zero,
|
|
378
|
+
BROTLI_BOOL* use_rle_for_zero) {
|
|
379
|
+
size_t total_reps_zero = 0;
|
|
380
|
+
size_t total_reps_non_zero = 0;
|
|
381
|
+
size_t count_reps_zero = 1;
|
|
382
|
+
size_t count_reps_non_zero = 1;
|
|
383
|
+
size_t i;
|
|
384
|
+
for (i = 0; i < length;) {
|
|
385
|
+
const uint8_t value = depth[i];
|
|
386
|
+
size_t reps = 1;
|
|
387
|
+
size_t k;
|
|
388
|
+
for (k = i + 1; k < length && depth[k] == value; ++k) {
|
|
389
|
+
++reps;
|
|
390
|
+
}
|
|
391
|
+
if (reps >= 3 && value == 0) {
|
|
392
|
+
total_reps_zero += reps;
|
|
393
|
+
++count_reps_zero;
|
|
394
|
+
}
|
|
395
|
+
if (reps >= 4 && value != 0) {
|
|
396
|
+
total_reps_non_zero += reps;
|
|
397
|
+
++count_reps_non_zero;
|
|
398
|
+
}
|
|
399
|
+
i += reps;
|
|
400
|
+
}
|
|
401
|
+
*use_rle_for_non_zero =
|
|
402
|
+
TO_BROTLI_BOOL(total_reps_non_zero > count_reps_non_zero * 2);
|
|
403
|
+
*use_rle_for_zero = TO_BROTLI_BOOL(total_reps_zero > count_reps_zero * 2);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
void BrotliWriteHuffmanTree(const uint8_t* depth,
|
|
407
|
+
size_t length,
|
|
408
|
+
size_t* tree_size,
|
|
409
|
+
uint8_t* tree,
|
|
410
|
+
uint8_t* extra_bits_data) {
|
|
411
|
+
uint8_t previous_value = BROTLI_INITIAL_REPEATED_CODE_LENGTH;
|
|
412
|
+
size_t i;
|
|
413
|
+
BROTLI_BOOL use_rle_for_non_zero = BROTLI_FALSE;
|
|
414
|
+
BROTLI_BOOL use_rle_for_zero = BROTLI_FALSE;
|
|
415
|
+
|
|
416
|
+
/* Throw away trailing zeros. */
|
|
417
|
+
size_t new_length = length;
|
|
418
|
+
for (i = 0; i < length; ++i) {
|
|
419
|
+
if (depth[length - i - 1] == 0) {
|
|
420
|
+
--new_length;
|
|
421
|
+
} else {
|
|
422
|
+
break;
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/* First gather statistics on if it is a good idea to do RLE. */
|
|
427
|
+
if (length > 50) {
|
|
428
|
+
/* Find RLE coding for longer codes.
|
|
429
|
+
Shorter codes seem not to benefit from RLE. */
|
|
430
|
+
DecideOverRleUse(depth, new_length,
|
|
431
|
+
&use_rle_for_non_zero, &use_rle_for_zero);
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/* Actual RLE coding. */
|
|
435
|
+
for (i = 0; i < new_length;) {
|
|
436
|
+
const uint8_t value = depth[i];
|
|
437
|
+
size_t reps = 1;
|
|
438
|
+
if ((value != 0 && use_rle_for_non_zero) ||
|
|
439
|
+
(value == 0 && use_rle_for_zero)) {
|
|
440
|
+
size_t k;
|
|
441
|
+
for (k = i + 1; k < new_length && depth[k] == value; ++k) {
|
|
442
|
+
++reps;
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
if (value == 0) {
|
|
446
|
+
BrotliWriteHuffmanTreeRepetitionsZeros(
|
|
447
|
+
reps, tree_size, tree, extra_bits_data);
|
|
448
|
+
} else {
|
|
449
|
+
BrotliWriteHuffmanTreeRepetitions(previous_value,
|
|
450
|
+
value, reps, tree_size,
|
|
451
|
+
tree, extra_bits_data);
|
|
452
|
+
previous_value = value;
|
|
453
|
+
}
|
|
454
|
+
i += reps;
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
static uint16_t BrotliReverseBits(size_t num_bits, uint16_t bits) {
|
|
459
|
+
static const size_t kLut[16] = { /* Pre-reversed 4-bit values. */
|
|
460
|
+
0x00, 0x08, 0x04, 0x0C, 0x02, 0x0A, 0x06, 0x0E,
|
|
461
|
+
0x01, 0x09, 0x05, 0x0D, 0x03, 0x0B, 0x07, 0x0F
|
|
462
|
+
};
|
|
463
|
+
size_t retval = kLut[bits & 0x0F];
|
|
464
|
+
size_t i;
|
|
465
|
+
for (i = 4; i < num_bits; i += 4) {
|
|
466
|
+
retval <<= 4;
|
|
467
|
+
bits = (uint16_t)(bits >> 4);
|
|
468
|
+
retval |= kLut[bits & 0x0F];
|
|
469
|
+
}
|
|
470
|
+
retval >>= ((0 - num_bits) & 0x03);
|
|
471
|
+
return (uint16_t)retval;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/* 0..15 are values for bits */
|
|
475
|
+
#define MAX_HUFFMAN_BITS 16
|
|
476
|
+
|
|
477
|
+
void BrotliConvertBitDepthsToSymbols(const uint8_t* depth,
|
|
478
|
+
size_t len,
|
|
479
|
+
uint16_t* bits) {
|
|
480
|
+
/* In Brotli, all bit depths are [1..15]
|
|
481
|
+
0 bit depth means that the symbol does not exist. */
|
|
482
|
+
uint16_t bl_count[MAX_HUFFMAN_BITS] = { 0 };
|
|
483
|
+
uint16_t next_code[MAX_HUFFMAN_BITS];
|
|
484
|
+
size_t i;
|
|
485
|
+
int code = 0;
|
|
486
|
+
for (i = 0; i < len; ++i) {
|
|
487
|
+
++bl_count[depth[i]];
|
|
488
|
+
}
|
|
489
|
+
bl_count[0] = 0;
|
|
490
|
+
next_code[0] = 0;
|
|
491
|
+
for (i = 1; i < MAX_HUFFMAN_BITS; ++i) {
|
|
492
|
+
code = (code + bl_count[i - 1]) << 1;
|
|
493
|
+
next_code[i] = (uint16_t)code;
|
|
494
|
+
}
|
|
495
|
+
for (i = 0; i < len; ++i) {
|
|
496
|
+
if (depth[i]) {
|
|
497
|
+
bits[i] = BrotliReverseBits(depth[i], next_code[depth[i]]++);
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
#if defined(__cplusplus) || defined(c_plusplus)
|
|
503
|
+
} /* extern "C" */
|
|
504
|
+
#endif
|