Lines Matching defs:p
50 void LzmaEncProps_Init(CLzmaEncProps *p)
52 p->level = 5;
53 p->dictSize = p->mc = 0;
54 p->lc = p->lp = p->pb = p->algo = p->fb = p->btMode = p->numHashBytes = p->numThreads = -1;
55 p->writeEndMark = 0;
58 void LzmaEncProps_Normalize(CLzmaEncProps *p)
60 int level = p->level;
62 p->level = level;
63 if (p->dictSize == 0) p->dictSize = (level <= 5 ? (1 << (level * 2 + 14)) : (level == 6 ? (1 << 25) : (1 << 26)));
64 if (p->lc < 0) p->lc = 3;
65 if (p->lp < 0) p->lp = 0;
66 if (p->pb < 0) p->pb = 2;
67 if (p->algo < 0) p->algo = (level < 5 ? 0 : 1);
68 if (p->fb < 0) p->fb = (level < 7 ? 32 : 64);
69 if (p->btMode < 0) p->btMode = (p->algo == 0 ? 0 : 1);
70 if (p->numHashBytes < 0) p->numHashBytes = 4;
71 if (p->mc == 0) p->mc = (16 + (p->fb >> 1)) >> (p->btMode ? 0 : 1);
72 if (p->numThreads < 0) p->numThreads = ((p->btMode && p->algo) ? 2 : 1);
123 res = p->g_FastPos[pos >> i] + (i * 2); }
126 p->g_FastPos[pos >> 6] + 12 : \
127 p->g_FastPos[pos >> (6 + kNumLogBits - 1)] + (6 + (kNumLogBits - 1)) * 2; }
130 #define GetPosSlot1(pos) p->g_FastPos[pos]
132 #define GetPosSlot(pos, res) { if (pos < kNumFullDistances) res = p->g_FastPos[pos]; else BSR2_RET(pos, res); }
214 CLenEnc p;
244 CSeqInStreamBuf *p = (CSeqInStreamBuf *)pp;
245 if (p->rem < curSize)
246 curSize = p->rem;
247 memcpy(data, p->data, curSize);
248 p->rem -= curSize;
249 p->data += curSize;
361 CLzmaEnc *p = (CLzmaEnc *)pp;
362 CSaveState *dest = &p->saveState;
364 dest->lenEnc = p->lenEnc;
365 dest->repLenEnc = p->repLenEnc;
366 dest->state = p->state;
370 memcpy(dest->isMatch[i], p->isMatch[i], sizeof(p->isMatch[i]));
371 memcpy(dest->isRep0Long[i], p->isRep0Long[i], sizeof(p->isRep0Long[i]));
374 memcpy(dest->posSlotEncoder[i], p->posSlotEncoder[i], sizeof(p->posSlotEncoder[i]));
375 memcpy(dest->isRep, p->isRep, sizeof(p->isRep));
376 memcpy(dest->isRepG0, p->isRepG0, sizeof(p->isRepG0));
377 memcpy(dest->isRepG1, p->isRepG1, sizeof(p->isRepG1));
378 memcpy(dest->isRepG2, p->isRepG2, sizeof(p->isRepG2));
379 memcpy(dest->posEncoders, p->posEncoders, sizeof(p->posEncoders));
380 memcpy(dest->posAlignEncoder, p->posAlignEncoder, sizeof(p->posAlignEncoder));
381 memcpy(dest->reps, p->reps, sizeof(p->reps));
382 memcpy(dest->litProbs, p->litProbs, (0x300 << p->lclp) * sizeof(CLzmaProb));
388 const CSaveState *p = &dest->saveState;
390 dest->lenEnc = p->lenEnc;
391 dest->repLenEnc = p->repLenEnc;
392 dest->state = p->state;
396 memcpy(dest->isMatch[i], p->isMatch[i], sizeof(p->isMatch[i]));
397 memcpy(dest->isRep0Long[i], p->isRep0Long[i], sizeof(p->isRep0Long[i]));
400 memcpy(dest->posSlotEncoder[i], p->posSlotEncoder[i], sizeof(p->posSlotEncoder[i]));
401 memcpy(dest->isRep, p->isRep, sizeof(p->isRep));
402 memcpy(dest->isRepG0, p->isRepG0, sizeof(p->isRepG0));
403 memcpy(dest->isRepG1, p->isRepG1, sizeof(p->isRepG1));
404 memcpy(dest->isRepG2, p->isRepG2, sizeof(p->isRepG2));
405 memcpy(dest->posEncoders, p->posEncoders, sizeof(p->posEncoders));
406 memcpy(dest->posAlignEncoder, p->posAlignEncoder, sizeof(p->posAlignEncoder));
407 memcpy(dest->reps, p->reps, sizeof(p->reps));
408 memcpy(dest->litProbs, p->litProbs, (0x300 << dest->lclp) * sizeof(CLzmaProb));
413 CLzmaEnc *p = (CLzmaEnc *)pp;
420 p->dictSize = props.dictSize;
421 p->matchFinderCycles = props.mc;
428 p->numFastBytes = fb;
430 p->lc = props.lc;
431 p->lp = props.lp;
432 p->pb = props.pb;
433 p->fastMode = (props.algo == 0);
434 p->matchFinderBase.btMode = props.btMode;
444 p->matchFinderBase.numHashBytes = numHashBytes;
447 p->matchFinderBase.cutValue = props.mc;
449 p->writeEndMark = props.writeEndMark;
459 p->multiThread = (props.numThreads > 1);
476 static void RangeEnc_Construct(CRangeEnc *p)
478 p->outStream = 0;
479 p->bufBase = 0;
482 #define RangeEnc_GetProcessed(p) ((p)->processed + ((p)->buf - (p)->bufBase) + (p)->cacheSize)
485 static int RangeEnc_Alloc(CRangeEnc *p, ISzAlloc *alloc)
487 if (p->bufBase == 0)
489 p->bufBase = (Byte *)alloc->Alloc(alloc, RC_BUF_SIZE);
490 if (p->bufBase == 0)
492 p->bufLim = p->bufBase + RC_BUF_SIZE;
497 static void RangeEnc_Free(CRangeEnc *p, ISzAlloc *alloc)
499 alloc->Free(alloc, p->bufBase, 0);
500 p->bufBase = 0;
503 static void RangeEnc_Init(CRangeEnc *p)
506 p->low = 0;
507 p->range = 0xFFFFFFFF;
508 p->cacheSize = 1;
509 p->cache = 0;
511 p->buf = p->bufBase;
513 p->processed = 0;
514 p->res = SZ_OK;
517 static void RangeEnc_FlushStream(CRangeEnc *p)
520 if (p->res != SZ_OK)
522 num = p->buf - p->bufBase;
523 if (num != p->outStream->Write(p->outStream, p->bufBase, num))
524 p->res = SZ_ERROR_WRITE;
525 p->processed += num;
526 p->buf = p->bufBase;
529 static void MY_FAST_CALL RangeEnc_ShiftLow(CRangeEnc *p)
531 if ((UInt32)p->low < (UInt32)0xFF000000 || (int)(p->low >> 32) != 0)
533 Byte temp = p->cache;
536 Byte *buf = p->buf;
537 *buf++ = (Byte)(temp + (Byte)(p->low >> 32));
538 p->buf = buf;
539 if (buf == p->bufLim)
540 RangeEnc_FlushStream(p);
543 while (--p->cacheSize != 0);
544 p->cache = (Byte)((UInt32)p->low >> 24);
546 p->cacheSize++;
547 p->low = (UInt32)p->low << 8;
550 static void RangeEnc_FlushData(CRangeEnc *p)
554 RangeEnc_ShiftLow(p);
557 static void RangeEnc_EncodeDirectBits(CRangeEnc *p, UInt32 value, int numBits)
561 p->range >>= 1;
562 p->low += p->range & (0 - ((value >> --numBits) & 1));
563 if (p->range < kTopValue)
565 p->range <<= 8;
566 RangeEnc_ShiftLow(p);
572 static void RangeEnc_EncodeBit(CRangeEnc *p, CLzmaProb *prob, UInt32 symbol)
575 UInt32 newBound = (p->range >> kNumBitModelTotalBits) * ttt;
578 p->range = newBound;
583 p->low += newBound;
584 p->range -= newBound;
588 if (p->range < kTopValue)
590 p->range <<= 8;
591 RangeEnc_ShiftLow(p);
595 static void LitEnc_Encode(CRangeEnc *p, CLzmaProb *probs, UInt32 symbol)
600 RangeEnc_EncodeBit(p, probs + (symbol >> 8), (symbol >> 7) & 1);
606 static void LitEnc_EncodeMatched(CRangeEnc *p, CLzmaProb *probs, UInt32 symbol, UInt32 matchByte)
613 RangeEnc_EncodeBit(p, probs + (offs + (matchByte & offs) + (symbol >> 8)), (symbol >> 7) & 1);
645 p->ProbPrices[((prob) ^ (((-(int)(symbol))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits];
650 #define GET_PRICE_0(prob) p->ProbPrices[(prob) >> kNumMoveReducingBits]
651 #define GET_PRICE_1(prob) p->ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]
741 static void LenEnc_Init(CLenEnc *p)
744 p->choice = p->choice2 = kProbInitValue;
746 p->low[i] = kProbInitValue;
748 p->mid[i] = kProbInitValue;
750 p->high[i] = kProbInitValue;
753 static void LenEnc_Encode(CLenEnc *p, CRangeEnc *rc, UInt32 symbol, UInt32 posState)
757 RangeEnc_EncodeBit(rc, &p->choice, 0);
758 RcTree_Encode(rc, p->low + (posState << kLenNumLowBits), kLenNumLowBits, symbol);
762 RangeEnc_EncodeBit(rc, &p->choice, 1);
765 RangeEnc_EncodeBit(rc, &p->choice2, 0);
766 RcTree_Encode(rc, p->mid + (posState << kLenNumMidBits), kLenNumMidBits, symbol - kLenNumLowSymbols);
770 RangeEnc_EncodeBit(rc, &p->choice2, 1);
771 RcTree_Encode(rc, p->high, kLenNumHighBits, symbol - kLenNumLowSymbols - kLenNumMidSymbols);
776 static void LenEnc_SetPrices(CLenEnc *p, UInt32 posState, UInt32 numSymbols, UInt32 *prices, UInt32 *ProbPrices)
778 UInt32 a0 = GET_PRICE_0a(p->choice);
779 UInt32 a1 = GET_PRICE_1a(p->choice);
780 UInt32 b0 = a1 + GET_PRICE_0a(p->choice2);
781 UInt32 b1 = a1 + GET_PRICE_1a(p->choice2);
787 prices[i] = a0 + RcTree_GetPrice(p->low + (posState << kLenNumLowBits), kLenNumLowBits, i, ProbPrices);
793 prices[i] = b0 + RcTree_GetPrice(p->mid + (posState << kLenNumMidBits), kLenNumMidBits, i - kLenNumLowSymbols, ProbPrices);
796 prices[i] = b1 + RcTree_GetPrice(p->high, kLenNumHighBits, i - kLenNumLowSymbols - kLenNumMidSymbols, ProbPrices);
799 static void MY_FAST_CALL LenPriceEnc_UpdateTable(CLenPriceEnc *p, UInt32 posState, UInt32 *ProbPrices)
801 LenEnc_SetPrices(&p->p, posState, p->tableSize, p->prices[posState], ProbPrices);
802 p->counters[posState] = p->tableSize;
805 static void LenPriceEnc_UpdateTables(CLenPriceEnc *p, UInt32 numPosStates, UInt32 *ProbPrices)
809 LenPriceEnc_UpdateTable(p, posState, ProbPrices);
812 static void LenEnc_Encode2(CLenPriceEnc *p, CRangeEnc *rc, UInt32 symbol, UInt32 posState, Bool updatePrice, UInt32 *ProbPrices)
814 LenEnc_Encode(&p->p, rc, symbol, posState);
816 if (--p->counters[posState] == 0)
817 LenPriceEnc_UpdateTable(p, posState, ProbPrices);
823 static void MovePos(CLzmaEnc *p, UInt32 num)
831 p->additionalOffset += num;
832 p->matchFinder.Skip(p->matchFinderObj, num);
836 static UInt32 ReadMatchDistances(CLzmaEnc *p, UInt32 *numDistancePairsRes)
839 p->numAvail = p->matchFinder.GetNumAvailableBytes(p->matchFinderObj);
840 numPairs = p->matchFinder.GetMatches(p->matchFinderObj, p->matches);
847 printf("%2d %6d | ", p->matches[i], p->matches[i + 1]);
852 lenRes = p->matches[numPairs - 2];
853 if (lenRes == p->numFastBytes)
855 const Byte *pby = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
856 UInt32 distance = p->matches[numPairs - 1] + 1;
857 UInt32 numAvail = p->numAvail;
866 p->additionalOffset++;
872 #define MakeAsChar(p) (p)->backPrev = (UInt32)(-1); (p)->prev1IsChar = False;
873 #define MakeAsShortRep(p) (p)->backPrev = 0; (p)->prev1IsChar = False;
874 #define IsShortRep(p) ((p)->backPrev == 0)
876 static UInt32 GetRepLen1Price(CLzmaEnc *p, UInt32 state, UInt32 posState)
879 GET_PRICE_0(p->isRepG0[state]) +
880 GET_PRICE_0(p->isRep0Long[state][posState]);
883 static UInt32 GetPureRepPrice(CLzmaEnc *p, UInt32 repIndex, UInt32 state, UInt32 posState)
888 price = GET_PRICE_0(p->isRepG0[state]);
889 price += GET_PRICE_1(p->isRep0Long[state][posState]);
893 price = GET_PRICE_1(p->isRepG0[state]);
895 price += GET_PRICE_0(p->isRepG1[state]);
898 price += GET_PRICE_1(p->isRepG1[state]);
899 price += GET_PRICE(p->isRepG2[state], repIndex - 2);
905 static UInt32 GetRepPrice(CLzmaEnc *p, UInt32 repIndex, UInt32 len, UInt32 state, UInt32 posState)
907 return p->repLenEnc.prices[posState][len - LZMA_MATCH_LEN_MIN] +
908 GetPureRepPrice(p, repIndex, state, posState);
911 static UInt32 Backward(CLzmaEnc *p, UInt32 *backRes, UInt32 cur)
913 UInt32 posMem = p->opt[cur].posPrev;
914 UInt32 backMem = p->opt[cur].backPrev;
915 p->optimumEndIndex = cur;
918 if (p->opt[cur].prev1IsChar)
920 MakeAsChar(&p->opt[posMem])
921 p->opt[posMem].posPrev = posMem - 1;
922 if (p->opt[cur].prev2)
924 p->opt[posMem - 1].prev1IsChar = False;
925 p->opt[posMem - 1].posPrev = p->opt[cur].posPrev2;
926 p->opt[posMem - 1].backPrev = p->opt[cur].backPrev2;
933 backMem = p->opt[posPrev].backPrev;
934 posMem = p->opt[posPrev].posPrev;
936 p->opt[posPrev].backPrev = backCur;
937 p->opt[posPrev].posPrev = cur;
942 *backRes = p->opt[0].backPrev;
943 p->optimumCurrentIndex = p->opt[0].posPrev;
944 return p->optimumCurrentIndex;
947 #define LIT_PROBS(pos, prevByte) (p->litProbs + ((((pos) & p->lpMask) << p->lc) + ((prevByte) >> (8 - p->lc))) * 0x300)
949 static UInt32 GetOptimum(CLzmaEnc *p, UInt32 position, UInt32 *backRes)
957 if (p->optimumEndIndex != p->optimumCurrentIndex)
959 const COptimal *opt = &p->opt[p->optimumCurrentIndex];
960 UInt32 lenRes = opt->posPrev - p->optimumCurrentIndex;
962 p->optimumCurrentIndex = opt->posPrev;
965 p->optimumCurrentIndex = p->optimumEndIndex = 0;
967 if (p->additionalOffset == 0)
968 mainLen = ReadMatchDistances(p, &numPairs);
971 mainLen = p->longestMatchLength;
972 numPairs = p->numPairs;
975 numAvail = p->numAvail;
984 data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
990 reps[i] = p->reps[i];
1002 if (repLens[repMaxIndex] >= p->numFastBytes)
1007 MovePos(p, lenRes - 1);
1011 matches = p->matches;
1012 if (mainLen >= p->numFastBytes)
1015 MovePos(p, mainLen - 1);
1027 p->opt[0].state = (CState)p->state;
1029 posState = (position & p->pbMask);
1033 p->opt[1].price = GET_PRICE_0(p->isMatch[p->state][posState]) +
1034 (!IsCharState(p->state) ?
1035 LitEnc_GetPriceMatched(probs, curByte, matchByte, p->ProbPrices) :
1036 LitEnc_GetPrice(probs, curByte, p->ProbPrices));
1039 MakeAsChar(&p->opt[1]);
1041 matchPrice = GET_PRICE_1(p->isMatch[p->state][posState]);
1042 repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[p->state]);
1046 UInt32 shortRepPrice = repMatchPrice + GetRepLen1Price(p, p->state, posState);
1047 if (shortRepPrice < p->opt[1].price)
1049 p->opt[1].price = shortRepPrice;
1050 MakeAsShortRep(&p->opt[1]);
1057 *backRes = p->opt[1].backPrev;
1061 p->opt[1].posPrev = 0;
1063 p->opt[0].backs[i] = reps[i];
1067 p->opt[len--].price = kInfinityPrice;
1076 price = repMatchPrice + GetPureRepPrice(p, i, p->state, posState);
1079 UInt32 curAndLenPrice = price + p->repLenEnc.prices[posState][repLen - 2];
1080 COptimal *opt = &p->opt[repLen];
1092 normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[p->state]);
1105 UInt32 curAndLenPrice = normalMatchPrice + p->lenEnc.prices[posState][len - LZMA_MATCH_LEN_MIN];
1108 curAndLenPrice += p->distancesPrices[lenToPosState][distance];
1113 curAndLenPrice += p->alignPrices[distance & kAlignMask] + p->posSlotPrices[lenToPosState][slot];
1115 opt = &p->opt[len];
1140 printf("\nprice[%4X] = %d", position - cur + i, p->opt[i].price);
1156 return Backward(p, backRes, cur);
1158 newLen = ReadMatchDistances(p, &numPairs);
1159 if (newLen >= p->numFastBytes)
1161 p->numPairs = numPairs;
1162 p->longestMatchLength = newLen;
1163 return Backward(p, backRes, cur);
1166 curOpt = &p->opt[cur];
1173 state = p->opt[curOpt->posPrev2].state;
1180 state = p->opt[posPrev].state;
1184 state = p->opt[posPrev].state;
1210 prevOpt = &p->opt[posPrev];
1237 data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
1241 posState = (position & p->pbMask);
1243 curAnd1Price = curPrice + GET_PRICE_0(p->isMatch[state][posState]);
1248 LitEnc_GetPriceMatched(probs, curByte, matchByte, p->ProbPrices) :
1249 LitEnc_GetPrice(probs, curByte, p->ProbPrices));
1252 nextOpt = &p->opt[cur + 1];
1262 matchPrice = curPrice + GET_PRICE_1(p->isMatch[state][posState]);
1263 repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[state]);
1267 UInt32 shortRepPrice = repMatchPrice + GetRepLen1Price(p, state, posState);
1276 numAvailFull = p->numAvail;
1285 numAvail = (numAvailFull <= p->numFastBytes ? numAvailFull : p->numFastBytes);
1293 UInt32 limit = p->numFastBytes + 1;
1302 UInt32 posStateNext = (position + 1) & p->pbMask;
1304 GET_PRICE_1(p->isMatch[state2][posStateNext]) +
1305 GET_PRICE_1(p->isRep[state2]);
1312 p->opt[++lenEnd].price = kInfinityPrice;
1313 curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
1314 opt = &p->opt[offset];
1340 p->opt[++lenEnd].price = kInfinityPrice;
1342 price = repMatchPrice + GetPureRepPrice(p, repIndex, state, posState);
1345 UInt32 curAndLenPrice = price + p->repLenEnc.prices[posState][lenTest - 2];
1346 COptimal *opt = &p->opt[cur + lenTest];
1364 UInt32 limit = lenTest2 + p->numFastBytes;
1373 UInt32 posStateNext = (position + lenTest) & p->pbMask;
1375 price + p->repLenEnc.prices[posState][lenTest - 2] +
1376 GET_PRICE_0(p->isMatch[state2][posStateNext]) +
1378 data[lenTest], data2[lenTest], p->ProbPrices);
1380 posStateNext = (position + lenTest + 1) & p->pbMask;
1382 GET_PRICE_1(p->isMatch[state2][posStateNext]) +
1383 GET_PRICE_1(p->isRep[state2]);
1391 p->opt[++lenEnd].price = kInfinityPrice;
1392 curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
1393 opt = &p->opt[offset];
1419 UInt32 normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[state]);
1423 p->opt[++lenEnd].price = kInfinityPrice;
1432 UInt32 curAndLenPrice = normalMatchPrice + p->lenEnc.prices[posState][lenTest - LZMA_MATCH_LEN_MIN];
1436 curAndLenPrice += p->distancesPrices[lenToPosState][curBack];
1438 curAndLenPrice += p->posSlotPrices[lenToPosState][posSlot] + p->alignPrices[curBack & kAlignMask];
1440 opt = &p->opt[cur + lenTest];
1454 UInt32 limit = lenTest2 + p->numFastBytes;
1463 UInt32 posStateNext = (position + lenTest) & p->pbMask;
1465 GET_PRICE_0(p->isMatch[state2][posStateNext]) +
1467 data[lenTest], data2[lenTest], p->ProbPrices);
1469 posStateNext = (posStateNext + 1) & p->pbMask;
1471 GET_PRICE_1(p->isMatch[state2][posStateNext]) +
1472 GET_PRICE_1(p->isRep[state2]);
1480 p->opt[++lenEnd].price = kInfinityPrice;
1481 curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
1482 opt = &p->opt[offset];
1509 static UInt32 GetOptimumFast(CLzmaEnc *p, UInt32 *backRes)
1515 if (p->additionalOffset == 0)
1516 mainLen = ReadMatchDistances(p, &numPairs);
1519 mainLen = p->longestMatchLength;
1520 numPairs = p->numPairs;
1523 numAvail = p->numAvail;
1529 data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
1535 const Byte *data2 = data - (p->reps[i] + 1);
1539 if (len >= p->numFastBytes)
1542 MovePos(p, len - 1);
1552 matches = p->matches;
1553 if (mainLen >= p->numFastBytes)
1556 MovePos(p, mainLen - 1);
1582 MovePos(p, repLen - 1);
1589 p->longestMatchLength = ReadMatchDistances(p, &p->numPairs);
1590 if (p->longestMatchLength >= 2)
1592 UInt32 newDistance = matches[p->numPairs - 1];
1593 if ((p->longestMatchLength >= mainLen && newDistance < mainDist) ||
1594 (p->longestMatchLength == mainLen + 1 && !ChangePair(mainDist, newDistance)) ||
1595 (p->longestMatchLength > mainLen + 1) ||
1596 (p->longestMatchLength + 1 >= mainLen && mainLen >= 3 && ChangePair(newDistance, mainDist)))
1600 data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
1604 const Byte *data2 = data - (p->reps[i] + 1);
1613 MovePos(p, mainLen - 2);
1617 static void WriteEndMarker(CLzmaEnc *p, UInt32 posState)
1620 RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 1);
1621 RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 0);
1622 p->state = kMatchNextStates[p->state];
1624 LenEnc_Encode2(&p->lenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
1625 RcTree_Encode(&p->rc, p->posSlotEncoder[GetLenToPosState(len)], kNumPosSlotBits, (1 << kNumPosSlotBits) - 1);
1626 RangeEnc_EncodeDirectBits(&p->rc, (((UInt32)1 << 30) - 1) >> kNumAlignBits, 30 - kNumAlignBits);
1627 RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, kAlignMask);
1630 static SRes CheckErrors(CLzmaEnc *p)
1632 if (p->result != SZ_OK)
1633 return p->result;
1634 if (p->rc.res != SZ_OK)
1635 p->result = SZ_ERROR_WRITE;
1636 if (p->matchFinderBase.result != SZ_OK)
1637 p->result = SZ_ERROR_READ;
1638 if (p->result != SZ_OK)
1639 p->finished = True;
1640 return p->result;
1643 static SRes Flush(CLzmaEnc *p, UInt32 nowPos)
1646 p->finished = True;
1647 if (p->writeEndMark)
1648 WriteEndMarker(p, nowPos & p->pbMask);
1649 RangeEnc_FlushData(&p->rc);
1650 RangeEnc_FlushStream(&p->rc);
1651 return CheckErrors(p);
1654 static void FillAlignPrices(CLzmaEnc *p)
1658 p->alignPrices[i] = RcTree_ReverseGetPrice(p->posAlignEncoder, kNumAlignBits, i, p->ProbPrices);
1659 p->alignPriceCount = 0;
1662 static void FillDistancesPrices(CLzmaEnc *p)
1671 tempPrices[i] = RcTree_ReverseGetPrice(p->posEncoders + base - posSlot - 1, footerBits, i - base, p->ProbPrices);
1677 const CLzmaProb *encoder = p->posSlotEncoder[lenToPosState];
1678 UInt32 *posSlotPrices = p->posSlotPrices[lenToPosState];
1679 for (posSlot = 0; posSlot < p->distTableSize; posSlot++)
1680 posSlotPrices[posSlot] = RcTree_GetPrice(encoder, kNumPosSlotBits, posSlot, p->ProbPrices);
1681 for (posSlot = kEndPosModelIndex; posSlot < p->distTableSize; posSlot++)
1685 UInt32 *distancesPrices = p->distancesPrices[lenToPosState];
1693 p->matchPriceCount = 0;
1696 void LzmaEnc_Construct(CLzmaEnc *p)
1698 RangeEnc_Construct(&p->rc);
1699 MatchFinder_Construct(&p->matchFinderBase);
1701 MatchFinderMt_Construct(&p->matchFinderMt);
1702 p->matchFinderMt.MatchFinder = &p->matchFinderBase;
1708 LzmaEnc_SetProps(p, &props);
1712 LzmaEnc_FastPosInit(p->g_FastPos);
1715 LzmaEnc_InitPriceTables(p->ProbPrices);
1716 p->litProbs = 0;
1717 p->saveState.litProbs = 0;
1722 void *p;
1723 p = alloc->Alloc(alloc, sizeof(CLzmaEnc));
1724 if (p != 0)
1725 LzmaEnc_Construct((CLzmaEnc *)p);
1726 return p;
1729 void LzmaEnc_FreeLits(CLzmaEnc *p, ISzAlloc *alloc)
1731 alloc->Free(alloc, p->litProbs, 0);
1732 alloc->Free(alloc, p->saveState.litProbs, 0);
1733 p->litProbs = 0;
1734 p->saveState.litProbs = 0;
1737 void LzmaEnc_Destruct(CLzmaEnc *p, ISzAlloc *alloc, ISzAlloc *allocBig)
1740 MatchFinderMt_Destruct(&p->matchFinderMt, allocBig);
1742 MatchFinder_Free(&p->matchFinderBase, allocBig);
1743 LzmaEnc_FreeLits(p, alloc);
1744 RangeEnc_Free(&p->rc, alloc);
1747 void LzmaEnc_Destroy(CLzmaEncHandle p, ISzAlloc *alloc, ISzAlloc *allocBig)
1749 LzmaEnc_Destruct((CLzmaEnc *)p, alloc, allocBig);
1750 alloc->Free(alloc, p, 0);
1753 static SRes LzmaEnc_CodeOneBlock(CLzmaEnc *p, Bool useLimits, UInt32 maxPackSize, UInt32 maxUnpackSize)
1756 if (p->inStream != 0)
1758 p->matchFinderBase.stream = p->inStream;
1759 p->matchFinder.Init(p->matchFinderObj);
1760 p->inStream = 0;
1763 if (p->finished)
1764 return p->result;
1765 RINOK(CheckErrors(p));
1767 nowPos32 = (UInt32)p->nowPos64;
1770 if (p->nowPos64 == 0)
1774 if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0)
1775 return Flush(p, nowPos32);
1776 ReadMatchDistances(p, &numPairs);
1777 RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][0], 0);
1778 p->state = kLiteralNextStates[p->state];
1779 curByte = p->matchFinder.GetIndexByte(p->matchFinderObj, 0 - p->additionalOffset);
1780 LitEnc_Encode(&p->rc, p->litProbs, curByte);
1781 p->additionalOffset--;
1785 if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) != 0)
1790 if (p->fastMode)
1791 len = GetOptimumFast(p, &pos);
1793 len = GetOptimum(p, nowPos32, &pos);
1799 posState = nowPos32 & p->pbMask;
1806 RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 0);
1807 data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset;
1810 if (IsCharState(p->state))
1811 LitEnc_Encode(&p->rc, probs, curByte);
1813 LitEnc_EncodeMatched(&p->rc, probs, curByte, *(data - p->reps[0] - 1));
1814 p->state = kLiteralNextStates[p->state];
1818 RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 1);
1821 RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 1);
1824 RangeEnc_EncodeBit(&p->rc, &p->isRepG0[p->state], 0);
1825 RangeEnc_EncodeBit(&p->rc, &p->isRep0Long[p->state][posState], ((len == 1) ? 0 : 1));
1829 UInt32 distance = p->reps[pos];
1830 RangeEnc_EncodeBit(&p->rc, &p->isRepG0[p->state], 1);
1832 RangeEnc_EncodeBit(&p->rc, &p->isRepG1[p->state], 0);
1835 RangeEnc_EncodeBit(&p->rc, &p->isRepG1[p->state], 1);
1836 RangeEnc_EncodeBit(&p->rc, &p->isRepG2[p->state], pos - 2);
1838 p->reps[3] = p->reps[2];
1839 p->reps[2] = p->reps[1];
1841 p->reps[1] = p->reps[0];
1842 p->reps[0] = distance;
1845 p->state = kShortRepNextStates[p->state];
1848 LenEnc_Encode2(&p->repLenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
1849 p->state = kRepNextStates[p->state];
1855 RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 0);
1856 p->state = kMatchNextStates[p->state];
1857 LenEnc_Encode2(&p->lenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
1860 RcTree_Encode(&p->rc, p->posSlotEncoder[GetLenToPosState(len)], kNumPosSlotBits, posSlot);
1869 RcTree_ReverseEncode(&p->rc, p->posEncoders + base - posSlot - 1, footerBits, posReduced);
1872 RangeEnc_EncodeDirectBits(&p->rc, posReduced >> kNumAlignBits, footerBits - kNumAlignBits);
1873 RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, posReduced & kAlignMask);
1874 p->alignPriceCount++;
1877 p->reps[3] = p->reps[2];
1878 p->reps[2] = p->reps[1];
1879 p->reps[1] = p->reps[0];
1880 p->reps[0] = pos;
1881 p->matchPriceCount++;
1884 p->additionalOffset -= len;
1886 if (p->additionalOffset == 0)
1889 if (!p->fastMode)
1891 if (p->matchPriceCount >= (1 << 7))
1892 FillDistancesPrices(p);
1893 if (p->alignPriceCount >= kAlignTableSize)
1894 FillAlignPrices(p);
1896 if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0)
1902 RangeEnc_GetProcessed(&p->rc) + kNumOpts * 2 >= maxPackSize)
1907 p->nowPos64 += nowPos32 - startPos32;
1908 return CheckErrors(p);
1912 p->nowPos64 += nowPos32 - startPos32;
1913 return Flush(p, nowPos32);
1918 static SRes LzmaEnc_Alloc(CLzmaEnc *p, UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig)
1922 if (!RangeEnc_Alloc(&p->rc, alloc))
1924 btMode = (p->matchFinderBase.btMode != 0);
1926 p->mtMode = (p->multiThread && !p->fastMode && btMode);
1930 unsigned lclp = p->lc + p->lp;
1931 if (p->litProbs == 0 || p->saveState.litProbs == 0 || p->lclp != lclp)
1933 LzmaEnc_FreeLits(p, alloc);
1934 p->litProbs = (CLzmaProb *)alloc->Alloc(alloc, (0x300 << lclp) * sizeof(CLzmaProb));
1935 p->saveState.litProbs = (CLzmaProb *)alloc->Alloc(alloc, (0x300 << lclp) * sizeof(CLzmaProb));
1936 if (p->litProbs == 0 || p->saveState.litProbs == 0)
1938 LzmaEnc_FreeLits(p, alloc);
1941 p->lclp = lclp;
1945 p->matchFinderBase.bigHash = (p->dictSize > kBigHashDicLimit);
1947 if (beforeSize + p->dictSize < keepWindowSize)
1948 beforeSize = keepWindowSize - p->dictSize;
1951 if (p->mtMode)
1953 RINOK(MatchFinderMt_Create(&p->matchFinderMt, p->dictSize, beforeSize, p->numFastBytes, LZMA_MATCH_LEN_MAX, allocBig));
1954 p->matchFinderObj = &p->matchFinderMt;
1955 MatchFinderMt_CreateVTable(&p->matchFinderMt, &p->matchFinder);
1960 if (!MatchFinder_Create(&p->matchFinderBase, p->dictSize, beforeSize, p->numFastBytes, LZMA_MATCH_LEN_MAX, allocBig))
1962 p->matchFinderObj = &p->matchFinderBase;
1963 MatchFinder_CreateVTable(&p->matchFinderBase, &p->matchFinder);
1968 void LzmaEnc_Init(CLzmaEnc *p)
1971 p->state = 0;
1973 p->reps[i] = 0;
1975 RangeEnc_Init(&p->rc);
1983 p->isMatch[i][j] = kProbInitValue;
1984 p->isRep0Long[i][j] = kProbInitValue;
1986 p->isRep[i] = kProbInitValue;
1987 p->isRepG0[i] = kProbInitValue;
1988 p->isRepG1[i] = kProbInitValue;
1989 p->isRepG2[i] = kProbInitValue;
1993 UInt32 num = 0x300 << (p->lp + p->lc);
1995 p->litProbs[i] = kProbInitValue;
2001 CLzmaProb *probs = p->posSlotEncoder[i];
2009 p->posEncoders[i] = kProbInitValue;
2012 LenEnc_Init(&p->lenEnc.p);
2013 LenEnc_Init(&p->repLenEnc.p);
2016 p->posAlignEncoder[i] = kProbInitValue;
2018 p->optimumEndIndex = 0;
2019 p->optimumCurrentIndex = 0;
2020 p->additionalOffset = 0;
2022 p->pbMask = (1 << p->pb) - 1;
2023 p->lpMask = (1 << p->lp) - 1;
2026 void LzmaEnc_InitPrices(CLzmaEnc *p)
2028 if (!p->fastMode)
2030 FillDistancesPrices(p);
2031 FillAlignPrices(p);
2034 p->lenEnc.tableSize =
2035 p->repLenEnc.tableSize =
2036 p->numFastBytes + 1 - LZMA_MATCH_LEN_MIN;
2037 LenPriceEnc_UpdateTables(&p->lenEnc, 1 << p->pb, p->ProbPrices);
2038 LenPriceEnc_UpdateTables(&p->repLenEnc, 1 << p->pb, p->ProbPrices);
2041 static SRes LzmaEnc_AllocAndInit(CLzmaEnc *p, UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig)
2045 if (p->dictSize <= ((UInt32)1 << i))
2047 p->distTableSize = i * 2;
2049 p->finished = False;
2050 p->result = SZ_OK;
2051 RINOK(LzmaEnc_Alloc(p, keepWindowSize, alloc, allocBig));
2052 LzmaEnc_Init(p);
2053 LzmaEnc_InitPrices(p);
2054 p->nowPos64 = 0;
2061 CLzmaEnc *p = (CLzmaEnc *)pp;
2062 p->inStream = inStream;
2063 p->rc.outStream = outStream;
2064 return LzmaEnc_AllocAndInit(p, 0, alloc, allocBig);
2071 CLzmaEnc *p = (CLzmaEnc *)pp;
2072 p->inStream = inStream;
2073 return LzmaEnc_AllocAndInit(p, keepWindowSize, alloc, allocBig);
2076 static void LzmaEnc_SetInputBuf(CLzmaEnc *p, const Byte *src, SizeT srcLen)
2078 p->seqBufInStream.funcTable.Read = MyRead;
2079 p->seqBufInStream.data = src;
2080 p->seqBufInStream.rem = srcLen;
2086 CLzmaEnc *p = (CLzmaEnc *)pp;
2087 LzmaEnc_SetInputBuf(p, src, srcLen);
2088 p->inStream = &p->seqBufInStream.funcTable;
2089 return LzmaEnc_AllocAndInit(p, keepWindowSize, alloc, allocBig);
2095 CLzmaEnc *p = (CLzmaEnc *)pp;
2096 if (p->mtMode)
2097 MatchFinderMt_ReleaseStream(&p->matchFinderMt);
2113 CSeqOutStreamBuf *p = (CSeqOutStreamBuf *)pp;
2114 if (p->rem < size)
2116 size = p->rem;
2117 p->overflow = True;
2119 memcpy(p->data, data, size);
2120 p->rem -= size;
2121 p->data += size;
2128 const CLzmaEnc *p = (CLzmaEnc *)pp;
2129 return p->matchFinder.GetNumAvailableBytes(p->matchFinderObj);
2134 const CLzmaEnc *p = (CLzmaEnc *)pp;
2135 return p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset;
2141 CLzmaEnc *p = (CLzmaEnc *)pp;
2151 p->writeEndMark = False;
2152 p->finished = False;
2153 p->result = SZ_OK;
2156 LzmaEnc_Init(p);
2157 LzmaEnc_InitPrices(p);
2158 nowPos64 = p->nowPos64;
2159 RangeEnc_Init(&p->rc);
2160 p->rc.outStream = &outStream.funcTable;
2162 res = LzmaEnc_CodeOneBlock(p, True, desiredPackSize, *unpackSize);
2164 *unpackSize = (UInt32)(p->nowPos64 - nowPos64);
2175 CLzmaEnc *p = (CLzmaEnc *)pp;
2189 res = LzmaEnc_CodeOneBlock(p, False, 0, 0);
2190 if (res != SZ_OK || p->finished != 0)
2194 res = progress->Progress(progress, p->nowPos64, RangeEnc_GetProcessed(&p->rc));
2208 CLzmaEnc *p = (CLzmaEnc *)pp;
2210 UInt32 dictSize = p->dictSize;
2214 props[0] = (Byte)((p->pb * 5 + p->lp) * 9 + p->lc);
2239 CLzmaEnc *p = (CLzmaEnc *)pp;
2243 LzmaEnc_SetInputBuf(p, src, srcLen);
2250 p->writeEndMark = writeEndMark;
2251 res = LzmaEnc_Encode(pp, &outStream.funcTable, &p->seqBufInStream.funcTable,
2264 CLzmaEnc *p = (CLzmaEnc *)LzmaEnc_Create(alloc);
2266 if (p == 0)
2269 res = LzmaEnc_SetProps(p, props);
2272 res = LzmaEnc_WriteProperties(p, propsEncoded, propsSize);
2274 res = LzmaEnc_MemEncode(p, dest, destLen, src, srcLen,
2278 LzmaEnc_Destroy(p, alloc, allocBig);