2015-11-27 10:27:11 +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-10-23 11:06:13 +00:00
|
|
|
// Models the histograms of literals, commands and distance codes.
|
|
|
|
|
|
|
|
#ifndef BROTLI_ENC_HISTOGRAM_H_
|
|
|
|
#define BROTLI_ENC_HISTOGRAM_H_
|
|
|
|
|
2016-01-07 15:27:49 +00:00
|
|
|
#include <cstring>
|
2015-10-06 09:23:44 +00:00
|
|
|
#include <limits>
|
2013-10-23 11:06:13 +00:00
|
|
|
#include <vector>
|
2016-01-07 15:27:49 +00:00
|
|
|
#include "./context.h"
|
2013-10-23 11:06:13 +00:00
|
|
|
#include "./command.h"
|
|
|
|
#include "./fast_log.h"
|
|
|
|
#include "./prefix.h"
|
2015-10-01 10:08:14 +00:00
|
|
|
#include "./types.h"
|
2013-10-23 11:06:13 +00:00
|
|
|
|
|
|
|
namespace brotli {
|
|
|
|
|
2015-10-01 09:40:05 +00:00
|
|
|
struct BlockSplit;
|
2013-10-23 11:06:13 +00:00
|
|
|
|
|
|
|
// A simple container for histograms of data in blocks.
|
|
|
|
template<int kDataSize>
|
|
|
|
struct Histogram {
|
2016-02-29 13:41:24 +00:00
|
|
|
Histogram(void) {
|
2013-10-23 11:06:13 +00:00
|
|
|
Clear();
|
|
|
|
}
|
2016-02-29 13:41:24 +00:00
|
|
|
void Clear(void) {
|
2013-10-23 11:06:13 +00:00
|
|
|
memset(data_, 0, sizeof(data_));
|
|
|
|
total_count_ = 0;
|
2015-10-06 09:23:44 +00:00
|
|
|
bit_cost_ = std::numeric_limits<double>::infinity();
|
2013-10-23 11:06:13 +00:00
|
|
|
}
|
2016-01-07 15:27:49 +00:00
|
|
|
void Add(size_t val) {
|
2013-10-23 11:06:13 +00:00
|
|
|
++data_[val];
|
|
|
|
++total_count_;
|
|
|
|
}
|
2016-01-07 15:27:49 +00:00
|
|
|
void Remove(size_t val) {
|
2013-10-23 11:06:13 +00:00
|
|
|
--data_[val];
|
|
|
|
--total_count_;
|
|
|
|
}
|
|
|
|
template<typename DataType>
|
|
|
|
void Add(const DataType *p, size_t n) {
|
2016-01-07 15:27:49 +00:00
|
|
|
total_count_ += n;
|
2013-10-23 11:06:13 +00:00
|
|
|
n += 1;
|
|
|
|
while(--n) ++data_[*p++];
|
|
|
|
}
|
|
|
|
void AddHistogram(const Histogram& v) {
|
|
|
|
total_count_ += v.total_count_;
|
2016-01-07 15:27:49 +00:00
|
|
|
for (size_t i = 0; i < kDataSize; ++i) {
|
2013-10-23 11:06:13 +00:00
|
|
|
data_[i] += v.data_[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-07 15:27:49 +00:00
|
|
|
uint32_t data_[kDataSize];
|
|
|
|
size_t total_count_;
|
2013-10-23 11:06:13 +00:00
|
|
|
double bit_cost_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Literal histogram.
|
|
|
|
typedef Histogram<256> HistogramLiteral;
|
|
|
|
// Prefix histograms.
|
|
|
|
typedef Histogram<kNumCommandPrefixes> HistogramCommand;
|
|
|
|
typedef Histogram<kNumDistancePrefixes> HistogramDistance;
|
|
|
|
typedef Histogram<kNumBlockLenPrefixes> HistogramBlockLength;
|
2013-12-12 12:18:04 +00:00
|
|
|
// Context map histogram, 256 Huffman tree indexes + 16 run length codes.
|
|
|
|
typedef Histogram<272> HistogramContextMap;
|
|
|
|
// Block type histogram, 256 block types + 2 special symbols.
|
|
|
|
typedef Histogram<258> HistogramBlockType;
|
2013-10-23 11:06:13 +00:00
|
|
|
|
2016-01-07 15:27:49 +00:00
|
|
|
static const size_t kLiteralContextBits = 6;
|
|
|
|
static const size_t kDistanceContextBits = 2;
|
2013-11-15 18:02:17 +00:00
|
|
|
|
2013-10-23 11:06:13 +00:00
|
|
|
void BuildHistograms(
|
2015-04-23 11:15:42 +00:00
|
|
|
const Command* cmds,
|
|
|
|
const size_t num_commands,
|
2013-10-23 11:06:13 +00:00
|
|
|
const BlockSplit& literal_split,
|
|
|
|
const BlockSplit& insert_and_copy_split,
|
|
|
|
const BlockSplit& dist_split,
|
2013-11-15 18:02:17 +00:00
|
|
|
const uint8_t* ringbuffer,
|
2013-10-23 11:06:13 +00:00
|
|
|
size_t pos,
|
2013-11-15 18:02:17 +00:00
|
|
|
size_t mask,
|
2015-04-23 14:20:29 +00:00
|
|
|
uint8_t prev_byte,
|
|
|
|
uint8_t prev_byte2,
|
2016-01-07 15:27:49 +00:00
|
|
|
const std::vector<ContextType>& context_modes,
|
2013-10-23 11:06:13 +00:00
|
|
|
std::vector<HistogramLiteral>* literal_histograms,
|
|
|
|
std::vector<HistogramCommand>* insert_and_copy_histograms,
|
|
|
|
std::vector<HistogramDistance>* copy_dist_histograms);
|
|
|
|
|
|
|
|
} // namespace brotli
|
|
|
|
|
|
|
|
#endif // BROTLI_ENC_HISTOGRAM_H_
|