?? lzmaenc.c
字號:
{
UInt32 state2 = kRepNextStates[state];
UInt32 posStateNext = (position + lenTest) & p->pbMask;
UInt32 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 curAndLenPrice;
COptimal *opt;
UInt32 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 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 normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[state]);
UInt32 offs, curBack, posSlot;
UInt32 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 curAndLenPrice = normalMatchPrice + p->lenEnc.prices[posState][lenTest - LZMA_MATCH_LEN_MIN];
UInt32 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 Byte *data2 = data - (curBack + 1);
UInt32 lenTest2 = lenTest + 1;
UInt32 limit = lenTest2 + p->numFastBytes;
UInt32 nextRepMatchPrice;
if (limit > numAvailFull)
limit = numAvailFull;
for (; lenTest2 < limit && data[lenTest2] == data2[lenTest2]; lenTest2++);
lenTest2 -= lenTest + 1;
if (lenTest2 >= 2)
{
UInt32 state2 = kMatchNextStates[state];
UInt32 posStateNext = (position + lenTest) & p->pbMask;
UInt32 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 offset = cur + lenTest + 1 + lenTest2;
UInt32 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 GetOptimumFast(CLzmaEnc *p, UInt32 *backRes)
{
UInt32 numAvail, mainLen, mainDist, numPairs, repIndex, repLen, i;
const Byte *data;
const UInt32 *matches;
if (p->additionalOffset == 0)
mainLen = ReadMatchDistances(p, &numPairs);
else
{
mainLen = p->longestMatchLength;
numPairs = p->numPairs;
}
numAvail = p->numAvail;
*backRes = (UInt32)-1;
if (numAvail < 2)
return 1;
if (numAvail > LZMA_MATCH_LEN_MAX)
numAvail = LZMA_MATCH_LEN_MAX;
data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
repLen = repIndex = 0;
for (i = 0; i < LZMA_NUM_REPS; i++)
{
UInt32 len;
const Byte *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 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 = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
for (i = 0; i < LZMA_NUM_REPS; i++)
{
UInt32 len, limit;
const Byte *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 WriteEndMarker(CLzmaEnc *p, UInt32 posState)
{
UInt32 len;
RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 1);
RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 0);
p->state = kMatchNextStates[p->state];
len = LZMA_MATCH_LEN_MIN;
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)1 << 30) - 1) >> kNumAlignBits, 30 - kNumAlignBits);
RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, kAlignMask);
}
static SRes 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 SRes Flush(CLzmaEnc *p, UInt32 nowPos)
{
/* ReleaseMFStream(); */
p->finished = True;
if (p->writeEndMark)
WriteEndMarker(p, nowPos & p->pbMask);
RangeEnc_FlushData(&p->rc);
RangeEnc_FlushStream(&p->rc);
return CheckErrors(p);
}
static void FillAlignPrices(CLzmaEnc *p)
{
UInt32 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 tempPrices[kNumFullDistances];
UInt32 i, lenToPosState;
for (i = kStartPosModelIndex; i < kNumFullDistances; i++)
{
UInt32 posSlot = GetPosSlot1(i);
UInt32 footerBits = ((posSlot >> 1) - 1);
UInt32 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 posSlot;
const CLzmaProb *encoder = p->posSlotEncoder[lenToPosState];
UInt32 *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 *distancesPrices = p->distancesPrices[lenToPosState];
UInt32 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 void LzmaEnc_Construct(CLzmaEnc *p)
{
RangeEnc_Construct(&p->rc);
MatchFinder_Construct(&p->matchFinderBase);
#ifdef COMPRESS_MF_MT
MatchFinderMt_Construct(&p->matchFinderMt);
p->matchFinderMt.MatchFinder = &p->matchFinderBase;
#endif
{
CLzmaEncProps props;
LzmaEncProps_Init(&props);
LzmaEnc_SetProps(p, &props);
}
#ifndef LZMA_LOG_BSR
LzmaEnc_FastPosInit(p->g_FastPos);
#endif
LzmaEnc_InitPriceTables(p->ProbPrices);
p->litProbs = 0;
p->saveState.litProbs = 0;
}
CLzmaEncHandle LzmaEnc_Create(ISzAlloc *alloc)
{
void *p;
p = alloc->Alloc(alloc, sizeof(CLzmaEnc));
if (p != 0)
LzmaEnc_Construct((CLzmaEnc *)p);
return p;
}
static void LzmaEnc_FreeLits(CLzmaEnc *p, ISzAlloc *alloc)
{
alloc->Free(alloc, p->litProbs);
alloc->Free(alloc, p->saveState.litProbs);
p->litProbs = 0;
p->saveState.litProbs = 0;
}
static void LzmaEnc_Destruct(CLzmaEnc *p, ISzAlloc *alloc, ISzAlloc *allocBig)
{
#ifdef COMPRESS_MF_MT
MatchFinderMt_Destruct(&p->matchFinderMt, allocBig);
#endif
MatchFinder_Free(&p->matchFinderBase, allocBig);
LzmaEnc_FreeLits(p, alloc);
RangeEnc_Free(&p->rc, alloc);
}
void LzmaEnc_Destroy(CLzmaEncHandle p, ISzAlloc *alloc, ISzAlloc *allocBig)
{
LzmaEnc_Destruct((CLzmaEnc *)p, alloc, allocBig);
alloc->Free(alloc, p);
}
static SRes LzmaEnc_CodeOneBlock(CLzmaEnc *p, Bool useLimits, UInt32 maxPackSize, UInt32 maxUnpackSize)
{
UInt32 nowPos32, startPos32;
if (p->inStream != 0)
{
p->matchFinderBase.stream = p->inStream;
p->matchFinder.Init(p->matchFinderObj);
p->inStream = 0;
}
if (p->finished)
return p->result;
RINOK(CheckErrors(p));
nowPos32 = (UInt32)p->nowPos64;
startPos32 = nowPos32;
if (p->nowPos64 == 0)
{
UInt32 numPairs;
Byte curByte;
if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0)
return Flush(p, nowPos32);
ReadMatchDistances(p, &numPairs);
p->state = kLiteralNextStates[p->state];
curByte = p->matchFinder.GetIndexByte(p->matchFinderObj, 0 - p->additionalOffset);
LitEnc_Encode(&p->rc, p->litProbs, curByte);
p->additionalOffset--;
nowPos32++;
}
if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) != 0)
for (;;)
{
UInt32 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)-1)
{
Byte curByte;
CLzmaProb *probs;
const Byte *data;
RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 0);
data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 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
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -