Debian Codegraph @

src/org/tukaani/xz/lzma/LZMAEncoderNormal.java

raw symbols SCIP
1 // SPDX-License-Identifier: 0BSD
2 // SPDX-FileCopyrightText: The XZ for Java authors and contributors
3 // SPDX-FileContributor: Lasse Collin <[email protected]>
4 // SPDX-FileContributor: Igor Pavlov <https://7-zip.org/>
5
6 package org.tukaani.xz.lzma;
7
8 import org.tukaani.xz.ArrayCache;
9 import org.tukaani.xz.lz.LZEncoder;
10 import org.tukaani.xz.lz.Matches;
11 import org.tukaani.xz.rangecoder.RangeEncoder;
12
13 final class LZMAEncoderNormal extends LZMAEncoder {
14 private static final int OPTS = 4096;
15
16 private static final int EXTRA_SIZE_BEFORE = OPTS;
17 private static final int EXTRA_SIZE_AFTER = OPTS;
18
19 private final Optimum[] opts = new Optimum[OPTS];
20 private int optCur = 0;
21 private int optEnd = 0;
22
23 private Matches matches;
24
25 // These are fields solely to avoid allocating the objects again and
26 // again on each function call.
27 private final int[] repLens = new int[REPS];
28 private final State nextState = new State();
29
30 static int getMemoryUsage(int dictSize, int extraSizeBefore, int mf) {
31 return LZEncoder.getMemoryUsage(dictSize,
32 Math.max(extraSizeBefore, EXTRA_SIZE_BEFORE),
33 EXTRA_SIZE_AFTER, MATCH_LEN_MAX, mf)
34 + OPTS * 64 / 1024;
35 }
36
37 LZMAEncoderNormal(RangeEncoder rc, int lc, int lp, int pb,
38 int dictSize, int extraSizeBefore,
39 int niceLen, int mf, int depthLimit,
40 ArrayCache arrayCache) {
41 super(rc, LZEncoder.getInstance(dictSize,
42 Math.max(extraSizeBefore,
43 EXTRA_SIZE_BEFORE),
44 EXTRA_SIZE_AFTER,
45 niceLen, MATCH_LEN_MAX,
46 mf, depthLimit, arrayCache),
47 lc, lp, pb, dictSize, niceLen);
48
49 for (int i = 0; i < OPTS; ++i)
50 opts[i] = new Optimum();
51 }
52
53 @Override
54 public void reset() {
55 optCur = 0;
56 optEnd = 0;
57 super.reset();
58 }
59
60 /**
61 * Converts the opts array from backward indexes to forward indexes.
62 * Then it will be simple to get the next symbol from the array
63 * in later calls to {@code getNextSymbol()}.
64 */
65 private int convertOpts() {
66 optEnd = optCur;
67
68 int optPrev = opts[optCur].optPrev;
69
70 do {
71 Optimum opt = opts[optCur];
72
73 if (opt.prev1IsLiteral) {
74 opts[optPrev].optPrev = optCur;
75 opts[optPrev].backPrev = -1;
76 optCur = optPrev--;
77
78 if (opt.hasPrev2) {
79 opts[optPrev].optPrev = optPrev + 1;
80 opts[optPrev].backPrev = opt.backPrev2;
81 optCur = optPrev;
82 optPrev = opt.optPrev2;
83 }
84 }
85
86 int temp = opts[optPrev].optPrev;
87 opts[optPrev].optPrev = optCur;
88 optCur = optPrev;
89 optPrev = temp;
90 } while (optCur > 0);
91
92 optCur = opts[0].optPrev;
93 back = opts[optCur].backPrev;
94 return optCur;
95 }
96
97 @Override
98 int getNextSymbol() {
99 // If there are pending symbols from an earlier call to this
100 // function, return those symbols first.
101 if (optCur < optEnd) {
102 int len = opts[optCur].optPrev - optCur;
103 optCur = opts[optCur].optPrev;
104 back = opts[optCur].backPrev;
105 return len;
106 }
107
108 assert optCur == optEnd;
109 optCur = 0;
110 optEnd = 0;
111 back = -1;
112
113 if (readAhead == -1)
114 matches = getMatches();
115
116 // Get the number of bytes available in the dictionary, but
117 // not more than the maximum match length. If there aren't
118 // enough bytes remaining to encode a match at all, return
119 // immediately to encode this byte as a literal.
120 int avail = Math.min(lz.getAvail(), MATCH_LEN_MAX);
121 if (avail < MATCH_LEN_MIN)
122 return 1;
123
124 // Get the lengths of repeated matches.
125 int repBest = 0;
126 for (int rep = 0; rep < REPS; ++rep) {
127 repLens[rep] = lz.getMatchLen(reps[rep], avail);
128
129 if (repLens[rep] < MATCH_LEN_MIN) {
130 repLens[rep] = 0;
131 continue;
132 }
133
134 if (repLens[rep] > repLens[repBest])
135 repBest = rep;
136 }
137
138 // Return if the best repeated match is at least niceLen bytes long.
139 if (repLens[repBest] >= niceLen) {
140 back = repBest;
141 skip(repLens[repBest] - 1);
142 return repLens[repBest];
143 }
144
145 // Initialize mainLen and mainDist to the longest match found
146 // by the match finder.
147 int mainLen = 0;
148 int mainDist = 0;
149 if (matches.count > 0) {
150 mainLen = matches.len[matches.count - 1];
151 mainDist = matches.dist[matches.count - 1];
152
153 // Return if it is at least niceLen bytes long.
154 if (mainLen >= niceLen) {
155 back = mainDist + REPS;
156 skip(mainLen - 1);
157 return mainLen;
158 }
159 }
160
161 int curByte = lz.getByte(0);
162 int matchByte = lz.getByte(reps[0] + 1);
163
164 // If the match finder found no matches and this byte cannot be
165 // encoded as a repeated match (short or long), we must be return
166 // to have the byte encoded as a literal.
167 if (mainLen < MATCH_LEN_MIN && curByte != matchByte
168 && repLens[repBest] < MATCH_LEN_MIN)
169 return 1;
170
171
172 int pos = lz.getPos();
173 int posState = pos & posMask;
174
175 // Calculate the price of encoding the current byte as a literal.
176 {
177 int prevByte = lz.getByte(1);
178 int literalPrice = literalEncoder.getPrice(curByte, matchByte,
179 prevByte, pos, state);
180 opts[1].set1(literalPrice, 0, -1);
181 }
182
183 int anyMatchPrice = getAnyMatchPrice(state, posState);
184 int anyRepPrice = getAnyRepPrice(anyMatchPrice, state);
185
186 // If it is possible to encode this byte as a short rep, see if
187 // it is cheaper than encoding it as a literal.
188 if (matchByte == curByte) {
189 int shortRepPrice = getShortRepPrice(anyRepPrice,
190 state, posState);
191 if (shortRepPrice < opts[1].price)
192 opts[1].set1(shortRepPrice, 0, 0);
193 }
194
195 // Return if there is neither normal nor long repeated match. Use
196 // a short match instead of a literal if is is possible and cheaper.
197 optEnd = Math.max(mainLen, repLens[repBest]);
198 if (optEnd < MATCH_LEN_MIN) {
199 assert optEnd == 0 : optEnd;
200 back = opts[1].backPrev;
201 return 1;
202 }
203
204
205 // Update the lookup tables for distances and lengths before using
206 // those price calculation functions. (The price function above
207 // don't need these tables.)
208 updatePrices();
209
210 // Initialize the state and reps of this position in opts[].
211 // updateOptStateAndReps() will need these to get the new
212 // state and reps for the next byte.
213 opts[0].state.set(state);
214 System.arraycopy(reps, 0, opts[0].reps, 0, REPS);
215
216 // Initialize the prices for latter opts that will be used below.
217 for (int i = optEnd; i >= MATCH_LEN_MIN; --i)
218 opts[i].reset();
219
220 // Calculate the prices of repeated matches of all lengths.
221 for (int rep = 0; rep < REPS; ++rep) {
222 int repLen = repLens[rep];
223 if (repLen < MATCH_LEN_MIN)
224 continue;
225
226 int longRepPrice = getLongRepPrice(anyRepPrice, rep,
227 state, posState);
228 do {
229 int price = longRepPrice + repLenEncoder.getPrice(repLen,
230 posState);
231 if (price < opts[repLen].price)
232 opts[repLen].set1(price, 0, rep);
233 } while (--repLen >= MATCH_LEN_MIN);
234 }
235
236 // Calculate the prices of normal matches that are longer than rep0.
237 {
238 int len = Math.max(repLens[0] + 1, MATCH_LEN_MIN);
239 if (len <= mainLen) {
240 int normalMatchPrice = getNormalMatchPrice(anyMatchPrice,
241 state);
242
243 // Set i to the index of the shortest match that is
244 // at least len bytes long.
245 int i = 0;
246 while (len > matches.len[i])
247 ++i;
248
249 while (true) {
250 int dist = matches.dist[i];
251 int price = getMatchAndLenPrice(normalMatchPrice,
252 dist, len, posState);
253 if (price < opts[len].price)
254 opts[len].set1(price, 0, dist + REPS);
255
256 if (len == matches.len[i])
257 if (++i == matches.count)
258 break;
259
260 ++len;
261 }
262 }
263 }
264
265
266 avail = Math.min(lz.getAvail(), OPTS - 1);
267
268 // Get matches for later bytes and optimize the use of LZMA symbols
269 // by calculating the prices and picking the cheapest symbol
270 // combinations.
271 while (++optCur < optEnd) {
272 matches = getMatches();
273 if (matches.count > 0
274 && matches.len[matches.count - 1] >= niceLen)
275 break;
276
277 --avail;
278 ++pos;
279 posState = pos & posMask;
280
281 updateOptStateAndReps();
282 anyMatchPrice = opts[optCur].price
283 + getAnyMatchPrice(opts[optCur].state, posState);
284 anyRepPrice = getAnyRepPrice(anyMatchPrice, opts[optCur].state);
285
286 calc1BytePrices(pos, posState, avail, anyRepPrice);
287
288 if (avail >= MATCH_LEN_MIN) {
289 int startLen = calcLongRepPrices(pos, posState,
290 avail, anyRepPrice);
291 if (matches.count > 0)
292 calcNormalMatchPrices(pos, posState, avail,
293 anyMatchPrice, startLen);
294 }
295 }
296
297 return convertOpts();
298 }
299
300 /**
301 * Updates the state and reps for the current byte in the opts array.
302 */
303 private void updateOptStateAndReps() {
304 int optPrev = opts[optCur].optPrev;
305 assert optPrev < optCur;
306
307 if (opts[optCur].prev1IsLiteral) {
308 --optPrev;
309
310 if (opts[optCur].hasPrev2) {
311 opts[optCur].state.set(opts[opts[optCur].optPrev2].state);
312 if (opts[optCur].backPrev2 < REPS)
313 opts[optCur].state.updateLongRep();
314 else
315 opts[optCur].state.updateMatch();
316 } else {
317 opts[optCur].state.set(opts[optPrev].state);
318 }
319
320 opts[optCur].state.updateLiteral();
321 } else {
322 opts[optCur].state.set(opts[optPrev].state);
323 }
324
325 if (optPrev == optCur - 1) {
326 // Must be either a short rep or a literal.
327 assert opts[optCur].backPrev == 0 || opts[optCur].backPrev == -1;
328
329 if (opts[optCur].backPrev == 0)
330 opts[optCur].state.updateShortRep();
331 else
332 opts[optCur].state.updateLiteral();
333
334 System.arraycopy(opts[optPrev].reps, 0,
335 opts[optCur].reps, 0, REPS);
336 } else {
337 int back;
338 if (opts[optCur].prev1IsLiteral && opts[optCur].hasPrev2) {
339 optPrev = opts[optCur].optPrev2;
340 back = opts[optCur].backPrev2;
341 opts[optCur].state.updateLongRep();
342 } else {
343 back = opts[optCur].backPrev;
344 if (back < REPS)
345 opts[optCur].state.updateLongRep();
346 else
347 opts[optCur].state.updateMatch();
348 }
349
350 if (back < REPS) {
351 opts[optCur].reps[0] = opts[optPrev].reps[back];
352
353 int rep;
354 for (rep = 1; rep <= back; ++rep)
355 opts[optCur].reps[rep] = opts[optPrev].reps[rep - 1];
356
357 for (; rep < REPS; ++rep)
358 opts[optCur].reps[rep] = opts[optPrev].reps[rep];
359 } else {
360 opts[optCur].reps[0] = back - REPS;
361 System.arraycopy(opts[optPrev].reps, 0,
362 opts[optCur].reps, 1, REPS - 1);
363 }
364 }
365 }
366
367 /**
368 * Calculates prices of a literal, a short rep, and literal + rep0.
369 */
370 private void calc1BytePrices(int pos, int posState,
371 int avail, int anyRepPrice) {
372 // This will be set to true if using a literal or a short rep.
373 boolean nextIsByte = false;
374
375 int curByte = lz.getByte(0);
376 int matchByte = lz.getByte(opts[optCur].reps[0] + 1);
377
378 // Try a literal.
379 int literalPrice = opts[optCur].price
380 + literalEncoder.getPrice(curByte, matchByte, lz.getByte(1),
381 pos, opts[optCur].state);
382 if (literalPrice < opts[optCur + 1].price) {
383 opts[optCur + 1].set1(literalPrice, optCur, -1);
384 nextIsByte = true;
385 }
386
387 // Try a short rep.
388 if (matchByte == curByte && (opts[optCur + 1].optPrev == optCur
389 || opts[optCur + 1].backPrev != 0)) {
390 int shortRepPrice = getShortRepPrice(anyRepPrice,
391 opts[optCur].state,
392 posState);
393 if (shortRepPrice <= opts[optCur + 1].price) {
394 opts[optCur + 1].set1(shortRepPrice, optCur, 0);
395 nextIsByte = true;
396 }
397 }
398
399 // If neither a literal nor a short rep was the cheapest choice,
400 // try literal + long rep0.
401 if (!nextIsByte && matchByte != curByte && avail > MATCH_LEN_MIN) {
402 int lenLimit = Math.min(niceLen, avail - 1);
403 int len = lz.getMatchLen(1, opts[optCur].reps[0], lenLimit);
404
405 if (len >= MATCH_LEN_MIN) {
406 nextState.set(opts[optCur].state);
407 nextState.updateLiteral();
408 int nextPosState = (pos + 1) & posMask;
409 int price = literalPrice
410 + getLongRepAndLenPrice(0, len,
411 nextState, nextPosState);
412
413 int i = optCur + 1 + len;
414 while (optEnd < i)
415 opts[++optEnd].reset();
416
417 if (price < opts[i].price)
418 opts[i].set2(price, optCur, 0);
419 }
420 }
421 }
422
423 /**
424 * Calculates prices of long rep and long rep + literal + rep0.
425 */
426 private int calcLongRepPrices(int pos, int posState,
427 int avail, int anyRepPrice) {
428 int startLen = MATCH_LEN_MIN;
429 int lenLimit = Math.min(avail, niceLen);
430
431 for (int rep = 0; rep < REPS; ++rep) {
432 int len = lz.getMatchLen(opts[optCur].reps[rep], lenLimit);
433 if (len < MATCH_LEN_MIN)
434 continue;
435
436 while (optEnd < optCur + len)
437 opts[++optEnd].reset();
438
439 int longRepPrice = getLongRepPrice(anyRepPrice, rep,
440 opts[optCur].state, posState);
441
442 for (int i = len; i >= MATCH_LEN_MIN; --i) {
443 int price = longRepPrice
444 + repLenEncoder.getPrice(i, posState);
445 if (price < opts[optCur + i].price)
446 opts[optCur + i].set1(price, optCur, rep);
447 }
448
449 if (rep == 0)
450 startLen = len + 1;
451
452 int len2Limit = avail - len - 1;
453 if (len2Limit < MATCH_LEN_MIN)
454 continue;
455
456 if (len2Limit > niceLen)
457 len2Limit = niceLen;
458
459 int len2 = lz.getMatchLen(len + 1, opts[optCur].reps[rep],
460 len2Limit);
461
462 if (len2 >= MATCH_LEN_MIN) {
463 // Rep
464 int price = longRepPrice
465 + repLenEncoder.getPrice(len, posState);
466 nextState.set(opts[optCur].state);
467 nextState.updateLongRep();
468
469 // Literal
470 int curByte = lz.getByte(len, 0);
471 int matchByte = lz.getByte(0); // lz.getByte(len, len)
472 int prevByte = lz.getByte(len, 1);
473 price += literalEncoder.getPrice(curByte, matchByte, prevByte,
474 pos + len, nextState);
475 nextState.updateLiteral();
476
477 // Rep0
478 int nextPosState = (pos + len + 1) & posMask;
479 price += getLongRepAndLenPrice(0, len2,
480 nextState, nextPosState);
481
482 int i = optCur + len + 1 + len2;
483 while (optEnd < i)
484 opts[++optEnd].reset();
485
486 if (price < opts[i].price)
487 opts[i].set3(price, optCur, rep, len, 0);
488 }
489 }
490
491 return startLen;
492 }
493
494 /**
495 * Calculates prices of a normal match and normal match + literal + rep0.
496 */
497 private void calcNormalMatchPrices(int pos, int posState, int avail,
498 int anyMatchPrice, int startLen) {
499 // If the longest match is so long that it would not fit into
500 // the opts array, shorten the matches.
501 if (matches.len[matches.count - 1] > avail) {
502 matches.count = 0;
503 while (matches.len[matches.count] < avail)
504 ++matches.count;
505
506 matches.len[matches.count++] = avail;
507 }
508
509 if (matches.len[matches.count - 1] < startLen)
510 return;
511
512 while (optEnd < optCur + matches.len[matches.count - 1])
513 opts[++optEnd].reset();
514
515 int normalMatchPrice = getNormalMatchPrice(anyMatchPrice,
516 opts[optCur].state);
517
518 int match = 0;
519 while (startLen > matches.len[match])
520 ++match;
521
522 for (int len = startLen; ; ++len) {
523 int dist = matches.dist[match];
524
525 // Calculate the price of a match of len bytes from the nearest
526 // possible distance.
527 int matchAndLenPrice = getMatchAndLenPrice(normalMatchPrice,
528 dist, len, posState);
529 if (matchAndLenPrice < opts[optCur + len].price)
530 opts[optCur + len].set1(matchAndLenPrice,
531 optCur, dist + REPS);
532
533 if (len != matches.len[match])
534 continue;
535
536 // Try match + literal + rep0. First get the length of the rep0.
537 int len2Limit = avail - len - 1;
538 if (len2Limit >= MATCH_LEN_MIN) {
539 if (len2Limit > niceLen)
540 len2Limit = niceLen;
541
542 int len2 = lz.getMatchLen(len + 1, dist, len2Limit);
543 if (len2 >= MATCH_LEN_MIN) {
544 nextState.set(opts[optCur].state);
545 nextState.updateMatch();
546
547 // Literal
548 int curByte = lz.getByte(len, 0);
549 int matchByte = lz.getByte(0); // lz.getByte(len, len)
550 int prevByte = lz.getByte(len, 1);
551 int price = matchAndLenPrice
552 + literalEncoder.getPrice(curByte, matchByte,
553 prevByte, pos + len,
554 nextState);
555 nextState.updateLiteral();
556
557 // Rep0
558 int nextPosState = (pos + len + 1) & posMask;
559 price += getLongRepAndLenPrice(0, len2,
560 nextState, nextPosState);
561
562 int i = optCur + len + 1 + len2;
563 while (optEnd < i)
564 opts[++optEnd].reset();
565
566 if (price < opts[i].price)
567 opts[i].set3(price, optCur, dist + REPS, len, 0);
568 }
569 }
570
571 if (++match == matches.count)
572 break;
573 }
574 }
575 }