OLD | NEW |
1 /* Copyright 2013 Google Inc. All Rights Reserved. | 1 /* Copyright 2013 Google Inc. All Rights Reserved. |
2 | 2 |
3 Distributed under MIT license. | 3 Distributed under MIT license. |
4 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT | 4 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT |
5 */ | 5 */ |
6 | 6 |
7 // Models the histograms of literals, commands and distance codes. | 7 /* Models the histograms of literals, commands and distance codes. */ |
8 | 8 |
9 #ifndef BROTLI_ENC_HISTOGRAM_H_ | 9 #ifndef BROTLI_ENC_HISTOGRAM_H_ |
10 #define BROTLI_ENC_HISTOGRAM_H_ | 10 #define BROTLI_ENC_HISTOGRAM_H_ |
11 | 11 |
12 #include <cstring> | 12 #include <string.h> /* memset */ |
13 #include <limits> | 13 |
14 #include <vector> | 14 #include "../common/constants.h" |
| 15 #include <brotli/types.h> |
| 16 #include "./block_splitter.h" |
| 17 #include "./command.h" |
15 #include "./context.h" | 18 #include "./context.h" |
16 #include "./command.h" | 19 #include "./port.h" |
17 #include "./fast_log.h" | |
18 #include "./prefix.h" | |
19 #include "./types.h" | |
20 | 20 |
21 namespace brotli { | 21 #if defined(__cplusplus) || defined(c_plusplus) |
| 22 extern "C" { |
| 23 #endif |
22 | 24 |
23 struct BlockSplit; | 25 #define FN(X) X ## Literal |
| 26 #define DATA_SIZE BROTLI_NUM_LITERAL_SYMBOLS |
| 27 #define DataType uint8_t |
| 28 #include "./histogram_inc.h" /* NOLINT(build/include) */ |
| 29 #undef DataType |
| 30 #undef DATA_SIZE |
| 31 #undef FN |
24 | 32 |
25 // A simple container for histograms of data in blocks. | 33 #define FN(X) X ## Command |
26 template<int kDataSize> | 34 #define DataType uint16_t |
27 struct Histogram { | 35 #define DATA_SIZE BROTLI_NUM_COMMAND_SYMBOLS |
28 Histogram(void) { | 36 #include "./histogram_inc.h" /* NOLINT(build/include) */ |
29 Clear(); | 37 #undef DATA_SIZE |
30 } | 38 #undef FN |
31 void Clear(void) { | |
32 memset(data_, 0, sizeof(data_)); | |
33 total_count_ = 0; | |
34 bit_cost_ = std::numeric_limits<double>::infinity(); | |
35 } | |
36 void Add(size_t val) { | |
37 ++data_[val]; | |
38 ++total_count_; | |
39 } | |
40 void Remove(size_t val) { | |
41 --data_[val]; | |
42 --total_count_; | |
43 } | |
44 template<typename DataType> | |
45 void Add(const DataType *p, size_t n) { | |
46 total_count_ += n; | |
47 n += 1; | |
48 while(--n) ++data_[*p++]; | |
49 } | |
50 void AddHistogram(const Histogram& v) { | |
51 total_count_ += v.total_count_; | |
52 for (size_t i = 0; i < kDataSize; ++i) { | |
53 data_[i] += v.data_[i]; | |
54 } | |
55 } | |
56 | 39 |
57 uint32_t data_[kDataSize]; | 40 #define FN(X) X ## Distance |
58 size_t total_count_; | 41 #define DATA_SIZE BROTLI_NUM_DISTANCE_SYMBOLS |
59 double bit_cost_; | 42 #include "./histogram_inc.h" /* NOLINT(build/include) */ |
60 }; | 43 #undef DataType |
| 44 #undef DATA_SIZE |
| 45 #undef FN |
61 | 46 |
62 // Literal histogram. | 47 BROTLI_INTERNAL void BrotliBuildHistogramsWithContext( |
63 typedef Histogram<256> HistogramLiteral; | 48 const Command* cmds, const size_t num_commands, |
64 // Prefix histograms. | 49 const BlockSplit* literal_split, const BlockSplit* insert_and_copy_split, |
65 typedef Histogram<kNumCommandPrefixes> HistogramCommand; | 50 const BlockSplit* dist_split, const uint8_t* ringbuffer, size_t pos, |
66 typedef Histogram<kNumDistancePrefixes> HistogramDistance; | 51 size_t mask, uint8_t prev_byte, uint8_t prev_byte2, |
67 typedef Histogram<kNumBlockLenPrefixes> HistogramBlockLength; | 52 const ContextType* context_modes, HistogramLiteral* literal_histograms, |
68 // Context map histogram, 256 Huffman tree indexes + 16 run length codes. | 53 HistogramCommand* insert_and_copy_histograms, |
69 typedef Histogram<272> HistogramContextMap; | 54 HistogramDistance* copy_dist_histograms); |
70 // Block type histogram, 256 block types + 2 special symbols. | |
71 typedef Histogram<258> HistogramBlockType; | |
72 | 55 |
73 static const size_t kLiteralContextBits = 6; | 56 #if defined(__cplusplus) || defined(c_plusplus) |
74 static const size_t kDistanceContextBits = 2; | 57 } /* extern "C" */ |
| 58 #endif |
75 | 59 |
76 void BuildHistograms( | 60 #endif /* BROTLI_ENC_HISTOGRAM_H_ */ |
77 const Command* cmds, | |
78 const size_t num_commands, | |
79 const BlockSplit& literal_split, | |
80 const BlockSplit& insert_and_copy_split, | |
81 const BlockSplit& dist_split, | |
82 const uint8_t* ringbuffer, | |
83 size_t pos, | |
84 size_t mask, | |
85 uint8_t prev_byte, | |
86 uint8_t prev_byte2, | |
87 const std::vector<ContextType>& context_modes, | |
88 std::vector<HistogramLiteral>* literal_histograms, | |
89 std::vector<HistogramCommand>* insert_and_copy_histograms, | |
90 std::vector<HistogramDistance>* copy_dist_histograms); | |
91 | |
92 } // namespace brotli | |
93 | |
94 #endif // BROTLI_ENC_HISTOGRAM_H_ | |
OLD | NEW |