| OLD | NEW |
| (Empty) | |
| 1 /* Copyright 2013 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 // Models the histograms of literals, commands and distance codes. |
| 8 |
| 9 #ifndef BROTLI_ENC_HISTOGRAM_H_ |
| 10 #define BROTLI_ENC_HISTOGRAM_H_ |
| 11 |
| 12 #include <cstring> |
| 13 #include <limits> |
| 14 #include <vector> |
| 15 #include "./context.h" |
| 16 #include "./command.h" |
| 17 #include "./fast_log.h" |
| 18 #include "./prefix.h" |
| 19 #include "./types.h" |
| 20 |
| 21 namespace brotli { |
| 22 |
| 23 struct BlockSplit; |
| 24 |
| 25 // A simple container for histograms of data in blocks. |
| 26 template<int kDataSize> |
| 27 struct Histogram { |
| 28 Histogram(void) { |
| 29 Clear(); |
| 30 } |
| 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 |
| 57 uint32_t data_[kDataSize]; |
| 58 size_t total_count_; |
| 59 double bit_cost_; |
| 60 }; |
| 61 |
| 62 // Literal histogram. |
| 63 typedef Histogram<256> HistogramLiteral; |
| 64 // Prefix histograms. |
| 65 typedef Histogram<kNumCommandPrefixes> HistogramCommand; |
| 66 typedef Histogram<kNumDistancePrefixes> HistogramDistance; |
| 67 typedef Histogram<kNumBlockLenPrefixes> HistogramBlockLength; |
| 68 // Context map histogram, 256 Huffman tree indexes + 16 run length codes. |
| 69 typedef Histogram<272> HistogramContextMap; |
| 70 // Block type histogram, 256 block types + 2 special symbols. |
| 71 typedef Histogram<258> HistogramBlockType; |
| 72 |
| 73 static const size_t kLiteralContextBits = 6; |
| 74 static const size_t kDistanceContextBits = 2; |
| 75 |
| 76 void BuildHistograms( |
| 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 |