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
|
|
|
|
*/
|
|
|
|
|
2016-06-03 09:19:23 +00:00
|
|
|
/* Build per-context histograms of literals, commands and distance codes. */
|
2013-10-23 11:06:13 +00:00
|
|
|
|
2021-09-08 07:18:45 +00:00
|
|
|
#include "histogram.h"
|
2013-10-23 11:06:13 +00:00
|
|
|
|
2018-02-26 14:04:36 +00:00
|
|
|
#include "../common/context.h"
|
2021-09-08 07:18:45 +00:00
|
|
|
#include "block_splitter.h"
|
|
|
|
#include "command.h"
|
2013-10-23 11:06:13 +00:00
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef struct BlockSplitIterator {
|
|
|
|
const BlockSplit* split_; /* Not owned. */
|
|
|
|
size_t idx_;
|
|
|
|
size_t type_;
|
|
|
|
size_t length_;
|
|
|
|
} BlockSplitIterator;
|
|
|
|
|
|
|
|
static void InitBlockSplitIterator(BlockSplitIterator* self,
|
|
|
|
const BlockSplit* split) {
|
|
|
|
self->split_ = split;
|
|
|
|
self->idx_ = 0;
|
|
|
|
self->type_ = 0;
|
|
|
|
self->length_ = split->lengths ? split->lengths[0] : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void BlockSplitIteratorNext(BlockSplitIterator* self) {
|
|
|
|
if (self->length_ == 0) {
|
|
|
|
++self->idx_;
|
|
|
|
self->type_ = self->split_->types[self->idx_];
|
|
|
|
self->length_ = self->split_->lengths[self->idx_];
|
|
|
|
}
|
|
|
|
--self->length_;
|
|
|
|
}
|
2013-10-23 11:06:13 +00:00
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
void BrotliBuildHistogramsWithContext(
|
|
|
|
const Command* cmds, const size_t num_commands,
|
|
|
|
const BlockSplit* literal_split, const BlockSplit* insert_and_copy_split,
|
|
|
|
const BlockSplit* dist_split, const uint8_t* ringbuffer, size_t start_pos,
|
|
|
|
size_t mask, uint8_t prev_byte, uint8_t prev_byte2,
|
|
|
|
const ContextType* context_modes, HistogramLiteral* literal_histograms,
|
|
|
|
HistogramCommand* insert_and_copy_histograms,
|
|
|
|
HistogramDistance* copy_dist_histograms) {
|
2015-04-23 14:20:29 +00:00
|
|
|
size_t pos = start_pos;
|
2016-06-13 09:01:04 +00:00
|
|
|
BlockSplitIterator literal_it;
|
|
|
|
BlockSplitIterator insert_and_copy_it;
|
|
|
|
BlockSplitIterator dist_it;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
InitBlockSplitIterator(&literal_it, literal_split);
|
|
|
|
InitBlockSplitIterator(&insert_and_copy_it, insert_and_copy_split);
|
|
|
|
InitBlockSplitIterator(&dist_it, dist_split);
|
|
|
|
for (i = 0; i < num_commands; ++i) {
|
|
|
|
const Command* cmd = &cmds[i];
|
|
|
|
size_t j;
|
|
|
|
BlockSplitIteratorNext(&insert_and_copy_it);
|
|
|
|
HistogramAddCommand(&insert_and_copy_histograms[insert_and_copy_it.type_],
|
|
|
|
cmd->cmd_prefix_);
|
2021-11-10 09:34:39 +00:00
|
|
|
/* TODO(eustas): unwrap iterator blocks. */
|
2016-06-13 09:01:04 +00:00
|
|
|
for (j = cmd->insert_len_; j != 0; --j) {
|
|
|
|
size_t context;
|
|
|
|
BlockSplitIteratorNext(&literal_it);
|
2018-02-26 14:04:36 +00:00
|
|
|
context = literal_it.type_;
|
|
|
|
if (context_modes) {
|
|
|
|
ContextLut lut = BROTLI_CONTEXT_LUT(context_modes[context]);
|
|
|
|
context = (context << BROTLI_LITERAL_CONTEXT_BITS) +
|
|
|
|
BROTLI_CONTEXT(prev_byte, prev_byte2, lut);
|
|
|
|
}
|
2016-06-13 09:01:04 +00:00
|
|
|
HistogramAddLiteral(&literal_histograms[context],
|
|
|
|
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;
|
|
|
|
}
|
2016-06-13 09:01:04 +00:00
|
|
|
pos += CommandCopyLen(cmd);
|
|
|
|
if (CommandCopyLen(cmd)) {
|
2015-04-23 14:20:29 +00:00
|
|
|
prev_byte2 = ringbuffer[(pos - 2) & mask];
|
|
|
|
prev_byte = ringbuffer[(pos - 1) & mask];
|
2016-06-13 09:01:04 +00:00
|
|
|
if (cmd->cmd_prefix_ >= 128) {
|
|
|
|
size_t context;
|
|
|
|
BlockSplitIteratorNext(&dist_it);
|
|
|
|
context = (dist_it.type_ << BROTLI_DISTANCE_CONTEXT_BITS) +
|
|
|
|
CommandDistanceContext(cmd);
|
|
|
|
HistogramAddDistance(©_dist_histograms[context],
|
2018-02-26 14:04:36 +00:00
|
|
|
cmd->dist_prefix_ & 0x3FF);
|
2013-11-15 18:02:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
} /* extern "C" */
|
|
|
|
#endif
|