1*f4ee7fbaSAndroid Build Coastguard Worker /* Copyright 2013 Google Inc. All Rights Reserved.
2*f4ee7fbaSAndroid Build Coastguard Worker
3*f4ee7fbaSAndroid Build Coastguard Worker Distributed under MIT license.
4*f4ee7fbaSAndroid Build Coastguard Worker See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
5*f4ee7fbaSAndroid Build Coastguard Worker */
6*f4ee7fbaSAndroid Build Coastguard Worker
7*f4ee7fbaSAndroid Build Coastguard Worker /* Function to find backward reference copies. */
8*f4ee7fbaSAndroid Build Coastguard Worker
9*f4ee7fbaSAndroid Build Coastguard Worker #include "./backward_references_hq.h"
10*f4ee7fbaSAndroid Build Coastguard Worker
11*f4ee7fbaSAndroid Build Coastguard Worker #include <string.h> /* memcpy, memset */
12*f4ee7fbaSAndroid Build Coastguard Worker
13*f4ee7fbaSAndroid Build Coastguard Worker #include "../common/constants.h"
14*f4ee7fbaSAndroid Build Coastguard Worker #include "../common/context.h"
15*f4ee7fbaSAndroid Build Coastguard Worker #include "../common/platform.h"
16*f4ee7fbaSAndroid Build Coastguard Worker #include <brotli/types.h>
17*f4ee7fbaSAndroid Build Coastguard Worker #include "./command.h"
18*f4ee7fbaSAndroid Build Coastguard Worker #include "./fast_log.h"
19*f4ee7fbaSAndroid Build Coastguard Worker #include "./find_match_length.h"
20*f4ee7fbaSAndroid Build Coastguard Worker #include "./literal_cost.h"
21*f4ee7fbaSAndroid Build Coastguard Worker #include "./memory.h"
22*f4ee7fbaSAndroid Build Coastguard Worker #include "./params.h"
23*f4ee7fbaSAndroid Build Coastguard Worker #include "./prefix.h"
24*f4ee7fbaSAndroid Build Coastguard Worker #include "./quality.h"
25*f4ee7fbaSAndroid Build Coastguard Worker
26*f4ee7fbaSAndroid Build Coastguard Worker #if defined(__cplusplus) || defined(c_plusplus)
27*f4ee7fbaSAndroid Build Coastguard Worker extern "C" {
28*f4ee7fbaSAndroid Build Coastguard Worker #endif
29*f4ee7fbaSAndroid Build Coastguard Worker
30*f4ee7fbaSAndroid Build Coastguard Worker /* BrotliCalculateDistanceCodeLimit(BROTLI_MAX_ALLOWED_DISTANCE, 3, 120). */
31*f4ee7fbaSAndroid Build Coastguard Worker #define BROTLI_MAX_EFFECTIVE_DISTANCE_ALPHABET_SIZE 544
32*f4ee7fbaSAndroid Build Coastguard Worker
33*f4ee7fbaSAndroid Build Coastguard Worker static const float kInfinity = 1.7e38f; /* ~= 2 ^ 127 */
34*f4ee7fbaSAndroid Build Coastguard Worker
35*f4ee7fbaSAndroid Build Coastguard Worker static const uint32_t kDistanceCacheIndex[] = {
36*f4ee7fbaSAndroid Build Coastguard Worker 0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1,
37*f4ee7fbaSAndroid Build Coastguard Worker };
38*f4ee7fbaSAndroid Build Coastguard Worker static const int kDistanceCacheOffset[] = {
39*f4ee7fbaSAndroid Build Coastguard Worker 0, 0, 0, 0, -1, 1, -2, 2, -3, 3, -1, 1, -2, 2, -3, 3
40*f4ee7fbaSAndroid Build Coastguard Worker };
41*f4ee7fbaSAndroid Build Coastguard Worker
BrotliInitZopfliNodes(ZopfliNode * array,size_t length)42*f4ee7fbaSAndroid Build Coastguard Worker void BrotliInitZopfliNodes(ZopfliNode* array, size_t length) {
43*f4ee7fbaSAndroid Build Coastguard Worker ZopfliNode stub;
44*f4ee7fbaSAndroid Build Coastguard Worker size_t i;
45*f4ee7fbaSAndroid Build Coastguard Worker stub.length = 1;
46*f4ee7fbaSAndroid Build Coastguard Worker stub.distance = 0;
47*f4ee7fbaSAndroid Build Coastguard Worker stub.dcode_insert_length = 0;
48*f4ee7fbaSAndroid Build Coastguard Worker stub.u.cost = kInfinity;
49*f4ee7fbaSAndroid Build Coastguard Worker for (i = 0; i < length; ++i) array[i] = stub;
50*f4ee7fbaSAndroid Build Coastguard Worker }
51*f4ee7fbaSAndroid Build Coastguard Worker
ZopfliNodeCopyLength(const ZopfliNode * self)52*f4ee7fbaSAndroid Build Coastguard Worker static BROTLI_INLINE uint32_t ZopfliNodeCopyLength(const ZopfliNode* self) {
53*f4ee7fbaSAndroid Build Coastguard Worker return self->length & 0x1FFFFFF;
54*f4ee7fbaSAndroid Build Coastguard Worker }
55*f4ee7fbaSAndroid Build Coastguard Worker
ZopfliNodeLengthCode(const ZopfliNode * self)56*f4ee7fbaSAndroid Build Coastguard Worker static BROTLI_INLINE uint32_t ZopfliNodeLengthCode(const ZopfliNode* self) {
57*f4ee7fbaSAndroid Build Coastguard Worker const uint32_t modifier = self->length >> 25;
58*f4ee7fbaSAndroid Build Coastguard Worker return ZopfliNodeCopyLength(self) + 9u - modifier;
59*f4ee7fbaSAndroid Build Coastguard Worker }
60*f4ee7fbaSAndroid Build Coastguard Worker
ZopfliNodeCopyDistance(const ZopfliNode * self)61*f4ee7fbaSAndroid Build Coastguard Worker static BROTLI_INLINE uint32_t ZopfliNodeCopyDistance(const ZopfliNode* self) {
62*f4ee7fbaSAndroid Build Coastguard Worker return self->distance;
63*f4ee7fbaSAndroid Build Coastguard Worker }
64*f4ee7fbaSAndroid Build Coastguard Worker
ZopfliNodeDistanceCode(const ZopfliNode * self)65*f4ee7fbaSAndroid Build Coastguard Worker static BROTLI_INLINE uint32_t ZopfliNodeDistanceCode(const ZopfliNode* self) {
66*f4ee7fbaSAndroid Build Coastguard Worker const uint32_t short_code = self->dcode_insert_length >> 27;
67*f4ee7fbaSAndroid Build Coastguard Worker return short_code == 0 ?
68*f4ee7fbaSAndroid Build Coastguard Worker ZopfliNodeCopyDistance(self) + BROTLI_NUM_DISTANCE_SHORT_CODES - 1 :
69*f4ee7fbaSAndroid Build Coastguard Worker short_code - 1;
70*f4ee7fbaSAndroid Build Coastguard Worker }
71*f4ee7fbaSAndroid Build Coastguard Worker
ZopfliNodeCommandLength(const ZopfliNode * self)72*f4ee7fbaSAndroid Build Coastguard Worker static BROTLI_INLINE uint32_t ZopfliNodeCommandLength(const ZopfliNode* self) {
73*f4ee7fbaSAndroid Build Coastguard Worker return ZopfliNodeCopyLength(self) + (self->dcode_insert_length & 0x7FFFFFF);
74*f4ee7fbaSAndroid Build Coastguard Worker }
75*f4ee7fbaSAndroid Build Coastguard Worker
76*f4ee7fbaSAndroid Build Coastguard Worker /* Histogram based cost model for zopflification. */
77*f4ee7fbaSAndroid Build Coastguard Worker typedef struct ZopfliCostModel {
78*f4ee7fbaSAndroid Build Coastguard Worker /* The insert and copy length symbols. */
79*f4ee7fbaSAndroid Build Coastguard Worker float cost_cmd_[BROTLI_NUM_COMMAND_SYMBOLS];
80*f4ee7fbaSAndroid Build Coastguard Worker float* cost_dist_;
81*f4ee7fbaSAndroid Build Coastguard Worker uint32_t distance_histogram_size;
82*f4ee7fbaSAndroid Build Coastguard Worker /* Cumulative costs of literals per position in the stream. */
83*f4ee7fbaSAndroid Build Coastguard Worker float* literal_costs_;
84*f4ee7fbaSAndroid Build Coastguard Worker float min_cost_cmd_;
85*f4ee7fbaSAndroid Build Coastguard Worker size_t num_bytes_;
86*f4ee7fbaSAndroid Build Coastguard Worker } ZopfliCostModel;
87*f4ee7fbaSAndroid Build Coastguard Worker
InitZopfliCostModel(MemoryManager * m,ZopfliCostModel * self,const BrotliDistanceParams * dist,size_t num_bytes)88*f4ee7fbaSAndroid Build Coastguard Worker static void InitZopfliCostModel(
89*f4ee7fbaSAndroid Build Coastguard Worker MemoryManager* m, ZopfliCostModel* self, const BrotliDistanceParams* dist,
90*f4ee7fbaSAndroid Build Coastguard Worker size_t num_bytes) {
91*f4ee7fbaSAndroid Build Coastguard Worker self->num_bytes_ = num_bytes;
92*f4ee7fbaSAndroid Build Coastguard Worker self->literal_costs_ = BROTLI_ALLOC(m, float, num_bytes + 2);
93*f4ee7fbaSAndroid Build Coastguard Worker self->cost_dist_ = BROTLI_ALLOC(m, float, dist->alphabet_size_limit);
94*f4ee7fbaSAndroid Build Coastguard Worker self->distance_histogram_size = dist->alphabet_size_limit;
95*f4ee7fbaSAndroid Build Coastguard Worker if (BROTLI_IS_OOM(m)) return;
96*f4ee7fbaSAndroid Build Coastguard Worker }
97*f4ee7fbaSAndroid Build Coastguard Worker
CleanupZopfliCostModel(MemoryManager * m,ZopfliCostModel * self)98*f4ee7fbaSAndroid Build Coastguard Worker static void CleanupZopfliCostModel(MemoryManager* m, ZopfliCostModel* self) {
99*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_FREE(m, self->literal_costs_);
100*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_FREE(m, self->cost_dist_);
101*f4ee7fbaSAndroid Build Coastguard Worker }
102*f4ee7fbaSAndroid Build Coastguard Worker
SetCost(const uint32_t * histogram,size_t histogram_size,BROTLI_BOOL literal_histogram,float * cost)103*f4ee7fbaSAndroid Build Coastguard Worker static void SetCost(const uint32_t* histogram, size_t histogram_size,
104*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_BOOL literal_histogram, float* cost) {
105*f4ee7fbaSAndroid Build Coastguard Worker size_t sum = 0;
106*f4ee7fbaSAndroid Build Coastguard Worker size_t missing_symbol_sum;
107*f4ee7fbaSAndroid Build Coastguard Worker float log2sum;
108*f4ee7fbaSAndroid Build Coastguard Worker float missing_symbol_cost;
109*f4ee7fbaSAndroid Build Coastguard Worker size_t i;
110*f4ee7fbaSAndroid Build Coastguard Worker for (i = 0; i < histogram_size; i++) {
111*f4ee7fbaSAndroid Build Coastguard Worker sum += histogram[i];
112*f4ee7fbaSAndroid Build Coastguard Worker }
113*f4ee7fbaSAndroid Build Coastguard Worker log2sum = (float)FastLog2(sum);
114*f4ee7fbaSAndroid Build Coastguard Worker missing_symbol_sum = sum;
115*f4ee7fbaSAndroid Build Coastguard Worker if (!literal_histogram) {
116*f4ee7fbaSAndroid Build Coastguard Worker for (i = 0; i < histogram_size; i++) {
117*f4ee7fbaSAndroid Build Coastguard Worker if (histogram[i] == 0) missing_symbol_sum++;
118*f4ee7fbaSAndroid Build Coastguard Worker }
119*f4ee7fbaSAndroid Build Coastguard Worker }
120*f4ee7fbaSAndroid Build Coastguard Worker missing_symbol_cost = (float)FastLog2(missing_symbol_sum) + 2;
121*f4ee7fbaSAndroid Build Coastguard Worker for (i = 0; i < histogram_size; i++) {
122*f4ee7fbaSAndroid Build Coastguard Worker if (histogram[i] == 0) {
123*f4ee7fbaSAndroid Build Coastguard Worker cost[i] = missing_symbol_cost;
124*f4ee7fbaSAndroid Build Coastguard Worker continue;
125*f4ee7fbaSAndroid Build Coastguard Worker }
126*f4ee7fbaSAndroid Build Coastguard Worker
127*f4ee7fbaSAndroid Build Coastguard Worker /* Shannon bits for this symbol. */
128*f4ee7fbaSAndroid Build Coastguard Worker cost[i] = log2sum - (float)FastLog2(histogram[i]);
129*f4ee7fbaSAndroid Build Coastguard Worker
130*f4ee7fbaSAndroid Build Coastguard Worker /* Cannot be coded with less than 1 bit */
131*f4ee7fbaSAndroid Build Coastguard Worker if (cost[i] < 1) cost[i] = 1;
132*f4ee7fbaSAndroid Build Coastguard Worker }
133*f4ee7fbaSAndroid Build Coastguard Worker }
134*f4ee7fbaSAndroid Build Coastguard Worker
ZopfliCostModelSetFromCommands(ZopfliCostModel * self,size_t position,const uint8_t * ringbuffer,size_t ringbuffer_mask,const Command * commands,size_t num_commands,size_t last_insert_len)135*f4ee7fbaSAndroid Build Coastguard Worker static void ZopfliCostModelSetFromCommands(ZopfliCostModel* self,
136*f4ee7fbaSAndroid Build Coastguard Worker size_t position,
137*f4ee7fbaSAndroid Build Coastguard Worker const uint8_t* ringbuffer,
138*f4ee7fbaSAndroid Build Coastguard Worker size_t ringbuffer_mask,
139*f4ee7fbaSAndroid Build Coastguard Worker const Command* commands,
140*f4ee7fbaSAndroid Build Coastguard Worker size_t num_commands,
141*f4ee7fbaSAndroid Build Coastguard Worker size_t last_insert_len) {
142*f4ee7fbaSAndroid Build Coastguard Worker uint32_t histogram_literal[BROTLI_NUM_LITERAL_SYMBOLS];
143*f4ee7fbaSAndroid Build Coastguard Worker uint32_t histogram_cmd[BROTLI_NUM_COMMAND_SYMBOLS];
144*f4ee7fbaSAndroid Build Coastguard Worker uint32_t histogram_dist[BROTLI_MAX_EFFECTIVE_DISTANCE_ALPHABET_SIZE];
145*f4ee7fbaSAndroid Build Coastguard Worker float cost_literal[BROTLI_NUM_LITERAL_SYMBOLS];
146*f4ee7fbaSAndroid Build Coastguard Worker size_t pos = position - last_insert_len;
147*f4ee7fbaSAndroid Build Coastguard Worker float min_cost_cmd = kInfinity;
148*f4ee7fbaSAndroid Build Coastguard Worker size_t i;
149*f4ee7fbaSAndroid Build Coastguard Worker float* cost_cmd = self->cost_cmd_;
150*f4ee7fbaSAndroid Build Coastguard Worker
151*f4ee7fbaSAndroid Build Coastguard Worker memset(histogram_literal, 0, sizeof(histogram_literal));
152*f4ee7fbaSAndroid Build Coastguard Worker memset(histogram_cmd, 0, sizeof(histogram_cmd));
153*f4ee7fbaSAndroid Build Coastguard Worker memset(histogram_dist, 0, sizeof(histogram_dist));
154*f4ee7fbaSAndroid Build Coastguard Worker
155*f4ee7fbaSAndroid Build Coastguard Worker for (i = 0; i < num_commands; i++) {
156*f4ee7fbaSAndroid Build Coastguard Worker size_t inslength = commands[i].insert_len_;
157*f4ee7fbaSAndroid Build Coastguard Worker size_t copylength = CommandCopyLen(&commands[i]);
158*f4ee7fbaSAndroid Build Coastguard Worker size_t distcode = commands[i].dist_prefix_ & 0x3FF;
159*f4ee7fbaSAndroid Build Coastguard Worker size_t cmdcode = commands[i].cmd_prefix_;
160*f4ee7fbaSAndroid Build Coastguard Worker size_t j;
161*f4ee7fbaSAndroid Build Coastguard Worker
162*f4ee7fbaSAndroid Build Coastguard Worker histogram_cmd[cmdcode]++;
163*f4ee7fbaSAndroid Build Coastguard Worker if (cmdcode >= 128) histogram_dist[distcode]++;
164*f4ee7fbaSAndroid Build Coastguard Worker
165*f4ee7fbaSAndroid Build Coastguard Worker for (j = 0; j < inslength; j++) {
166*f4ee7fbaSAndroid Build Coastguard Worker histogram_literal[ringbuffer[(pos + j) & ringbuffer_mask]]++;
167*f4ee7fbaSAndroid Build Coastguard Worker }
168*f4ee7fbaSAndroid Build Coastguard Worker
169*f4ee7fbaSAndroid Build Coastguard Worker pos += inslength + copylength;
170*f4ee7fbaSAndroid Build Coastguard Worker }
171*f4ee7fbaSAndroid Build Coastguard Worker
172*f4ee7fbaSAndroid Build Coastguard Worker SetCost(histogram_literal, BROTLI_NUM_LITERAL_SYMBOLS, BROTLI_TRUE,
173*f4ee7fbaSAndroid Build Coastguard Worker cost_literal);
174*f4ee7fbaSAndroid Build Coastguard Worker SetCost(histogram_cmd, BROTLI_NUM_COMMAND_SYMBOLS, BROTLI_FALSE,
175*f4ee7fbaSAndroid Build Coastguard Worker cost_cmd);
176*f4ee7fbaSAndroid Build Coastguard Worker SetCost(histogram_dist, self->distance_histogram_size, BROTLI_FALSE,
177*f4ee7fbaSAndroid Build Coastguard Worker self->cost_dist_);
178*f4ee7fbaSAndroid Build Coastguard Worker
179*f4ee7fbaSAndroid Build Coastguard Worker for (i = 0; i < BROTLI_NUM_COMMAND_SYMBOLS; ++i) {
180*f4ee7fbaSAndroid Build Coastguard Worker min_cost_cmd = BROTLI_MIN(float, min_cost_cmd, cost_cmd[i]);
181*f4ee7fbaSAndroid Build Coastguard Worker }
182*f4ee7fbaSAndroid Build Coastguard Worker self->min_cost_cmd_ = min_cost_cmd;
183*f4ee7fbaSAndroid Build Coastguard Worker
184*f4ee7fbaSAndroid Build Coastguard Worker {
185*f4ee7fbaSAndroid Build Coastguard Worker float* literal_costs = self->literal_costs_;
186*f4ee7fbaSAndroid Build Coastguard Worker float literal_carry = 0.0;
187*f4ee7fbaSAndroid Build Coastguard Worker size_t num_bytes = self->num_bytes_;
188*f4ee7fbaSAndroid Build Coastguard Worker literal_costs[0] = 0.0;
189*f4ee7fbaSAndroid Build Coastguard Worker for (i = 0; i < num_bytes; ++i) {
190*f4ee7fbaSAndroid Build Coastguard Worker literal_carry +=
191*f4ee7fbaSAndroid Build Coastguard Worker cost_literal[ringbuffer[(position + i) & ringbuffer_mask]];
192*f4ee7fbaSAndroid Build Coastguard Worker literal_costs[i + 1] = literal_costs[i] + literal_carry;
193*f4ee7fbaSAndroid Build Coastguard Worker literal_carry -= literal_costs[i + 1] - literal_costs[i];
194*f4ee7fbaSAndroid Build Coastguard Worker }
195*f4ee7fbaSAndroid Build Coastguard Worker }
196*f4ee7fbaSAndroid Build Coastguard Worker }
197*f4ee7fbaSAndroid Build Coastguard Worker
ZopfliCostModelSetFromLiteralCosts(ZopfliCostModel * self,size_t position,const uint8_t * ringbuffer,size_t ringbuffer_mask)198*f4ee7fbaSAndroid Build Coastguard Worker static void ZopfliCostModelSetFromLiteralCosts(ZopfliCostModel* self,
199*f4ee7fbaSAndroid Build Coastguard Worker size_t position,
200*f4ee7fbaSAndroid Build Coastguard Worker const uint8_t* ringbuffer,
201*f4ee7fbaSAndroid Build Coastguard Worker size_t ringbuffer_mask) {
202*f4ee7fbaSAndroid Build Coastguard Worker float* literal_costs = self->literal_costs_;
203*f4ee7fbaSAndroid Build Coastguard Worker float literal_carry = 0.0;
204*f4ee7fbaSAndroid Build Coastguard Worker float* cost_dist = self->cost_dist_;
205*f4ee7fbaSAndroid Build Coastguard Worker float* cost_cmd = self->cost_cmd_;
206*f4ee7fbaSAndroid Build Coastguard Worker size_t num_bytes = self->num_bytes_;
207*f4ee7fbaSAndroid Build Coastguard Worker size_t i;
208*f4ee7fbaSAndroid Build Coastguard Worker BrotliEstimateBitCostsForLiterals(position, num_bytes, ringbuffer_mask,
209*f4ee7fbaSAndroid Build Coastguard Worker ringbuffer, &literal_costs[1]);
210*f4ee7fbaSAndroid Build Coastguard Worker literal_costs[0] = 0.0;
211*f4ee7fbaSAndroid Build Coastguard Worker for (i = 0; i < num_bytes; ++i) {
212*f4ee7fbaSAndroid Build Coastguard Worker literal_carry += literal_costs[i + 1];
213*f4ee7fbaSAndroid Build Coastguard Worker literal_costs[i + 1] = literal_costs[i] + literal_carry;
214*f4ee7fbaSAndroid Build Coastguard Worker literal_carry -= literal_costs[i + 1] - literal_costs[i];
215*f4ee7fbaSAndroid Build Coastguard Worker }
216*f4ee7fbaSAndroid Build Coastguard Worker for (i = 0; i < BROTLI_NUM_COMMAND_SYMBOLS; ++i) {
217*f4ee7fbaSAndroid Build Coastguard Worker cost_cmd[i] = (float)FastLog2(11 + (uint32_t)i);
218*f4ee7fbaSAndroid Build Coastguard Worker }
219*f4ee7fbaSAndroid Build Coastguard Worker for (i = 0; i < self->distance_histogram_size; ++i) {
220*f4ee7fbaSAndroid Build Coastguard Worker cost_dist[i] = (float)FastLog2(20 + (uint32_t)i);
221*f4ee7fbaSAndroid Build Coastguard Worker }
222*f4ee7fbaSAndroid Build Coastguard Worker self->min_cost_cmd_ = (float)FastLog2(11);
223*f4ee7fbaSAndroid Build Coastguard Worker }
224*f4ee7fbaSAndroid Build Coastguard Worker
ZopfliCostModelGetCommandCost(const ZopfliCostModel * self,uint16_t cmdcode)225*f4ee7fbaSAndroid Build Coastguard Worker static BROTLI_INLINE float ZopfliCostModelGetCommandCost(
226*f4ee7fbaSAndroid Build Coastguard Worker const ZopfliCostModel* self, uint16_t cmdcode) {
227*f4ee7fbaSAndroid Build Coastguard Worker return self->cost_cmd_[cmdcode];
228*f4ee7fbaSAndroid Build Coastguard Worker }
229*f4ee7fbaSAndroid Build Coastguard Worker
ZopfliCostModelGetDistanceCost(const ZopfliCostModel * self,size_t distcode)230*f4ee7fbaSAndroid Build Coastguard Worker static BROTLI_INLINE float ZopfliCostModelGetDistanceCost(
231*f4ee7fbaSAndroid Build Coastguard Worker const ZopfliCostModel* self, size_t distcode) {
232*f4ee7fbaSAndroid Build Coastguard Worker return self->cost_dist_[distcode];
233*f4ee7fbaSAndroid Build Coastguard Worker }
234*f4ee7fbaSAndroid Build Coastguard Worker
ZopfliCostModelGetLiteralCosts(const ZopfliCostModel * self,size_t from,size_t to)235*f4ee7fbaSAndroid Build Coastguard Worker static BROTLI_INLINE float ZopfliCostModelGetLiteralCosts(
236*f4ee7fbaSAndroid Build Coastguard Worker const ZopfliCostModel* self, size_t from, size_t to) {
237*f4ee7fbaSAndroid Build Coastguard Worker return self->literal_costs_[to] - self->literal_costs_[from];
238*f4ee7fbaSAndroid Build Coastguard Worker }
239*f4ee7fbaSAndroid Build Coastguard Worker
ZopfliCostModelGetMinCostCmd(const ZopfliCostModel * self)240*f4ee7fbaSAndroid Build Coastguard Worker static BROTLI_INLINE float ZopfliCostModelGetMinCostCmd(
241*f4ee7fbaSAndroid Build Coastguard Worker const ZopfliCostModel* self) {
242*f4ee7fbaSAndroid Build Coastguard Worker return self->min_cost_cmd_;
243*f4ee7fbaSAndroid Build Coastguard Worker }
244*f4ee7fbaSAndroid Build Coastguard Worker
245*f4ee7fbaSAndroid Build Coastguard Worker /* REQUIRES: len >= 2, start_pos <= pos */
246*f4ee7fbaSAndroid Build Coastguard Worker /* REQUIRES: cost < kInfinity, nodes[start_pos].cost < kInfinity */
247*f4ee7fbaSAndroid Build Coastguard Worker /* Maintains the "ZopfliNode array invariant". */
UpdateZopfliNode(ZopfliNode * nodes,size_t pos,size_t start_pos,size_t len,size_t len_code,size_t dist,size_t short_code,float cost)248*f4ee7fbaSAndroid Build Coastguard Worker static BROTLI_INLINE void UpdateZopfliNode(ZopfliNode* nodes, size_t pos,
249*f4ee7fbaSAndroid Build Coastguard Worker size_t start_pos, size_t len, size_t len_code, size_t dist,
250*f4ee7fbaSAndroid Build Coastguard Worker size_t short_code, float cost) {
251*f4ee7fbaSAndroid Build Coastguard Worker ZopfliNode* next = &nodes[pos + len];
252*f4ee7fbaSAndroid Build Coastguard Worker next->length = (uint32_t)(len | ((len + 9u - len_code) << 25));
253*f4ee7fbaSAndroid Build Coastguard Worker next->distance = (uint32_t)dist;
254*f4ee7fbaSAndroid Build Coastguard Worker next->dcode_insert_length = (uint32_t)(
255*f4ee7fbaSAndroid Build Coastguard Worker (short_code << 27) | (pos - start_pos));
256*f4ee7fbaSAndroid Build Coastguard Worker next->u.cost = cost;
257*f4ee7fbaSAndroid Build Coastguard Worker }
258*f4ee7fbaSAndroid Build Coastguard Worker
259*f4ee7fbaSAndroid Build Coastguard Worker typedef struct PosData {
260*f4ee7fbaSAndroid Build Coastguard Worker size_t pos;
261*f4ee7fbaSAndroid Build Coastguard Worker int distance_cache[4];
262*f4ee7fbaSAndroid Build Coastguard Worker float costdiff;
263*f4ee7fbaSAndroid Build Coastguard Worker float cost;
264*f4ee7fbaSAndroid Build Coastguard Worker } PosData;
265*f4ee7fbaSAndroid Build Coastguard Worker
266*f4ee7fbaSAndroid Build Coastguard Worker /* Maintains the smallest 8 cost difference together with their positions */
267*f4ee7fbaSAndroid Build Coastguard Worker typedef struct StartPosQueue {
268*f4ee7fbaSAndroid Build Coastguard Worker PosData q_[8];
269*f4ee7fbaSAndroid Build Coastguard Worker size_t idx_;
270*f4ee7fbaSAndroid Build Coastguard Worker } StartPosQueue;
271*f4ee7fbaSAndroid Build Coastguard Worker
InitStartPosQueue(StartPosQueue * self)272*f4ee7fbaSAndroid Build Coastguard Worker static BROTLI_INLINE void InitStartPosQueue(StartPosQueue* self) {
273*f4ee7fbaSAndroid Build Coastguard Worker self->idx_ = 0;
274*f4ee7fbaSAndroid Build Coastguard Worker }
275*f4ee7fbaSAndroid Build Coastguard Worker
StartPosQueueSize(const StartPosQueue * self)276*f4ee7fbaSAndroid Build Coastguard Worker static size_t StartPosQueueSize(const StartPosQueue* self) {
277*f4ee7fbaSAndroid Build Coastguard Worker return BROTLI_MIN(size_t, self->idx_, 8);
278*f4ee7fbaSAndroid Build Coastguard Worker }
279*f4ee7fbaSAndroid Build Coastguard Worker
StartPosQueuePush(StartPosQueue * self,const PosData * posdata)280*f4ee7fbaSAndroid Build Coastguard Worker static void StartPosQueuePush(StartPosQueue* self, const PosData* posdata) {
281*f4ee7fbaSAndroid Build Coastguard Worker size_t offset = ~(self->idx_++) & 7;
282*f4ee7fbaSAndroid Build Coastguard Worker size_t len = StartPosQueueSize(self);
283*f4ee7fbaSAndroid Build Coastguard Worker size_t i;
284*f4ee7fbaSAndroid Build Coastguard Worker PosData* q = self->q_;
285*f4ee7fbaSAndroid Build Coastguard Worker q[offset] = *posdata;
286*f4ee7fbaSAndroid Build Coastguard Worker /* Restore the sorted order. In the list of |len| items at most |len - 1|
287*f4ee7fbaSAndroid Build Coastguard Worker adjacent element comparisons / swaps are required. */
288*f4ee7fbaSAndroid Build Coastguard Worker for (i = 1; i < len; ++i) {
289*f4ee7fbaSAndroid Build Coastguard Worker if (q[offset & 7].costdiff > q[(offset + 1) & 7].costdiff) {
290*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_SWAP(PosData, q, offset & 7, (offset + 1) & 7);
291*f4ee7fbaSAndroid Build Coastguard Worker }
292*f4ee7fbaSAndroid Build Coastguard Worker ++offset;
293*f4ee7fbaSAndroid Build Coastguard Worker }
294*f4ee7fbaSAndroid Build Coastguard Worker }
295*f4ee7fbaSAndroid Build Coastguard Worker
StartPosQueueAt(const StartPosQueue * self,size_t k)296*f4ee7fbaSAndroid Build Coastguard Worker static const PosData* StartPosQueueAt(const StartPosQueue* self, size_t k) {
297*f4ee7fbaSAndroid Build Coastguard Worker return &self->q_[(k - self->idx_) & 7];
298*f4ee7fbaSAndroid Build Coastguard Worker }
299*f4ee7fbaSAndroid Build Coastguard Worker
300*f4ee7fbaSAndroid Build Coastguard Worker /* Returns the minimum possible copy length that can improve the cost of any */
301*f4ee7fbaSAndroid Build Coastguard Worker /* future position. */
ComputeMinimumCopyLength(const float start_cost,const ZopfliNode * nodes,const size_t num_bytes,const size_t pos)302*f4ee7fbaSAndroid Build Coastguard Worker static size_t ComputeMinimumCopyLength(const float start_cost,
303*f4ee7fbaSAndroid Build Coastguard Worker const ZopfliNode* nodes,
304*f4ee7fbaSAndroid Build Coastguard Worker const size_t num_bytes,
305*f4ee7fbaSAndroid Build Coastguard Worker const size_t pos) {
306*f4ee7fbaSAndroid Build Coastguard Worker /* Compute the minimum possible cost of reaching any future position. */
307*f4ee7fbaSAndroid Build Coastguard Worker float min_cost = start_cost;
308*f4ee7fbaSAndroid Build Coastguard Worker size_t len = 2;
309*f4ee7fbaSAndroid Build Coastguard Worker size_t next_len_bucket = 4;
310*f4ee7fbaSAndroid Build Coastguard Worker size_t next_len_offset = 10;
311*f4ee7fbaSAndroid Build Coastguard Worker while (pos + len <= num_bytes && nodes[pos + len].u.cost <= min_cost) {
312*f4ee7fbaSAndroid Build Coastguard Worker /* We already reached (pos + len) with no more cost than the minimum
313*f4ee7fbaSAndroid Build Coastguard Worker possible cost of reaching anything from this pos, so there is no point in
314*f4ee7fbaSAndroid Build Coastguard Worker looking for lengths <= len. */
315*f4ee7fbaSAndroid Build Coastguard Worker ++len;
316*f4ee7fbaSAndroid Build Coastguard Worker if (len == next_len_offset) {
317*f4ee7fbaSAndroid Build Coastguard Worker /* We reached the next copy length code bucket, so we add one more
318*f4ee7fbaSAndroid Build Coastguard Worker extra bit to the minimum cost. */
319*f4ee7fbaSAndroid Build Coastguard Worker min_cost += 1.0f;
320*f4ee7fbaSAndroid Build Coastguard Worker next_len_offset += next_len_bucket;
321*f4ee7fbaSAndroid Build Coastguard Worker next_len_bucket *= 2;
322*f4ee7fbaSAndroid Build Coastguard Worker }
323*f4ee7fbaSAndroid Build Coastguard Worker }
324*f4ee7fbaSAndroid Build Coastguard Worker return len;
325*f4ee7fbaSAndroid Build Coastguard Worker }
326*f4ee7fbaSAndroid Build Coastguard Worker
327*f4ee7fbaSAndroid Build Coastguard Worker /* REQUIRES: nodes[pos].cost < kInfinity
328*f4ee7fbaSAndroid Build Coastguard Worker REQUIRES: nodes[0..pos] satisfies that "ZopfliNode array invariant". */
ComputeDistanceShortcut(const size_t block_start,const size_t pos,const size_t max_backward_limit,const size_t gap,const ZopfliNode * nodes)329*f4ee7fbaSAndroid Build Coastguard Worker static uint32_t ComputeDistanceShortcut(const size_t block_start,
330*f4ee7fbaSAndroid Build Coastguard Worker const size_t pos,
331*f4ee7fbaSAndroid Build Coastguard Worker const size_t max_backward_limit,
332*f4ee7fbaSAndroid Build Coastguard Worker const size_t gap,
333*f4ee7fbaSAndroid Build Coastguard Worker const ZopfliNode* nodes) {
334*f4ee7fbaSAndroid Build Coastguard Worker const size_t clen = ZopfliNodeCopyLength(&nodes[pos]);
335*f4ee7fbaSAndroid Build Coastguard Worker const size_t ilen = nodes[pos].dcode_insert_length & 0x7FFFFFF;
336*f4ee7fbaSAndroid Build Coastguard Worker const size_t dist = ZopfliNodeCopyDistance(&nodes[pos]);
337*f4ee7fbaSAndroid Build Coastguard Worker /* Since |block_start + pos| is the end position of the command, the copy part
338*f4ee7fbaSAndroid Build Coastguard Worker starts from |block_start + pos - clen|. Distances that are greater than
339*f4ee7fbaSAndroid Build Coastguard Worker this or greater than |max_backward_limit| + |gap| are static dictionary
340*f4ee7fbaSAndroid Build Coastguard Worker references, and do not update the last distances.
341*f4ee7fbaSAndroid Build Coastguard Worker Also distance code 0 (last distance) does not update the last distances. */
342*f4ee7fbaSAndroid Build Coastguard Worker if (pos == 0) {
343*f4ee7fbaSAndroid Build Coastguard Worker return 0;
344*f4ee7fbaSAndroid Build Coastguard Worker } else if (dist + clen <= block_start + pos + gap &&
345*f4ee7fbaSAndroid Build Coastguard Worker dist <= max_backward_limit + gap &&
346*f4ee7fbaSAndroid Build Coastguard Worker ZopfliNodeDistanceCode(&nodes[pos]) > 0) {
347*f4ee7fbaSAndroid Build Coastguard Worker return (uint32_t)pos;
348*f4ee7fbaSAndroid Build Coastguard Worker } else {
349*f4ee7fbaSAndroid Build Coastguard Worker return nodes[pos - clen - ilen].u.shortcut;
350*f4ee7fbaSAndroid Build Coastguard Worker }
351*f4ee7fbaSAndroid Build Coastguard Worker }
352*f4ee7fbaSAndroid Build Coastguard Worker
353*f4ee7fbaSAndroid Build Coastguard Worker /* Fills in dist_cache[0..3] with the last four distances (as defined by
354*f4ee7fbaSAndroid Build Coastguard Worker Section 4. of the Spec) that would be used at (block_start + pos) if we
355*f4ee7fbaSAndroid Build Coastguard Worker used the shortest path of commands from block_start, computed from
356*f4ee7fbaSAndroid Build Coastguard Worker nodes[0..pos]. The last four distances at block_start are in
357*f4ee7fbaSAndroid Build Coastguard Worker starting_dist_cache[0..3].
358*f4ee7fbaSAndroid Build Coastguard Worker REQUIRES: nodes[pos].cost < kInfinity
359*f4ee7fbaSAndroid Build Coastguard Worker REQUIRES: nodes[0..pos] satisfies that "ZopfliNode array invariant". */
ComputeDistanceCache(const size_t pos,const int * starting_dist_cache,const ZopfliNode * nodes,int * dist_cache)360*f4ee7fbaSAndroid Build Coastguard Worker static void ComputeDistanceCache(const size_t pos,
361*f4ee7fbaSAndroid Build Coastguard Worker const int* starting_dist_cache,
362*f4ee7fbaSAndroid Build Coastguard Worker const ZopfliNode* nodes,
363*f4ee7fbaSAndroid Build Coastguard Worker int* dist_cache) {
364*f4ee7fbaSAndroid Build Coastguard Worker int idx = 0;
365*f4ee7fbaSAndroid Build Coastguard Worker size_t p = nodes[pos].u.shortcut;
366*f4ee7fbaSAndroid Build Coastguard Worker while (idx < 4 && p > 0) {
367*f4ee7fbaSAndroid Build Coastguard Worker const size_t ilen = nodes[p].dcode_insert_length & 0x7FFFFFF;
368*f4ee7fbaSAndroid Build Coastguard Worker const size_t clen = ZopfliNodeCopyLength(&nodes[p]);
369*f4ee7fbaSAndroid Build Coastguard Worker const size_t dist = ZopfliNodeCopyDistance(&nodes[p]);
370*f4ee7fbaSAndroid Build Coastguard Worker dist_cache[idx++] = (int)dist;
371*f4ee7fbaSAndroid Build Coastguard Worker /* Because of prerequisite, p >= clen + ilen >= 2. */
372*f4ee7fbaSAndroid Build Coastguard Worker p = nodes[p - clen - ilen].u.shortcut;
373*f4ee7fbaSAndroid Build Coastguard Worker }
374*f4ee7fbaSAndroid Build Coastguard Worker for (; idx < 4; ++idx) {
375*f4ee7fbaSAndroid Build Coastguard Worker dist_cache[idx] = *starting_dist_cache++;
376*f4ee7fbaSAndroid Build Coastguard Worker }
377*f4ee7fbaSAndroid Build Coastguard Worker }
378*f4ee7fbaSAndroid Build Coastguard Worker
379*f4ee7fbaSAndroid Build Coastguard Worker /* Maintains "ZopfliNode array invariant" and pushes node to the queue, if it
380*f4ee7fbaSAndroid Build Coastguard Worker is eligible. */
EvaluateNode(const size_t block_start,const size_t pos,const size_t max_backward_limit,const size_t gap,const int * starting_dist_cache,const ZopfliCostModel * model,StartPosQueue * queue,ZopfliNode * nodes)381*f4ee7fbaSAndroid Build Coastguard Worker static void EvaluateNode(
382*f4ee7fbaSAndroid Build Coastguard Worker const size_t block_start, const size_t pos, const size_t max_backward_limit,
383*f4ee7fbaSAndroid Build Coastguard Worker const size_t gap, const int* starting_dist_cache,
384*f4ee7fbaSAndroid Build Coastguard Worker const ZopfliCostModel* model, StartPosQueue* queue, ZopfliNode* nodes) {
385*f4ee7fbaSAndroid Build Coastguard Worker /* Save cost, because ComputeDistanceCache invalidates it. */
386*f4ee7fbaSAndroid Build Coastguard Worker float node_cost = nodes[pos].u.cost;
387*f4ee7fbaSAndroid Build Coastguard Worker nodes[pos].u.shortcut = ComputeDistanceShortcut(
388*f4ee7fbaSAndroid Build Coastguard Worker block_start, pos, max_backward_limit, gap, nodes);
389*f4ee7fbaSAndroid Build Coastguard Worker if (node_cost <= ZopfliCostModelGetLiteralCosts(model, 0, pos)) {
390*f4ee7fbaSAndroid Build Coastguard Worker PosData posdata;
391*f4ee7fbaSAndroid Build Coastguard Worker posdata.pos = pos;
392*f4ee7fbaSAndroid Build Coastguard Worker posdata.cost = node_cost;
393*f4ee7fbaSAndroid Build Coastguard Worker posdata.costdiff = node_cost -
394*f4ee7fbaSAndroid Build Coastguard Worker ZopfliCostModelGetLiteralCosts(model, 0, pos);
395*f4ee7fbaSAndroid Build Coastguard Worker ComputeDistanceCache(
396*f4ee7fbaSAndroid Build Coastguard Worker pos, starting_dist_cache, nodes, posdata.distance_cache);
397*f4ee7fbaSAndroid Build Coastguard Worker StartPosQueuePush(queue, &posdata);
398*f4ee7fbaSAndroid Build Coastguard Worker }
399*f4ee7fbaSAndroid Build Coastguard Worker }
400*f4ee7fbaSAndroid Build Coastguard Worker
401*f4ee7fbaSAndroid Build Coastguard Worker /* Returns longest copy length. */
UpdateNodes(const size_t num_bytes,const size_t block_start,const size_t pos,const uint8_t * ringbuffer,const size_t ringbuffer_mask,const BrotliEncoderParams * params,const size_t max_backward_limit,const int * starting_dist_cache,const size_t num_matches,const BackwardMatch * matches,const ZopfliCostModel * model,StartPosQueue * queue,ZopfliNode * nodes)402*f4ee7fbaSAndroid Build Coastguard Worker static size_t UpdateNodes(
403*f4ee7fbaSAndroid Build Coastguard Worker const size_t num_bytes, const size_t block_start, const size_t pos,
404*f4ee7fbaSAndroid Build Coastguard Worker const uint8_t* ringbuffer, const size_t ringbuffer_mask,
405*f4ee7fbaSAndroid Build Coastguard Worker const BrotliEncoderParams* params, const size_t max_backward_limit,
406*f4ee7fbaSAndroid Build Coastguard Worker const int* starting_dist_cache, const size_t num_matches,
407*f4ee7fbaSAndroid Build Coastguard Worker const BackwardMatch* matches, const ZopfliCostModel* model,
408*f4ee7fbaSAndroid Build Coastguard Worker StartPosQueue* queue, ZopfliNode* nodes) {
409*f4ee7fbaSAndroid Build Coastguard Worker const size_t stream_offset = params->stream_offset;
410*f4ee7fbaSAndroid Build Coastguard Worker const size_t cur_ix = block_start + pos;
411*f4ee7fbaSAndroid Build Coastguard Worker const size_t cur_ix_masked = cur_ix & ringbuffer_mask;
412*f4ee7fbaSAndroid Build Coastguard Worker const size_t max_distance = BROTLI_MIN(size_t, cur_ix, max_backward_limit);
413*f4ee7fbaSAndroid Build Coastguard Worker const size_t dictionary_start = BROTLI_MIN(size_t,
414*f4ee7fbaSAndroid Build Coastguard Worker cur_ix + stream_offset, max_backward_limit);
415*f4ee7fbaSAndroid Build Coastguard Worker const size_t max_len = num_bytes - pos;
416*f4ee7fbaSAndroid Build Coastguard Worker const size_t max_zopfli_len = MaxZopfliLen(params);
417*f4ee7fbaSAndroid Build Coastguard Worker const size_t max_iters = MaxZopfliCandidates(params);
418*f4ee7fbaSAndroid Build Coastguard Worker size_t min_len;
419*f4ee7fbaSAndroid Build Coastguard Worker size_t result = 0;
420*f4ee7fbaSAndroid Build Coastguard Worker size_t k;
421*f4ee7fbaSAndroid Build Coastguard Worker size_t gap = 0;
422*f4ee7fbaSAndroid Build Coastguard Worker
423*f4ee7fbaSAndroid Build Coastguard Worker EvaluateNode(block_start + stream_offset, pos, max_backward_limit, gap,
424*f4ee7fbaSAndroid Build Coastguard Worker starting_dist_cache, model, queue, nodes);
425*f4ee7fbaSAndroid Build Coastguard Worker
426*f4ee7fbaSAndroid Build Coastguard Worker {
427*f4ee7fbaSAndroid Build Coastguard Worker const PosData* posdata = StartPosQueueAt(queue, 0);
428*f4ee7fbaSAndroid Build Coastguard Worker float min_cost = (posdata->cost + ZopfliCostModelGetMinCostCmd(model) +
429*f4ee7fbaSAndroid Build Coastguard Worker ZopfliCostModelGetLiteralCosts(model, posdata->pos, pos));
430*f4ee7fbaSAndroid Build Coastguard Worker min_len = ComputeMinimumCopyLength(min_cost, nodes, num_bytes, pos);
431*f4ee7fbaSAndroid Build Coastguard Worker }
432*f4ee7fbaSAndroid Build Coastguard Worker
433*f4ee7fbaSAndroid Build Coastguard Worker /* Go over the command starting positions in order of increasing cost
434*f4ee7fbaSAndroid Build Coastguard Worker difference. */
435*f4ee7fbaSAndroid Build Coastguard Worker for (k = 0; k < max_iters && k < StartPosQueueSize(queue); ++k) {
436*f4ee7fbaSAndroid Build Coastguard Worker const PosData* posdata = StartPosQueueAt(queue, k);
437*f4ee7fbaSAndroid Build Coastguard Worker const size_t start = posdata->pos;
438*f4ee7fbaSAndroid Build Coastguard Worker const uint16_t inscode = GetInsertLengthCode(pos - start);
439*f4ee7fbaSAndroid Build Coastguard Worker const float start_costdiff = posdata->costdiff;
440*f4ee7fbaSAndroid Build Coastguard Worker const float base_cost = start_costdiff + (float)GetInsertExtra(inscode) +
441*f4ee7fbaSAndroid Build Coastguard Worker ZopfliCostModelGetLiteralCosts(model, 0, pos);
442*f4ee7fbaSAndroid Build Coastguard Worker
443*f4ee7fbaSAndroid Build Coastguard Worker /* Look for last distance matches using the distance cache from this
444*f4ee7fbaSAndroid Build Coastguard Worker starting position. */
445*f4ee7fbaSAndroid Build Coastguard Worker size_t best_len = min_len - 1;
446*f4ee7fbaSAndroid Build Coastguard Worker size_t j = 0;
447*f4ee7fbaSAndroid Build Coastguard Worker for (; j < BROTLI_NUM_DISTANCE_SHORT_CODES && best_len < max_len; ++j) {
448*f4ee7fbaSAndroid Build Coastguard Worker const size_t idx = kDistanceCacheIndex[j];
449*f4ee7fbaSAndroid Build Coastguard Worker const size_t backward =
450*f4ee7fbaSAndroid Build Coastguard Worker (size_t)(posdata->distance_cache[idx] + kDistanceCacheOffset[j]);
451*f4ee7fbaSAndroid Build Coastguard Worker size_t prev_ix = cur_ix - backward;
452*f4ee7fbaSAndroid Build Coastguard Worker size_t len = 0;
453*f4ee7fbaSAndroid Build Coastguard Worker uint8_t continuation = ringbuffer[cur_ix_masked + best_len];
454*f4ee7fbaSAndroid Build Coastguard Worker if (cur_ix_masked + best_len > ringbuffer_mask) {
455*f4ee7fbaSAndroid Build Coastguard Worker break;
456*f4ee7fbaSAndroid Build Coastguard Worker }
457*f4ee7fbaSAndroid Build Coastguard Worker if (BROTLI_PREDICT_FALSE(backward > dictionary_start + gap)) {
458*f4ee7fbaSAndroid Build Coastguard Worker /* Word dictionary -> ignore. */
459*f4ee7fbaSAndroid Build Coastguard Worker continue;
460*f4ee7fbaSAndroid Build Coastguard Worker }
461*f4ee7fbaSAndroid Build Coastguard Worker if (backward <= max_distance) {
462*f4ee7fbaSAndroid Build Coastguard Worker /* Regular backward reference. */
463*f4ee7fbaSAndroid Build Coastguard Worker if (prev_ix >= cur_ix) {
464*f4ee7fbaSAndroid Build Coastguard Worker continue;
465*f4ee7fbaSAndroid Build Coastguard Worker }
466*f4ee7fbaSAndroid Build Coastguard Worker
467*f4ee7fbaSAndroid Build Coastguard Worker prev_ix &= ringbuffer_mask;
468*f4ee7fbaSAndroid Build Coastguard Worker if (prev_ix + best_len > ringbuffer_mask ||
469*f4ee7fbaSAndroid Build Coastguard Worker continuation != ringbuffer[prev_ix + best_len]) {
470*f4ee7fbaSAndroid Build Coastguard Worker continue;
471*f4ee7fbaSAndroid Build Coastguard Worker }
472*f4ee7fbaSAndroid Build Coastguard Worker len = FindMatchLengthWithLimit(&ringbuffer[prev_ix],
473*f4ee7fbaSAndroid Build Coastguard Worker &ringbuffer[cur_ix_masked],
474*f4ee7fbaSAndroid Build Coastguard Worker max_len);
475*f4ee7fbaSAndroid Build Coastguard Worker } else {
476*f4ee7fbaSAndroid Build Coastguard Worker /* "Gray" area. It is addressable by decoder, but this encoder
477*f4ee7fbaSAndroid Build Coastguard Worker instance does not have that data -> should not touch it. */
478*f4ee7fbaSAndroid Build Coastguard Worker continue;
479*f4ee7fbaSAndroid Build Coastguard Worker }
480*f4ee7fbaSAndroid Build Coastguard Worker {
481*f4ee7fbaSAndroid Build Coastguard Worker const float dist_cost = base_cost +
482*f4ee7fbaSAndroid Build Coastguard Worker ZopfliCostModelGetDistanceCost(model, j);
483*f4ee7fbaSAndroid Build Coastguard Worker size_t l;
484*f4ee7fbaSAndroid Build Coastguard Worker for (l = best_len + 1; l <= len; ++l) {
485*f4ee7fbaSAndroid Build Coastguard Worker const uint16_t copycode = GetCopyLengthCode(l);
486*f4ee7fbaSAndroid Build Coastguard Worker const uint16_t cmdcode =
487*f4ee7fbaSAndroid Build Coastguard Worker CombineLengthCodes(inscode, copycode, j == 0);
488*f4ee7fbaSAndroid Build Coastguard Worker const float cost = (cmdcode < 128 ? base_cost : dist_cost) +
489*f4ee7fbaSAndroid Build Coastguard Worker (float)GetCopyExtra(copycode) +
490*f4ee7fbaSAndroid Build Coastguard Worker ZopfliCostModelGetCommandCost(model, cmdcode);
491*f4ee7fbaSAndroid Build Coastguard Worker if (cost < nodes[pos + l].u.cost) {
492*f4ee7fbaSAndroid Build Coastguard Worker UpdateZopfliNode(nodes, pos, start, l, l, backward, j + 1, cost);
493*f4ee7fbaSAndroid Build Coastguard Worker result = BROTLI_MAX(size_t, result, l);
494*f4ee7fbaSAndroid Build Coastguard Worker }
495*f4ee7fbaSAndroid Build Coastguard Worker best_len = l;
496*f4ee7fbaSAndroid Build Coastguard Worker }
497*f4ee7fbaSAndroid Build Coastguard Worker }
498*f4ee7fbaSAndroid Build Coastguard Worker }
499*f4ee7fbaSAndroid Build Coastguard Worker
500*f4ee7fbaSAndroid Build Coastguard Worker /* At higher iterations look only for new last distance matches, since
501*f4ee7fbaSAndroid Build Coastguard Worker looking only for new command start positions with the same distances
502*f4ee7fbaSAndroid Build Coastguard Worker does not help much. */
503*f4ee7fbaSAndroid Build Coastguard Worker if (k >= 2) continue;
504*f4ee7fbaSAndroid Build Coastguard Worker
505*f4ee7fbaSAndroid Build Coastguard Worker {
506*f4ee7fbaSAndroid Build Coastguard Worker /* Loop through all possible copy lengths at this position. */
507*f4ee7fbaSAndroid Build Coastguard Worker size_t len = min_len;
508*f4ee7fbaSAndroid Build Coastguard Worker for (j = 0; j < num_matches; ++j) {
509*f4ee7fbaSAndroid Build Coastguard Worker BackwardMatch match = matches[j];
510*f4ee7fbaSAndroid Build Coastguard Worker size_t dist = match.distance;
511*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_BOOL is_dictionary_match =
512*f4ee7fbaSAndroid Build Coastguard Worker TO_BROTLI_BOOL(dist > dictionary_start + gap);
513*f4ee7fbaSAndroid Build Coastguard Worker /* We already tried all possible last distance matches, so we can use
514*f4ee7fbaSAndroid Build Coastguard Worker normal distance code here. */
515*f4ee7fbaSAndroid Build Coastguard Worker size_t dist_code = dist + BROTLI_NUM_DISTANCE_SHORT_CODES - 1;
516*f4ee7fbaSAndroid Build Coastguard Worker uint16_t dist_symbol;
517*f4ee7fbaSAndroid Build Coastguard Worker uint32_t distextra;
518*f4ee7fbaSAndroid Build Coastguard Worker uint32_t distnumextra;
519*f4ee7fbaSAndroid Build Coastguard Worker float dist_cost;
520*f4ee7fbaSAndroid Build Coastguard Worker size_t max_match_len;
521*f4ee7fbaSAndroid Build Coastguard Worker PrefixEncodeCopyDistance(
522*f4ee7fbaSAndroid Build Coastguard Worker dist_code, params->dist.num_direct_distance_codes,
523*f4ee7fbaSAndroid Build Coastguard Worker params->dist.distance_postfix_bits, &dist_symbol, &distextra);
524*f4ee7fbaSAndroid Build Coastguard Worker distnumextra = dist_symbol >> 10;
525*f4ee7fbaSAndroid Build Coastguard Worker dist_cost = base_cost + (float)distnumextra +
526*f4ee7fbaSAndroid Build Coastguard Worker ZopfliCostModelGetDistanceCost(model, dist_symbol & 0x3FF);
527*f4ee7fbaSAndroid Build Coastguard Worker
528*f4ee7fbaSAndroid Build Coastguard Worker /* Try all copy lengths up until the maximum copy length corresponding
529*f4ee7fbaSAndroid Build Coastguard Worker to this distance. If the distance refers to the static dictionary, or
530*f4ee7fbaSAndroid Build Coastguard Worker the maximum length is long enough, try only one maximum length. */
531*f4ee7fbaSAndroid Build Coastguard Worker max_match_len = BackwardMatchLength(&match);
532*f4ee7fbaSAndroid Build Coastguard Worker if (len < max_match_len &&
533*f4ee7fbaSAndroid Build Coastguard Worker (is_dictionary_match || max_match_len > max_zopfli_len)) {
534*f4ee7fbaSAndroid Build Coastguard Worker len = max_match_len;
535*f4ee7fbaSAndroid Build Coastguard Worker }
536*f4ee7fbaSAndroid Build Coastguard Worker for (; len <= max_match_len; ++len) {
537*f4ee7fbaSAndroid Build Coastguard Worker const size_t len_code =
538*f4ee7fbaSAndroid Build Coastguard Worker is_dictionary_match ? BackwardMatchLengthCode(&match) : len;
539*f4ee7fbaSAndroid Build Coastguard Worker const uint16_t copycode = GetCopyLengthCode(len_code);
540*f4ee7fbaSAndroid Build Coastguard Worker const uint16_t cmdcode = CombineLengthCodes(inscode, copycode, 0);
541*f4ee7fbaSAndroid Build Coastguard Worker const float cost = dist_cost + (float)GetCopyExtra(copycode) +
542*f4ee7fbaSAndroid Build Coastguard Worker ZopfliCostModelGetCommandCost(model, cmdcode);
543*f4ee7fbaSAndroid Build Coastguard Worker if (cost < nodes[pos + len].u.cost) {
544*f4ee7fbaSAndroid Build Coastguard Worker UpdateZopfliNode(nodes, pos, start, len, len_code, dist, 0, cost);
545*f4ee7fbaSAndroid Build Coastguard Worker result = BROTLI_MAX(size_t, result, len);
546*f4ee7fbaSAndroid Build Coastguard Worker }
547*f4ee7fbaSAndroid Build Coastguard Worker }
548*f4ee7fbaSAndroid Build Coastguard Worker }
549*f4ee7fbaSAndroid Build Coastguard Worker }
550*f4ee7fbaSAndroid Build Coastguard Worker }
551*f4ee7fbaSAndroid Build Coastguard Worker return result;
552*f4ee7fbaSAndroid Build Coastguard Worker }
553*f4ee7fbaSAndroid Build Coastguard Worker
ComputeShortestPathFromNodes(size_t num_bytes,ZopfliNode * nodes)554*f4ee7fbaSAndroid Build Coastguard Worker static size_t ComputeShortestPathFromNodes(size_t num_bytes,
555*f4ee7fbaSAndroid Build Coastguard Worker ZopfliNode* nodes) {
556*f4ee7fbaSAndroid Build Coastguard Worker size_t index = num_bytes;
557*f4ee7fbaSAndroid Build Coastguard Worker size_t num_commands = 0;
558*f4ee7fbaSAndroid Build Coastguard Worker while ((nodes[index].dcode_insert_length & 0x7FFFFFF) == 0 &&
559*f4ee7fbaSAndroid Build Coastguard Worker nodes[index].length == 1) --index;
560*f4ee7fbaSAndroid Build Coastguard Worker nodes[index].u.next = BROTLI_UINT32_MAX;
561*f4ee7fbaSAndroid Build Coastguard Worker while (index != 0) {
562*f4ee7fbaSAndroid Build Coastguard Worker size_t len = ZopfliNodeCommandLength(&nodes[index]);
563*f4ee7fbaSAndroid Build Coastguard Worker index -= len;
564*f4ee7fbaSAndroid Build Coastguard Worker nodes[index].u.next = (uint32_t)len;
565*f4ee7fbaSAndroid Build Coastguard Worker num_commands++;
566*f4ee7fbaSAndroid Build Coastguard Worker }
567*f4ee7fbaSAndroid Build Coastguard Worker return num_commands;
568*f4ee7fbaSAndroid Build Coastguard Worker }
569*f4ee7fbaSAndroid Build Coastguard Worker
570*f4ee7fbaSAndroid Build Coastguard Worker /* REQUIRES: nodes != NULL and len(nodes) >= num_bytes + 1 */
BrotliZopfliCreateCommands(const size_t num_bytes,const size_t block_start,const ZopfliNode * nodes,int * dist_cache,size_t * last_insert_len,const BrotliEncoderParams * params,Command * commands,size_t * num_literals)571*f4ee7fbaSAndroid Build Coastguard Worker void BrotliZopfliCreateCommands(const size_t num_bytes,
572*f4ee7fbaSAndroid Build Coastguard Worker const size_t block_start, const ZopfliNode* nodes, int* dist_cache,
573*f4ee7fbaSAndroid Build Coastguard Worker size_t* last_insert_len, const BrotliEncoderParams* params,
574*f4ee7fbaSAndroid Build Coastguard Worker Command* commands, size_t* num_literals) {
575*f4ee7fbaSAndroid Build Coastguard Worker const size_t stream_offset = params->stream_offset;
576*f4ee7fbaSAndroid Build Coastguard Worker const size_t max_backward_limit = BROTLI_MAX_BACKWARD_LIMIT(params->lgwin);
577*f4ee7fbaSAndroid Build Coastguard Worker size_t pos = 0;
578*f4ee7fbaSAndroid Build Coastguard Worker uint32_t offset = nodes[0].u.next;
579*f4ee7fbaSAndroid Build Coastguard Worker size_t i;
580*f4ee7fbaSAndroid Build Coastguard Worker size_t gap = 0;
581*f4ee7fbaSAndroid Build Coastguard Worker for (i = 0; offset != BROTLI_UINT32_MAX; i++) {
582*f4ee7fbaSAndroid Build Coastguard Worker const ZopfliNode* next = &nodes[pos + offset];
583*f4ee7fbaSAndroid Build Coastguard Worker size_t copy_length = ZopfliNodeCopyLength(next);
584*f4ee7fbaSAndroid Build Coastguard Worker size_t insert_length = next->dcode_insert_length & 0x7FFFFFF;
585*f4ee7fbaSAndroid Build Coastguard Worker pos += insert_length;
586*f4ee7fbaSAndroid Build Coastguard Worker offset = next->u.next;
587*f4ee7fbaSAndroid Build Coastguard Worker if (i == 0) {
588*f4ee7fbaSAndroid Build Coastguard Worker insert_length += *last_insert_len;
589*f4ee7fbaSAndroid Build Coastguard Worker *last_insert_len = 0;
590*f4ee7fbaSAndroid Build Coastguard Worker }
591*f4ee7fbaSAndroid Build Coastguard Worker {
592*f4ee7fbaSAndroid Build Coastguard Worker size_t distance = ZopfliNodeCopyDistance(next);
593*f4ee7fbaSAndroid Build Coastguard Worker size_t len_code = ZopfliNodeLengthCode(next);
594*f4ee7fbaSAndroid Build Coastguard Worker size_t dictionary_start = BROTLI_MIN(size_t,
595*f4ee7fbaSAndroid Build Coastguard Worker block_start + pos + stream_offset, max_backward_limit);
596*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_BOOL is_dictionary =
597*f4ee7fbaSAndroid Build Coastguard Worker TO_BROTLI_BOOL(distance > dictionary_start + gap);
598*f4ee7fbaSAndroid Build Coastguard Worker size_t dist_code = ZopfliNodeDistanceCode(next);
599*f4ee7fbaSAndroid Build Coastguard Worker InitCommand(&commands[i], ¶ms->dist, insert_length,
600*f4ee7fbaSAndroid Build Coastguard Worker copy_length, (int)len_code - (int)copy_length, dist_code);
601*f4ee7fbaSAndroid Build Coastguard Worker
602*f4ee7fbaSAndroid Build Coastguard Worker if (!is_dictionary && dist_code > 0) {
603*f4ee7fbaSAndroid Build Coastguard Worker dist_cache[3] = dist_cache[2];
604*f4ee7fbaSAndroid Build Coastguard Worker dist_cache[2] = dist_cache[1];
605*f4ee7fbaSAndroid Build Coastguard Worker dist_cache[1] = dist_cache[0];
606*f4ee7fbaSAndroid Build Coastguard Worker dist_cache[0] = (int)distance;
607*f4ee7fbaSAndroid Build Coastguard Worker }
608*f4ee7fbaSAndroid Build Coastguard Worker }
609*f4ee7fbaSAndroid Build Coastguard Worker
610*f4ee7fbaSAndroid Build Coastguard Worker *num_literals += insert_length;
611*f4ee7fbaSAndroid Build Coastguard Worker pos += copy_length;
612*f4ee7fbaSAndroid Build Coastguard Worker }
613*f4ee7fbaSAndroid Build Coastguard Worker *last_insert_len += num_bytes - pos;
614*f4ee7fbaSAndroid Build Coastguard Worker }
615*f4ee7fbaSAndroid Build Coastguard Worker
ZopfliIterate(size_t num_bytes,size_t position,const uint8_t * ringbuffer,size_t ringbuffer_mask,const BrotliEncoderParams * params,const size_t gap,const int * dist_cache,const ZopfliCostModel * model,const uint32_t * num_matches,const BackwardMatch * matches,ZopfliNode * nodes)616*f4ee7fbaSAndroid Build Coastguard Worker static size_t ZopfliIterate(size_t num_bytes, size_t position,
617*f4ee7fbaSAndroid Build Coastguard Worker const uint8_t* ringbuffer, size_t ringbuffer_mask,
618*f4ee7fbaSAndroid Build Coastguard Worker const BrotliEncoderParams* params, const size_t gap, const int* dist_cache,
619*f4ee7fbaSAndroid Build Coastguard Worker const ZopfliCostModel* model, const uint32_t* num_matches,
620*f4ee7fbaSAndroid Build Coastguard Worker const BackwardMatch* matches, ZopfliNode* nodes) {
621*f4ee7fbaSAndroid Build Coastguard Worker const size_t stream_offset = params->stream_offset;
622*f4ee7fbaSAndroid Build Coastguard Worker const size_t max_backward_limit = BROTLI_MAX_BACKWARD_LIMIT(params->lgwin);
623*f4ee7fbaSAndroid Build Coastguard Worker const size_t max_zopfli_len = MaxZopfliLen(params);
624*f4ee7fbaSAndroid Build Coastguard Worker StartPosQueue queue;
625*f4ee7fbaSAndroid Build Coastguard Worker size_t cur_match_pos = 0;
626*f4ee7fbaSAndroid Build Coastguard Worker size_t i;
627*f4ee7fbaSAndroid Build Coastguard Worker nodes[0].length = 0;
628*f4ee7fbaSAndroid Build Coastguard Worker nodes[0].u.cost = 0;
629*f4ee7fbaSAndroid Build Coastguard Worker InitStartPosQueue(&queue);
630*f4ee7fbaSAndroid Build Coastguard Worker for (i = 0; i + 3 < num_bytes; i++) {
631*f4ee7fbaSAndroid Build Coastguard Worker size_t skip = UpdateNodes(num_bytes, position, i, ringbuffer,
632*f4ee7fbaSAndroid Build Coastguard Worker ringbuffer_mask, params, max_backward_limit, dist_cache,
633*f4ee7fbaSAndroid Build Coastguard Worker num_matches[i], &matches[cur_match_pos], model, &queue, nodes);
634*f4ee7fbaSAndroid Build Coastguard Worker if (skip < BROTLI_LONG_COPY_QUICK_STEP) skip = 0;
635*f4ee7fbaSAndroid Build Coastguard Worker cur_match_pos += num_matches[i];
636*f4ee7fbaSAndroid Build Coastguard Worker if (num_matches[i] == 1 &&
637*f4ee7fbaSAndroid Build Coastguard Worker BackwardMatchLength(&matches[cur_match_pos - 1]) > max_zopfli_len) {
638*f4ee7fbaSAndroid Build Coastguard Worker skip = BROTLI_MAX(size_t,
639*f4ee7fbaSAndroid Build Coastguard Worker BackwardMatchLength(&matches[cur_match_pos - 1]), skip);
640*f4ee7fbaSAndroid Build Coastguard Worker }
641*f4ee7fbaSAndroid Build Coastguard Worker if (skip > 1) {
642*f4ee7fbaSAndroid Build Coastguard Worker skip--;
643*f4ee7fbaSAndroid Build Coastguard Worker while (skip) {
644*f4ee7fbaSAndroid Build Coastguard Worker i++;
645*f4ee7fbaSAndroid Build Coastguard Worker if (i + 3 >= num_bytes) break;
646*f4ee7fbaSAndroid Build Coastguard Worker EvaluateNode(position + stream_offset, i, max_backward_limit, gap,
647*f4ee7fbaSAndroid Build Coastguard Worker dist_cache, model, &queue, nodes);
648*f4ee7fbaSAndroid Build Coastguard Worker cur_match_pos += num_matches[i];
649*f4ee7fbaSAndroid Build Coastguard Worker skip--;
650*f4ee7fbaSAndroid Build Coastguard Worker }
651*f4ee7fbaSAndroid Build Coastguard Worker }
652*f4ee7fbaSAndroid Build Coastguard Worker }
653*f4ee7fbaSAndroid Build Coastguard Worker return ComputeShortestPathFromNodes(num_bytes, nodes);
654*f4ee7fbaSAndroid Build Coastguard Worker }
655*f4ee7fbaSAndroid Build Coastguard Worker
656*f4ee7fbaSAndroid Build Coastguard Worker /* REQUIRES: nodes != NULL and len(nodes) >= num_bytes + 1 */
BrotliZopfliComputeShortestPath(MemoryManager * m,size_t num_bytes,size_t position,const uint8_t * ringbuffer,size_t ringbuffer_mask,ContextLut literal_context_lut,const BrotliEncoderParams * params,const int * dist_cache,Hasher * hasher,ZopfliNode * nodes)657*f4ee7fbaSAndroid Build Coastguard Worker size_t BrotliZopfliComputeShortestPath(MemoryManager* m, size_t num_bytes,
658*f4ee7fbaSAndroid Build Coastguard Worker size_t position, const uint8_t* ringbuffer, size_t ringbuffer_mask,
659*f4ee7fbaSAndroid Build Coastguard Worker ContextLut literal_context_lut, const BrotliEncoderParams* params,
660*f4ee7fbaSAndroid Build Coastguard Worker const int* dist_cache, Hasher* hasher, ZopfliNode* nodes) {
661*f4ee7fbaSAndroid Build Coastguard Worker const size_t stream_offset = params->stream_offset;
662*f4ee7fbaSAndroid Build Coastguard Worker const size_t max_backward_limit = BROTLI_MAX_BACKWARD_LIMIT(params->lgwin);
663*f4ee7fbaSAndroid Build Coastguard Worker const size_t max_zopfli_len = MaxZopfliLen(params);
664*f4ee7fbaSAndroid Build Coastguard Worker ZopfliCostModel model;
665*f4ee7fbaSAndroid Build Coastguard Worker StartPosQueue queue;
666*f4ee7fbaSAndroid Build Coastguard Worker BackwardMatch matches[2 * (MAX_NUM_MATCHES_H10 + 64)];
667*f4ee7fbaSAndroid Build Coastguard Worker const size_t store_end = num_bytes >= StoreLookaheadH10() ?
668*f4ee7fbaSAndroid Build Coastguard Worker position + num_bytes - StoreLookaheadH10() + 1 : position;
669*f4ee7fbaSAndroid Build Coastguard Worker size_t i;
670*f4ee7fbaSAndroid Build Coastguard Worker size_t gap = 0;
671*f4ee7fbaSAndroid Build Coastguard Worker size_t lz_matches_offset = 0;
672*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_UNUSED(literal_context_lut);
673*f4ee7fbaSAndroid Build Coastguard Worker nodes[0].length = 0;
674*f4ee7fbaSAndroid Build Coastguard Worker nodes[0].u.cost = 0;
675*f4ee7fbaSAndroid Build Coastguard Worker InitZopfliCostModel(m, &model, ¶ms->dist, num_bytes);
676*f4ee7fbaSAndroid Build Coastguard Worker if (BROTLI_IS_OOM(m)) return 0;
677*f4ee7fbaSAndroid Build Coastguard Worker ZopfliCostModelSetFromLiteralCosts(
678*f4ee7fbaSAndroid Build Coastguard Worker &model, position, ringbuffer, ringbuffer_mask);
679*f4ee7fbaSAndroid Build Coastguard Worker InitStartPosQueue(&queue);
680*f4ee7fbaSAndroid Build Coastguard Worker for (i = 0; i + HashTypeLengthH10() - 1 < num_bytes; i++) {
681*f4ee7fbaSAndroid Build Coastguard Worker const size_t pos = position + i;
682*f4ee7fbaSAndroid Build Coastguard Worker const size_t max_distance = BROTLI_MIN(size_t, pos, max_backward_limit);
683*f4ee7fbaSAndroid Build Coastguard Worker const size_t dictionary_start = BROTLI_MIN(size_t,
684*f4ee7fbaSAndroid Build Coastguard Worker pos + stream_offset, max_backward_limit);
685*f4ee7fbaSAndroid Build Coastguard Worker size_t skip;
686*f4ee7fbaSAndroid Build Coastguard Worker size_t num_matches;
687*f4ee7fbaSAndroid Build Coastguard Worker num_matches = FindAllMatchesH10(&hasher->privat._H10,
688*f4ee7fbaSAndroid Build Coastguard Worker ¶ms->dictionary,
689*f4ee7fbaSAndroid Build Coastguard Worker ringbuffer, ringbuffer_mask, pos, num_bytes - i, max_distance,
690*f4ee7fbaSAndroid Build Coastguard Worker dictionary_start + gap, params, &matches[lz_matches_offset]);
691*f4ee7fbaSAndroid Build Coastguard Worker if (num_matches > 0 &&
692*f4ee7fbaSAndroid Build Coastguard Worker BackwardMatchLength(&matches[num_matches - 1]) > max_zopfli_len) {
693*f4ee7fbaSAndroid Build Coastguard Worker matches[0] = matches[num_matches - 1];
694*f4ee7fbaSAndroid Build Coastguard Worker num_matches = 1;
695*f4ee7fbaSAndroid Build Coastguard Worker }
696*f4ee7fbaSAndroid Build Coastguard Worker skip = UpdateNodes(num_bytes, position, i, ringbuffer, ringbuffer_mask,
697*f4ee7fbaSAndroid Build Coastguard Worker params, max_backward_limit, dist_cache, num_matches, matches, &model,
698*f4ee7fbaSAndroid Build Coastguard Worker &queue, nodes);
699*f4ee7fbaSAndroid Build Coastguard Worker if (skip < BROTLI_LONG_COPY_QUICK_STEP) skip = 0;
700*f4ee7fbaSAndroid Build Coastguard Worker if (num_matches == 1 && BackwardMatchLength(&matches[0]) > max_zopfli_len) {
701*f4ee7fbaSAndroid Build Coastguard Worker skip = BROTLI_MAX(size_t, BackwardMatchLength(&matches[0]), skip);
702*f4ee7fbaSAndroid Build Coastguard Worker }
703*f4ee7fbaSAndroid Build Coastguard Worker if (skip > 1) {
704*f4ee7fbaSAndroid Build Coastguard Worker /* Add the tail of the copy to the hasher. */
705*f4ee7fbaSAndroid Build Coastguard Worker StoreRangeH10(&hasher->privat._H10,
706*f4ee7fbaSAndroid Build Coastguard Worker ringbuffer, ringbuffer_mask, pos + 1, BROTLI_MIN(
707*f4ee7fbaSAndroid Build Coastguard Worker size_t, pos + skip, store_end));
708*f4ee7fbaSAndroid Build Coastguard Worker skip--;
709*f4ee7fbaSAndroid Build Coastguard Worker while (skip) {
710*f4ee7fbaSAndroid Build Coastguard Worker i++;
711*f4ee7fbaSAndroid Build Coastguard Worker if (i + HashTypeLengthH10() - 1 >= num_bytes) break;
712*f4ee7fbaSAndroid Build Coastguard Worker EvaluateNode(position + stream_offset, i, max_backward_limit, gap,
713*f4ee7fbaSAndroid Build Coastguard Worker dist_cache, &model, &queue, nodes);
714*f4ee7fbaSAndroid Build Coastguard Worker skip--;
715*f4ee7fbaSAndroid Build Coastguard Worker }
716*f4ee7fbaSAndroid Build Coastguard Worker }
717*f4ee7fbaSAndroid Build Coastguard Worker }
718*f4ee7fbaSAndroid Build Coastguard Worker CleanupZopfliCostModel(m, &model);
719*f4ee7fbaSAndroid Build Coastguard Worker return ComputeShortestPathFromNodes(num_bytes, nodes);
720*f4ee7fbaSAndroid Build Coastguard Worker }
721*f4ee7fbaSAndroid Build Coastguard Worker
BrotliCreateZopfliBackwardReferences(MemoryManager * m,size_t num_bytes,size_t position,const uint8_t * ringbuffer,size_t ringbuffer_mask,ContextLut literal_context_lut,const BrotliEncoderParams * params,Hasher * hasher,int * dist_cache,size_t * last_insert_len,Command * commands,size_t * num_commands,size_t * num_literals)722*f4ee7fbaSAndroid Build Coastguard Worker void BrotliCreateZopfliBackwardReferences(MemoryManager* m, size_t num_bytes,
723*f4ee7fbaSAndroid Build Coastguard Worker size_t position, const uint8_t* ringbuffer, size_t ringbuffer_mask,
724*f4ee7fbaSAndroid Build Coastguard Worker ContextLut literal_context_lut, const BrotliEncoderParams* params,
725*f4ee7fbaSAndroid Build Coastguard Worker Hasher* hasher, int* dist_cache, size_t* last_insert_len,
726*f4ee7fbaSAndroid Build Coastguard Worker Command* commands, size_t* num_commands, size_t* num_literals) {
727*f4ee7fbaSAndroid Build Coastguard Worker ZopfliNode* nodes = BROTLI_ALLOC(m, ZopfliNode, num_bytes + 1);
728*f4ee7fbaSAndroid Build Coastguard Worker if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(nodes)) return;
729*f4ee7fbaSAndroid Build Coastguard Worker BrotliInitZopfliNodes(nodes, num_bytes + 1);
730*f4ee7fbaSAndroid Build Coastguard Worker *num_commands += BrotliZopfliComputeShortestPath(m, num_bytes,
731*f4ee7fbaSAndroid Build Coastguard Worker position, ringbuffer, ringbuffer_mask, literal_context_lut, params,
732*f4ee7fbaSAndroid Build Coastguard Worker dist_cache, hasher, nodes);
733*f4ee7fbaSAndroid Build Coastguard Worker if (BROTLI_IS_OOM(m)) return;
734*f4ee7fbaSAndroid Build Coastguard Worker BrotliZopfliCreateCommands(num_bytes, position, nodes, dist_cache,
735*f4ee7fbaSAndroid Build Coastguard Worker last_insert_len, params, commands, num_literals);
736*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_FREE(m, nodes);
737*f4ee7fbaSAndroid Build Coastguard Worker }
738*f4ee7fbaSAndroid Build Coastguard Worker
BrotliCreateHqZopfliBackwardReferences(MemoryManager * m,size_t num_bytes,size_t position,const uint8_t * ringbuffer,size_t ringbuffer_mask,ContextLut literal_context_lut,const BrotliEncoderParams * params,Hasher * hasher,int * dist_cache,size_t * last_insert_len,Command * commands,size_t * num_commands,size_t * num_literals)739*f4ee7fbaSAndroid Build Coastguard Worker void BrotliCreateHqZopfliBackwardReferences(MemoryManager* m, size_t num_bytes,
740*f4ee7fbaSAndroid Build Coastguard Worker size_t position, const uint8_t* ringbuffer, size_t ringbuffer_mask,
741*f4ee7fbaSAndroid Build Coastguard Worker ContextLut literal_context_lut, const BrotliEncoderParams* params,
742*f4ee7fbaSAndroid Build Coastguard Worker Hasher* hasher, int* dist_cache, size_t* last_insert_len,
743*f4ee7fbaSAndroid Build Coastguard Worker Command* commands, size_t* num_commands, size_t* num_literals) {
744*f4ee7fbaSAndroid Build Coastguard Worker const size_t stream_offset = params->stream_offset;
745*f4ee7fbaSAndroid Build Coastguard Worker const size_t max_backward_limit = BROTLI_MAX_BACKWARD_LIMIT(params->lgwin);
746*f4ee7fbaSAndroid Build Coastguard Worker uint32_t* num_matches = BROTLI_ALLOC(m, uint32_t, num_bytes);
747*f4ee7fbaSAndroid Build Coastguard Worker size_t matches_size = 4 * num_bytes;
748*f4ee7fbaSAndroid Build Coastguard Worker const size_t store_end = num_bytes >= StoreLookaheadH10() ?
749*f4ee7fbaSAndroid Build Coastguard Worker position + num_bytes - StoreLookaheadH10() + 1 : position;
750*f4ee7fbaSAndroid Build Coastguard Worker size_t cur_match_pos = 0;
751*f4ee7fbaSAndroid Build Coastguard Worker size_t i;
752*f4ee7fbaSAndroid Build Coastguard Worker size_t orig_num_literals;
753*f4ee7fbaSAndroid Build Coastguard Worker size_t orig_last_insert_len;
754*f4ee7fbaSAndroid Build Coastguard Worker int orig_dist_cache[4];
755*f4ee7fbaSAndroid Build Coastguard Worker size_t orig_num_commands;
756*f4ee7fbaSAndroid Build Coastguard Worker ZopfliCostModel model;
757*f4ee7fbaSAndroid Build Coastguard Worker ZopfliNode* nodes;
758*f4ee7fbaSAndroid Build Coastguard Worker BackwardMatch* matches = BROTLI_ALLOC(m, BackwardMatch, matches_size);
759*f4ee7fbaSAndroid Build Coastguard Worker size_t gap = 0;
760*f4ee7fbaSAndroid Build Coastguard Worker size_t shadow_matches = 0;
761*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_UNUSED(literal_context_lut);
762*f4ee7fbaSAndroid Build Coastguard Worker if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(num_matches) ||
763*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_IS_NULL(matches)) {
764*f4ee7fbaSAndroid Build Coastguard Worker return;
765*f4ee7fbaSAndroid Build Coastguard Worker }
766*f4ee7fbaSAndroid Build Coastguard Worker for (i = 0; i + HashTypeLengthH10() - 1 < num_bytes; ++i) {
767*f4ee7fbaSAndroid Build Coastguard Worker const size_t pos = position + i;
768*f4ee7fbaSAndroid Build Coastguard Worker size_t max_distance = BROTLI_MIN(size_t, pos, max_backward_limit);
769*f4ee7fbaSAndroid Build Coastguard Worker size_t dictionary_start = BROTLI_MIN(size_t,
770*f4ee7fbaSAndroid Build Coastguard Worker pos + stream_offset, max_backward_limit);
771*f4ee7fbaSAndroid Build Coastguard Worker size_t max_length = num_bytes - i;
772*f4ee7fbaSAndroid Build Coastguard Worker size_t num_found_matches;
773*f4ee7fbaSAndroid Build Coastguard Worker size_t cur_match_end;
774*f4ee7fbaSAndroid Build Coastguard Worker size_t j;
775*f4ee7fbaSAndroid Build Coastguard Worker /* Ensure that we have enough free slots. */
776*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_ENSURE_CAPACITY(m, BackwardMatch, matches, matches_size,
777*f4ee7fbaSAndroid Build Coastguard Worker cur_match_pos + MAX_NUM_MATCHES_H10 + shadow_matches);
778*f4ee7fbaSAndroid Build Coastguard Worker if (BROTLI_IS_OOM(m)) return;
779*f4ee7fbaSAndroid Build Coastguard Worker num_found_matches = FindAllMatchesH10(&hasher->privat._H10,
780*f4ee7fbaSAndroid Build Coastguard Worker ¶ms->dictionary,
781*f4ee7fbaSAndroid Build Coastguard Worker ringbuffer, ringbuffer_mask, pos, max_length,
782*f4ee7fbaSAndroid Build Coastguard Worker max_distance, dictionary_start + gap, params,
783*f4ee7fbaSAndroid Build Coastguard Worker &matches[cur_match_pos + shadow_matches]);
784*f4ee7fbaSAndroid Build Coastguard Worker cur_match_end = cur_match_pos + num_found_matches;
785*f4ee7fbaSAndroid Build Coastguard Worker for (j = cur_match_pos; j + 1 < cur_match_end; ++j) {
786*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_DCHECK(BackwardMatchLength(&matches[j]) <=
787*f4ee7fbaSAndroid Build Coastguard Worker BackwardMatchLength(&matches[j + 1]));
788*f4ee7fbaSAndroid Build Coastguard Worker }
789*f4ee7fbaSAndroid Build Coastguard Worker num_matches[i] = (uint32_t)num_found_matches;
790*f4ee7fbaSAndroid Build Coastguard Worker if (num_found_matches > 0) {
791*f4ee7fbaSAndroid Build Coastguard Worker const size_t match_len = BackwardMatchLength(&matches[cur_match_end - 1]);
792*f4ee7fbaSAndroid Build Coastguard Worker if (match_len > MAX_ZOPFLI_LEN_QUALITY_11) {
793*f4ee7fbaSAndroid Build Coastguard Worker const size_t skip = match_len - 1;
794*f4ee7fbaSAndroid Build Coastguard Worker matches[cur_match_pos++] = matches[cur_match_end - 1];
795*f4ee7fbaSAndroid Build Coastguard Worker num_matches[i] = 1;
796*f4ee7fbaSAndroid Build Coastguard Worker /* Add the tail of the copy to the hasher. */
797*f4ee7fbaSAndroid Build Coastguard Worker StoreRangeH10(&hasher->privat._H10,
798*f4ee7fbaSAndroid Build Coastguard Worker ringbuffer, ringbuffer_mask, pos + 1,
799*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_MIN(size_t, pos + match_len, store_end));
800*f4ee7fbaSAndroid Build Coastguard Worker memset(&num_matches[i + 1], 0, skip * sizeof(num_matches[0]));
801*f4ee7fbaSAndroid Build Coastguard Worker i += skip;
802*f4ee7fbaSAndroid Build Coastguard Worker } else {
803*f4ee7fbaSAndroid Build Coastguard Worker cur_match_pos = cur_match_end;
804*f4ee7fbaSAndroid Build Coastguard Worker }
805*f4ee7fbaSAndroid Build Coastguard Worker }
806*f4ee7fbaSAndroid Build Coastguard Worker }
807*f4ee7fbaSAndroid Build Coastguard Worker orig_num_literals = *num_literals;
808*f4ee7fbaSAndroid Build Coastguard Worker orig_last_insert_len = *last_insert_len;
809*f4ee7fbaSAndroid Build Coastguard Worker memcpy(orig_dist_cache, dist_cache, 4 * sizeof(dist_cache[0]));
810*f4ee7fbaSAndroid Build Coastguard Worker orig_num_commands = *num_commands;
811*f4ee7fbaSAndroid Build Coastguard Worker nodes = BROTLI_ALLOC(m, ZopfliNode, num_bytes + 1);
812*f4ee7fbaSAndroid Build Coastguard Worker if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(nodes)) return;
813*f4ee7fbaSAndroid Build Coastguard Worker InitZopfliCostModel(m, &model, ¶ms->dist, num_bytes);
814*f4ee7fbaSAndroid Build Coastguard Worker if (BROTLI_IS_OOM(m)) return;
815*f4ee7fbaSAndroid Build Coastguard Worker for (i = 0; i < 2; i++) {
816*f4ee7fbaSAndroid Build Coastguard Worker BrotliInitZopfliNodes(nodes, num_bytes + 1);
817*f4ee7fbaSAndroid Build Coastguard Worker if (i == 0) {
818*f4ee7fbaSAndroid Build Coastguard Worker ZopfliCostModelSetFromLiteralCosts(
819*f4ee7fbaSAndroid Build Coastguard Worker &model, position, ringbuffer, ringbuffer_mask);
820*f4ee7fbaSAndroid Build Coastguard Worker } else {
821*f4ee7fbaSAndroid Build Coastguard Worker ZopfliCostModelSetFromCommands(&model, position, ringbuffer,
822*f4ee7fbaSAndroid Build Coastguard Worker ringbuffer_mask, commands, *num_commands - orig_num_commands,
823*f4ee7fbaSAndroid Build Coastguard Worker orig_last_insert_len);
824*f4ee7fbaSAndroid Build Coastguard Worker }
825*f4ee7fbaSAndroid Build Coastguard Worker *num_commands = orig_num_commands;
826*f4ee7fbaSAndroid Build Coastguard Worker *num_literals = orig_num_literals;
827*f4ee7fbaSAndroid Build Coastguard Worker *last_insert_len = orig_last_insert_len;
828*f4ee7fbaSAndroid Build Coastguard Worker memcpy(dist_cache, orig_dist_cache, 4 * sizeof(dist_cache[0]));
829*f4ee7fbaSAndroid Build Coastguard Worker *num_commands += ZopfliIterate(num_bytes, position, ringbuffer,
830*f4ee7fbaSAndroid Build Coastguard Worker ringbuffer_mask, params, gap, dist_cache, &model, num_matches, matches,
831*f4ee7fbaSAndroid Build Coastguard Worker nodes);
832*f4ee7fbaSAndroid Build Coastguard Worker BrotliZopfliCreateCommands(num_bytes, position, nodes, dist_cache,
833*f4ee7fbaSAndroid Build Coastguard Worker last_insert_len, params, commands, num_literals);
834*f4ee7fbaSAndroid Build Coastguard Worker }
835*f4ee7fbaSAndroid Build Coastguard Worker CleanupZopfliCostModel(m, &model);
836*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_FREE(m, nodes);
837*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_FREE(m, matches);
838*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_FREE(m, num_matches);
839*f4ee7fbaSAndroid Build Coastguard Worker }
840*f4ee7fbaSAndroid Build Coastguard Worker
841*f4ee7fbaSAndroid Build Coastguard Worker #if defined(__cplusplus) || defined(c_plusplus)
842*f4ee7fbaSAndroid Build Coastguard Worker } /* extern "C" */
843*f4ee7fbaSAndroid Build Coastguard Worker #endif
844