1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707
|
/* LzmaEnc.c -- LZMA Encoder
2009-11-24 : Igor Pavlov : Public domain */
#define _FILE_OFFSET_BITS 64
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* #define SHOW_STAT */
/* #define SHOW_STAT2 */
#include "lzip.h"
#include "LzmaEnc.h"
#include "LzFind.h"
#ifdef SHOW_STAT
static int ttt = 0;
#endif
#define kNumTopBits 24
#define kTopValue ((uint32_t)1 << kNumTopBits)
#define kNumBitModelTotalBits 11
#define kBitModelTotal (1 << kNumBitModelTotalBits)
#define kNumMoveBits 5
#define kProbInitValue (kBitModelTotal >> 1)
#define kNumMoveReducingBits 4
#define kNumBitPriceShiftBits 4
#define kNumLogBits (9 + (int)sizeof(uint32_t) / 2)
#define kDicLogSizeMaxCompress ((kNumLogBits - 1) * 2 + 7)
static void LzmaEnc_FastPosInit(uint8_t *g_FastPos)
{
int c = 2, slotFast;
g_FastPos[0] = 0;
g_FastPos[1] = 1;
for (slotFast = 2; slotFast < kNumLogBits * 2; slotFast++)
{
uint32_t k = (1 << ((slotFast >> 1) - 1));
uint32_t j;
for (j = 0; j < k; j++, c++)
g_FastPos[c] = (uint8_t)slotFast;
}
}
#define BSR2_RET(pos, res) { uint32_t i = 6 + ((kNumLogBits - 1) & \
(0 - (((((uint32_t)1 << (kNumLogBits + 6)) - 1) - pos) >> 31))); \
res = p->g_FastPos[pos >> i] + (i * 2); }
/*
#define BSR2_RET(pos, res) { res = (pos < (1 << (kNumLogBits + 6))) ? \
p->g_FastPos[pos >> 6] + 12 : \
p->g_FastPos[pos >> (6 + kNumLogBits - 1)] + (6 + (kNumLogBits - 1)) * 2; }
*/
#define GetPosSlot1(pos) p->g_FastPos[pos]
#define GetPosSlot2(pos, res) { BSR2_RET(pos, res); }
#define GetPosSlot(pos, res) { if (pos < kNumFullDistances) res = p->g_FastPos[pos]; else BSR2_RET(pos, res); }
#define LZMA_NUM_REPS 4
typedef struct
{
uint32_t price;
State state;
uint32_t posPrev2;
uint32_t backPrev2;
uint32_t posPrev;
uint32_t backPrev;
uint32_t backs[LZMA_NUM_REPS];
bool prev1IsChar;
bool prev2;
} COptimal;
#define kNumOpts (1 << 12)
#define kNumLenToPosStates 4
#define kNumPosSlotBits 6
#define kDicLogSizeMin 0
#define kDicLogSizeMax 32
#define kDistTableSizeMax (kDicLogSizeMax * 2)
#define kNumAlignBits 4
#define kAlignTableSize (1 << kNumAlignBits)
#define kAlignMask (kAlignTableSize - 1)
#define kStartPosModelIndex 4
#define kEndPosModelIndex 14
#define kNumPosModels (kEndPosModelIndex - kStartPosModelIndex)
#define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
#define LZMA_LC_MAX 8
#define LZMA_LP_MAX 4
#define LZMA_PB_MAX 4
#define LZMA_NUM_PB_STATES_MAX (1 << LZMA_PB_MAX)
#define kLenNumLowBits 3
#define kLenNumLowSymbols (1 << kLenNumLowBits)
#define kLenNumMidBits 3
#define kLenNumMidSymbols (1 << kLenNumMidBits)
#define kLenNumHighBits 8
#define kLenNumHighSymbols (1 << kLenNumHighBits)
#define kLenNumSymbolsTotal (kLenNumLowSymbols + kLenNumMidSymbols + kLenNumHighSymbols)
#define LZMA_MATCH_LEN_MIN 2
#define LZMA_MATCH_LEN_MAX (LZMA_MATCH_LEN_MIN + kLenNumSymbolsTotal - 1)
#define kNumStates 12
typedef struct
{
int choice;
int choice2;
int low[LZMA_NUM_PB_STATES_MAX << kLenNumLowBits];
int mid[LZMA_NUM_PB_STATES_MAX << kLenNumMidBits];
int high[kLenNumHighSymbols];
} CLenEnc;
typedef struct
{
CLenEnc p;
uint32_t prices[LZMA_NUM_PB_STATES_MAX][kLenNumSymbolsTotal];
uint32_t tableSize;
uint32_t counters[LZMA_NUM_PB_STATES_MAX];
} CLenPriceEnc;
typedef struct
{
uint64_t low;
uint64_t processed;
uint8_t *bufBase;
uint8_t *buf;
uint8_t *bufLim;
uint32_t range;
uint32_t cacheSize;
int outfd;
int res;
uint8_t cache;
} CRangeEnc;
typedef struct
{
uint64_t nowPos64;
int *litProbs;
IMatchFinder matchFinder;
CMatchFinder matchFinderBase;
uint32_t optimumEndIndex;
uint32_t optimumCurrentIndex;
uint32_t longestMatchLength;
uint32_t numPairs;
uint32_t numAvail;
COptimal opt[kNumOpts];
uint8_t g_FastPos[1 << kNumLogBits];
uint32_t ProbPrices[kBitModelTotal >> kNumMoveReducingBits];
uint32_t matches[LZMA_MATCH_LEN_MAX * 2 + 2 + 1];
uint32_t numFastBytes;
uint32_t additionalOffset;
uint32_t reps[LZMA_NUM_REPS];
State state;
uint32_t posSlotPrices[kNumLenToPosStates][kDistTableSizeMax];
uint32_t distancesPrices[kNumLenToPosStates][kNumFullDistances];
uint32_t alignPrices[kAlignTableSize];
uint32_t alignPriceCount;
uint32_t distTableSize;
unsigned lc, lp, pb;
unsigned lpMask, pbMask;
int isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX];
int isRep[kNumStates];
int isRepG0[kNumStates];
int isRepG1[kNumStates];
int isRepG2[kNumStates];
int isRep0Long[kNumStates][LZMA_NUM_PB_STATES_MAX];
int posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits];
int posEncoders[kNumFullDistances - kEndPosModelIndex];
int posAlignEncoder[1 << kNumAlignBits];
CLenPriceEnc lenEnc;
CLenPriceEnc repLenEnc;
CRangeEnc rc;
uint32_t matchPriceCount;
int result;
uint32_t dictSize;
bool fastMode;
bool finished;
} CLzmaEnc;
static const int kLiteralNextStates[kNumStates] = {0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5};
static const int kMatchNextStates[kNumStates] = {7, 7, 7, 7, 7, 7, 7, 10, 10, 10, 10, 10};
static const int kRepNextStates[kNumStates] = {8, 8, 8, 8, 8, 8, 8, 11, 11, 11, 11, 11};
static const int kShortRepNextStates[kNumStates]= {9, 9, 9, 9, 9, 9, 9, 11, 11, 11, 11, 11};
#define IsCharState(s) ((s) < 7)
#define GetLenToPosState(len) (((len) < kNumLenToPosStates + 1) ? (len) - 2 : kNumLenToPosStates - 1)
#define kInfinityPrice (1 << 30)
#define RC_BUF_SIZE (1 << 16)
static int RangeEnc_Init( CRangeEnc *p, const int outfd )
{
p->low = 0;
p->processed = 0;
p->range = 0xFFFFFFFF;
p->cacheSize = 1;
p->outfd = outfd;
p->res = SZ_OK;
p->cache = 0;
p->buf = p->bufBase = (uint8_t *)malloc( RC_BUF_SIZE );
if( !p->bufBase ) return 0;
p->bufLim = p->bufBase + RC_BUF_SIZE;
return 1;
}
static void RangeEnc_Free(CRangeEnc *p)
{
free(p->bufBase);
p->bufBase = 0;
}
static void RangeEnc_FlushStream(CRangeEnc *p)
{
int num;
if (p->res != SZ_OK)
return;
num = p->buf - p->bufBase;
if (num != writeblock(p->outfd, p->bufBase, num))
p->res = SZ_ERROR_WRITE;
p->processed += num;
p->buf = p->bufBase;
}
static void RangeEnc_ShiftLow(CRangeEnc *p)
{
if ((uint32_t)p->low < (uint32_t)0xFF000000 || (int)(p->low >> 32) != 0)
{
uint8_t temp = p->cache;
do
{
uint8_t *buf = p->buf;
*buf++ = (uint8_t)(temp + (uint8_t)(p->low >> 32));
p->buf = buf;
if (buf == p->bufLim)
RangeEnc_FlushStream(p);
temp = 0xFF;
}
while (--p->cacheSize != 0);
p->cache = (uint8_t)((uint32_t)p->low >> 24);
}
p->cacheSize++;
p->low = (uint32_t)p->low << 8;
}
static void RangeEnc_FlushData(CRangeEnc *p)
{
int i;
for (i = 0; i < 5; i++)
RangeEnc_ShiftLow(p);
}
static void RangeEnc_EncodeDirectBits(CRangeEnc *p, uint32_t value, int numBits)
{
do
{
p->range >>= 1;
p->low += p->range & (0 - ((value >> --numBits) & 1));
if (p->range < kTopValue)
{
p->range <<= 8;
RangeEnc_ShiftLow(p);
}
}
while (numBits != 0);
}
static void RangeEnc_EncodeBit(CRangeEnc *p, int *prob, uint32_t symbol)
{
uint32_t ttt = *prob;
uint32_t newBound = (p->range >> kNumBitModelTotalBits) * ttt;
if (symbol == 0)
{
p->range = newBound;
ttt += (kBitModelTotal - ttt) >> kNumMoveBits;
}
else
{
p->low += newBound;
p->range -= newBound;
ttt -= ttt >> kNumMoveBits;
}
*prob = (int)ttt;
if (p->range < kTopValue)
{
p->range <<= 8;
RangeEnc_ShiftLow(p);
}
}
static void LitEnc_Encode(CRangeEnc *p, int *probs, uint32_t symbol)
{
symbol |= 0x100;
do
{
RangeEnc_EncodeBit(p, probs + (symbol >> 8), (symbol >> 7) & 1);
symbol <<= 1;
}
while (symbol < 0x10000);
}
static void LitEnc_EncodeMatched(CRangeEnc *p, int *probs, uint32_t symbol, uint32_t matchByte)
{
uint32_t offs = 0x100;
symbol |= 0x100;
do
{
matchByte <<= 1;
RangeEnc_EncodeBit(p, probs + (offs + (matchByte & offs) + (symbol >> 8)), (symbol >> 7) & 1);
symbol <<= 1;
offs &= ~(matchByte ^ symbol);
}
while (symbol < 0x10000);
}
static void LzmaEnc_InitPriceTables(uint32_t *ProbPrices)
{
uint32_t i;
for (i = (1 << kNumMoveReducingBits) / 2; i < kBitModelTotal; i += (1 << kNumMoveReducingBits))
{
const int kCyclesBits = kNumBitPriceShiftBits;
uint32_t w = i;
uint32_t bitCount = 0;
int j;
for (j = 0; j < kCyclesBits; j++)
{
w = w * w;
bitCount <<= 1;
while (w >= ((uint32_t)1 << 16))
{
w >>= 1;
bitCount++;
}
}
ProbPrices[i >> kNumMoveReducingBits] = ((kNumBitModelTotalBits << kCyclesBits) - 15 - bitCount);
}
}
#define GET_PRICE(prob, symbol) \
p->ProbPrices[((prob) ^ (((-(int)(symbol))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits];
#define GET_PRICEa(prob, symbol) \
ProbPrices[((prob) ^ ((-((int)(symbol))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits];
#define GET_PRICE_0(prob) p->ProbPrices[(prob) >> kNumMoveReducingBits]
#define GET_PRICE_1(prob) p->ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]
#define GET_PRICE_0a(prob) ProbPrices[(prob) >> kNumMoveReducingBits]
#define GET_PRICE_1a(prob) ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]
static uint32_t LitEnc_GetPrice(const int *probs, uint32_t symbol, uint32_t *ProbPrices)
{
uint32_t price = 0;
symbol |= 0x100;
do
{
price += GET_PRICEa(probs[symbol >> 8], (symbol >> 7) & 1);
symbol <<= 1;
}
while (symbol < 0x10000);
return price;
}
static uint32_t LitEnc_GetPriceMatched(const int *probs, uint32_t symbol, uint32_t matchByte, uint32_t *ProbPrices)
{
uint32_t price = 0;
uint32_t offs = 0x100;
symbol |= 0x100;
do
{
matchByte <<= 1;
price += GET_PRICEa(probs[offs + (matchByte & offs) + (symbol >> 8)], (symbol >> 7) & 1);
symbol <<= 1;
offs &= ~(matchByte ^ symbol);
}
while (symbol < 0x10000);
return price;
}
static void RcTree_Encode(CRangeEnc *rc, int *probs, int numBitLevels, uint32_t symbol)
{
uint32_t m = 1;
int i;
for (i = numBitLevels; i != 0;)
{
uint32_t bit;
i--;
bit = (symbol >> i) & 1;
RangeEnc_EncodeBit(rc, probs + m, bit);
m = (m << 1) | bit;
}
}
static void RcTree_ReverseEncode(CRangeEnc *rc, int *probs, int numBitLevels, uint32_t symbol)
{
uint32_t m = 1;
int i;
for (i = 0; i < numBitLevels; i++)
{
uint32_t bit = symbol & 1;
RangeEnc_EncodeBit(rc, probs + m, bit);
m = (m << 1) | bit;
symbol >>= 1;
}
}
static uint32_t RcTree_GetPrice(const int *probs, int numBitLevels, uint32_t symbol, uint32_t *ProbPrices)
{
uint32_t price = 0;
symbol |= (1 << numBitLevels);
while (symbol != 1)
{
price += GET_PRICEa(probs[symbol >> 1], symbol & 1);
symbol >>= 1;
}
return price;
}
static uint32_t RcTree_ReverseGetPrice(const int *probs, int numBitLevels, uint32_t symbol, uint32_t *ProbPrices)
{
uint32_t price = 0;
uint32_t m = 1;
int i;
for (i = numBitLevels; i != 0; i--)
{
uint32_t bit = symbol & 1;
symbol >>= 1;
price += GET_PRICEa(probs[m], bit);
m = (m << 1) | bit;
}
return price;
}
static void LenEnc_Init(CLenEnc *p)
{
unsigned i;
p->choice = p->choice2 = kProbInitValue;
for (i = 0; i < (LZMA_NUM_PB_STATES_MAX << kLenNumLowBits); i++)
p->low[i] = kProbInitValue;
for (i = 0; i < (LZMA_NUM_PB_STATES_MAX << kLenNumMidBits); i++)
p->mid[i] = kProbInitValue;
for (i = 0; i < kLenNumHighSymbols; i++)
p->high[i] = kProbInitValue;
}
static void LenEnc_Encode(CLenEnc *p, CRangeEnc *rc, uint32_t symbol, uint32_t posState)
{
if (symbol < kLenNumLowSymbols)
{
RangeEnc_EncodeBit(rc, &p->choice, 0);
RcTree_Encode(rc, p->low + (posState << kLenNumLowBits), kLenNumLowBits, symbol);
}
else
{
RangeEnc_EncodeBit(rc, &p->choice, 1);
if (symbol < kLenNumLowSymbols + kLenNumMidSymbols)
{
RangeEnc_EncodeBit(rc, &p->choice2, 0);
RcTree_Encode(rc, p->mid + (posState << kLenNumMidBits), kLenNumMidBits, symbol - kLenNumLowSymbols);
}
else
{
RangeEnc_EncodeBit(rc, &p->choice2, 1);
RcTree_Encode(rc, p->high, kLenNumHighBits, symbol - kLenNumLowSymbols - kLenNumMidSymbols);
}
}
}
static void LenEnc_SetPrices(CLenEnc *p, uint32_t posState, uint32_t numSymbols, uint32_t *prices, uint32_t *ProbPrices)
{
uint32_t a0 = GET_PRICE_0a(p->choice);
uint32_t a1 = GET_PRICE_1a(p->choice);
uint32_t b0 = a1 + GET_PRICE_0a(p->choice2);
uint32_t b1 = a1 + GET_PRICE_1a(p->choice2);
uint32_t i = 0;
for (i = 0; i < kLenNumLowSymbols; i++)
{
if (i >= numSymbols)
return;
prices[i] = a0 + RcTree_GetPrice(p->low + (posState << kLenNumLowBits), kLenNumLowBits, i, ProbPrices);
}
for (; i < kLenNumLowSymbols + kLenNumMidSymbols; i++)
{
if (i >= numSymbols)
return;
prices[i] = b0 + RcTree_GetPrice(p->mid + (posState << kLenNumMidBits), kLenNumMidBits, i - kLenNumLowSymbols, ProbPrices);
}
for (; i < numSymbols; i++)
prices[i] = b1 + RcTree_GetPrice(p->high, kLenNumHighBits, i - kLenNumLowSymbols - kLenNumMidSymbols, ProbPrices);
}
static void LenPriceEnc_UpdateTable(CLenPriceEnc *p, uint32_t posState, uint32_t *ProbPrices)
{
LenEnc_SetPrices(&p->p, posState, p->tableSize, p->prices[posState], ProbPrices);
p->counters[posState] = p->tableSize;
}
static void LenPriceEnc_UpdateTables(CLenPriceEnc *p, uint32_t numPosStates, uint32_t *ProbPrices)
{
uint32_t posState;
for (posState = 0; posState < numPosStates; posState++)
LenPriceEnc_UpdateTable(p, posState, ProbPrices);
}
static void LenEnc_Encode2(CLenPriceEnc *p, CRangeEnc *rc, uint32_t symbol, uint32_t posState, bool updatePrice, uint32_t *ProbPrices)
{
LenEnc_Encode(&p->p, rc, symbol, posState);
if (updatePrice)
if (--p->counters[posState] == 0)
LenPriceEnc_UpdateTable(p, posState, ProbPrices);
}
static void MovePos(CLzmaEnc *p, uint32_t num)
{
#ifdef SHOW_STAT
ttt += num;
printf("\n MovePos %d", num);
#endif
if (num != 0)
{
p->additionalOffset += num;
p->matchFinder.Skip(&p->matchFinderBase, num);
}
}
static uint32_t ReadMatchDistances(CLzmaEnc *p, uint32_t *numDistancePairsRes)
{
uint32_t lenRes = 0, numPairs;
p->numAvail = Mf_GetNumAvailableBytes(&p->matchFinderBase);
numPairs = p->matchFinder.GetMatches(&p->matchFinderBase, p->matches);
#ifdef SHOW_STAT
printf("\n i = %d numPairs = %d ", ttt, numPairs / 2);
ttt++;
{
uint32_t i;
for (i = 0; i < numPairs; i += 2)
printf("%2d %6d | ", p->matches[i], p->matches[i + 1]);
}
#endif
if (numPairs > 0)
{
lenRes = p->matches[numPairs - 2];
if (lenRes == p->numFastBytes)
{
const uint8_t *pby = Mf_GetPointerToCurrentPos(&p->matchFinderBase) - 1;
uint32_t distance = p->matches[numPairs - 1] + 1;
uint32_t numAvail = p->numAvail;
if (numAvail > LZMA_MATCH_LEN_MAX)
numAvail = LZMA_MATCH_LEN_MAX;
{
const uint8_t *pby2 = pby - distance;
for (; lenRes < numAvail && pby[lenRes] == pby2[lenRes]; lenRes++) ;
}
}
}
p->additionalOffset++;
*numDistancePairsRes = numPairs;
return lenRes;
}
#define MakeAsChar(p) (p)->backPrev = (uint32_t)(-1); (p)->prev1IsChar = false;
#define MakeAsShortRep(p) (p)->backPrev = 0; (p)->prev1IsChar = false;
#define IsShortRep(p) ((p)->backPrev == 0)
static uint32_t GetRepLen1Price(CLzmaEnc *p, State state, uint32_t posState)
{
return
GET_PRICE_0(p->isRepG0[state]) +
GET_PRICE_0(p->isRep0Long[state][posState]);
}
static uint32_t GetPureRepPrice(CLzmaEnc *p, uint32_t repIndex, State state, uint32_t posState)
{
uint32_t price;
if (repIndex == 0)
{
price = GET_PRICE_0(p->isRepG0[state]);
price += GET_PRICE_1(p->isRep0Long[state][posState]);
}
else
{
price = GET_PRICE_1(p->isRepG0[state]);
if (repIndex == 1)
price += GET_PRICE_0(p->isRepG1[state]);
else
{
price += GET_PRICE_1(p->isRepG1[state]);
price += GET_PRICE(p->isRepG2[state], repIndex - 2);
}
}
return price;
}
static uint32_t GetRepPrice(CLzmaEnc *p, uint32_t repIndex, uint32_t len, State state, uint32_t posState)
{
return p->repLenEnc.prices[posState][len - LZMA_MATCH_LEN_MIN] +
GetPureRepPrice(p, repIndex, state, posState);
}
static uint32_t Backward(CLzmaEnc *p, uint32_t *backRes, uint32_t cur)
{
uint32_t posMem = p->opt[cur].posPrev;
uint32_t backMem = p->opt[cur].backPrev;
p->optimumEndIndex = cur;
do
{
if (p->opt[cur].prev1IsChar)
{
MakeAsChar(&p->opt[posMem])
p->opt[posMem].posPrev = posMem - 1;
if (p->opt[cur].prev2)
{
p->opt[posMem - 1].prev1IsChar = false;
p->opt[posMem - 1].posPrev = p->opt[cur].posPrev2;
p->opt[posMem - 1].backPrev = p->opt[cur].backPrev2;
}
}
{
uint32_t posPrev = posMem;
uint32_t backCur = backMem;
backMem = p->opt[posPrev].backPrev;
posMem = p->opt[posPrev].posPrev;
p->opt[posPrev].backPrev = backCur;
p->opt[posPrev].posPrev = cur;
cur = posPrev;
}
}
while (cur != 0);
*backRes = p->opt[0].backPrev;
p->optimumCurrentIndex = p->opt[0].posPrev;
return p->optimumCurrentIndex;
}
#define LIT_PROBS(pos, prevByte) (p->litProbs + ((((pos) & p->lpMask) << p->lc) + ((prevByte) >> (8 - p->lc))) * 0x300)
static uint32_t GetOptimum(CLzmaEnc *p, uint32_t position, uint32_t *backRes)
{
uint32_t numAvail, mainLen, numPairs, repMaxIndex, i, posState, lenEnd, len, cur;
uint32_t matchPrice, repMatchPrice, normalMatchPrice;
uint32_t reps[LZMA_NUM_REPS], repLens[LZMA_NUM_REPS];
uint32_t *matches;
const uint8_t *data;
uint8_t curByte, matchByte;
if (p->optimumEndIndex != p->optimumCurrentIndex)
{
const COptimal *opt = &p->opt[p->optimumCurrentIndex];
uint32_t lenRes = opt->posPrev - p->optimumCurrentIndex;
*backRes = opt->backPrev;
p->optimumCurrentIndex = opt->posPrev;
return lenRes;
}
p->optimumCurrentIndex = p->optimumEndIndex = 0;
if (p->additionalOffset == 0)
mainLen = ReadMatchDistances(p, &numPairs);
else
{
mainLen = p->longestMatchLength;
numPairs = p->numPairs;
}
numAvail = p->numAvail;
if (numAvail < 2)
{
*backRes = (uint32_t)(-1);
return 1;
}
if (numAvail > LZMA_MATCH_LEN_MAX)
numAvail = LZMA_MATCH_LEN_MAX;
data = Mf_GetPointerToCurrentPos(&p->matchFinderBase) - 1;
repMaxIndex = 0;
for (i = 0; i < LZMA_NUM_REPS; i++)
{
uint32_t lenTest;
const uint8_t *data2;
reps[i] = p->reps[i];
data2 = data - (reps[i] + 1);
if (data[0] != data2[0] || data[1] != data2[1])
{
repLens[i] = 0;
continue;
}
for (lenTest = 2; lenTest < numAvail && data[lenTest] == data2[lenTest]; lenTest++) ;
repLens[i] = lenTest;
if (lenTest > repLens[repMaxIndex])
repMaxIndex = i;
}
if (repLens[repMaxIndex] >= p->numFastBytes)
{
uint32_t lenRes;
*backRes = repMaxIndex;
lenRes = repLens[repMaxIndex];
MovePos(p, lenRes - 1);
return lenRes;
}
matches = p->matches;
if (mainLen >= p->numFastBytes)
{
*backRes = matches[numPairs - 1] + LZMA_NUM_REPS;
MovePos(p, mainLen - 1);
return mainLen;
}
curByte = *data;
matchByte = *(data - (reps[0] + 1));
if (mainLen < 2 && curByte != matchByte && repLens[repMaxIndex] < 2)
{
*backRes = (uint32_t)-1;
return 1;
}
p->opt[0].state = p->state;
posState = (position & p->pbMask);
{
const int *probs = LIT_PROBS(position, *(data - 1));
p->opt[1].price = GET_PRICE_0(p->isMatch[p->state][posState]) +
(!IsCharState(p->state) ?
LitEnc_GetPriceMatched(probs, curByte, matchByte, p->ProbPrices) :
LitEnc_GetPrice(probs, curByte, p->ProbPrices));
}
MakeAsChar(&p->opt[1]);
matchPrice = GET_PRICE_1(p->isMatch[p->state][posState]);
repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[p->state]);
if (matchByte == curByte)
{
uint32_t shortRepPrice = repMatchPrice + GetRepLen1Price(p, p->state, posState);
if (shortRepPrice < p->opt[1].price)
{
p->opt[1].price = shortRepPrice;
MakeAsShortRep(&p->opt[1]);
}
}
lenEnd = ((mainLen >= repLens[repMaxIndex]) ? mainLen : repLens[repMaxIndex]);
if (lenEnd < 2)
{
*backRes = p->opt[1].backPrev;
return 1;
}
p->opt[1].posPrev = 0;
for (i = 0; i < LZMA_NUM_REPS; i++)
p->opt[0].backs[i] = reps[i];
len = lenEnd;
do
p->opt[len--].price = kInfinityPrice;
while (len >= 2);
for (i = 0; i < LZMA_NUM_REPS; i++)
{
uint32_t repLen = repLens[i];
uint32_t price;
if (repLen < 2)
continue;
price = repMatchPrice + GetPureRepPrice(p, i, p->state, posState);
do
{
uint32_t curAndLenPrice = price + p->repLenEnc.prices[posState][repLen - 2];
COptimal *opt = &p->opt[repLen];
if (curAndLenPrice < opt->price)
{
opt->price = curAndLenPrice;
opt->posPrev = 0;
opt->backPrev = i;
opt->prev1IsChar = false;
}
}
while (--repLen >= 2);
}
normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[p->state]);
len = ((repLens[0] >= 2) ? repLens[0] + 1 : 2);
if (len <= mainLen)
{
uint32_t offs = 0;
while (len > matches[offs])
offs += 2;
for (; ; len++)
{
COptimal *opt;
uint32_t distance = matches[offs + 1];
uint32_t curAndLenPrice = normalMatchPrice + p->lenEnc.prices[posState][len - LZMA_MATCH_LEN_MIN];
uint32_t lenToPosState = GetLenToPosState(len);
if (distance < kNumFullDistances)
curAndLenPrice += p->distancesPrices[lenToPosState][distance];
else
{
uint32_t slot;
GetPosSlot2(distance, slot);
curAndLenPrice += p->alignPrices[distance & kAlignMask] + p->posSlotPrices[lenToPosState][slot];
}
opt = &p->opt[len];
if (curAndLenPrice < opt->price)
{
opt->price = curAndLenPrice;
opt->posPrev = 0;
opt->backPrev = distance + LZMA_NUM_REPS;
opt->prev1IsChar = false;
}
if (len == matches[offs])
{
offs += 2;
if (offs == numPairs)
break;
}
}
}
cur = 0;
#ifdef SHOW_STAT2
if (position >= 0)
{
unsigned i;
printf("\n pos = %4X", position);
for (i = cur; i <= lenEnd; i++)
printf("\nprice[%4X] = %d", position - cur + i, p->opt[i].price);
}
#endif
for (;;)
{
uint32_t numAvailFull, newLen, numPairs, posPrev, state, posState, startLen;
uint32_t curPrice, curAnd1Price, matchPrice, repMatchPrice;
bool nextIsChar;
uint8_t curByte, matchByte;
const uint8_t *data;
COptimal *curOpt;
COptimal *nextOpt;
cur++;
if (cur == lenEnd)
return Backward(p, backRes, cur);
newLen = ReadMatchDistances(p, &numPairs);
if (newLen >= p->numFastBytes)
{
p->numPairs = numPairs;
p->longestMatchLength = newLen;
return Backward(p, backRes, cur);
}
position++;
curOpt = &p->opt[cur];
posPrev = curOpt->posPrev;
if (curOpt->prev1IsChar)
{
posPrev--;
if (curOpt->prev2)
{
state = p->opt[curOpt->posPrev2].state;
if (curOpt->backPrev2 < LZMA_NUM_REPS)
state = kRepNextStates[state];
else
state = kMatchNextStates[state];
}
else
state = p->opt[posPrev].state;
state = kLiteralNextStates[state];
}
else
state = p->opt[posPrev].state;
if (posPrev == cur - 1)
{
if (IsShortRep(curOpt))
state = kShortRepNextStates[state];
else
state = kLiteralNextStates[state];
}
else
{
uint32_t pos;
const COptimal *prevOpt;
if (curOpt->prev1IsChar && curOpt->prev2)
{
posPrev = curOpt->posPrev2;
pos = curOpt->backPrev2;
state = kRepNextStates[state];
}
else
{
pos = curOpt->backPrev;
if (pos < LZMA_NUM_REPS)
state = kRepNextStates[state];
else
state = kMatchNextStates[state];
}
prevOpt = &p->opt[posPrev];
if (pos < LZMA_NUM_REPS)
{
uint32_t i;
reps[0] = prevOpt->backs[pos];
for (i = 1; i <= pos; i++)
reps[i] = prevOpt->backs[i - 1];
for (; i < LZMA_NUM_REPS; i++)
reps[i] = prevOpt->backs[i];
}
else
{
uint32_t i;
reps[0] = (pos - LZMA_NUM_REPS);
for (i = 1; i < LZMA_NUM_REPS; i++)
reps[i] = prevOpt->backs[i - 1];
}
}
curOpt->state = state;
curOpt->backs[0] = reps[0];
curOpt->backs[1] = reps[1];
curOpt->backs[2] = reps[2];
curOpt->backs[3] = reps[3];
curPrice = curOpt->price;
nextIsChar = false;
data = Mf_GetPointerToCurrentPos(&p->matchFinderBase) - 1;
curByte = *data;
matchByte = *(data - (reps[0] + 1));
posState = (position & p->pbMask);
curAnd1Price = curPrice + GET_PRICE_0(p->isMatch[state][posState]);
{
const int *probs = LIT_PROBS(position, *(data - 1));
curAnd1Price +=
(!IsCharState(state) ?
LitEnc_GetPriceMatched(probs, curByte, matchByte, p->ProbPrices) :
LitEnc_GetPrice(probs, curByte, p->ProbPrices));
}
nextOpt = &p->opt[cur + 1];
if (curAnd1Price < nextOpt->price)
{
nextOpt->price = curAnd1Price;
nextOpt->posPrev = cur;
MakeAsChar(nextOpt);
nextIsChar = true;
}
matchPrice = curPrice + GET_PRICE_1(p->isMatch[state][posState]);
repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[state]);
if (matchByte == curByte && !(nextOpt->posPrev < cur && nextOpt->backPrev == 0))
{
uint32_t shortRepPrice = repMatchPrice + GetRepLen1Price(p, state, posState);
if (shortRepPrice <= nextOpt->price)
{
nextOpt->price = shortRepPrice;
nextOpt->posPrev = cur;
MakeAsShortRep(nextOpt);
nextIsChar = true;
}
}
numAvailFull = p->numAvail;
{
uint32_t temp = kNumOpts - 1 - cur;
if (temp < numAvailFull)
numAvailFull = temp;
}
if (numAvailFull < 2)
continue;
numAvail = (numAvailFull <= p->numFastBytes ? numAvailFull : p->numFastBytes);
if (!nextIsChar && matchByte != curByte) /* speed optimization */
{
/* try Literal + rep0 */
uint32_t temp;
uint32_t lenTest2;
const uint8_t *data2 = data - (reps[0] + 1);
uint32_t limit = p->numFastBytes + 1;
if (limit > numAvailFull)
limit = numAvailFull;
for (temp = 1; temp < limit && data[temp] == data2[temp]; temp++) ;
lenTest2 = temp - 1;
if (lenTest2 >= 2)
{
State state2 = kLiteralNextStates[state];
uint32_t posStateNext = (position + 1) & p->pbMask;
uint32_t nextRepMatchPrice = curAnd1Price +
GET_PRICE_1(p->isMatch[state2][posStateNext]) +
GET_PRICE_1(p->isRep[state2]);
/* for (; lenTest2 >= 2; lenTest2--) */
{
uint32_t curAndLenPrice;
COptimal *opt;
uint32_t offset = cur + 1 + lenTest2;
while (lenEnd < offset)
p->opt[++lenEnd].price = kInfinityPrice;
curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
opt = &p->opt[offset];
if (curAndLenPrice < opt->price)
{
opt->price = curAndLenPrice;
opt->posPrev = cur + 1;
opt->backPrev = 0;
opt->prev1IsChar = true;
opt->prev2 = false;
}
}
}
}
startLen = 2; /* speed optimization */
{
uint32_t repIndex;
for (repIndex = 0; repIndex < LZMA_NUM_REPS; repIndex++)
{
uint32_t lenTest;
uint32_t lenTestTemp;
uint32_t price;
const uint8_t *data2 = data - (reps[repIndex] + 1);
if (data[0] != data2[0] || data[1] != data2[1])
continue;
for (lenTest = 2; lenTest < numAvail && data[lenTest] == data2[lenTest]; lenTest++) ;
while (lenEnd < cur + lenTest)
p->opt[++lenEnd].price = kInfinityPrice;
lenTestTemp = lenTest;
price = repMatchPrice + GetPureRepPrice(p, repIndex, state, posState);
do
{
uint32_t curAndLenPrice = price + p->repLenEnc.prices[posState][lenTest - 2];
COptimal *opt = &p->opt[cur + lenTest];
if (curAndLenPrice < opt->price)
{
opt->price = curAndLenPrice;
opt->posPrev = cur;
opt->backPrev = repIndex;
opt->prev1IsChar = false;
}
}
while (--lenTest >= 2);
lenTest = lenTestTemp;
if (repIndex == 0)
startLen = lenTest + 1;
/* if (_maxMode) */
{
uint32_t lenTest2 = lenTest + 1;
uint32_t limit = lenTest2 + p->numFastBytes;
uint32_t nextRepMatchPrice;
if (limit > numAvailFull)
limit = numAvailFull;
for (; lenTest2 < limit && data[lenTest2] == data2[lenTest2]; lenTest2++) ;
lenTest2 -= lenTest + 1;
if (lenTest2 >= 2)
{
State state2 = kRepNextStates[state];
uint32_t posStateNext = (position + lenTest) & p->pbMask;
uint32_t curAndLenCharPrice =
price + p->repLenEnc.prices[posState][lenTest - 2] +
GET_PRICE_0(p->isMatch[state2][posStateNext]) +
LitEnc_GetPriceMatched(LIT_PROBS(position + lenTest, data[lenTest - 1]),
data[lenTest], data2[lenTest], p->ProbPrices);
state2 = kLiteralNextStates[state2];
posStateNext = (position + lenTest + 1) & p->pbMask;
nextRepMatchPrice = curAndLenCharPrice +
GET_PRICE_1(p->isMatch[state2][posStateNext]) +
GET_PRICE_1(p->isRep[state2]);
/* for (; lenTest2 >= 2; lenTest2--) */
{
uint32_t curAndLenPrice;
COptimal *opt;
uint32_t offset = cur + lenTest + 1 + lenTest2;
while (lenEnd < offset)
p->opt[++lenEnd].price = kInfinityPrice;
curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
opt = &p->opt[offset];
if (curAndLenPrice < opt->price)
{
opt->price = curAndLenPrice;
opt->posPrev = cur + lenTest + 1;
opt->backPrev = 0;
opt->prev1IsChar = true;
opt->prev2 = true;
opt->posPrev2 = cur;
opt->backPrev2 = repIndex;
}
}
}
}
}
}
/* for (uint32_t lenTest = 2; lenTest <= newLen; lenTest++) */
if (newLen > numAvail)
{
newLen = numAvail;
for (numPairs = 0; newLen > matches[numPairs]; numPairs += 2) ;
matches[numPairs] = newLen;
numPairs += 2;
}
if (newLen >= startLen)
{
uint32_t normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[state]);
uint32_t offs, curBack, posSlot;
uint32_t lenTest;
while (lenEnd < cur + newLen)
p->opt[++lenEnd].price = kInfinityPrice;
offs = 0;
while (startLen > matches[offs])
offs += 2;
curBack = matches[offs + 1];
GetPosSlot2(curBack, posSlot);
for (lenTest = /*2*/ startLen; ; lenTest++)
{
uint32_t curAndLenPrice = normalMatchPrice + p->lenEnc.prices[posState][lenTest - LZMA_MATCH_LEN_MIN];
uint32_t lenToPosState = GetLenToPosState(lenTest);
COptimal *opt;
if (curBack < kNumFullDistances)
curAndLenPrice += p->distancesPrices[lenToPosState][curBack];
else
curAndLenPrice += p->posSlotPrices[lenToPosState][posSlot] + p->alignPrices[curBack & kAlignMask];
opt = &p->opt[cur + lenTest];
if (curAndLenPrice < opt->price)
{
opt->price = curAndLenPrice;
opt->posPrev = cur;
opt->backPrev = curBack + LZMA_NUM_REPS;
opt->prev1IsChar = false;
}
if (/*_maxMode && */lenTest == matches[offs])
{
/* Try Match + Literal + Rep0 */
const uint8_t *data2 = data - (curBack + 1);
uint32_t lenTest2 = lenTest + 1;
uint32_t limit = lenTest2 + p->numFastBytes;
uint32_t nextRepMatchPrice;
if (limit > numAvailFull)
limit = numAvailFull;
for (; lenTest2 < limit && data[lenTest2] == data2[lenTest2]; lenTest2++) ;
lenTest2 -= lenTest + 1;
if (lenTest2 >= 2)
{
State state2 = kMatchNextStates[state];
uint32_t posStateNext = (position + lenTest) & p->pbMask;
uint32_t curAndLenCharPrice = curAndLenPrice +
GET_PRICE_0(p->isMatch[state2][posStateNext]) +
LitEnc_GetPriceMatched(LIT_PROBS(position + lenTest, data[lenTest - 1]),
data[lenTest], data2[lenTest], p->ProbPrices);
state2 = kLiteralNextStates[state2];
posStateNext = (posStateNext + 1) & p->pbMask;
nextRepMatchPrice = curAndLenCharPrice +
GET_PRICE_1(p->isMatch[state2][posStateNext]) +
GET_PRICE_1(p->isRep[state2]);
/* for (; lenTest2 >= 2; lenTest2--) */
{
uint32_t offset = cur + lenTest + 1 + lenTest2;
uint32_t curAndLenPrice;
COptimal *opt;
while (lenEnd < offset)
p->opt[++lenEnd].price = kInfinityPrice;
curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
opt = &p->opt[offset];
if (curAndLenPrice < opt->price)
{
opt->price = curAndLenPrice;
opt->posPrev = cur + lenTest + 1;
opt->backPrev = 0;
opt->prev1IsChar = true;
opt->prev2 = true;
opt->posPrev2 = cur;
opt->backPrev2 = curBack + LZMA_NUM_REPS;
}
}
}
offs += 2;
if (offs == numPairs)
break;
curBack = matches[offs + 1];
if (curBack >= kNumFullDistances)
GetPosSlot2(curBack, posSlot);
}
}
}
}
}
#define ChangePair(smallDist, bigDist) (((bigDist) >> 7) > (smallDist))
static uint32_t GetOptimumFast(CLzmaEnc *p, uint32_t *backRes)
{
uint32_t numAvail, mainLen, mainDist, numPairs, repIndex, repLen, i;
const uint8_t *data;
const uint32_t *matches;
if (p->additionalOffset == 0)
mainLen = ReadMatchDistances(p, &numPairs);
else
{
mainLen = p->longestMatchLength;
numPairs = p->numPairs;
}
numAvail = p->numAvail;
*backRes = (uint32_t)-1;
if (numAvail < 2)
return 1;
if (numAvail > LZMA_MATCH_LEN_MAX)
numAvail = LZMA_MATCH_LEN_MAX;
data = Mf_GetPointerToCurrentPos(&p->matchFinderBase) - 1;
repLen = repIndex = 0;
for (i = 0; i < LZMA_NUM_REPS; i++)
{
uint32_t len;
const uint8_t *data2 = data - (p->reps[i] + 1);
if (data[0] != data2[0] || data[1] != data2[1])
continue;
for (len = 2; len < numAvail && data[len] == data2[len]; len++) ;
if (len >= p->numFastBytes)
{
*backRes = i;
MovePos(p, len - 1);
return len;
}
if (len > repLen)
{
repIndex = i;
repLen = len;
}
}
matches = p->matches;
if (mainLen >= p->numFastBytes)
{
*backRes = matches[numPairs - 1] + LZMA_NUM_REPS;
MovePos(p, mainLen - 1);
return mainLen;
}
mainDist = 0; /* for GCC */
if (mainLen >= 2)
{
mainDist = matches[numPairs - 1];
while (numPairs > 2 && mainLen == matches[numPairs - 4] + 1)
{
if (!ChangePair(matches[numPairs - 3], mainDist))
break;
numPairs -= 2;
mainLen = matches[numPairs - 2];
mainDist = matches[numPairs - 1];
}
if (mainLen == 2 && mainDist >= 0x80)
mainLen = 1;
}
if (repLen >= 2 && (
(repLen + 1 >= mainLen) ||
(repLen + 2 >= mainLen && mainDist >= (1 << 9)) ||
(repLen + 3 >= mainLen && mainDist >= (1 << 15))))
{
*backRes = repIndex;
MovePos(p, repLen - 1);
return repLen;
}
if (mainLen < 2 || numAvail <= 2)
return 1;
p->longestMatchLength = ReadMatchDistances(p, &p->numPairs);
if (p->longestMatchLength >= 2)
{
uint32_t newDistance = matches[p->numPairs - 1];
if ((p->longestMatchLength >= mainLen && newDistance < mainDist) ||
(p->longestMatchLength == mainLen + 1 && !ChangePair(mainDist, newDistance)) ||
(p->longestMatchLength > mainLen + 1) ||
(p->longestMatchLength + 1 >= mainLen && mainLen >= 3 && ChangePair(newDistance, mainDist)))
return 1;
}
data = Mf_GetPointerToCurrentPos(&p->matchFinderBase) - 1;
for (i = 0; i < LZMA_NUM_REPS; i++)
{
uint32_t len, limit;
const uint8_t *data2 = data - (p->reps[i] + 1);
if (data[0] != data2[0] || data[1] != data2[1])
continue;
limit = mainLen - 1;
for (len = 2; len < limit && data[len] == data2[len]; len++) ;
if (len >= limit)
return 1;
}
*backRes = mainDist + LZMA_NUM_REPS;
MovePos(p, mainLen - 2);
return mainLen;
}
static void LZe_full_flush(CLzmaEnc *p, uint32_t posState)
{
const uint32_t len = LZMA_MATCH_LEN_MIN;
Lzip_trailer trailer;
RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 1);
RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 0);
p->state = kMatchNextStates[p->state];
LenEnc_Encode2(&p->lenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
RcTree_Encode(&p->rc, p->posSlotEncoder[GetLenToPosState(len)], kNumPosSlotBits, (1 << kNumPosSlotBits) - 1);
RangeEnc_EncodeDirectBits(&p->rc, (((uint32_t)1 << 30) - 1) >> kNumAlignBits, 30 - kNumAlignBits);
RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, kAlignMask);
RangeEnc_FlushData(&p->rc);
RangeEnc_FlushStream(&p->rc);
Lt_set_data_crc( trailer, p->matchFinderBase.crc ^ 0xFFFFFFFFU );
Lt_set_data_size( trailer, p->nowPos64 );
Lt_set_member_size( trailer, p->rc.processed + Lh_size + Lt_size );
if( writeblock( p->rc.outfd, trailer, Lt_size ) != Lt_size )
p->rc.res = SZ_ERROR_WRITE;
if( verbosity >= 1 )
{
unsigned long long in_size = p->nowPos64;
unsigned long long out_size = p->rc.processed + Lh_size + Lt_size;
if( in_size == 0 || out_size == 0 )
fputs( " no data compressed.\n", stderr );
else
fprintf( stderr, "%6.3f:1, %5.2f%% ratio, %5.2f%% saved, "
"%llu in, %llu out.\n",
(double)in_size / out_size,
( 100.0 * out_size ) / in_size,
100.0 - ( ( 100.0 * out_size ) / in_size ),
in_size, out_size );
}
}
static int CheckErrors(CLzmaEnc *p)
{
if (p->result != SZ_OK)
return p->result;
if (p->rc.res != SZ_OK)
p->result = SZ_ERROR_WRITE;
if (p->matchFinderBase.result != SZ_OK)
p->result = SZ_ERROR_READ;
if (p->result != SZ_OK)
p->finished = true;
return p->result;
}
static int Flush(CLzmaEnc *p, uint32_t nowPos)
{
/* ReleaseMFStream(); */
p->finished = true;
LZe_full_flush(p, nowPos & p->pbMask);
return CheckErrors(p);
}
static void FillAlignPrices(CLzmaEnc *p)
{
uint32_t i;
for (i = 0; i < kAlignTableSize; i++)
p->alignPrices[i] = RcTree_ReverseGetPrice(p->posAlignEncoder, kNumAlignBits, i, p->ProbPrices);
p->alignPriceCount = 0;
}
static void FillDistancesPrices(CLzmaEnc *p)
{
uint32_t tempPrices[kNumFullDistances];
uint32_t i, lenToPosState;
for (i = kStartPosModelIndex; i < kNumFullDistances; i++)
{
uint32_t posSlot = GetPosSlot1(i);
uint32_t footerBits = ((posSlot >> 1) - 1);
uint32_t base = ((2 | (posSlot & 1)) << footerBits);
tempPrices[i] = RcTree_ReverseGetPrice(p->posEncoders + base - posSlot - 1, footerBits, i - base, p->ProbPrices);
}
for (lenToPosState = 0; lenToPosState < kNumLenToPosStates; lenToPosState++)
{
uint32_t posSlot;
const int *encoder = p->posSlotEncoder[lenToPosState];
uint32_t *posSlotPrices = p->posSlotPrices[lenToPosState];
for (posSlot = 0; posSlot < p->distTableSize; posSlot++)
posSlotPrices[posSlot] = RcTree_GetPrice(encoder, kNumPosSlotBits, posSlot, p->ProbPrices);
for (posSlot = kEndPosModelIndex; posSlot < p->distTableSize; posSlot++)
posSlotPrices[posSlot] += ((((posSlot >> 1) - 1) - kNumAlignBits) << kNumBitPriceShiftBits);
{
uint32_t *distancesPrices = p->distancesPrices[lenToPosState];
uint32_t i;
for (i = 0; i < kStartPosModelIndex; i++)
distancesPrices[i] = posSlotPrices[i];
for (; i < kNumFullDistances; i++)
distancesPrices[i] = posSlotPrices[GetPosSlot1(i)] + tempPrices[i];
}
}
p->matchPriceCount = 0;
}
static int LzmaEnc_CodeOneBlock(CLzmaEnc *p)
{
uint32_t nowPos32, startPos32;
if (p->finished)
return p->result;
if( CheckErrors(p) != 0 ) return p->result;
nowPos32 = (uint32_t)p->nowPos64;
startPos32 = nowPos32;
if (p->nowPos64 == 0)
{
uint32_t numPairs;
uint8_t curByte;
if (Mf_GetNumAvailableBytes(&p->matchFinderBase) == 0)
return Flush(p, nowPos32);
ReadMatchDistances(p, &numPairs);
RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][0], 0);
p->state = kLiteralNextStates[p->state];
curByte = Mf_GetIndexByte(&p->matchFinderBase, 0 - p->additionalOffset);
LitEnc_Encode(&p->rc, p->litProbs, curByte);
p->additionalOffset--;
nowPos32++;
}
if (Mf_GetNumAvailableBytes(&p->matchFinderBase) != 0)
for (;;)
{
uint32_t pos, len, posState;
if (p->fastMode)
len = GetOptimumFast(p, &pos);
else
len = GetOptimum(p, nowPos32, &pos);
#ifdef SHOW_STAT2
printf("\n pos = %4X, len = %d pos = %d", nowPos32, len, pos);
#endif
posState = nowPos32 & p->pbMask;
if (len == 1 && pos == (uint32_t)-1)
{
uint8_t curByte;
int *probs;
const uint8_t *data;
RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 0);
data = Mf_GetPointerToCurrentPos(&p->matchFinderBase) - p->additionalOffset;
curByte = *data;
probs = LIT_PROBS(nowPos32, *(data - 1));
if (IsCharState(p->state))
LitEnc_Encode(&p->rc, probs, curByte);
else
LitEnc_EncodeMatched(&p->rc, probs, curByte, *(data - p->reps[0] - 1));
p->state = kLiteralNextStates[p->state];
}
else
{
RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 1);
if (pos < LZMA_NUM_REPS)
{
RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 1);
if (pos == 0)
{
RangeEnc_EncodeBit(&p->rc, &p->isRepG0[p->state], 0);
RangeEnc_EncodeBit(&p->rc, &p->isRep0Long[p->state][posState], ((len == 1) ? 0 : 1));
}
else
{
uint32_t distance = p->reps[pos];
RangeEnc_EncodeBit(&p->rc, &p->isRepG0[p->state], 1);
if (pos == 1)
RangeEnc_EncodeBit(&p->rc, &p->isRepG1[p->state], 0);
else
{
RangeEnc_EncodeBit(&p->rc, &p->isRepG1[p->state], 1);
RangeEnc_EncodeBit(&p->rc, &p->isRepG2[p->state], pos - 2);
if (pos == 3)
p->reps[3] = p->reps[2];
p->reps[2] = p->reps[1];
}
p->reps[1] = p->reps[0];
p->reps[0] = distance;
}
if (len == 1)
p->state = kShortRepNextStates[p->state];
else
{
LenEnc_Encode2(&p->repLenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
p->state = kRepNextStates[p->state];
}
}
else
{
uint32_t posSlot;
RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 0);
p->state = kMatchNextStates[p->state];
LenEnc_Encode2(&p->lenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
pos -= LZMA_NUM_REPS;
GetPosSlot(pos, posSlot);
RcTree_Encode(&p->rc, p->posSlotEncoder[GetLenToPosState(len)], kNumPosSlotBits, posSlot);
if (posSlot >= kStartPosModelIndex)
{
uint32_t footerBits = ((posSlot >> 1) - 1);
uint32_t base = ((2 | (posSlot & 1)) << footerBits);
uint32_t posReduced = pos - base;
if (posSlot < kEndPosModelIndex)
RcTree_ReverseEncode(&p->rc, p->posEncoders + base - posSlot - 1, footerBits, posReduced);
else
{
RangeEnc_EncodeDirectBits(&p->rc, posReduced >> kNumAlignBits, footerBits - kNumAlignBits);
RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, posReduced & kAlignMask);
p->alignPriceCount++;
}
}
p->reps[3] = p->reps[2];
p->reps[2] = p->reps[1];
p->reps[1] = p->reps[0];
p->reps[0] = pos;
p->matchPriceCount++;
}
}
p->additionalOffset -= len;
nowPos32 += len;
if (p->additionalOffset == 0)
{
uint32_t processed;
if (!p->fastMode)
{
if (p->matchPriceCount >= (1 << 7))
FillDistancesPrices(p);
if (p->alignPriceCount >= kAlignTableSize)
FillAlignPrices(p);
}
if (Mf_GetNumAvailableBytes(&p->matchFinderBase) == 0)
break;
processed = nowPos32 - startPos32;
if (processed >= (1 << 15))
{
p->nowPos64 += nowPos32 - startPos32;
return CheckErrors(p);
}
}
}
p->nowPos64 += nowPos32 - startPos32;
return Flush(p, nowPos32);
}
CLzmaEncHandle LzmaEnc_Init( const int dict_size, const int match_len_limit,
const int infd, const int outfd )
{
int i;
const uint32_t beforeSize = kNumOpts;
CLzmaEnc * const p = (CLzmaEnc *)malloc(sizeof(CLzmaEnc));
if( !p ) return 0;
p->nowPos64 = 0;
p->dictSize = dict_size;
p->numFastBytes = match_len_limit;
p->lc = literal_context_bits;
p->lp = 0;
p->pb = pos_state_bits;
p->optimumEndIndex = 0;
p->optimumCurrentIndex = 0;
p->additionalOffset = 0;
p->state = 0;
p->result = SZ_OK;
p->fastMode = false;
p->finished = false;
if (!Mf_Init(&p->matchFinderBase, infd, 16 + ( match_len_limit / 2 ), p->dictSize, beforeSize, p->numFastBytes, LZMA_MATCH_LEN_MAX))
{ free( p ); return 0; }
Mf_CreateVTable(&p->matchFinderBase, &p->matchFinder);
LzmaEnc_FastPosInit(p->g_FastPos);
LzmaEnc_InitPriceTables(p->ProbPrices);
for (i = 0; i < kDicLogSizeMaxCompress; i++)
if (p->dictSize <= ((uint32_t)1 << i))
break;
p->distTableSize = i * 2;
if( !RangeEnc_Init( &p->rc, outfd ) ) { free( p ); return 0; }
p->litProbs = (int *)malloc((0x300 << (p->lc + p->lp)) * sizeof(int));
if( !p->litProbs ) { free( p ); return 0; }
for (i = 0 ; i < LZMA_NUM_REPS; i++)
p->reps[i] = 0;
for (i = 0; i < kNumStates; i++)
{
int j;
for (j = 0; j < LZMA_NUM_PB_STATES_MAX; j++)
{
p->isMatch[i][j] = kProbInitValue;
p->isRep0Long[i][j] = kProbInitValue;
}
p->isRep[i] = kProbInitValue;
p->isRepG0[i] = kProbInitValue;
p->isRepG1[i] = kProbInitValue;
p->isRepG2[i] = kProbInitValue;
}
{
const int num = 0x300 << (p->lp + p->lc);
for (i = 0; i < num; i++)
p->litProbs[i] = kProbInitValue;
}
for (i = 0; i < kNumLenToPosStates; i++)
{
int *probs = p->posSlotEncoder[i];
uint32_t j;
for (j = 0; j < (1 << kNumPosSlotBits); j++)
probs[j] = kProbInitValue;
}
for (i = 0; i < kNumFullDistances - kEndPosModelIndex; i++)
p->posEncoders[i] = kProbInitValue;
LenEnc_Init(&p->lenEnc.p);
LenEnc_Init(&p->repLenEnc.p);
for (i = 0; i < (1 << kNumAlignBits); i++)
p->posAlignEncoder[i] = kProbInitValue;
p->pbMask = (1 << p->pb) - 1;
p->lpMask = (1 << p->lp) - 1;
if (!p->fastMode) { FillDistancesPrices(p); FillAlignPrices(p); }
p->lenEnc.tableSize =
p->repLenEnc.tableSize =
p->numFastBytes + 1 - LZMA_MATCH_LEN_MIN;
LenPriceEnc_UpdateTables(&p->lenEnc, 1 << p->pb, p->ProbPrices);
LenPriceEnc_UpdateTables(&p->repLenEnc, 1 << p->pb, p->ProbPrices);
return p;
}
void LzmaEnc_Free(CLzmaEncHandle pp)
{
CLzmaEnc *p = (CLzmaEnc *)pp;
Mf_Free(&p->matchFinderBase);
free(p->litProbs);
p->litProbs = 0;
RangeEnc_Free(&p->rc);
free(p);
}
int LzmaEnc_Encode(CLzmaEncHandle pp)
{
int res = SZ_OK;
CLzmaEnc *p = (CLzmaEnc *)pp;
for (;;)
{
res = LzmaEnc_CodeOneBlock(p);
if( res != SZ_OK || p->finished )
break;
}
return res;
}
|