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 /* Build per-context histograms of literals, commands and distance codes. */
8 
9 #include "./histogram.h"
10 
11 #include "./block_splitter.h"
12 #include "./command.h"
13 #include "./context.h"
14 
15 #if defined(__cplusplus) || defined(c_plusplus)
16 extern "C" {
17 #endif
18 
19 typedef struct BlockSplitIterator {
20   const BlockSplit* split_;  /* Not owned. */
21   size_t idx_;
22   size_t type_;
23   size_t length_;
24 } BlockSplitIterator;
25 
InitBlockSplitIterator(BlockSplitIterator * self,const BlockSplit * split)26 static void InitBlockSplitIterator(BlockSplitIterator* self,
27     const BlockSplit* split) {
28   self->split_ = split;
29   self->idx_ = 0;
30   self->type_ = 0;
31   self->length_ = split->lengths ? split->lengths[0] : 0;
32 }
33 
BlockSplitIteratorNext(BlockSplitIterator * self)34 static void BlockSplitIteratorNext(BlockSplitIterator* self) {
35   if (self->length_ == 0) {
36     ++self->idx_;
37     self->type_ = self->split_->types[self->idx_];
38     self->length_ = self->split_->lengths[self->idx_];
39   }
40   --self->length_;
41 }
42 
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)43 void BrotliBuildHistogramsWithContext(
44     const Command* cmds, const size_t num_commands,
45     const BlockSplit* literal_split, const BlockSplit* insert_and_copy_split,
46     const BlockSplit* dist_split, const uint8_t* ringbuffer, size_t start_pos,
47     size_t mask, uint8_t prev_byte, uint8_t prev_byte2,
48     const ContextType* context_modes, HistogramLiteral* literal_histograms,
49     HistogramCommand* insert_and_copy_histograms,
50     HistogramDistance* copy_dist_histograms) {
51   size_t pos = start_pos;
52   BlockSplitIterator literal_it;
53   BlockSplitIterator insert_and_copy_it;
54   BlockSplitIterator dist_it;
55   size_t i;
56 
57   InitBlockSplitIterator(&literal_it, literal_split);
58   InitBlockSplitIterator(&insert_and_copy_it, insert_and_copy_split);
59   InitBlockSplitIterator(&dist_it, dist_split);
60   for (i = 0; i < num_commands; ++i) {
61     const Command* cmd = &cmds[i];
62     size_t j;
63     BlockSplitIteratorNext(&insert_and_copy_it);
64     HistogramAddCommand(&insert_and_copy_histograms[insert_and_copy_it.type_],
65         cmd->cmd_prefix_);
66     for (j = cmd->insert_len_; j != 0; --j) {
67       size_t context;
68       BlockSplitIteratorNext(&literal_it);
69       context = context_modes ?
70           ((literal_it.type_ << BROTLI_LITERAL_CONTEXT_BITS) +
71               Context(prev_byte, prev_byte2, context_modes[literal_it.type_])) :
72           literal_it.type_;
73       HistogramAddLiteral(&literal_histograms[context],
74           ringbuffer[pos & mask]);
75       prev_byte2 = prev_byte;
76       prev_byte = ringbuffer[pos & mask];
77       ++pos;
78     }
79     pos += CommandCopyLen(cmd);
80     if (CommandCopyLen(cmd)) {
81       prev_byte2 = ringbuffer[(pos - 2) & mask];
82       prev_byte = ringbuffer[(pos - 1) & mask];
83       if (cmd->cmd_prefix_ >= 128) {
84         size_t context;
85         BlockSplitIteratorNext(&dist_it);
86         context = (dist_it.type_ << BROTLI_DISTANCE_CONTEXT_BITS) +
87             CommandDistanceContext(cmd);
88         HistogramAddDistance(&copy_dist_histograms[context],
89             cmd->dist_prefix_);
90       }
91     }
92   }
93 }
94 
95 #if defined(__cplusplus) || defined(c_plusplus)
96 }  /* extern "C" */
97 #endif
98