2013-10-23 11:06:13 +00:00
|
|
|
// Copyright 2013 Google Inc. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
// Build per-context histograms of literals, commands and distance codes.
|
|
|
|
|
|
|
|
#include "./histogram.h"
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
#include "./block_splitter.h"
|
|
|
|
#include "./command.h"
|
|
|
|
#include "./context.h"
|
|
|
|
#include "./prefix.h"
|
|
|
|
|
|
|
|
namespace brotli {
|
|
|
|
|
|
|
|
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,
|
2015-04-23 14:20:29 +00:00
|
|
|
size_t start_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,
|
2013-11-15 18:02:17 +00:00
|
|
|
const std::vector<int>& 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) {
|
2015-04-23 14:20:29 +00:00
|
|
|
size_t pos = start_pos;
|
2013-10-23 11:06:13 +00:00
|
|
|
BlockSplitIterator literal_it(literal_split);
|
|
|
|
BlockSplitIterator insert_and_copy_it(insert_and_copy_split);
|
|
|
|
BlockSplitIterator dist_it(dist_split);
|
2015-04-23 11:15:42 +00:00
|
|
|
for (int i = 0; i < num_commands; ++i) {
|
2013-10-23 11:06:13 +00:00
|
|
|
const Command &cmd = cmds[i];
|
|
|
|
insert_and_copy_it.Next();
|
|
|
|
(*insert_and_copy_histograms)[insert_and_copy_it.type_].Add(
|
2014-10-28 12:25:22 +00:00
|
|
|
cmd.cmd_prefix_);
|
|
|
|
for (int j = 0; j < cmd.insert_len_; ++j) {
|
2013-10-23 11:06:13 +00:00
|
|
|
literal_it.Next();
|
2013-11-15 18:02:17 +00:00
|
|
|
int context = (literal_it.type_ << kLiteralContextBits) +
|
|
|
|
Context(prev_byte, prev_byte2, context_modes[literal_it.type_]);
|
|
|
|
(*literal_histograms)[context].Add(ringbuffer[pos & mask]);
|
2015-04-23 14:20:29 +00:00
|
|
|
prev_byte2 = prev_byte;
|
|
|
|
prev_byte = ringbuffer[pos & mask];
|
2013-10-23 11:06:13 +00:00
|
|
|
++pos;
|
|
|
|
}
|
2014-10-28 12:25:22 +00:00
|
|
|
pos += cmd.copy_len_;
|
2015-04-23 14:20:29 +00:00
|
|
|
if (cmd.copy_len_ > 0) {
|
|
|
|
prev_byte2 = ringbuffer[(pos - 2) & mask];
|
|
|
|
prev_byte = ringbuffer[(pos - 1) & mask];
|
|
|
|
if (cmd.cmd_prefix_ >= 128) {
|
|
|
|
dist_it.Next();
|
|
|
|
int context = (dist_it.type_ << kDistanceContextBits) +
|
|
|
|
cmd.DistanceContext();
|
|
|
|
(*copy_dist_histograms)[context].Add(cmd.dist_prefix_);
|
2013-11-15 18:02:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-23 11:06:13 +00:00
|
|
|
} // namespace brotli
|