1 /* LzmaEnc.c -- LZMA Encoder
2 2010-04-16 : Igor Pavlov : Public domain */
6 /* #define SHOW_STAT */
7 /* #define SHOW_STAT2 */
9 #if defined(SHOW_STAT) || defined(SHOW_STAT2)
24 #define kBlockSizeMax ((1 << LZMA_NUM_BLOCK_SIZE_BITS) - 1)
26 #define kBlockSize (9 << 10)
27 #define kUnpackBlockSize (1 << 18)
28 #define kMatchArraySize (1 << 21)
29 #define kMatchRecordMaxSize ((LZMA_MATCH_LEN_MAX * 2 + 3) * LZMA_MATCH_LEN_MAX)
31 #define kNumMaxDirectBits (31)
33 #define kNumTopBits 24
34 #define kTopValue ((UInt32)1 << kNumTopBits)
36 #define kNumBitModelTotalBits 11
37 #define kBitModelTotal (1 << kNumBitModelTotalBits)
38 #define kNumMoveBits 5
39 #define kProbInitValue (kBitModelTotal >> 1)
41 #define kNumMoveReducingBits 4
42 #define kNumBitPriceShiftBits 4
43 #define kBitPrice (1 << kNumBitPriceShiftBits)
45 void LzmaEncProps_Init(CLzmaEncProps
*p
)
48 p
->dictSize
= p
->mc
= 0;
49 p
->lc
= p
->lp
= p
->pb
= p
->algo
= p
->fb
= p
->btMode
= p
->numHashBytes
= p
->numThreads
= -1;
53 void LzmaEncProps_Normalize(CLzmaEncProps
*p
)
56 if (level
< 0) level
= 5;
58 if (p
->dictSize
== 0) p
->dictSize
= (level
<= 5 ? (1 << (level
* 2 + 14)) : (level
== 6 ? (1 << 25) : (1 << 26)));
59 if (p
->lc
< 0) p
->lc
= 3;
60 if (p
->lp
< 0) p
->lp
= 0;
61 if (p
->pb
< 0) p
->pb
= 2;
62 if (p
->algo
< 0) p
->algo
= (level
< 5 ? 0 : 1);
63 if (p
->fb
< 0) p
->fb
= (level
< 7 ? 32 : 64);
64 if (p
->btMode
< 0) p
->btMode
= (p
->algo
== 0 ? 0 : 1);
65 if (p
->numHashBytes
< 0) p
->numHashBytes
= 4;
66 if (p
->mc
== 0) p
->mc
= (16 + (p
->fb
>> 1)) >> (p
->btMode
? 0 : 1);
67 if (p
->numThreads
< 0)
70 ((p
->btMode
&& p
->algo
) ? 2 : 1);
76 UInt32
LzmaEncProps_GetDictSize(const CLzmaEncProps
*props2
)
78 CLzmaEncProps props
= *props2
;
79 LzmaEncProps_Normalize(&props
);
80 return props
.dictSize
;
83 /* #define LZMA_LOG_BSR */
84 /* Define it for Intel's CPU */
89 #define kDicLogSizeMaxCompress 30
91 #define BSR2_RET(pos, res) { unsigned long i; _BitScanReverse(&i, (pos)); res = (i + i) + ((pos >> (i - 1)) & 1); }
93 UInt32
GetPosSlot1(UInt32 pos
)
99 #define GetPosSlot2(pos, res) { BSR2_RET(pos, res); }
100 #define GetPosSlot(pos, res) { if (pos < 2) res = pos; else BSR2_RET(pos, res); }
104 #define kNumLogBits (9 + (int)sizeof(size_t) / 2)
105 #define kDicLogSizeMaxCompress ((kNumLogBits - 1) * 2 + 7)
107 void LzmaEnc_FastPosInit(Byte
*g_FastPos
)
113 for (slotFast
= 2; slotFast
< kNumLogBits
* 2; slotFast
++)
115 UInt32 k
= (1 << ((slotFast
>> 1) - 1));
117 for (j
= 0; j
< k
; j
++, c
++)
118 g_FastPos
[c
] = (Byte
)slotFast
;
122 #define BSR2_RET(pos, res) { UInt32 i = 6 + ((kNumLogBits - 1) & \
123 (0 - (((((UInt32)1 << (kNumLogBits + 6)) - 1) - pos) >> 31))); \
124 res = p->g_FastPos[pos >> i] + (i * 2); }
126 #define BSR2_RET(pos, res) { res = (pos < (1 << (kNumLogBits + 6))) ? \
127 p->g_FastPos[pos >> 6] + 12 : \
128 p->g_FastPos[pos >> (6 + kNumLogBits - 1)] + (6 + (kNumLogBits - 1)) * 2; }
131 #define GetPosSlot1(pos) p->g_FastPos[pos]
132 #define GetPosSlot2(pos, res) { BSR2_RET(pos, res); }
133 #define GetPosSlot(pos, res) { if (pos < kNumFullDistances) res = p->g_FastPos[pos]; else BSR2_RET(pos, res); }
138 #define LZMA_NUM_REPS 4
140 typedef unsigned CState
;
155 UInt32 backs
[LZMA_NUM_REPS
];
158 #define kNumOpts (1 << 12)
160 #define kNumLenToPosStates 4
161 #define kNumPosSlotBits 6
162 #define kDicLogSizeMin 0
163 #define kDicLogSizeMax 32
164 #define kDistTableSizeMax (kDicLogSizeMax * 2)
167 #define kNumAlignBits 4
168 #define kAlignTableSize (1 << kNumAlignBits)
169 #define kAlignMask (kAlignTableSize - 1)
171 #define kStartPosModelIndex 4
172 #define kEndPosModelIndex 14
173 #define kNumPosModels (kEndPosModelIndex - kStartPosModelIndex)
175 #define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
178 #define CLzmaProb UInt32
180 #define CLzmaProb UInt16
183 #define LZMA_PB_MAX 4
184 #define LZMA_LC_MAX 8
185 #define LZMA_LP_MAX 4
187 #define LZMA_NUM_PB_STATES_MAX (1 << LZMA_PB_MAX)
190 #define kLenNumLowBits 3
191 #define kLenNumLowSymbols (1 << kLenNumLowBits)
192 #define kLenNumMidBits 3
193 #define kLenNumMidSymbols (1 << kLenNumMidBits)
194 #define kLenNumHighBits 8
195 #define kLenNumHighSymbols (1 << kLenNumHighBits)
197 #define kLenNumSymbolsTotal (kLenNumLowSymbols + kLenNumMidSymbols + kLenNumHighSymbols)
199 #define LZMA_MATCH_LEN_MIN 2
200 #define LZMA_MATCH_LEN_MAX (LZMA_MATCH_LEN_MIN + kLenNumSymbolsTotal - 1)
202 #define kNumStates 12
208 CLzmaProb low
[LZMA_NUM_PB_STATES_MAX
<< kLenNumLowBits
];
209 CLzmaProb mid
[LZMA_NUM_PB_STATES_MAX
<< kLenNumMidBits
];
210 CLzmaProb high
[kLenNumHighSymbols
];
216 UInt32 prices
[LZMA_NUM_PB_STATES_MAX
][kLenNumSymbolsTotal
];
218 UInt32 counters
[LZMA_NUM_PB_STATES_MAX
];
230 ISeqOutStream
*outStream
;
239 CLzmaProb isMatch
[kNumStates
][LZMA_NUM_PB_STATES_MAX
];
240 CLzmaProb isRep
[kNumStates
];
241 CLzmaProb isRepG0
[kNumStates
];
242 CLzmaProb isRepG1
[kNumStates
];
243 CLzmaProb isRepG2
[kNumStates
];
244 CLzmaProb isRep0Long
[kNumStates
][LZMA_NUM_PB_STATES_MAX
];
246 CLzmaProb posSlotEncoder
[kNumLenToPosStates
][1 << kNumPosSlotBits
];
247 CLzmaProb posEncoders
[kNumFullDistances
- kEndPosModelIndex
];
248 CLzmaProb posAlignEncoder
[1 << kNumAlignBits
];
251 CLenPriceEnc repLenEnc
;
253 UInt32 reps
[LZMA_NUM_REPS
];
259 IMatchFinder matchFinder
;
260 void *matchFinderObj
;
264 CMatchFinderMt matchFinderMt
;
267 CMatchFinder matchFinderBase
;
273 UInt32 optimumEndIndex
;
274 UInt32 optimumCurrentIndex
;
276 UInt32 longestMatchLength
;
279 COptimal opt
[kNumOpts
];
282 Byte g_FastPos
[1 << kNumLogBits
];
285 UInt32 ProbPrices
[kBitModelTotal
>> kNumMoveReducingBits
];
286 UInt32 matches
[LZMA_MATCH_LEN_MAX
* 2 + 2 + 1];
288 UInt32 additionalOffset
;
289 UInt32 reps
[LZMA_NUM_REPS
];
292 UInt32 posSlotPrices
[kNumLenToPosStates
][kDistTableSizeMax
];
293 UInt32 distancesPrices
[kNumLenToPosStates
][kNumFullDistances
];
294 UInt32 alignPrices
[kAlignTableSize
];
295 UInt32 alignPriceCount
;
297 UInt32 distTableSize
;
300 unsigned lpMask
, pbMask
;
304 CLzmaProb isMatch
[kNumStates
][LZMA_NUM_PB_STATES_MAX
];
305 CLzmaProb isRep
[kNumStates
];
306 CLzmaProb isRepG0
[kNumStates
];
307 CLzmaProb isRepG1
[kNumStates
];
308 CLzmaProb isRepG2
[kNumStates
];
309 CLzmaProb isRep0Long
[kNumStates
][LZMA_NUM_PB_STATES_MAX
];
311 CLzmaProb posSlotEncoder
[kNumLenToPosStates
][1 << kNumPosSlotBits
];
312 CLzmaProb posEncoders
[kNumFullDistances
- kEndPosModelIndex
];
313 CLzmaProb posAlignEncoder
[1 << kNumAlignBits
];
316 CLenPriceEnc repLenEnc
;
326 UInt32 matchPriceCount
;
332 UInt32 matchFinderCycles
;
336 CSaveState saveState
;
339 void LzmaEnc_SaveState(CLzmaEncHandle pp
)
341 CLzmaEnc
*p
= (CLzmaEnc
*)pp
;
342 CSaveState
*dest
= &p
->saveState
;
344 dest
->lenEnc
= p
->lenEnc
;
345 dest
->repLenEnc
= p
->repLenEnc
;
346 dest
->state
= p
->state
;
348 for (i
= 0; i
< kNumStates
; i
++)
350 memcpy(dest
->isMatch
[i
], p
->isMatch
[i
], sizeof(p
->isMatch
[i
]));
351 memcpy(dest
->isRep0Long
[i
], p
->isRep0Long
[i
], sizeof(p
->isRep0Long
[i
]));
353 for (i
= 0; i
< kNumLenToPosStates
; i
++)
354 memcpy(dest
->posSlotEncoder
[i
], p
->posSlotEncoder
[i
], sizeof(p
->posSlotEncoder
[i
]));
355 memcpy(dest
->isRep
, p
->isRep
, sizeof(p
->isRep
));
356 memcpy(dest
->isRepG0
, p
->isRepG0
, sizeof(p
->isRepG0
));
357 memcpy(dest
->isRepG1
, p
->isRepG1
, sizeof(p
->isRepG1
));
358 memcpy(dest
->isRepG2
, p
->isRepG2
, sizeof(p
->isRepG2
));
359 memcpy(dest
->posEncoders
, p
->posEncoders
, sizeof(p
->posEncoders
));
360 memcpy(dest
->posAlignEncoder
, p
->posAlignEncoder
, sizeof(p
->posAlignEncoder
));
361 memcpy(dest
->reps
, p
->reps
, sizeof(p
->reps
));
362 memcpy(dest
->litProbs
, p
->litProbs
, (0x300 << p
->lclp
) * sizeof(CLzmaProb
));
365 void LzmaEnc_RestoreState(CLzmaEncHandle pp
)
367 CLzmaEnc
*dest
= (CLzmaEnc
*)pp
;
368 const CSaveState
*p
= &dest
->saveState
;
370 dest
->lenEnc
= p
->lenEnc
;
371 dest
->repLenEnc
= p
->repLenEnc
;
372 dest
->state
= p
->state
;
374 for (i
= 0; i
< kNumStates
; i
++)
376 memcpy(dest
->isMatch
[i
], p
->isMatch
[i
], sizeof(p
->isMatch
[i
]));
377 memcpy(dest
->isRep0Long
[i
], p
->isRep0Long
[i
], sizeof(p
->isRep0Long
[i
]));
379 for (i
= 0; i
< kNumLenToPosStates
; i
++)
380 memcpy(dest
->posSlotEncoder
[i
], p
->posSlotEncoder
[i
], sizeof(p
->posSlotEncoder
[i
]));
381 memcpy(dest
->isRep
, p
->isRep
, sizeof(p
->isRep
));
382 memcpy(dest
->isRepG0
, p
->isRepG0
, sizeof(p
->isRepG0
));
383 memcpy(dest
->isRepG1
, p
->isRepG1
, sizeof(p
->isRepG1
));
384 memcpy(dest
->isRepG2
, p
->isRepG2
, sizeof(p
->isRepG2
));
385 memcpy(dest
->posEncoders
, p
->posEncoders
, sizeof(p
->posEncoders
));
386 memcpy(dest
->posAlignEncoder
, p
->posAlignEncoder
, sizeof(p
->posAlignEncoder
));
387 memcpy(dest
->reps
, p
->reps
, sizeof(p
->reps
));
388 memcpy(dest
->litProbs
, p
->litProbs
, (0x300 << dest
->lclp
) * sizeof(CLzmaProb
));
391 SRes
LzmaEnc_SetProps(CLzmaEncHandle pp
, const CLzmaEncProps
*props2
)
393 CLzmaEnc
*p
= (CLzmaEnc
*)pp
;
394 CLzmaEncProps props
= *props2
;
395 LzmaEncProps_Normalize(&props
);
397 if (props
.lc
> LZMA_LC_MAX
|| props
.lp
> LZMA_LP_MAX
|| props
.pb
> LZMA_PB_MAX
||
398 props
.dictSize
> ((UInt32
)1 << kDicLogSizeMaxCompress
) || props
.dictSize
> ((UInt32
)1 << 30))
399 return SZ_ERROR_PARAM
;
400 p
->dictSize
= props
.dictSize
;
401 p
->matchFinderCycles
= props
.mc
;
403 unsigned fb
= props
.fb
;
406 if (fb
> LZMA_MATCH_LEN_MAX
)
407 fb
= LZMA_MATCH_LEN_MAX
;
408 p
->numFastBytes
= fb
;
413 p
->fastMode
= (props
.algo
== 0);
414 p
->matchFinderBase
.btMode
= props
.btMode
;
416 UInt32 numHashBytes
= 4;
419 if (props
.numHashBytes
< 2)
421 else if (props
.numHashBytes
< 4)
422 numHashBytes
= props
.numHashBytes
;
424 p
->matchFinderBase
.numHashBytes
= numHashBytes
;
427 p
->matchFinderBase
.cutValue
= props
.mc
;
429 p
->writeEndMark
= props
.writeEndMark
;
433 if (newMultiThread != _multiThread)
435 ReleaseMatchFinder();
436 _multiThread = newMultiThread;
439 p
->multiThread
= (props
.numThreads
> 1);
445 static const int kLiteralNextStates
[kNumStates
] = {0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5};
446 static const int kMatchNextStates
[kNumStates
] = {7, 7, 7, 7, 7, 7, 7, 10, 10, 10, 10, 10};
447 static const int kRepNextStates
[kNumStates
] = {8, 8, 8, 8, 8, 8, 8, 11, 11, 11, 11, 11};
448 static const int kShortRepNextStates
[kNumStates
]= {9, 9, 9, 9, 9, 9, 9, 11, 11, 11, 11, 11};
450 #define IsCharState(s) ((s) < 7)
452 #define GetLenToPosState(len) (((len) < kNumLenToPosStates + 1) ? (len) - 2 : kNumLenToPosStates - 1)
454 #define kInfinityPrice (1 << 30)
456 static void RangeEnc_Construct(CRangeEnc
*p
)
462 #define RangeEnc_GetProcessed(p) ((p)->processed + ((p)->buf - (p)->bufBase) + (p)->cacheSize)
464 #define RC_BUF_SIZE (1 << 16)
465 static int RangeEnc_Alloc(CRangeEnc
*p
, ISzAlloc
*alloc
)
469 p
->bufBase
= (Byte
*)alloc
->Alloc(alloc
, RC_BUF_SIZE
);
472 p
->bufLim
= p
->bufBase
+ RC_BUF_SIZE
;
477 static void RangeEnc_Free(CRangeEnc
*p
, ISzAlloc
*alloc
)
479 alloc
->Free(alloc
, p
->bufBase
);
483 static void RangeEnc_Init(CRangeEnc
*p
)
487 p
->range
= 0xFFFFFFFF;
497 static void RangeEnc_FlushStream(CRangeEnc
*p
)
502 num
= p
->buf
- p
->bufBase
;
503 if (num
!= p
->outStream
->Write(p
->outStream
, p
->bufBase
, num
))
504 p
->res
= SZ_ERROR_WRITE
;
509 static void MY_FAST_CALL
RangeEnc_ShiftLow(CRangeEnc
*p
)
511 if ((UInt32
)p
->low
< (UInt32
)0xFF000000 || (int)(p
->low
>> 32) != 0)
513 Byte temp
= p
->cache
;
517 *buf
++ = (Byte
)(temp
+ (Byte
)(p
->low
>> 32));
519 if (buf
== p
->bufLim
)
520 RangeEnc_FlushStream(p
);
523 while (--p
->cacheSize
!= 0);
524 p
->cache
= (Byte
)((UInt32
)p
->low
>> 24);
527 p
->low
= (UInt32
)p
->low
<< 8;
530 static void RangeEnc_FlushData(CRangeEnc
*p
)
533 for (i
= 0; i
< 5; i
++)
534 RangeEnc_ShiftLow(p
);
537 static void RangeEnc_EncodeDirectBits(CRangeEnc
*p
, UInt32 value
, int numBits
)
542 p
->low
+= p
->range
& (0 - ((value
>> --numBits
) & 1));
543 if (p
->range
< kTopValue
)
546 RangeEnc_ShiftLow(p
);
549 while (numBits
!= 0);
552 static void RangeEnc_EncodeBit(CRangeEnc
*p
, CLzmaProb
*prob
, UInt32 symbol
)
555 UInt32 newBound
= (p
->range
>> kNumBitModelTotalBits
) * ttt
;
559 ttt
+= (kBitModelTotal
- ttt
) >> kNumMoveBits
;
564 p
->range
-= newBound
;
565 ttt
-= ttt
>> kNumMoveBits
;
567 *prob
= (CLzmaProb
)ttt
;
568 if (p
->range
< kTopValue
)
571 RangeEnc_ShiftLow(p
);
575 static void LitEnc_Encode(CRangeEnc
*p
, CLzmaProb
*probs
, UInt32 symbol
)
580 RangeEnc_EncodeBit(p
, probs
+ (symbol
>> 8), (symbol
>> 7) & 1);
583 while (symbol
< 0x10000);
586 static void LitEnc_EncodeMatched(CRangeEnc
*p
, CLzmaProb
*probs
, UInt32 symbol
, UInt32 matchByte
)
593 RangeEnc_EncodeBit(p
, probs
+ (offs
+ (matchByte
& offs
) + (symbol
>> 8)), (symbol
>> 7) & 1);
595 offs
&= ~(matchByte
^ symbol
);
597 while (symbol
< 0x10000);
600 void LzmaEnc_InitPriceTables(UInt32
*ProbPrices
)
603 for (i
= (1 << kNumMoveReducingBits
) / 2; i
< kBitModelTotal
; i
+= (1 << kNumMoveReducingBits
))
605 const int kCyclesBits
= kNumBitPriceShiftBits
;
609 for (j
= 0; j
< kCyclesBits
; j
++)
613 while (w
>= ((UInt32
)1 << 16))
619 ProbPrices
[i
>> kNumMoveReducingBits
] = ((kNumBitModelTotalBits
<< kCyclesBits
) - 15 - bitCount
);
624 #define GET_PRICE(prob, symbol) \
625 p->ProbPrices[((prob) ^ (((-(int)(symbol))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits];
627 #define GET_PRICEa(prob, symbol) \
628 ProbPrices[((prob) ^ ((-((int)(symbol))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits];
630 #define GET_PRICE_0(prob) p->ProbPrices[(prob) >> kNumMoveReducingBits]
631 #define GET_PRICE_1(prob) p->ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]
633 #define GET_PRICE_0a(prob) ProbPrices[(prob) >> kNumMoveReducingBits]
634 #define GET_PRICE_1a(prob) ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]
636 static UInt32
LitEnc_GetPrice(const CLzmaProb
*probs
, UInt32 symbol
, UInt32
*ProbPrices
)
642 price
+= GET_PRICEa(probs
[symbol
>> 8], (symbol
>> 7) & 1);
645 while (symbol
< 0x10000);
649 static UInt32
LitEnc_GetPriceMatched(const CLzmaProb
*probs
, UInt32 symbol
, UInt32 matchByte
, UInt32
*ProbPrices
)
657 price
+= GET_PRICEa(probs
[offs
+ (matchByte
& offs
) + (symbol
>> 8)], (symbol
>> 7) & 1);
659 offs
&= ~(matchByte
^ symbol
);
661 while (symbol
< 0x10000);
666 static void RcTree_Encode(CRangeEnc
*rc
, CLzmaProb
*probs
, int numBitLevels
, UInt32 symbol
)
670 for (i
= numBitLevels
; i
!= 0;)
674 bit
= (symbol
>> i
) & 1;
675 RangeEnc_EncodeBit(rc
, probs
+ m
, bit
);
680 static void RcTree_ReverseEncode(CRangeEnc
*rc
, CLzmaProb
*probs
, int numBitLevels
, UInt32 symbol
)
684 for (i
= 0; i
< numBitLevels
; i
++)
686 UInt32 bit
= symbol
& 1;
687 RangeEnc_EncodeBit(rc
, probs
+ m
, bit
);
693 static UInt32
RcTree_GetPrice(const CLzmaProb
*probs
, int numBitLevels
, UInt32 symbol
, UInt32
*ProbPrices
)
696 symbol
|= (1 << numBitLevels
);
699 price
+= GET_PRICEa(probs
[symbol
>> 1], symbol
& 1);
705 static UInt32
RcTree_ReverseGetPrice(const CLzmaProb
*probs
, int numBitLevels
, UInt32 symbol
, UInt32
*ProbPrices
)
710 for (i
= numBitLevels
; i
!= 0; i
--)
712 UInt32 bit
= symbol
& 1;
714 price
+= GET_PRICEa(probs
[m
], bit
);
721 static void LenEnc_Init(CLenEnc
*p
)
724 p
->choice
= p
->choice2
= kProbInitValue
;
725 for (i
= 0; i
< (LZMA_NUM_PB_STATES_MAX
<< kLenNumLowBits
); i
++)
726 p
->low
[i
] = kProbInitValue
;
727 for (i
= 0; i
< (LZMA_NUM_PB_STATES_MAX
<< kLenNumMidBits
); i
++)
728 p
->mid
[i
] = kProbInitValue
;
729 for (i
= 0; i
< kLenNumHighSymbols
; i
++)
730 p
->high
[i
] = kProbInitValue
;
733 static void LenEnc_Encode(CLenEnc
*p
, CRangeEnc
*rc
, UInt32 symbol
, UInt32 posState
)
735 if (symbol
< kLenNumLowSymbols
)
737 RangeEnc_EncodeBit(rc
, &p
->choice
, 0);
738 RcTree_Encode(rc
, p
->low
+ (posState
<< kLenNumLowBits
), kLenNumLowBits
, symbol
);
742 RangeEnc_EncodeBit(rc
, &p
->choice
, 1);
743 if (symbol
< kLenNumLowSymbols
+ kLenNumMidSymbols
)
745 RangeEnc_EncodeBit(rc
, &p
->choice2
, 0);
746 RcTree_Encode(rc
, p
->mid
+ (posState
<< kLenNumMidBits
), kLenNumMidBits
, symbol
- kLenNumLowSymbols
);
750 RangeEnc_EncodeBit(rc
, &p
->choice2
, 1);
751 RcTree_Encode(rc
, p
->high
, kLenNumHighBits
, symbol
- kLenNumLowSymbols
- kLenNumMidSymbols
);
756 static void LenEnc_SetPrices(CLenEnc
*p
, UInt32 posState
, UInt32 numSymbols
, UInt32
*prices
, UInt32
*ProbPrices
)
758 UInt32 a0
= GET_PRICE_0a(p
->choice
);
759 UInt32 a1
= GET_PRICE_1a(p
->choice
);
760 UInt32 b0
= a1
+ GET_PRICE_0a(p
->choice2
);
761 UInt32 b1
= a1
+ GET_PRICE_1a(p
->choice2
);
763 for (i
= 0; i
< kLenNumLowSymbols
; i
++)
767 prices
[i
] = a0
+ RcTree_GetPrice(p
->low
+ (posState
<< kLenNumLowBits
), kLenNumLowBits
, i
, ProbPrices
);
769 for (; i
< kLenNumLowSymbols
+ kLenNumMidSymbols
; i
++)
773 prices
[i
] = b0
+ RcTree_GetPrice(p
->mid
+ (posState
<< kLenNumMidBits
), kLenNumMidBits
, i
- kLenNumLowSymbols
, ProbPrices
);
775 for (; i
< numSymbols
; i
++)
776 prices
[i
] = b1
+ RcTree_GetPrice(p
->high
, kLenNumHighBits
, i
- kLenNumLowSymbols
- kLenNumMidSymbols
, ProbPrices
);
779 static void MY_FAST_CALL
LenPriceEnc_UpdateTable(CLenPriceEnc
*p
, UInt32 posState
, UInt32
*ProbPrices
)
781 LenEnc_SetPrices(&p
->p
, posState
, p
->tableSize
, p
->prices
[posState
], ProbPrices
);
782 p
->counters
[posState
] = p
->tableSize
;
785 static void LenPriceEnc_UpdateTables(CLenPriceEnc
*p
, UInt32 numPosStates
, UInt32
*ProbPrices
)
788 for (posState
= 0; posState
< numPosStates
; posState
++)
789 LenPriceEnc_UpdateTable(p
, posState
, ProbPrices
);
792 static void LenEnc_Encode2(CLenPriceEnc
*p
, CRangeEnc
*rc
, UInt32 symbol
, UInt32 posState
, Bool updatePrice
, UInt32
*ProbPrices
)
794 LenEnc_Encode(&p
->p
, rc
, symbol
, posState
);
796 if (--p
->counters
[posState
] == 0)
797 LenPriceEnc_UpdateTable(p
, posState
, ProbPrices
);
803 static void MovePos(CLzmaEnc
*p
, UInt32 num
)
807 printf("\n MovePos %d", num
);
811 p
->additionalOffset
+= num
;
812 p
->matchFinder
.Skip(p
->matchFinderObj
, num
);
816 static UInt32
ReadMatchDistances(CLzmaEnc
*p
, UInt32
*numDistancePairsRes
)
818 UInt32 lenRes
= 0, numPairs
;
819 p
->numAvail
= p
->matchFinder
.GetNumAvailableBytes(p
->matchFinderObj
);
820 numPairs
= p
->matchFinder
.GetMatches(p
->matchFinderObj
, p
->matches
);
822 printf("\n i = %d numPairs = %d ", ttt
, numPairs
/ 2);
826 for (i
= 0; i
< numPairs
; i
+= 2)
827 printf("%2d %6d | ", p
->matches
[i
], p
->matches
[i
+ 1]);
832 lenRes
= p
->matches
[numPairs
- 2];
833 if (lenRes
== p
->numFastBytes
)
835 const Byte
*pby
= p
->matchFinder
.GetPointerToCurrentPos(p
->matchFinderObj
) - 1;
836 UInt32 distance
= p
->matches
[numPairs
- 1] + 1;
837 UInt32 numAvail
= p
->numAvail
;
838 if (numAvail
> LZMA_MATCH_LEN_MAX
)
839 numAvail
= LZMA_MATCH_LEN_MAX
;
841 const Byte
*pby2
= pby
- distance
;
842 for (; lenRes
< numAvail
&& pby
[lenRes
] == pby2
[lenRes
]; lenRes
++);
846 p
->additionalOffset
++;
847 *numDistancePairsRes
= numPairs
;
852 #define MakeAsChar(p) (p)->backPrev = (UInt32)(-1); (p)->prev1IsChar = False;
853 #define MakeAsShortRep(p) (p)->backPrev = 0; (p)->prev1IsChar = False;
854 #define IsShortRep(p) ((p)->backPrev == 0)
856 static UInt32
GetRepLen1Price(CLzmaEnc
*p
, UInt32 state
, UInt32 posState
)
859 GET_PRICE_0(p
->isRepG0
[state
]) +
860 GET_PRICE_0(p
->isRep0Long
[state
][posState
]);
863 static UInt32
GetPureRepPrice(CLzmaEnc
*p
, UInt32 repIndex
, UInt32 state
, UInt32 posState
)
868 price
= GET_PRICE_0(p
->isRepG0
[state
]);
869 price
+= GET_PRICE_1(p
->isRep0Long
[state
][posState
]);
873 price
= GET_PRICE_1(p
->isRepG0
[state
]);
875 price
+= GET_PRICE_0(p
->isRepG1
[state
]);
878 price
+= GET_PRICE_1(p
->isRepG1
[state
]);
879 price
+= GET_PRICE(p
->isRepG2
[state
], repIndex
- 2);
885 static UInt32
GetRepPrice(CLzmaEnc
*p
, UInt32 repIndex
, UInt32 len
, UInt32 state
, UInt32 posState
)
887 return p
->repLenEnc
.prices
[posState
][len
- LZMA_MATCH_LEN_MIN
] +
888 GetPureRepPrice(p
, repIndex
, state
, posState
);
891 static UInt32
Backward(CLzmaEnc
*p
, UInt32
*backRes
, UInt32 cur
)
893 UInt32 posMem
= p
->opt
[cur
].posPrev
;
894 UInt32 backMem
= p
->opt
[cur
].backPrev
;
895 p
->optimumEndIndex
= cur
;
898 if (p
->opt
[cur
].prev1IsChar
)
900 MakeAsChar(&p
->opt
[posMem
])
901 p
->opt
[posMem
].posPrev
= posMem
- 1;
902 if (p
->opt
[cur
].prev2
)
904 p
->opt
[posMem
- 1].prev1IsChar
= False
;
905 p
->opt
[posMem
- 1].posPrev
= p
->opt
[cur
].posPrev2
;
906 p
->opt
[posMem
- 1].backPrev
= p
->opt
[cur
].backPrev2
;
910 UInt32 posPrev
= posMem
;
911 UInt32 backCur
= backMem
;
913 backMem
= p
->opt
[posPrev
].backPrev
;
914 posMem
= p
->opt
[posPrev
].posPrev
;
916 p
->opt
[posPrev
].backPrev
= backCur
;
917 p
->opt
[posPrev
].posPrev
= cur
;
922 *backRes
= p
->opt
[0].backPrev
;
923 p
->optimumCurrentIndex
= p
->opt
[0].posPrev
;
924 return p
->optimumCurrentIndex
;
927 #define LIT_PROBS(pos, prevByte) (p->litProbs + ((((pos) & p->lpMask) << p->lc) + ((prevByte) >> (8 - p->lc))) * 0x300)
929 static UInt32
GetOptimum(CLzmaEnc
*p
, UInt32 position
, UInt32
*backRes
)
931 UInt32 numAvail
, mainLen
, numPairs
, repMaxIndex
, i
, posState
, lenEnd
, len
, cur
;
932 UInt32 matchPrice
, repMatchPrice
, normalMatchPrice
;
933 UInt32 reps
[LZMA_NUM_REPS
], repLens
[LZMA_NUM_REPS
];
936 Byte curByte
, matchByte
;
937 if (p
->optimumEndIndex
!= p
->optimumCurrentIndex
)
939 const COptimal
*opt
= &p
->opt
[p
->optimumCurrentIndex
];
940 UInt32 lenRes
= opt
->posPrev
- p
->optimumCurrentIndex
;
941 *backRes
= opt
->backPrev
;
942 p
->optimumCurrentIndex
= opt
->posPrev
;
945 p
->optimumCurrentIndex
= p
->optimumEndIndex
= 0;
947 if (p
->additionalOffset
== 0)
948 mainLen
= ReadMatchDistances(p
, &numPairs
);
951 mainLen
= p
->longestMatchLength
;
952 numPairs
= p
->numPairs
;
955 numAvail
= p
->numAvail
;
958 *backRes
= (UInt32
)(-1);
961 if (numAvail
> LZMA_MATCH_LEN_MAX
)
962 numAvail
= LZMA_MATCH_LEN_MAX
;
964 data
= p
->matchFinder
.GetPointerToCurrentPos(p
->matchFinderObj
) - 1;
966 for (i
= 0; i
< LZMA_NUM_REPS
; i
++)
970 reps
[i
] = p
->reps
[i
];
971 data2
= data
- (reps
[i
] + 1);
972 if (data
[0] != data2
[0] || data
[1] != data2
[1])
977 for (lenTest
= 2; lenTest
< numAvail
&& data
[lenTest
] == data2
[lenTest
]; lenTest
++);
978 repLens
[i
] = lenTest
;
979 if (lenTest
> repLens
[repMaxIndex
])
982 if (repLens
[repMaxIndex
] >= p
->numFastBytes
)
985 *backRes
= repMaxIndex
;
986 lenRes
= repLens
[repMaxIndex
];
987 MovePos(p
, lenRes
- 1);
991 matches
= p
->matches
;
992 if (mainLen
>= p
->numFastBytes
)
994 *backRes
= matches
[numPairs
- 1] + LZMA_NUM_REPS
;
995 MovePos(p
, mainLen
- 1);
999 matchByte
= *(data
- (reps
[0] + 1));
1001 if (mainLen
< 2 && curByte
!= matchByte
&& repLens
[repMaxIndex
] < 2)
1003 *backRes
= (UInt32
)-1;
1007 p
->opt
[0].state
= (CState
)p
->state
;
1009 posState
= (position
& p
->pbMask
);
1012 const CLzmaProb
*probs
= LIT_PROBS(position
, *(data
- 1));
1013 p
->opt
[1].price
= GET_PRICE_0(p
->isMatch
[p
->state
][posState
]) +
1014 (!IsCharState(p
->state
) ?
1015 LitEnc_GetPriceMatched(probs
, curByte
, matchByte
, p
->ProbPrices
) :
1016 LitEnc_GetPrice(probs
, curByte
, p
->ProbPrices
));
1019 MakeAsChar(&p
->opt
[1]);
1021 matchPrice
= GET_PRICE_1(p
->isMatch
[p
->state
][posState
]);
1022 repMatchPrice
= matchPrice
+ GET_PRICE_1(p
->isRep
[p
->state
]);
1024 if (matchByte
== curByte
)
1026 UInt32 shortRepPrice
= repMatchPrice
+ GetRepLen1Price(p
, p
->state
, posState
);
1027 if (shortRepPrice
< p
->opt
[1].price
)
1029 p
->opt
[1].price
= shortRepPrice
;
1030 MakeAsShortRep(&p
->opt
[1]);
1033 lenEnd
= ((mainLen
>= repLens
[repMaxIndex
]) ? mainLen
: repLens
[repMaxIndex
]);
1037 *backRes
= p
->opt
[1].backPrev
;
1041 p
->opt
[1].posPrev
= 0;
1042 for (i
= 0; i
< LZMA_NUM_REPS
; i
++)
1043 p
->opt
[0].backs
[i
] = reps
[i
];
1047 p
->opt
[len
--].price
= kInfinityPrice
;
1050 for (i
= 0; i
< LZMA_NUM_REPS
; i
++)
1052 UInt32 repLen
= repLens
[i
];
1056 price
= repMatchPrice
+ GetPureRepPrice(p
, i
, p
->state
, posState
);
1059 UInt32 curAndLenPrice
= price
+ p
->repLenEnc
.prices
[posState
][repLen
- 2];
1060 COptimal
*opt
= &p
->opt
[repLen
];
1061 if (curAndLenPrice
< opt
->price
)
1063 opt
->price
= curAndLenPrice
;
1066 opt
->prev1IsChar
= False
;
1069 while (--repLen
>= 2);
1072 normalMatchPrice
= matchPrice
+ GET_PRICE_0(p
->isRep
[p
->state
]);
1074 len
= ((repLens
[0] >= 2) ? repLens
[0] + 1 : 2);
1078 while (len
> matches
[offs
])
1083 UInt32 distance
= matches
[offs
+ 1];
1085 UInt32 curAndLenPrice
= normalMatchPrice
+ p
->lenEnc
.prices
[posState
][len
- LZMA_MATCH_LEN_MIN
];
1086 UInt32 lenToPosState
= GetLenToPosState(len
);
1087 if (distance
< kNumFullDistances
)
1088 curAndLenPrice
+= p
->distancesPrices
[lenToPosState
][distance
];
1092 GetPosSlot2(distance
, slot
);
1093 curAndLenPrice
+= p
->alignPrices
[distance
& kAlignMask
] + p
->posSlotPrices
[lenToPosState
][slot
];
1096 if (curAndLenPrice
< opt
->price
)
1098 opt
->price
= curAndLenPrice
;
1100 opt
->backPrev
= distance
+ LZMA_NUM_REPS
;
1101 opt
->prev1IsChar
= False
;
1103 if (len
== matches
[offs
])
1106 if (offs
== numPairs
)
1118 printf("\n pos = %4X", position
);
1119 for (i
= cur
; i
<= lenEnd
; i
++)
1120 printf("\nprice[%4X] = %d", position
- cur
+ i
, p
->opt
[i
].price
);
1126 UInt32 numAvailFull
, newLen
, numPairs
, posPrev
, state
, posState
, startLen
;
1127 UInt32 curPrice
, curAnd1Price
, matchPrice
, repMatchPrice
;
1129 Byte curByte
, matchByte
;
1136 return Backward(p
, backRes
, cur
);
1138 newLen
= ReadMatchDistances(p
, &numPairs
);
1139 if (newLen
>= p
->numFastBytes
)
1141 p
->numPairs
= numPairs
;
1142 p
->longestMatchLength
= newLen
;
1143 return Backward(p
, backRes
, cur
);
1146 curOpt
= &p
->opt
[cur
];
1147 posPrev
= curOpt
->posPrev
;
1148 if (curOpt
->prev1IsChar
)
1153 state
= p
->opt
[curOpt
->posPrev2
].state
;
1154 if (curOpt
->backPrev2
< LZMA_NUM_REPS
)
1155 state
= kRepNextStates
[state
];
1157 state
= kMatchNextStates
[state
];
1160 state
= p
->opt
[posPrev
].state
;
1161 state
= kLiteralNextStates
[state
];
1164 state
= p
->opt
[posPrev
].state
;
1165 if (posPrev
== cur
- 1)
1167 if (IsShortRep(curOpt
))
1168 state
= kShortRepNextStates
[state
];
1170 state
= kLiteralNextStates
[state
];
1175 const COptimal
*prevOpt
;
1176 if (curOpt
->prev1IsChar
&& curOpt
->prev2
)
1178 posPrev
= curOpt
->posPrev2
;
1179 pos
= curOpt
->backPrev2
;
1180 state
= kRepNextStates
[state
];
1184 pos
= curOpt
->backPrev
;
1185 if (pos
< LZMA_NUM_REPS
)
1186 state
= kRepNextStates
[state
];
1188 state
= kMatchNextStates
[state
];
1190 prevOpt
= &p
->opt
[posPrev
];
1191 if (pos
< LZMA_NUM_REPS
)
1194 reps
[0] = prevOpt
->backs
[pos
];
1195 for (i
= 1; i
<= pos
; i
++)
1196 reps
[i
] = prevOpt
->backs
[i
- 1];
1197 for (; i
< LZMA_NUM_REPS
; i
++)
1198 reps
[i
] = prevOpt
->backs
[i
];
1203 reps
[0] = (pos
- LZMA_NUM_REPS
);
1204 for (i
= 1; i
< LZMA_NUM_REPS
; i
++)
1205 reps
[i
] = prevOpt
->backs
[i
- 1];
1208 curOpt
->state
= (CState
)state
;
1210 curOpt
->backs
[0] = reps
[0];
1211 curOpt
->backs
[1] = reps
[1];
1212 curOpt
->backs
[2] = reps
[2];
1213 curOpt
->backs
[3] = reps
[3];
1215 curPrice
= curOpt
->price
;
1217 data
= p
->matchFinder
.GetPointerToCurrentPos(p
->matchFinderObj
) - 1;
1219 matchByte
= *(data
- (reps
[0] + 1));
1221 posState
= (position
& p
->pbMask
);
1223 curAnd1Price
= curPrice
+ GET_PRICE_0(p
->isMatch
[state
][posState
]);
1225 const CLzmaProb
*probs
= LIT_PROBS(position
, *(data
- 1));
1227 (!IsCharState(state
) ?
1228 LitEnc_GetPriceMatched(probs
, curByte
, matchByte
, p
->ProbPrices
) :
1229 LitEnc_GetPrice(probs
, curByte
, p
->ProbPrices
));
1232 nextOpt
= &p
->opt
[cur
+ 1];
1234 if (curAnd1Price
< nextOpt
->price
)
1236 nextOpt
->price
= curAnd1Price
;
1237 nextOpt
->posPrev
= cur
;
1238 MakeAsChar(nextOpt
);
1242 matchPrice
= curPrice
+ GET_PRICE_1(p
->isMatch
[state
][posState
]);
1243 repMatchPrice
= matchPrice
+ GET_PRICE_1(p
->isRep
[state
]);
1245 if (matchByte
== curByte
&& !(nextOpt
->posPrev
< cur
&& nextOpt
->backPrev
== 0))
1247 UInt32 shortRepPrice
= repMatchPrice
+ GetRepLen1Price(p
, state
, posState
);
1248 if (shortRepPrice
<= nextOpt
->price
)
1250 nextOpt
->price
= shortRepPrice
;
1251 nextOpt
->posPrev
= cur
;
1252 MakeAsShortRep(nextOpt
);
1256 numAvailFull
= p
->numAvail
;
1258 UInt32 temp
= kNumOpts
- 1 - cur
;
1259 if (temp
< numAvailFull
)
1260 numAvailFull
= temp
;
1263 if (numAvailFull
< 2)
1265 numAvail
= (numAvailFull
<= p
->numFastBytes
? numAvailFull
: p
->numFastBytes
);
1267 if (!nextIsChar
&& matchByte
!= curByte
) /* speed optimization */
1269 /* try Literal + rep0 */
1272 const Byte
*data2
= data
- (reps
[0] + 1);
1273 UInt32 limit
= p
->numFastBytes
+ 1;
1274 if (limit
> numAvailFull
)
1275 limit
= numAvailFull
;
1277 for (temp
= 1; temp
< limit
&& data
[temp
] == data2
[temp
]; temp
++);
1278 lenTest2
= temp
- 1;
1281 UInt32 state2
= kLiteralNextStates
[state
];
1282 UInt32 posStateNext
= (position
+ 1) & p
->pbMask
;
1283 UInt32 nextRepMatchPrice
= curAnd1Price
+
1284 GET_PRICE_1(p
->isMatch
[state2
][posStateNext
]) +
1285 GET_PRICE_1(p
->isRep
[state2
]);
1286 /* for (; lenTest2 >= 2; lenTest2--) */
1288 UInt32 curAndLenPrice
;
1290 UInt32 offset
= cur
+ 1 + lenTest2
;
1291 while (lenEnd
< offset
)
1292 p
->opt
[++lenEnd
].price
= kInfinityPrice
;
1293 curAndLenPrice
= nextRepMatchPrice
+ GetRepPrice(p
, 0, lenTest2
, state2
, posStateNext
);
1294 opt
= &p
->opt
[offset
];
1295 if (curAndLenPrice
< opt
->price
)
1297 opt
->price
= curAndLenPrice
;
1298 opt
->posPrev
= cur
+ 1;
1300 opt
->prev1IsChar
= True
;
1307 startLen
= 2; /* speed optimization */
1310 for (repIndex
= 0; repIndex
< LZMA_NUM_REPS
; repIndex
++)
1315 const Byte
*data2
= data
- (reps
[repIndex
] + 1);
1316 if (data
[0] != data2
[0] || data
[1] != data2
[1])
1318 for (lenTest
= 2; lenTest
< numAvail
&& data
[lenTest
] == data2
[lenTest
]; lenTest
++);
1319 while (lenEnd
< cur
+ lenTest
)
1320 p
->opt
[++lenEnd
].price
= kInfinityPrice
;
1321 lenTestTemp
= lenTest
;
1322 price
= repMatchPrice
+ GetPureRepPrice(p
, repIndex
, state
, posState
);
1325 UInt32 curAndLenPrice
= price
+ p
->repLenEnc
.prices
[posState
][lenTest
- 2];
1326 COptimal
*opt
= &p
->opt
[cur
+ lenTest
];
1327 if (curAndLenPrice
< opt
->price
)
1329 opt
->price
= curAndLenPrice
;
1331 opt
->backPrev
= repIndex
;
1332 opt
->prev1IsChar
= False
;
1335 while (--lenTest
>= 2);
1336 lenTest
= lenTestTemp
;
1339 startLen
= lenTest
+ 1;
1343 UInt32 lenTest2
= lenTest
+ 1;
1344 UInt32 limit
= lenTest2
+ p
->numFastBytes
;
1345 UInt32 nextRepMatchPrice
;
1346 if (limit
> numAvailFull
)
1347 limit
= numAvailFull
;
1348 for (; lenTest2
< limit
&& data
[lenTest2
] == data2
[lenTest2
]; lenTest2
++);
1349 lenTest2
-= lenTest
+ 1;
1352 UInt32 state2
= kRepNextStates
[state
];
1353 UInt32 posStateNext
= (position
+ lenTest
) & p
->pbMask
;
1354 UInt32 curAndLenCharPrice
=
1355 price
+ p
->repLenEnc
.prices
[posState
][lenTest
- 2] +
1356 GET_PRICE_0(p
->isMatch
[state2
][posStateNext
]) +
1357 LitEnc_GetPriceMatched(LIT_PROBS(position
+ lenTest
, data
[lenTest
- 1]),
1358 data
[lenTest
], data2
[lenTest
], p
->ProbPrices
);
1359 state2
= kLiteralNextStates
[state2
];
1360 posStateNext
= (position
+ lenTest
+ 1) & p
->pbMask
;
1361 nextRepMatchPrice
= curAndLenCharPrice
+
1362 GET_PRICE_1(p
->isMatch
[state2
][posStateNext
]) +
1363 GET_PRICE_1(p
->isRep
[state2
]);
1365 /* for (; lenTest2 >= 2; lenTest2--) */
1367 UInt32 curAndLenPrice
;
1369 UInt32 offset
= cur
+ lenTest
+ 1 + lenTest2
;
1370 while (lenEnd
< offset
)
1371 p
->opt
[++lenEnd
].price
= kInfinityPrice
;
1372 curAndLenPrice
= nextRepMatchPrice
+ GetRepPrice(p
, 0, lenTest2
, state2
, posStateNext
);
1373 opt
= &p
->opt
[offset
];
1374 if (curAndLenPrice
< opt
->price
)
1376 opt
->price
= curAndLenPrice
;
1377 opt
->posPrev
= cur
+ lenTest
+ 1;
1379 opt
->prev1IsChar
= True
;
1381 opt
->posPrev2
= cur
;
1382 opt
->backPrev2
= repIndex
;
1389 /* for (UInt32 lenTest = 2; lenTest <= newLen; lenTest++) */
1390 if (newLen
> numAvail
)
1393 for (numPairs
= 0; newLen
> matches
[numPairs
]; numPairs
+= 2);
1394 matches
[numPairs
] = newLen
;
1397 if (newLen
>= startLen
)
1399 UInt32 normalMatchPrice
= matchPrice
+ GET_PRICE_0(p
->isRep
[state
]);
1400 UInt32 offs
, curBack
, posSlot
;
1402 while (lenEnd
< cur
+ newLen
)
1403 p
->opt
[++lenEnd
].price
= kInfinityPrice
;
1406 while (startLen
> matches
[offs
])
1408 curBack
= matches
[offs
+ 1];
1409 GetPosSlot2(curBack
, posSlot
);
1410 for (lenTest
= /*2*/ startLen
; ; lenTest
++)
1412 UInt32 curAndLenPrice
= normalMatchPrice
+ p
->lenEnc
.prices
[posState
][lenTest
- LZMA_MATCH_LEN_MIN
];
1413 UInt32 lenToPosState
= GetLenToPosState(lenTest
);
1415 if (curBack
< kNumFullDistances
)
1416 curAndLenPrice
+= p
->distancesPrices
[lenToPosState
][curBack
];
1418 curAndLenPrice
+= p
->posSlotPrices
[lenToPosState
][posSlot
] + p
->alignPrices
[curBack
& kAlignMask
];
1420 opt
= &p
->opt
[cur
+ lenTest
];
1421 if (curAndLenPrice
< opt
->price
)
1423 opt
->price
= curAndLenPrice
;
1425 opt
->backPrev
= curBack
+ LZMA_NUM_REPS
;
1426 opt
->prev1IsChar
= False
;
1429 if (/*_maxMode && */lenTest
== matches
[offs
])
1431 /* Try Match + Literal + Rep0 */
1432 const Byte
*data2
= data
- (curBack
+ 1);
1433 UInt32 lenTest2
= lenTest
+ 1;
1434 UInt32 limit
= lenTest2
+ p
->numFastBytes
;
1435 UInt32 nextRepMatchPrice
;
1436 if (limit
> numAvailFull
)
1437 limit
= numAvailFull
;
1438 for (; lenTest2
< limit
&& data
[lenTest2
] == data2
[lenTest2
]; lenTest2
++);
1439 lenTest2
-= lenTest
+ 1;
1442 UInt32 state2
= kMatchNextStates
[state
];
1443 UInt32 posStateNext
= (position
+ lenTest
) & p
->pbMask
;
1444 UInt32 curAndLenCharPrice
= curAndLenPrice
+
1445 GET_PRICE_0(p
->isMatch
[state2
][posStateNext
]) +
1446 LitEnc_GetPriceMatched(LIT_PROBS(position
+ lenTest
, data
[lenTest
- 1]),
1447 data
[lenTest
], data2
[lenTest
], p
->ProbPrices
);
1448 state2
= kLiteralNextStates
[state2
];
1449 posStateNext
= (posStateNext
+ 1) & p
->pbMask
;
1450 nextRepMatchPrice
= curAndLenCharPrice
+
1451 GET_PRICE_1(p
->isMatch
[state2
][posStateNext
]) +
1452 GET_PRICE_1(p
->isRep
[state2
]);
1454 /* for (; lenTest2 >= 2; lenTest2--) */
1456 UInt32 offset
= cur
+ lenTest
+ 1 + lenTest2
;
1457 UInt32 curAndLenPrice
;
1459 while (lenEnd
< offset
)
1460 p
->opt
[++lenEnd
].price
= kInfinityPrice
;
1461 curAndLenPrice
= nextRepMatchPrice
+ GetRepPrice(p
, 0, lenTest2
, state2
, posStateNext
);
1462 opt
= &p
->opt
[offset
];
1463 if (curAndLenPrice
< opt
->price
)
1465 opt
->price
= curAndLenPrice
;
1466 opt
->posPrev
= cur
+ lenTest
+ 1;
1468 opt
->prev1IsChar
= True
;
1470 opt
->posPrev2
= cur
;
1471 opt
->backPrev2
= curBack
+ LZMA_NUM_REPS
;
1476 if (offs
== numPairs
)
1478 curBack
= matches
[offs
+ 1];
1479 if (curBack
>= kNumFullDistances
)
1480 GetPosSlot2(curBack
, posSlot
);
1487 #define ChangePair(smallDist, bigDist) (((bigDist) >> 7) > (smallDist))
1489 static UInt32
GetOptimumFast(CLzmaEnc
*p
, UInt32
*backRes
)
1491 UInt32 numAvail
, mainLen
, mainDist
, numPairs
, repIndex
, repLen
, i
;
1493 const UInt32
*matches
;
1495 if (p
->additionalOffset
== 0)
1496 mainLen
= ReadMatchDistances(p
, &numPairs
);
1499 mainLen
= p
->longestMatchLength
;
1500 numPairs
= p
->numPairs
;
1503 numAvail
= p
->numAvail
;
1504 *backRes
= (UInt32
)-1;
1507 if (numAvail
> LZMA_MATCH_LEN_MAX
)
1508 numAvail
= LZMA_MATCH_LEN_MAX
;
1509 data
= p
->matchFinder
.GetPointerToCurrentPos(p
->matchFinderObj
) - 1;
1511 repLen
= repIndex
= 0;
1512 for (i
= 0; i
< LZMA_NUM_REPS
; i
++)
1515 const Byte
*data2
= data
- (p
->reps
[i
] + 1);
1516 if (data
[0] != data2
[0] || data
[1] != data2
[1])
1518 for (len
= 2; len
< numAvail
&& data
[len
] == data2
[len
]; len
++);
1519 if (len
>= p
->numFastBytes
)
1522 MovePos(p
, len
- 1);
1532 matches
= p
->matches
;
1533 if (mainLen
>= p
->numFastBytes
)
1535 *backRes
= matches
[numPairs
- 1] + LZMA_NUM_REPS
;
1536 MovePos(p
, mainLen
- 1);
1540 mainDist
= 0; /* for GCC */
1543 mainDist
= matches
[numPairs
- 1];
1544 while (numPairs
> 2 && mainLen
== matches
[numPairs
- 4] + 1)
1546 if (!ChangePair(matches
[numPairs
- 3], mainDist
))
1549 mainLen
= matches
[numPairs
- 2];
1550 mainDist
= matches
[numPairs
- 1];
1552 if (mainLen
== 2 && mainDist
>= 0x80)
1556 if (repLen
>= 2 && (
1557 (repLen
+ 1 >= mainLen
) ||
1558 (repLen
+ 2 >= mainLen
&& mainDist
>= (1 << 9)) ||
1559 (repLen
+ 3 >= mainLen
&& mainDist
>= (1 << 15))))
1561 *backRes
= repIndex
;
1562 MovePos(p
, repLen
- 1);
1566 if (mainLen
< 2 || numAvail
<= 2)
1569 p
->longestMatchLength
= ReadMatchDistances(p
, &p
->numPairs
);
1570 if (p
->longestMatchLength
>= 2)
1572 UInt32 newDistance
= matches
[p
->numPairs
- 1];
1573 if ((p
->longestMatchLength
>= mainLen
&& newDistance
< mainDist
) ||
1574 (p
->longestMatchLength
== mainLen
+ 1 && !ChangePair(mainDist
, newDistance
)) ||
1575 (p
->longestMatchLength
> mainLen
+ 1) ||
1576 (p
->longestMatchLength
+ 1 >= mainLen
&& mainLen
>= 3 && ChangePair(newDistance
, mainDist
)))
1580 data
= p
->matchFinder
.GetPointerToCurrentPos(p
->matchFinderObj
) - 1;
1581 for (i
= 0; i
< LZMA_NUM_REPS
; i
++)
1584 const Byte
*data2
= data
- (p
->reps
[i
] + 1);
1585 if (data
[0] != data2
[0] || data
[1] != data2
[1])
1587 limit
= mainLen
- 1;
1588 for (len
= 2; len
< limit
&& data
[len
] == data2
[len
]; len
++);
1592 *backRes
= mainDist
+ LZMA_NUM_REPS
;
1593 MovePos(p
, mainLen
- 2);
1597 static void WriteEndMarker(CLzmaEnc
*p
, UInt32 posState
)
1600 RangeEnc_EncodeBit(&p
->rc
, &p
->isMatch
[p
->state
][posState
], 1);
1601 RangeEnc_EncodeBit(&p
->rc
, &p
->isRep
[p
->state
], 0);
1602 p
->state
= kMatchNextStates
[p
->state
];
1603 len
= LZMA_MATCH_LEN_MIN
;
1604 LenEnc_Encode2(&p
->lenEnc
, &p
->rc
, len
- LZMA_MATCH_LEN_MIN
, posState
, !p
->fastMode
, p
->ProbPrices
);
1605 RcTree_Encode(&p
->rc
, p
->posSlotEncoder
[GetLenToPosState(len
)], kNumPosSlotBits
, (1 << kNumPosSlotBits
) - 1);
1606 RangeEnc_EncodeDirectBits(&p
->rc
, (((UInt32
)1 << 30) - 1) >> kNumAlignBits
, 30 - kNumAlignBits
);
1607 RcTree_ReverseEncode(&p
->rc
, p
->posAlignEncoder
, kNumAlignBits
, kAlignMask
);
1610 static SRes
CheckErrors(CLzmaEnc
*p
)
1612 if (p
->result
!= SZ_OK
)
1614 if (p
->rc
.res
!= SZ_OK
)
1615 p
->result
= SZ_ERROR_WRITE
;
1616 if (p
->matchFinderBase
.result
!= SZ_OK
)
1617 p
->result
= SZ_ERROR_READ
;
1618 if (p
->result
!= SZ_OK
)
1623 static SRes
Flush(CLzmaEnc
*p
, UInt32 nowPos
)
1625 /* ReleaseMFStream(); */
1627 if (p
->writeEndMark
)
1628 WriteEndMarker(p
, nowPos
& p
->pbMask
);
1629 RangeEnc_FlushData(&p
->rc
);
1630 RangeEnc_FlushStream(&p
->rc
);
1631 return CheckErrors(p
);
1634 static void FillAlignPrices(CLzmaEnc
*p
)
1637 for (i
= 0; i
< kAlignTableSize
; i
++)
1638 p
->alignPrices
[i
] = RcTree_ReverseGetPrice(p
->posAlignEncoder
, kNumAlignBits
, i
, p
->ProbPrices
);
1639 p
->alignPriceCount
= 0;
1642 static void FillDistancesPrices(CLzmaEnc
*p
)
1644 UInt32 tempPrices
[kNumFullDistances
];
1645 UInt32 i
, lenToPosState
;
1646 for (i
= kStartPosModelIndex
; i
< kNumFullDistances
; i
++)
1648 UInt32 posSlot
= GetPosSlot1(i
);
1649 UInt32 footerBits
= ((posSlot
>> 1) - 1);
1650 UInt32 base
= ((2 | (posSlot
& 1)) << footerBits
);
1651 tempPrices
[i
] = RcTree_ReverseGetPrice(p
->posEncoders
+ base
- posSlot
- 1, footerBits
, i
- base
, p
->ProbPrices
);
1654 for (lenToPosState
= 0; lenToPosState
< kNumLenToPosStates
; lenToPosState
++)
1657 const CLzmaProb
*encoder
= p
->posSlotEncoder
[lenToPosState
];
1658 UInt32
*posSlotPrices
= p
->posSlotPrices
[lenToPosState
];
1659 for (posSlot
= 0; posSlot
< p
->distTableSize
; posSlot
++)
1660 posSlotPrices
[posSlot
] = RcTree_GetPrice(encoder
, kNumPosSlotBits
, posSlot
, p
->ProbPrices
);
1661 for (posSlot
= kEndPosModelIndex
; posSlot
< p
->distTableSize
; posSlot
++)
1662 posSlotPrices
[posSlot
] += ((((posSlot
>> 1) - 1) - kNumAlignBits
) << kNumBitPriceShiftBits
);
1665 UInt32
*distancesPrices
= p
->distancesPrices
[lenToPosState
];
1667 for (i
= 0; i
< kStartPosModelIndex
; i
++)
1668 distancesPrices
[i
] = posSlotPrices
[i
];
1669 for (; i
< kNumFullDistances
; i
++)
1670 distancesPrices
[i
] = posSlotPrices
[GetPosSlot1(i
)] + tempPrices
[i
];
1673 p
->matchPriceCount
= 0;
1676 void LzmaEnc_Construct(CLzmaEnc
*p
)
1678 RangeEnc_Construct(&p
->rc
);
1679 MatchFinder_Construct(&p
->matchFinderBase
);
1681 MatchFinderMt_Construct(&p
->matchFinderMt
);
1682 p
->matchFinderMt
.MatchFinder
= &p
->matchFinderBase
;
1686 CLzmaEncProps props
;
1687 LzmaEncProps_Init(&props
);
1688 LzmaEnc_SetProps(p
, &props
);
1691 #ifndef LZMA_LOG_BSR
1692 LzmaEnc_FastPosInit(p
->g_FastPos
);
1695 LzmaEnc_InitPriceTables(p
->ProbPrices
);
1697 p
->saveState
.litProbs
= 0;
1700 CLzmaEncHandle
LzmaEnc_Create(ISzAlloc
*alloc
)
1703 p
= alloc
->Alloc(alloc
, sizeof(CLzmaEnc
));
1705 LzmaEnc_Construct((CLzmaEnc
*)p
);
1709 void LzmaEnc_FreeLits(CLzmaEnc
*p
, ISzAlloc
*alloc
)
1711 alloc
->Free(alloc
, p
->litProbs
);
1712 alloc
->Free(alloc
, p
->saveState
.litProbs
);
1714 p
->saveState
.litProbs
= 0;
1717 void LzmaEnc_Destruct(CLzmaEnc
*p
, ISzAlloc
*alloc
, ISzAlloc
*allocBig
)
1720 MatchFinderMt_Destruct(&p
->matchFinderMt
, allocBig
);
1722 MatchFinder_Free(&p
->matchFinderBase
, allocBig
);
1723 LzmaEnc_FreeLits(p
, alloc
);
1724 RangeEnc_Free(&p
->rc
, alloc
);
1727 void LzmaEnc_Destroy(CLzmaEncHandle p
, ISzAlloc
*alloc
, ISzAlloc
*allocBig
)
1729 LzmaEnc_Destruct((CLzmaEnc
*)p
, alloc
, allocBig
);
1730 alloc
->Free(alloc
, p
);
1733 static SRes
LzmaEnc_CodeOneBlock(CLzmaEnc
*p
, Bool useLimits
, UInt32 maxPackSize
, UInt32 maxUnpackSize
)
1735 UInt32 nowPos32
, startPos32
;
1738 p
->matchFinder
.Init(p
->matchFinderObj
);
1744 RINOK(CheckErrors(p
));
1746 nowPos32
= (UInt32
)p
->nowPos64
;
1747 startPos32
= nowPos32
;
1749 if (p
->nowPos64
== 0)
1753 if (p
->matchFinder
.GetNumAvailableBytes(p
->matchFinderObj
) == 0)
1754 return Flush(p
, nowPos32
);
1755 ReadMatchDistances(p
, &numPairs
);
1756 RangeEnc_EncodeBit(&p
->rc
, &p
->isMatch
[p
->state
][0], 0);
1757 p
->state
= kLiteralNextStates
[p
->state
];
1758 curByte
= p
->matchFinder
.GetIndexByte(p
->matchFinderObj
, 0 - p
->additionalOffset
);
1759 LitEnc_Encode(&p
->rc
, p
->litProbs
, curByte
);
1760 p
->additionalOffset
--;
1764 if (p
->matchFinder
.GetNumAvailableBytes(p
->matchFinderObj
) != 0)
1767 UInt32 pos
, len
, posState
;
1770 len
= GetOptimumFast(p
, &pos
);
1772 len
= GetOptimum(p
, nowPos32
, &pos
);
1775 printf("\n pos = %4X, len = %d pos = %d", nowPos32
, len
, pos
);
1778 posState
= nowPos32
& p
->pbMask
;
1779 if (len
== 1 && pos
== (UInt32
)-1)
1785 RangeEnc_EncodeBit(&p
->rc
, &p
->isMatch
[p
->state
][posState
], 0);
1786 data
= p
->matchFinder
.GetPointerToCurrentPos(p
->matchFinderObj
) - p
->additionalOffset
;
1788 probs
= LIT_PROBS(nowPos32
, *(data
- 1));
1789 if (IsCharState(p
->state
))
1790 LitEnc_Encode(&p
->rc
, probs
, curByte
);
1792 LitEnc_EncodeMatched(&p
->rc
, probs
, curByte
, *(data
- p
->reps
[0] - 1));
1793 p
->state
= kLiteralNextStates
[p
->state
];
1797 RangeEnc_EncodeBit(&p
->rc
, &p
->isMatch
[p
->state
][posState
], 1);
1798 if (pos
< LZMA_NUM_REPS
)
1800 RangeEnc_EncodeBit(&p
->rc
, &p
->isRep
[p
->state
], 1);
1803 RangeEnc_EncodeBit(&p
->rc
, &p
->isRepG0
[p
->state
], 0);
1804 RangeEnc_EncodeBit(&p
->rc
, &p
->isRep0Long
[p
->state
][posState
], ((len
== 1) ? 0 : 1));
1808 UInt32 distance
= p
->reps
[pos
];
1809 RangeEnc_EncodeBit(&p
->rc
, &p
->isRepG0
[p
->state
], 1);
1811 RangeEnc_EncodeBit(&p
->rc
, &p
->isRepG1
[p
->state
], 0);
1814 RangeEnc_EncodeBit(&p
->rc
, &p
->isRepG1
[p
->state
], 1);
1815 RangeEnc_EncodeBit(&p
->rc
, &p
->isRepG2
[p
->state
], pos
- 2);
1817 p
->reps
[3] = p
->reps
[2];
1818 p
->reps
[2] = p
->reps
[1];
1820 p
->reps
[1] = p
->reps
[0];
1821 p
->reps
[0] = distance
;
1824 p
->state
= kShortRepNextStates
[p
->state
];
1827 LenEnc_Encode2(&p
->repLenEnc
, &p
->rc
, len
- LZMA_MATCH_LEN_MIN
, posState
, !p
->fastMode
, p
->ProbPrices
);
1828 p
->state
= kRepNextStates
[p
->state
];
1834 RangeEnc_EncodeBit(&p
->rc
, &p
->isRep
[p
->state
], 0);
1835 p
->state
= kMatchNextStates
[p
->state
];
1836 LenEnc_Encode2(&p
->lenEnc
, &p
->rc
, len
- LZMA_MATCH_LEN_MIN
, posState
, !p
->fastMode
, p
->ProbPrices
);
1837 pos
-= LZMA_NUM_REPS
;
1838 GetPosSlot(pos
, posSlot
);
1839 RcTree_Encode(&p
->rc
, p
->posSlotEncoder
[GetLenToPosState(len
)], kNumPosSlotBits
, posSlot
);
1841 if (posSlot
>= kStartPosModelIndex
)
1843 UInt32 footerBits
= ((posSlot
>> 1) - 1);
1844 UInt32 base
= ((2 | (posSlot
& 1)) << footerBits
);
1845 UInt32 posReduced
= pos
- base
;
1847 if (posSlot
< kEndPosModelIndex
)
1848 RcTree_ReverseEncode(&p
->rc
, p
->posEncoders
+ base
- posSlot
- 1, footerBits
, posReduced
);
1851 RangeEnc_EncodeDirectBits(&p
->rc
, posReduced
>> kNumAlignBits
, footerBits
- kNumAlignBits
);
1852 RcTree_ReverseEncode(&p
->rc
, p
->posAlignEncoder
, kNumAlignBits
, posReduced
& kAlignMask
);
1853 p
->alignPriceCount
++;
1856 p
->reps
[3] = p
->reps
[2];
1857 p
->reps
[2] = p
->reps
[1];
1858 p
->reps
[1] = p
->reps
[0];
1860 p
->matchPriceCount
++;
1863 p
->additionalOffset
-= len
;
1865 if (p
->additionalOffset
== 0)
1870 if (p
->matchPriceCount
>= (1 << 7))
1871 FillDistancesPrices(p
);
1872 if (p
->alignPriceCount
>= kAlignTableSize
)
1875 if (p
->matchFinder
.GetNumAvailableBytes(p
->matchFinderObj
) == 0)
1877 processed
= nowPos32
- startPos32
;
1880 if (processed
+ kNumOpts
+ 300 >= maxUnpackSize
||
1881 RangeEnc_GetProcessed(&p
->rc
) + kNumOpts
* 2 >= maxPackSize
)
1884 else if (processed
>= (1 << 15))
1886 p
->nowPos64
+= nowPos32
- startPos32
;
1887 return CheckErrors(p
);
1891 p
->nowPos64
+= nowPos32
- startPos32
;
1892 return Flush(p
, nowPos32
);
1895 #define kBigHashDicLimit ((UInt32)1 << 24)
1897 static SRes
LzmaEnc_Alloc(CLzmaEnc
*p
, UInt32 keepWindowSize
, ISzAlloc
*alloc
, ISzAlloc
*allocBig
)
1899 UInt32 beforeSize
= kNumOpts
;
1901 if (!RangeEnc_Alloc(&p
->rc
, alloc
))
1902 return SZ_ERROR_MEM
;
1903 btMode
= (p
->matchFinderBase
.btMode
!= 0);
1905 p
->mtMode
= (p
->multiThread
&& !p
->fastMode
&& btMode
);
1909 unsigned lclp
= p
->lc
+ p
->lp
;
1910 if (p
->litProbs
== 0 || p
->saveState
.litProbs
== 0 || p
->lclp
!= lclp
)
1912 LzmaEnc_FreeLits(p
, alloc
);
1913 p
->litProbs
= (CLzmaProb
*)alloc
->Alloc(alloc
, (0x300 << lclp
) * sizeof(CLzmaProb
));
1914 p
->saveState
.litProbs
= (CLzmaProb
*)alloc
->Alloc(alloc
, (0x300 << lclp
) * sizeof(CLzmaProb
));
1915 if (p
->litProbs
== 0 || p
->saveState
.litProbs
== 0)
1917 LzmaEnc_FreeLits(p
, alloc
);
1918 return SZ_ERROR_MEM
;
1924 p
->matchFinderBase
.bigHash
= (p
->dictSize
> kBigHashDicLimit
);
1926 if (beforeSize
+ p
->dictSize
< keepWindowSize
)
1927 beforeSize
= keepWindowSize
- p
->dictSize
;
1932 RINOK(MatchFinderMt_Create(&p
->matchFinderMt
, p
->dictSize
, beforeSize
, p
->numFastBytes
, LZMA_MATCH_LEN_MAX
, allocBig
));
1933 p
->matchFinderObj
= &p
->matchFinderMt
;
1934 MatchFinderMt_CreateVTable(&p
->matchFinderMt
, &p
->matchFinder
);
1939 if (!MatchFinder_Create(&p
->matchFinderBase
, p
->dictSize
, beforeSize
, p
->numFastBytes
, LZMA_MATCH_LEN_MAX
, allocBig
))
1940 return SZ_ERROR_MEM
;
1941 p
->matchFinderObj
= &p
->matchFinderBase
;
1942 MatchFinder_CreateVTable(&p
->matchFinderBase
, &p
->matchFinder
);
1947 void LzmaEnc_Init(CLzmaEnc
*p
)
1951 for (i
= 0 ; i
< LZMA_NUM_REPS
; i
++)
1954 RangeEnc_Init(&p
->rc
);
1957 for (i
= 0; i
< kNumStates
; i
++)
1960 for (j
= 0; j
< LZMA_NUM_PB_STATES_MAX
; j
++)
1962 p
->isMatch
[i
][j
] = kProbInitValue
;
1963 p
->isRep0Long
[i
][j
] = kProbInitValue
;
1965 p
->isRep
[i
] = kProbInitValue
;
1966 p
->isRepG0
[i
] = kProbInitValue
;
1967 p
->isRepG1
[i
] = kProbInitValue
;
1968 p
->isRepG2
[i
] = kProbInitValue
;
1972 UInt32 num
= 0x300 << (p
->lp
+ p
->lc
);
1973 for (i
= 0; i
< num
; i
++)
1974 p
->litProbs
[i
] = kProbInitValue
;
1978 for (i
= 0; i
< kNumLenToPosStates
; i
++)
1980 CLzmaProb
*probs
= p
->posSlotEncoder
[i
];
1982 for (j
= 0; j
< (1 << kNumPosSlotBits
); j
++)
1983 probs
[j
] = kProbInitValue
;
1987 for (i
= 0; i
< kNumFullDistances
- kEndPosModelIndex
; i
++)
1988 p
->posEncoders
[i
] = kProbInitValue
;
1991 LenEnc_Init(&p
->lenEnc
.p
);
1992 LenEnc_Init(&p
->repLenEnc
.p
);
1994 for (i
= 0; i
< (1 << kNumAlignBits
); i
++)
1995 p
->posAlignEncoder
[i
] = kProbInitValue
;
1997 p
->optimumEndIndex
= 0;
1998 p
->optimumCurrentIndex
= 0;
1999 p
->additionalOffset
= 0;
2001 p
->pbMask
= (1 << p
->pb
) - 1;
2002 p
->lpMask
= (1 << p
->lp
) - 1;
2005 void LzmaEnc_InitPrices(CLzmaEnc
*p
)
2009 FillDistancesPrices(p
);
2013 p
->lenEnc
.tableSize
=
2014 p
->repLenEnc
.tableSize
=
2015 p
->numFastBytes
+ 1 - LZMA_MATCH_LEN_MIN
;
2016 LenPriceEnc_UpdateTables(&p
->lenEnc
, 1 << p
->pb
, p
->ProbPrices
);
2017 LenPriceEnc_UpdateTables(&p
->repLenEnc
, 1 << p
->pb
, p
->ProbPrices
);
2020 static SRes
LzmaEnc_AllocAndInit(CLzmaEnc
*p
, UInt32 keepWindowSize
, ISzAlloc
*alloc
, ISzAlloc
*allocBig
)
2023 for (i
= 0; i
< (UInt32
)kDicLogSizeMaxCompress
; i
++)
2024 if (p
->dictSize
<= ((UInt32
)1 << i
))
2026 p
->distTableSize
= i
* 2;
2028 p
->finished
= False
;
2030 RINOK(LzmaEnc_Alloc(p
, keepWindowSize
, alloc
, allocBig
));
2032 LzmaEnc_InitPrices(p
);
2037 static SRes
LzmaEnc_Prepare(CLzmaEncHandle pp
, ISeqOutStream
*outStream
, ISeqInStream
*inStream
,
2038 ISzAlloc
*alloc
, ISzAlloc
*allocBig
)
2040 CLzmaEnc
*p
= (CLzmaEnc
*)pp
;
2041 p
->matchFinderBase
.stream
= inStream
;
2043 p
->rc
.outStream
= outStream
;
2044 return LzmaEnc_AllocAndInit(p
, 0, alloc
, allocBig
);
2047 SRes
LzmaEnc_PrepareForLzma2(CLzmaEncHandle pp
,
2048 ISeqInStream
*inStream
, UInt32 keepWindowSize
,
2049 ISzAlloc
*alloc
, ISzAlloc
*allocBig
)
2051 CLzmaEnc
*p
= (CLzmaEnc
*)pp
;
2052 p
->matchFinderBase
.stream
= inStream
;
2054 return LzmaEnc_AllocAndInit(p
, keepWindowSize
, alloc
, allocBig
);
2057 static void LzmaEnc_SetInputBuf(CLzmaEnc
*p
, const Byte
*src
, SizeT srcLen
)
2059 p
->matchFinderBase
.directInput
= 1;
2060 p
->matchFinderBase
.bufferBase
= (Byte
*)src
;
2061 p
->matchFinderBase
.directInputRem
= srcLen
;
2064 SRes
LzmaEnc_MemPrepare(CLzmaEncHandle pp
, const Byte
*src
, SizeT srcLen
,
2065 UInt32 keepWindowSize
, ISzAlloc
*alloc
, ISzAlloc
*allocBig
)
2067 CLzmaEnc
*p
= (CLzmaEnc
*)pp
;
2068 LzmaEnc_SetInputBuf(p
, src
, srcLen
);
2071 return LzmaEnc_AllocAndInit(p
, keepWindowSize
, alloc
, allocBig
);
2074 void LzmaEnc_Finish(CLzmaEncHandle pp
)
2077 CLzmaEnc
*p
= (CLzmaEnc
*)pp
;
2079 MatchFinderMt_ReleaseStream(&p
->matchFinderMt
);
2087 ISeqOutStream funcTable
;
2093 static size_t MyWrite(void *pp
, const void *data
, size_t size
)
2095 CSeqOutStreamBuf
*p
= (CSeqOutStreamBuf
*)pp
;
2101 memcpy(p
->data
, data
, size
);
2108 UInt32
LzmaEnc_GetNumAvailableBytes(CLzmaEncHandle pp
)
2110 const CLzmaEnc
*p
= (CLzmaEnc
*)pp
;
2111 return p
->matchFinder
.GetNumAvailableBytes(p
->matchFinderObj
);
2114 const Byte
*LzmaEnc_GetCurBuf(CLzmaEncHandle pp
)
2116 const CLzmaEnc
*p
= (CLzmaEnc
*)pp
;
2117 return p
->matchFinder
.GetPointerToCurrentPos(p
->matchFinderObj
) - p
->additionalOffset
;
2120 SRes
LzmaEnc_CodeOneMemBlock(CLzmaEncHandle pp
, Bool reInit
,
2121 Byte
*dest
, size_t *destLen
, UInt32 desiredPackSize
, UInt32
*unpackSize
)
2123 CLzmaEnc
*p
= (CLzmaEnc
*)pp
;
2126 CSeqOutStreamBuf outStream
;
2128 outStream
.funcTable
.Write
= MyWrite
;
2129 outStream
.data
= dest
;
2130 outStream
.rem
= *destLen
;
2131 outStream
.overflow
= False
;
2133 p
->writeEndMark
= False
;
2134 p
->finished
= False
;
2139 LzmaEnc_InitPrices(p
);
2140 nowPos64
= p
->nowPos64
;
2141 RangeEnc_Init(&p
->rc
);
2142 p
->rc
.outStream
= &outStream
.funcTable
;
2144 res
= LzmaEnc_CodeOneBlock(p
, True
, desiredPackSize
, *unpackSize
);
2146 *unpackSize
= (UInt32
)(p
->nowPos64
- nowPos64
);
2147 *destLen
-= outStream
.rem
;
2148 if (outStream
.overflow
)
2149 return SZ_ERROR_OUTPUT_EOF
;
2154 static SRes
LzmaEnc_Encode2(CLzmaEnc
*p
, ICompressProgress
*progress
)
2159 Byte allocaDummy
[0x300];
2161 for (i
= 0; i
< 16; i
++)
2162 allocaDummy
[i
] = (Byte
)i
;
2167 res
= LzmaEnc_CodeOneBlock(p
, False
, 0, 0);
2168 if (res
!= SZ_OK
|| p
->finished
!= 0)
2172 res
= progress
->Progress(progress
, p
->nowPos64
, RangeEnc_GetProcessed(&p
->rc
));
2175 res
= SZ_ERROR_PROGRESS
;
2184 SRes
LzmaEnc_Encode(CLzmaEncHandle pp
, ISeqOutStream
*outStream
, ISeqInStream
*inStream
, ICompressProgress
*progress
,
2185 ISzAlloc
*alloc
, ISzAlloc
*allocBig
)
2187 RINOK(LzmaEnc_Prepare(pp
, outStream
, inStream
, alloc
, allocBig
));
2188 return LzmaEnc_Encode2((CLzmaEnc
*)pp
, progress
);
2191 SRes
LzmaEnc_WriteProperties(CLzmaEncHandle pp
, Byte
*props
, SizeT
*size
)
2193 CLzmaEnc
*p
= (CLzmaEnc
*)pp
;
2195 UInt32 dictSize
= p
->dictSize
;
2196 if (*size
< LZMA_PROPS_SIZE
)
2197 return SZ_ERROR_PARAM
;
2198 *size
= LZMA_PROPS_SIZE
;
2199 props
[0] = (Byte
)((p
->pb
* 5 + p
->lp
) * 9 + p
->lc
);
2201 for (i
= 11; i
<= 30; i
++)
2203 if (dictSize
<= ((UInt32
)2 << i
))
2205 dictSize
= (2 << i
);
2208 if (dictSize
<= ((UInt32
)3 << i
))
2210 dictSize
= (3 << i
);
2215 for (i
= 0; i
< 4; i
++)
2216 props
[1 + i
] = (Byte
)(dictSize
>> (8 * i
));
2220 SRes
LzmaEnc_MemEncode(CLzmaEncHandle pp
, Byte
*dest
, SizeT
*destLen
, const Byte
*src
, SizeT srcLen
,
2221 int writeEndMark
, ICompressProgress
*progress
, ISzAlloc
*alloc
, ISzAlloc
*allocBig
)
2224 CLzmaEnc
*p
= (CLzmaEnc
*)pp
;
2226 CSeqOutStreamBuf outStream
;
2228 LzmaEnc_SetInputBuf(p
, src
, srcLen
);
2230 outStream
.funcTable
.Write
= MyWrite
;
2231 outStream
.data
= dest
;
2232 outStream
.rem
= *destLen
;
2233 outStream
.overflow
= False
;
2235 p
->writeEndMark
= writeEndMark
;
2237 p
->rc
.outStream
= &outStream
.funcTable
;
2238 res
= LzmaEnc_MemPrepare(pp
, src
, srcLen
, 0, alloc
, allocBig
);
2240 res
= LzmaEnc_Encode2(p
, progress
);
2242 *destLen
-= outStream
.rem
;
2243 if (outStream
.overflow
)
2244 return SZ_ERROR_OUTPUT_EOF
;
2248 SRes
LzmaEncode(Byte
*dest
, SizeT
*destLen
, const Byte
*src
, SizeT srcLen
,
2249 const CLzmaEncProps
*props
, Byte
*propsEncoded
, SizeT
*propsSize
, int writeEndMark
,
2250 ICompressProgress
*progress
, ISzAlloc
*alloc
, ISzAlloc
*allocBig
)
2252 CLzmaEnc
*p
= (CLzmaEnc
*)LzmaEnc_Create(alloc
);
2255 return SZ_ERROR_MEM
;
2257 res
= LzmaEnc_SetProps(p
, props
);
2260 res
= LzmaEnc_WriteProperties(p
, propsEncoded
, propsSize
);
2262 res
= LzmaEnc_MemEncode(p
, dest
, destLen
, src
, srcLen
,
2263 writeEndMark
, progress
, alloc
, allocBig
);
2266 LzmaEnc_Destroy(p
, alloc
, allocBig
);