| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 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 |
|
| 26 |
|
| 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 |
|
| 62 |
|
| 63 |
|
| 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 |
|
| 100 |
|
| 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 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
int avail = Math.min(lz.getAvail(), MATCH_LEN_MAX); |
| 121 |
if (avail < MATCH_LEN_MIN) |
| 122 |
return 1; |
| 123 |
|
| 124 |
|
| 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 |
|
| 139 |
if (repLens[repBest] >= niceLen) { |
| 140 |
back = repBest; |
| 141 |
skip(repLens[repBest] - 1); |
| 142 |
return repLens[repBest]; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 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 |
|
| 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 |
|
| 165 |
|
| 166 |
|
| 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 |
|
| 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 |
|
| 187 |
|
| 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 |
|
| 196 |
|
| 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 |
|
| 206 |
|
| 207 |
|
| 208 |
updatePrices(); |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
opts[0].state.set(state); |
| 214 |
System.arraycopy(reps, 0, opts[0].reps, 0, REPS); |
| 215 |
|
| 216 |
|
| 217 |
for (int i = optEnd; i >= MATCH_LEN_MIN; --i) |
| 218 |
opts[i].reset(); |
| 219 |
|
| 220 |
|
| 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 |
|
| 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 |
|
| 244 |
|
| 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 |
|
| 269 |
|
| 270 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 369 |
|
| 370 |
private void calc1BytePrices(int pos, int posState, |
| 371 |
int avail, int anyRepPrice) { |
| 372 |
|
| 373 |
boolean nextIsByte = false; |
| 374 |
|
| 375 |
int curByte = lz.getByte(0); |
| 376 |
int matchByte = lz.getByte(opts[optCur].reps[0] + 1); |
| 377 |
|
| 378 |
|
| 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 |
|
| 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 |
|
| 400 |
|
| 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 |
|
| 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 |
|
| 464 |
int price = longRepPrice |
| 465 |
+ repLenEncoder.getPrice(len, posState); |
| 466 |
nextState.set(opts[optCur].state); |
| 467 |
nextState.updateLongRep(); |
| 468 |
|
| 469 |
|
| 470 |
int curByte = lz.getByte(len, 0); |
| 471 |
int matchByte = lz.getByte(0); |
| 472 |
int prevByte = lz.getByte(len, 1); |
| 473 |
price += literalEncoder.getPrice(curByte, matchByte, prevByte, |
| 474 |
pos + len, nextState); |
| 475 |
nextState.updateLiteral(); |
| 476 |
|
| 477 |
|
| 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 |
|
| 496 |
|
| 497 |
private void calcNormalMatchPrices(int pos, int posState, int avail, |
| 498 |
int anyMatchPrice, int startLen) { |
| 499 |
|
| 500 |
|
| 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 |
|
| 526 |
|
| 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 |
|
| 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 |
|
| 548 |
int curByte = lz.getByte(len, 0); |
| 549 |
int matchByte = lz.getByte(0); |
| 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 |
|
| 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 |
} |