1 /* LzmaEnc.c -- LZMA Encoder
2 2019-01-10: Igor Pavlov : Public domain */
8 /* #define SHOW_STAT */
9 /* #define SHOW_STAT2 */
11 #if defined(SHOW_STAT) || defined(SHOW_STAT2)
23 static unsigned g_STAT_OFFSET
= 0;
26 #define kLzmaMaxHistorySize ((UInt32)3 << 29)
27 /* #define kLzmaMaxHistorySize ((UInt32)7 << 29) */
29 #define kNumTopBits 24
30 #define kTopValue ((UInt32)1 << kNumTopBits)
32 #define kNumBitModelTotalBits 11
33 #define kBitModelTotal (1 << kNumBitModelTotalBits)
34 #define kNumMoveBits 5
35 #define kProbInitValue (kBitModelTotal >> 1)
37 #define kNumMoveReducingBits 4
38 #define kNumBitPriceShiftBits 4
39 #define kBitPrice (1 << kNumBitPriceShiftBits)
41 #define REP_LEN_COUNT 64
43 void LzmaEncProps_Init(CLzmaEncProps
*p
)
46 p
->dictSize
= p
->mc
= 0;
47 p
->reduceSize
= (UInt64
)(Int64
)-1;
48 p
->lc
= p
->lp
= p
->pb
= p
->algo
= p
->fb
= p
->btMode
= p
->numHashBytes
= p
->numThreads
= -1;
52 void LzmaEncProps_Normalize(CLzmaEncProps
*p
)
55 if (level
< 0) level
= 5;
58 if (p
->dictSize
== 0) p
->dictSize
= (level
<= 5 ? (1 << (level
* 2 + 14)) : (level
<= 7 ? (1 << 25) : (1 << 26)));
59 if (p
->dictSize
> p
->reduceSize
)
62 UInt32 reduceSize
= (UInt32
)p
->reduceSize
;
63 for (i
= 11; i
<= 30; i
++)
65 if (reduceSize
<= ((UInt32
)2 << i
)) { p
->dictSize
= ((UInt32
)2 << i
); break; }
66 if (reduceSize
<= ((UInt32
)3 << i
)) { p
->dictSize
= ((UInt32
)3 << i
); break; }
70 if (p
->lc
< 0) p
->lc
= 3;
71 if (p
->lp
< 0) p
->lp
= 0;
72 if (p
->pb
< 0) p
->pb
= 2;
74 if (p
->algo
< 0) p
->algo
= (level
< 5 ? 0 : 1);
75 if (p
->fb
< 0) p
->fb
= (level
< 7 ? 32 : 64);
76 if (p
->btMode
< 0) p
->btMode
= (p
->algo
== 0 ? 0 : 1);
77 if (p
->numHashBytes
< 0) p
->numHashBytes
= 4;
78 if (p
->mc
== 0) p
->mc
= (16 + (p
->fb
>> 1)) >> (p
->btMode
? 0 : 1);
80 if (p
->numThreads
< 0)
83 ((p
->btMode
&& p
->algo
) ? 2 : 1);
89 UInt32
LzmaEncProps_GetDictSize(const CLzmaEncProps
*props2
)
91 CLzmaEncProps props
= *props2
;
92 LzmaEncProps_Normalize(&props
);
93 return props
.dictSize
;
96 #if (_MSC_VER >= 1400)
97 /* BSR code is fast for some new CPUs */
98 /* #define LZMA_LOG_BSR */
103 #define kDicLogSizeMaxCompress 32
105 #define BSR2_RET(pos, res) { unsigned long zz; _BitScanReverse(&zz, (pos)); res = (zz + zz) + ((pos >> (zz - 1)) & 1); }
107 static unsigned GetPosSlot1(UInt32 pos
)
113 #define GetPosSlot2(pos, res) { BSR2_RET(pos, res); }
114 #define GetPosSlot(pos, res) { if (pos < 2) res = pos; else BSR2_RET(pos, res); }
118 #define kNumLogBits (9 + sizeof(size_t) / 2)
119 /* #define kNumLogBits (11 + sizeof(size_t) / 8 * 3) */
121 #define kDicLogSizeMaxCompress ((kNumLogBits - 1) * 2 + 7)
123 static void LzmaEnc_FastPosInit(Byte
*g_FastPos
)
130 for (slot
= 2; slot
< kNumLogBits
* 2; slot
++)
132 size_t k
= ((size_t)1 << ((slot
>> 1) - 1));
134 for (j
= 0; j
< k
; j
++)
135 g_FastPos
[j
] = (Byte
)slot
;
140 /* we can use ((limit - pos) >> 31) only if (pos < ((UInt32)1 << 31)) */
142 #define BSR2_RET(pos, res) { unsigned zz = 6 + ((kNumLogBits - 1) & \
143 (0 - (((((UInt32)1 << (kNumLogBits + 6)) - 1) - pos) >> 31))); \
144 res = p->g_FastPos[pos >> zz] + (zz * 2); }
148 #define BSR2_RET(pos, res) { unsigned zz = 6 + ((kNumLogBits - 1) & \
149 (0 - (((((UInt32)1 << (kNumLogBits)) - 1) - (pos >> 6)) >> 31))); \
150 res = p->g_FastPos[pos >> zz] + (zz * 2); }
153 #define BSR2_RET(pos, res) { unsigned zz = (pos < (1 << (kNumLogBits + 6))) ? 6 : 6 + kNumLogBits - 1; \
154 res = p->g_FastPos[pos >> zz] + (zz * 2); }
157 #define BSR2_RET(pos, res) { res = (pos < (1 << (kNumLogBits + 6))) ? \
158 p->g_FastPos[pos >> 6] + 12 : \
159 p->g_FastPos[pos >> (6 + kNumLogBits - 1)] + (6 + (kNumLogBits - 1)) * 2; }
162 #define GetPosSlot1(pos) p->g_FastPos[pos]
163 #define GetPosSlot2(pos, res) { BSR2_RET(pos, res); }
164 #define GetPosSlot(pos, res) { if (pos < kNumFullDistances) res = p->g_FastPos[pos & (kNumFullDistances - 1)]; else BSR2_RET(pos, res); }
169 #define LZMA_NUM_REPS 4
171 typedef UInt16 CState
;
172 typedef UInt16 CExtra
;
181 // > 1 : MATCH (extra-1) : LIT : REP0 (len)
184 UInt32 reps
[LZMA_NUM_REPS
];
189 #define kNumOpts (1 << 11)
190 #define kPackReserve (kNumOpts * 8)
191 // #define kNumOpts (1 << 12)
192 // #define kPackReserve (1 + kNumOpts * 2)
194 #define kNumLenToPosStates 4
195 #define kNumPosSlotBits 6
196 #define kDicLogSizeMin 0
197 #define kDicLogSizeMax 32
198 #define kDistTableSizeMax (kDicLogSizeMax * 2)
200 #define kNumAlignBits 4
201 #define kAlignTableSize (1 << kNumAlignBits)
202 #define kAlignMask (kAlignTableSize - 1)
204 #define kStartPosModelIndex 4
205 #define kEndPosModelIndex 14
206 #define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
216 #define LZMA_PB_MAX 4
217 #define LZMA_LC_MAX 8
218 #define LZMA_LP_MAX 4
220 #define LZMA_NUM_PB_STATES_MAX (1 << LZMA_PB_MAX)
222 #define kLenNumLowBits 3
223 #define kLenNumLowSymbols (1 << kLenNumLowBits)
224 #define kLenNumHighBits 8
225 #define kLenNumHighSymbols (1 << kLenNumHighBits)
226 #define kLenNumSymbolsTotal (kLenNumLowSymbols * 2 + kLenNumHighSymbols)
228 #define LZMA_MATCH_LEN_MIN 2
229 #define LZMA_MATCH_LEN_MAX (LZMA_MATCH_LEN_MIN + kLenNumSymbolsTotal - 1)
231 #define kNumStates 12
236 CLzmaProb low
[LZMA_NUM_PB_STATES_MAX
<< (kLenNumLowBits
+ 1)];
237 CLzmaProb high
[kLenNumHighSymbols
];
244 UInt32 prices
[LZMA_NUM_PB_STATES_MAX
][kLenNumSymbolsTotal
];
245 // UInt32 prices1[LZMA_NUM_PB_STATES_MAX][kLenNumLowSymbols * 2];
246 // UInt32 prices2[kLenNumSymbolsTotal];
249 #define GET_PRICE_LEN(p, posState, len) \
250 ((p)->prices[posState][(size_t)(len) - LZMA_MATCH_LEN_MIN])
253 #define GET_PRICE_LEN(p, posState, len) \
254 ((p)->prices2[(size_t)(len) - 2] + ((p)->prices1[posState][((len) - 2) & (kLenNumLowSymbols * 2 - 1)] & (((len) - 2 - kLenNumLowSymbols * 2) >> 9)))
266 ISeqOutStream
*outStream
;
277 UInt32 reps
[LZMA_NUM_REPS
];
279 CLzmaProb posAlignEncoder
[1 << kNumAlignBits
];
280 CLzmaProb isRep
[kNumStates
];
281 CLzmaProb isRepG0
[kNumStates
];
282 CLzmaProb isRepG1
[kNumStates
];
283 CLzmaProb isRepG2
[kNumStates
];
284 CLzmaProb isMatch
[kNumStates
][LZMA_NUM_PB_STATES_MAX
];
285 CLzmaProb isRep0Long
[kNumStates
][LZMA_NUM_PB_STATES_MAX
];
287 CLzmaProb posSlotEncoder
[kNumLenToPosStates
][1 << kNumPosSlotBits
];
288 CLzmaProb posEncoders
[kNumFullDistances
];
296 typedef UInt32 CProbPrice
;
301 void *matchFinderObj
;
302 IMatchFinder matchFinder
;
307 unsigned longestMatchLen
;
312 unsigned numFastBytes
;
313 unsigned additionalOffset
;
314 UInt32 reps
[LZMA_NUM_REPS
];
315 unsigned lpMask
, pbMask
;
325 BoolInt writeEndMark
;
333 unsigned matchPriceCount
;
334 // unsigned alignPriceCount;
335 int repLenEncCounter
;
337 unsigned distTableSize
;
344 // begin of CMatchFinderMt is used in LZ thread
345 CMatchFinderMt matchFinderMt
;
346 // end of CMatchFinderMt is used in BT and HASH threads
349 CMatchFinder matchFinderBase
;
356 CProbPrice ProbPrices
[kBitModelTotal
>> kNumMoveReducingBits
];
358 UInt32 matches
[LZMA_MATCH_LEN_MAX
* 2 + 2 + 1];
360 UInt32 alignPrices
[kAlignTableSize
];
361 UInt32 posSlotPrices
[kNumLenToPosStates
][kDistTableSizeMax
];
362 UInt32 distancesPrices
[kNumLenToPosStates
][kNumFullDistances
];
364 CLzmaProb posAlignEncoder
[1 << kNumAlignBits
];
365 CLzmaProb isRep
[kNumStates
];
366 CLzmaProb isRepG0
[kNumStates
];
367 CLzmaProb isRepG1
[kNumStates
];
368 CLzmaProb isRepG2
[kNumStates
];
369 CLzmaProb isMatch
[kNumStates
][LZMA_NUM_PB_STATES_MAX
];
370 CLzmaProb isRep0Long
[kNumStates
][LZMA_NUM_PB_STATES_MAX
];
371 CLzmaProb posSlotEncoder
[kNumLenToPosStates
][1 << kNumPosSlotBits
];
372 CLzmaProb posEncoders
[kNumFullDistances
];
378 Byte g_FastPos
[1 << kNumLogBits
];
382 CLenPriceEnc repLenEnc
;
384 COptimal opt
[kNumOpts
];
386 CSaveState saveState
;
395 #define COPY_ARR(dest, src, arr) memcpy(dest->arr, src->arr, sizeof(src->arr));
397 void LzmaEnc_SaveState(CLzmaEncHandle pp
)
399 CLzmaEnc
*p
= (CLzmaEnc
*)pp
;
400 CSaveState
*dest
= &p
->saveState
;
402 dest
->state
= p
->state
;
404 dest
->lenProbs
= p
->lenProbs
;
405 dest
->repLenProbs
= p
->repLenProbs
;
407 COPY_ARR(dest
, p
, reps
);
409 COPY_ARR(dest
, p
, posAlignEncoder
);
410 COPY_ARR(dest
, p
, isRep
);
411 COPY_ARR(dest
, p
, isRepG0
);
412 COPY_ARR(dest
, p
, isRepG1
);
413 COPY_ARR(dest
, p
, isRepG2
);
414 COPY_ARR(dest
, p
, isMatch
);
415 COPY_ARR(dest
, p
, isRep0Long
);
416 COPY_ARR(dest
, p
, posSlotEncoder
);
417 COPY_ARR(dest
, p
, posEncoders
);
419 memcpy(dest
->litProbs
, p
->litProbs
, ((UInt32
)0x300 << p
->lclp
) * sizeof(CLzmaProb
));
423 void LzmaEnc_RestoreState(CLzmaEncHandle pp
)
425 CLzmaEnc
*dest
= (CLzmaEnc
*)pp
;
426 const CSaveState
*p
= &dest
->saveState
;
428 dest
->state
= p
->state
;
430 dest
->lenProbs
= p
->lenProbs
;
431 dest
->repLenProbs
= p
->repLenProbs
;
433 COPY_ARR(dest
, p
, reps
);
435 COPY_ARR(dest
, p
, posAlignEncoder
);
436 COPY_ARR(dest
, p
, isRep
);
437 COPY_ARR(dest
, p
, isRepG0
);
438 COPY_ARR(dest
, p
, isRepG1
);
439 COPY_ARR(dest
, p
, isRepG2
);
440 COPY_ARR(dest
, p
, isMatch
);
441 COPY_ARR(dest
, p
, isRep0Long
);
442 COPY_ARR(dest
, p
, posSlotEncoder
);
443 COPY_ARR(dest
, p
, posEncoders
);
445 memcpy(dest
->litProbs
, p
->litProbs
, ((UInt32
)0x300 << dest
->lclp
) * sizeof(CLzmaProb
));
450 SRes
LzmaEnc_SetProps(CLzmaEncHandle pp
, const CLzmaEncProps
*props2
)
452 CLzmaEnc
*p
= (CLzmaEnc
*)pp
;
453 CLzmaEncProps props
= *props2
;
454 LzmaEncProps_Normalize(&props
);
456 if (props
.lc
> LZMA_LC_MAX
457 || props
.lp
> LZMA_LP_MAX
458 || props
.pb
> LZMA_PB_MAX
459 || props
.dictSize
> ((UInt64
)1 << kDicLogSizeMaxCompress
)
460 || props
.dictSize
> kLzmaMaxHistorySize
)
461 return SZ_ERROR_PARAM
;
463 p
->dictSize
= props
.dictSize
;
465 unsigned fb
= props
.fb
;
468 if (fb
> LZMA_MATCH_LEN_MAX
)
469 fb
= LZMA_MATCH_LEN_MAX
;
470 p
->numFastBytes
= fb
;
475 p
->fastMode
= (props
.algo
== 0);
476 // p->_maxMode = True;
477 p
->matchFinderBase
.btMode
= (Byte
)(props
.btMode
? 1 : 0);
479 unsigned numHashBytes
= 4;
482 if (props
.numHashBytes
< 2)
484 else if (props
.numHashBytes
< 4)
485 numHashBytes
= props
.numHashBytes
;
487 p
->matchFinderBase
.numHashBytes
= numHashBytes
;
490 p
->matchFinderBase
.cutValue
= props
.mc
;
492 p
->writeEndMark
= props
.writeEndMark
;
496 if (newMultiThread != _multiThread)
498 ReleaseMatchFinder();
499 _multiThread = newMultiThread;
502 p
->multiThread
= (props
.numThreads
> 1);
509 void LzmaEnc_SetDataSize(CLzmaEncHandle pp
, UInt64 expectedDataSiize
)
511 CLzmaEnc
*p
= (CLzmaEnc
*)pp
;
512 p
->matchFinderBase
.expectedDataSize
= expectedDataSiize
;
516 #define kState_Start 0
517 #define kState_LitAfterMatch 4
518 #define kState_LitAfterRep 5
519 #define kState_MatchAfterLit 7
520 #define kState_RepAfterLit 8
522 static const Byte kLiteralNextStates
[kNumStates
] = {0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5};
523 static const Byte kMatchNextStates
[kNumStates
] = {7, 7, 7, 7, 7, 7, 7, 10, 10, 10, 10, 10};
524 static const Byte kRepNextStates
[kNumStates
] = {8, 8, 8, 8, 8, 8, 8, 11, 11, 11, 11, 11};
525 static const Byte kShortRepNextStates
[kNumStates
]= {9, 9, 9, 9, 9, 9, 9, 11, 11, 11, 11, 11};
527 #define IsLitState(s) ((s) < 7)
528 #define GetLenToPosState2(len) (((len) < kNumLenToPosStates - 1) ? (len) : kNumLenToPosStates - 1)
529 #define GetLenToPosState(len) (((len) < kNumLenToPosStates + 1) ? (len) - 2 : kNumLenToPosStates - 1)
531 #define kInfinityPrice (1 << 30)
533 static void RangeEnc_Construct(CRangeEnc
*p
)
539 #define RangeEnc_GetProcessed(p) ((p)->processed + ((p)->buf - (p)->bufBase) + (p)->cacheSize)
540 #define RangeEnc_GetProcessed_sizet(p) ((size_t)(p)->processed + ((p)->buf - (p)->bufBase) + (size_t)(p)->cacheSize)
542 #define RC_BUF_SIZE (1 << 16)
544 static int RangeEnc_Alloc(CRangeEnc
*p
, ISzAllocPtr alloc
)
548 p
->bufBase
= (Byte
*)ISzAlloc_Alloc(alloc
, RC_BUF_SIZE
);
551 p
->bufLim
= p
->bufBase
+ RC_BUF_SIZE
;
556 static void RangeEnc_Free(CRangeEnc
*p
, ISzAllocPtr alloc
)
558 ISzAlloc_Free(alloc
, p
->bufBase
);
562 static void RangeEnc_Init(CRangeEnc
*p
)
565 p
->range
= 0xFFFFFFFF;
576 MY_NO_INLINE
static void RangeEnc_FlushStream(CRangeEnc
*p
)
581 num
= p
->buf
- p
->bufBase
;
582 if (num
!= ISeqOutStream_Write(p
->outStream
, p
->bufBase
, num
))
583 p
->res
= SZ_ERROR_WRITE
;
588 MY_NO_INLINE
static void MY_FAST_CALL
RangeEnc_ShiftLow(CRangeEnc
*p
)
590 UInt32 low
= (UInt32
)p
->low
;
591 unsigned high
= (unsigned)(p
->low
>> 32);
592 p
->low
= (UInt32
)(low
<< 8);
593 if (low
< (UInt32
)0xFF000000 || high
!= 0)
597 *buf
++ = (Byte
)(p
->cache
+ high
);
598 p
->cache
= (unsigned)(low
>> 24);
600 if (buf
== p
->bufLim
)
601 RangeEnc_FlushStream(p
);
602 if (p
->cacheSize
== 0)
609 *buf
++ = (Byte
)(high
);
611 if (buf
== p
->bufLim
)
612 RangeEnc_FlushStream(p
);
613 if (--p
->cacheSize
== 0)
620 static void RangeEnc_FlushData(CRangeEnc
*p
)
623 for (i
= 0; i
< 5; i
++)
624 RangeEnc_ShiftLow(p
);
627 #define RC_NORM(p) if (range < kTopValue) { range <<= 8; RangeEnc_ShiftLow(p); }
629 #define RC_BIT_PRE(p, prob) \
631 newBound = (range >> kNumBitModelTotalBits) * ttt;
633 // #define _LZMA_ENC_USE_BRANCH
635 #ifdef _LZMA_ENC_USE_BRANCH
637 #define RC_BIT(p, prob, bit) { \
638 RC_BIT_PRE(p, prob) \
639 if (bit == 0) { range = newBound; ttt += (kBitModelTotal - ttt) >> kNumMoveBits; } \
640 else { (p)->low += newBound; range -= newBound; ttt -= ttt >> kNumMoveBits; } \
641 *(prob) = (CLzmaProb)ttt; \
647 #define RC_BIT(p, prob, bit) { \
649 RC_BIT_PRE(p, prob) \
650 mask = 0 - (UInt32)bit; \
655 mask = (UInt32)bit - 1; \
656 range += newBound & mask; \
657 mask &= (kBitModelTotal - ((1 << kNumMoveBits) - 1)); \
658 mask += ((1 << kNumMoveBits) - 1); \
659 ttt += (Int32)(mask - ttt) >> kNumMoveBits; \
660 *(prob) = (CLzmaProb)ttt; \
669 #define RC_BIT_0_BASE(p, prob) \
670 range = newBound; *(prob) = (CLzmaProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits));
672 #define RC_BIT_1_BASE(p, prob) \
673 range -= newBound; (p)->low += newBound; *(prob) = (CLzmaProb)(ttt - (ttt >> kNumMoveBits)); \
675 #define RC_BIT_0(p, prob) \
676 RC_BIT_0_BASE(p, prob) \
679 #define RC_BIT_1(p, prob) \
680 RC_BIT_1_BASE(p, prob) \
683 static void RangeEnc_EncodeBit_0(CRangeEnc
*p
, CLzmaProb
*prob
)
685 UInt32 range
, ttt
, newBound
;
692 static void LitEnc_Encode(CRangeEnc
*p
, CLzmaProb
*probs
, UInt32 sym
)
694 UInt32 range
= p
->range
;
698 UInt32 ttt
, newBound
;
699 // RangeEnc_EncodeBit(p, probs + (sym >> 8), (sym >> 7) & 1);
700 CLzmaProb
*prob
= probs
+ (sym
>> 8);
701 UInt32 bit
= (sym
>> 7) & 1;
703 RC_BIT(p
, prob
, bit
);
705 while (sym
< 0x10000);
709 static void LitEnc_EncodeMatched(CRangeEnc
*p
, CLzmaProb
*probs
, UInt32 sym
, UInt32 matchByte
)
711 UInt32 range
= p
->range
;
716 UInt32 ttt
, newBound
;
720 // RangeEnc_EncodeBit(p, probs + (offs + (matchByte & offs) + (sym >> 8)), (sym >> 7) & 1);
721 prob
= probs
+ (offs
+ (matchByte
& offs
) + (sym
>> 8));
722 bit
= (sym
>> 7) & 1;
724 offs
&= ~(matchByte
^ sym
);
725 RC_BIT(p
, prob
, bit
);
727 while (sym
< 0x10000);
733 static void LzmaEnc_InitPriceTables(CProbPrice
*ProbPrices
)
736 for (i
= 0; i
< (kBitModelTotal
>> kNumMoveReducingBits
); i
++)
738 const unsigned kCyclesBits
= kNumBitPriceShiftBits
;
739 UInt32 w
= (i
<< kNumMoveReducingBits
) + (1 << (kNumMoveReducingBits
- 1));
740 unsigned bitCount
= 0;
742 for (j
= 0; j
< kCyclesBits
; j
++)
746 while (w
>= ((UInt32
)1 << 16))
752 ProbPrices
[i
] = (CProbPrice
)((kNumBitModelTotalBits
<< kCyclesBits
) - 15 - bitCount
);
753 // printf("\n%3d: %5d", i, ProbPrices[i]);
758 #define GET_PRICE(prob, bit) \
759 p->ProbPrices[((prob) ^ (unsigned)(((-(int)(bit))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits];
761 #define GET_PRICEa(prob, bit) \
762 ProbPrices[((prob) ^ (unsigned)((-((int)(bit))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits];
764 #define GET_PRICE_0(prob) p->ProbPrices[(prob) >> kNumMoveReducingBits]
765 #define GET_PRICE_1(prob) p->ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]
767 #define GET_PRICEa_0(prob) ProbPrices[(prob) >> kNumMoveReducingBits]
768 #define GET_PRICEa_1(prob) ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]
771 static UInt32
LitEnc_GetPrice(const CLzmaProb
*probs
, UInt32 sym
, const CProbPrice
*ProbPrices
)
777 unsigned bit
= sym
& 1;
779 price
+= GET_PRICEa(probs
[sym
], bit
);
786 static UInt32
LitEnc_Matched_GetPrice(const CLzmaProb
*probs
, UInt32 sym
, UInt32 matchByte
, const CProbPrice
*ProbPrices
)
794 price
+= GET_PRICEa(probs
[offs
+ (matchByte
& offs
) + (sym
>> 8)], (sym
>> 7) & 1);
796 offs
&= ~(matchByte
^ sym
);
798 while (sym
< 0x10000);
803 static void RcTree_ReverseEncode(CRangeEnc
*rc
, CLzmaProb
*probs
, unsigned numBits
, unsigned sym
)
805 UInt32 range
= rc
->range
;
809 UInt32 ttt
, newBound
;
810 unsigned bit
= sym
& 1;
811 // RangeEnc_EncodeBit(rc, probs + m, bit);
813 RC_BIT(rc
, probs
+ m
, bit
);
822 static void LenEnc_Init(CLenEnc
*p
)
825 for (i
= 0; i
< (LZMA_NUM_PB_STATES_MAX
<< (kLenNumLowBits
+ 1)); i
++)
826 p
->low
[i
] = kProbInitValue
;
827 for (i
= 0; i
< kLenNumHighSymbols
; i
++)
828 p
->high
[i
] = kProbInitValue
;
831 static void LenEnc_Encode(CLenEnc
*p
, CRangeEnc
*rc
, unsigned sym
, unsigned posState
)
833 UInt32 range
, ttt
, newBound
;
834 CLzmaProb
*probs
= p
->low
;
836 RC_BIT_PRE(rc
, probs
);
837 if (sym
>= kLenNumLowSymbols
)
840 probs
+= kLenNumLowSymbols
;
841 RC_BIT_PRE(rc
, probs
);
842 if (sym
>= kLenNumLowSymbols
* 2)
846 // RcTree_Encode(rc, p->high, kLenNumHighBits, sym - kLenNumLowSymbols * 2);
847 LitEnc_Encode(rc
, p
->high
, sym
- kLenNumLowSymbols
* 2);
850 sym
-= kLenNumLowSymbols
;
853 // RcTree_Encode(rc, probs + (posState << kLenNumLowBits), kLenNumLowBits, sym);
858 probs
+= (posState
<< (1 + kLenNumLowBits
));
859 bit
= (sym
>> 2) ; RC_BIT(rc
, probs
+ 1, bit
); m
= (1 << 1) + bit
;
860 bit
= (sym
>> 1) & 1; RC_BIT(rc
, probs
+ m
, bit
); m
= (m
<< 1) + bit
;
861 bit
= sym
& 1; RC_BIT(rc
, probs
+ m
, bit
);
866 static void SetPrices_3(const CLzmaProb
*probs
, UInt32 startPrice
, UInt32
*prices
, const CProbPrice
*ProbPrices
)
869 for (i
= 0; i
< 8; i
+= 2)
871 UInt32 price
= startPrice
;
873 price
+= GET_PRICEa(probs
[1 ], (i
>> 2));
874 price
+= GET_PRICEa(probs
[2 + (i
>> 2)], (i
>> 1) & 1);
875 prob
= probs
[4 + (i
>> 1)];
876 prices
[i
] = price
+ GET_PRICEa_0(prob
);
877 prices
[i
+ 1] = price
+ GET_PRICEa_1(prob
);
882 MY_NO_INLINE
static void MY_FAST_CALL
LenPriceEnc_UpdateTables(
884 unsigned numPosStates
,
886 const CProbPrice
*ProbPrices
)
891 unsigned prob
= enc
->low
[0];
894 b
= GET_PRICEa_1(prob
);
895 a
= GET_PRICEa_0(prob
);
896 c
= b
+ GET_PRICEa_0(enc
->low
[kLenNumLowSymbols
]);
897 for (posState
= 0; posState
< numPosStates
; posState
++)
899 UInt32
*prices
= p
->prices
[posState
];
900 const CLzmaProb
*probs
= enc
->low
+ (posState
<< (1 + kLenNumLowBits
));
901 SetPrices_3(probs
, a
, prices
, ProbPrices
);
902 SetPrices_3(probs
+ kLenNumLowSymbols
, c
, prices
+ kLenNumLowSymbols
, ProbPrices
);
910 a = GET_PRICEa_0(enc->low[0]);
911 for (i = 0; i < kLenNumLowSymbols; i++)
913 a = GET_PRICEa_1(enc->low[0]);
914 b = a + GET_PRICEa_0(enc->low[kLenNumLowSymbols]);
915 for (i = kLenNumLowSymbols; i < kLenNumLowSymbols * 2; i++)
917 a += GET_PRICEa_1(enc->low[kLenNumLowSymbols]);
921 // p->counter = numSymbols;
925 unsigned i
= p
->tableSize
;
927 if (i
> kLenNumLowSymbols
* 2)
929 const CLzmaProb
*probs
= enc
->high
;
930 UInt32
*prices
= p
->prices
[0] + kLenNumLowSymbols
* 2;
931 i
-= kLenNumLowSymbols
* 2 - 1;
933 b
+= GET_PRICEa_1(enc
->low
[kLenNumLowSymbols
]);
938 // RcTree_GetPrice(enc->high, kLenNumHighBits, i - kLenNumLowSymbols * 2, ProbPrices);
939 LitEnc_GetPrice(probs, i - kLenNumLowSymbols * 2, ProbPrices);
941 // UInt32 price = a + RcTree_GetPrice(probs, kLenNumHighBits - 1, sym, ProbPrices);
942 unsigned sym
= --i
+ (1 << (kLenNumHighBits
- 1));
946 unsigned bit
= sym
& 1;
948 price
+= GET_PRICEa(probs
[sym
], bit
);
953 unsigned prob
= probs
[(size_t)i
+ (1 << (kLenNumHighBits
- 1))];
954 prices
[(size_t)i
* 2 ] = price
+ GET_PRICEa_0(prob
);
955 prices
[(size_t)i
* 2 + 1] = price
+ GET_PRICEa_1(prob
);
962 size_t num
= (p
->tableSize
- kLenNumLowSymbols
* 2) * sizeof(p
->prices
[0][0]);
963 for (posState
= 1; posState
< numPosStates
; posState
++)
964 memcpy(p
->prices
[posState
] + kLenNumLowSymbols
* 2, p
->prices
[0] + kLenNumLowSymbols
* 2, num
);
972 g_STAT_OFFSET += num;
973 printf("\n MovePos %u", num);
977 #define MOVE_POS(p, num) { \
978 p->additionalOffset += (num); \
979 p->matchFinder.Skip(p->matchFinderObj, (UInt32)(num)); }
982 static unsigned ReadMatchDistances(CLzmaEnc
*p
, unsigned *numPairsRes
)
986 p
->additionalOffset
++;
987 p
->numAvail
= p
->matchFinder
.GetNumAvailableBytes(p
->matchFinderObj
);
988 numPairs
= p
->matchFinder
.GetMatches(p
->matchFinderObj
, p
->matches
);
989 *numPairsRes
= numPairs
;
992 printf("\n i = %u numPairs = %u ", g_STAT_OFFSET
, numPairs
/ 2);
996 for (i
= 0; i
< numPairs
; i
+= 2)
997 printf("%2u %6u | ", p
->matches
[i
], p
->matches
[i
+ 1]);
1004 unsigned len
= p
->matches
[(size_t)numPairs
- 2];
1005 if (len
!= p
->numFastBytes
)
1008 UInt32 numAvail
= p
->numAvail
;
1009 if (numAvail
> LZMA_MATCH_LEN_MAX
)
1010 numAvail
= LZMA_MATCH_LEN_MAX
;
1012 const Byte
*p1
= p
->matchFinder
.GetPointerToCurrentPos(p
->matchFinderObj
) - 1;
1013 const Byte
*p2
= p1
+ len
;
1014 ptrdiff_t dif
= (ptrdiff_t)-1 - p
->matches
[(size_t)numPairs
- 1];
1015 const Byte
*lim
= p1
+ numAvail
;
1016 for (; p2
!= lim
&& *p2
== p2
[dif
]; p2
++)
1018 return (unsigned)(p2
- p1
);
1024 #define MARK_LIT ((UInt32)(Int32)-1)
1026 #define MakeAs_Lit(p) { (p)->dist = MARK_LIT; (p)->extra = 0; }
1027 #define MakeAs_ShortRep(p) { (p)->dist = 0; (p)->extra = 0; }
1028 #define IsShortRep(p) ((p)->dist == 0)
1031 #define GetPrice_ShortRep(p, state, posState) \
1032 ( GET_PRICE_0(p->isRepG0[state]) + GET_PRICE_0(p->isRep0Long[state][posState]))
1034 #define GetPrice_Rep_0(p, state, posState) ( \
1035 GET_PRICE_1(p->isMatch[state][posState]) \
1036 + GET_PRICE_1(p->isRep0Long[state][posState])) \
1037 + GET_PRICE_1(p->isRep[state]) \
1038 + GET_PRICE_0(p->isRepG0[state])
1041 static UInt32
GetPrice_PureRep(const CLzmaEnc
*p
, unsigned repIndex
, size_t state
, size_t posState
)
1044 UInt32 prob
= p
->isRepG0
[state
];
1047 price
= GET_PRICE_0(prob
);
1048 price
+= GET_PRICE_1(p
->isRep0Long
[state
][posState
]);
1052 price
= GET_PRICE_1(prob
);
1053 prob
= p
->isRepG1
[state
];
1055 price
+= GET_PRICE_0(prob
);
1058 price
+= GET_PRICE_1(prob
);
1059 price
+= GET_PRICE(p
->isRepG2
[state
], repIndex
- 2);
1066 static unsigned Backward(CLzmaEnc
*p
, unsigned cur
)
1068 unsigned wr
= cur
+ 1;
1073 UInt32 dist
= p
->opt
[cur
].dist
;
1074 unsigned len
= (unsigned)p
->opt
[cur
].len
;
1075 unsigned extra
= (unsigned)p
->opt
[cur
].extra
;
1081 p
->opt
[wr
].len
= (UInt32
)len
;
1086 p
->opt
[wr
].dist
= dist
;
1091 p
->opt
[wr
].dist
= 0;
1094 p
->opt
[wr
].dist
= MARK_LIT
;
1107 p
->opt
[wr
].dist
= dist
;
1108 p
->opt
[wr
].len
= (UInt32
)len
;
1114 #define LIT_PROBS(pos, prevByte) \
1115 (p->litProbs + (UInt32)3 * (((((pos) << 8) + (prevByte)) & p->lpMask) << p->lc))
1118 static unsigned GetOptimum(CLzmaEnc
*p
, UInt32 position
)
1121 UInt32 reps
[LZMA_NUM_REPS
];
1122 unsigned repLens
[LZMA_NUM_REPS
];
1127 unsigned numPairs
, mainLen
, repMaxIndex
, i
, posState
;
1128 UInt32 matchPrice
, repMatchPrice
;
1130 Byte curByte
, matchByte
;
1132 p
->optCur
= p
->optEnd
= 0;
1134 if (p
->additionalOffset
== 0)
1135 mainLen
= ReadMatchDistances(p
, &numPairs
);
1138 mainLen
= p
->longestMatchLen
;
1139 numPairs
= p
->numPairs
;
1142 numAvail
= p
->numAvail
;
1145 p
->backRes
= MARK_LIT
;
1148 if (numAvail
> LZMA_MATCH_LEN_MAX
)
1149 numAvail
= LZMA_MATCH_LEN_MAX
;
1151 data
= p
->matchFinder
.GetPointerToCurrentPos(p
->matchFinderObj
) - 1;
1154 for (i
= 0; i
< LZMA_NUM_REPS
; i
++)
1158 reps
[i
] = p
->reps
[i
];
1159 data2
= data
- reps
[i
];
1160 if (data
[0] != data2
[0] || data
[1] != data2
[1])
1165 for (len
= 2; len
< numAvail
&& data
[len
] == data2
[len
]; len
++)
1168 if (len
> repLens
[repMaxIndex
])
1172 if (repLens
[repMaxIndex
] >= p
->numFastBytes
)
1175 p
->backRes
= (UInt32
)repMaxIndex
;
1176 len
= repLens
[repMaxIndex
];
1177 MOVE_POS(p
, len
- 1)
1181 matches
= p
->matches
;
1183 if (mainLen
>= p
->numFastBytes
)
1185 p
->backRes
= matches
[(size_t)numPairs
- 1] + LZMA_NUM_REPS
;
1186 MOVE_POS(p
, mainLen
- 1)
1191 matchByte
= *(data
- reps
[0]);
1193 last
= repLens
[repMaxIndex
];
1194 if (last
<= mainLen
)
1197 if (last
< 2 && curByte
!= matchByte
)
1199 p
->backRes
= MARK_LIT
;
1203 p
->opt
[0].state
= (CState
)p
->state
;
1205 posState
= (position
& p
->pbMask
);
1208 const CLzmaProb
*probs
= LIT_PROBS(position
, *(data
- 1));
1209 p
->opt
[1].price
= GET_PRICE_0(p
->isMatch
[p
->state
][posState
]) +
1210 (!IsLitState(p
->state
) ?
1211 LitEnc_Matched_GetPrice(probs
, curByte
, matchByte
, p
->ProbPrices
) :
1212 LitEnc_GetPrice(probs
, curByte
, p
->ProbPrices
));
1215 MakeAs_Lit(&p
->opt
[1]);
1217 matchPrice
= GET_PRICE_1(p
->isMatch
[p
->state
][posState
]);
1218 repMatchPrice
= matchPrice
+ GET_PRICE_1(p
->isRep
[p
->state
]);
1221 if (matchByte
== curByte
&& repLens
[0] == 0)
1223 UInt32 shortRepPrice
= repMatchPrice
+ GetPrice_ShortRep(p
, p
->state
, posState
);
1224 if (shortRepPrice
< p
->opt
[1].price
)
1226 p
->opt
[1].price
= shortRepPrice
;
1227 MakeAs_ShortRep(&p
->opt
[1]);
1231 p
->backRes
= p
->opt
[1].dist
;
1238 p
->opt
[0].reps
[0] = reps
[0];
1239 p
->opt
[0].reps
[1] = reps
[1];
1240 p
->opt
[0].reps
[2] = reps
[2];
1241 p
->opt
[0].reps
[3] = reps
[3];
1243 // ---------- REP ----------
1245 for (i
= 0; i
< LZMA_NUM_REPS
; i
++)
1247 unsigned repLen
= repLens
[i
];
1251 price
= repMatchPrice
+ GetPrice_PureRep(p
, i
, p
->state
, posState
);
1254 UInt32 price2
= price
+ GET_PRICE_LEN(&p
->repLenEnc
, posState
, repLen
);
1255 COptimal
*opt
= &p
->opt
[repLen
];
1256 if (price2
< opt
->price
)
1258 opt
->price
= price2
;
1259 opt
->len
= (UInt32
)repLen
;
1260 opt
->dist
= (UInt32
)i
;
1264 while (--repLen
>= 2);
1268 // ---------- MATCH ----------
1270 unsigned len
= repLens
[0] + 1;
1274 UInt32 normalMatchPrice
= matchPrice
+ GET_PRICE_0(p
->isRep
[p
->state
]);
1279 while (len
> matches
[offs
])
1285 UInt32 dist
= matches
[(size_t)offs
+ 1];
1286 UInt32 price
= normalMatchPrice
+ GET_PRICE_LEN(&p
->lenEnc
, posState
, len
);
1287 unsigned lenToPosState
= GetLenToPosState(len
);
1289 if (dist
< kNumFullDistances
)
1290 price
+= p
->distancesPrices
[lenToPosState
][dist
& (kNumFullDistances
- 1)];
1294 GetPosSlot2(dist
, slot
);
1295 price
+= p
->alignPrices
[dist
& kAlignMask
];
1296 price
+= p
->posSlotPrices
[lenToPosState
][slot
];
1301 if (price
< opt
->price
)
1304 opt
->len
= (UInt32
)len
;
1305 opt
->dist
= dist
+ LZMA_NUM_REPS
;
1309 if (len
== matches
[offs
])
1312 if (offs
== numPairs
)
1323 /* if (position >= 0) */
1326 printf("\n pos = %4X", position
);
1327 for (i
= cur
; i
<= last
; i
++)
1328 printf("\nprice[%4X] = %u", position
- cur
+ i
, p
->opt
[i
].price
);
1335 // ---------- Optimal Parsing ----------
1340 UInt32 numAvailFull
;
1341 unsigned newLen
, numPairs
, prev
, state
, posState
, startLen
;
1342 UInt32 litPrice
, matchPrice
, repMatchPrice
;
1344 Byte curByte
, matchByte
;
1346 COptimal
*curOpt
, *nextOpt
;
1352 if (cur
>= kNumOpts
- 64)
1355 UInt32 price
= p
->opt
[cur
].price
;
1357 for (j
= cur
+ 1; j
<= last
; j
++)
1359 UInt32 price2
= p
->opt
[j
].price
;
1360 if (price
>= price2
)
1367 unsigned delta
= best
- cur
;
1377 newLen
= ReadMatchDistances(p
, &numPairs
);
1379 if (newLen
>= p
->numFastBytes
)
1381 p
->numPairs
= numPairs
;
1382 p
->longestMatchLen
= newLen
;
1386 curOpt
= &p
->opt
[cur
];
1390 // we need that check here, if skip_items in p->opt are possible
1392 if (curOpt->price >= kInfinityPrice)
1396 prev
= cur
- curOpt
->len
;
1398 if (curOpt
->len
== 1)
1400 state
= (unsigned)p
->opt
[prev
].state
;
1401 if (IsShortRep(curOpt
))
1402 state
= kShortRepNextStates
[state
];
1404 state
= kLiteralNextStates
[state
];
1408 const COptimal
*prevOpt
;
1410 UInt32 dist
= curOpt
->dist
;
1414 prev
-= (unsigned)curOpt
->extra
;
1415 state
= kState_RepAfterLit
;
1416 if (curOpt
->extra
== 1)
1417 state
= (dist
< LZMA_NUM_REPS
? kState_RepAfterLit
: kState_MatchAfterLit
);
1421 state
= (unsigned)p
->opt
[prev
].state
;
1422 if (dist
< LZMA_NUM_REPS
)
1423 state
= kRepNextStates
[state
];
1425 state
= kMatchNextStates
[state
];
1428 prevOpt
= &p
->opt
[prev
];
1429 b0
= prevOpt
->reps
[0];
1431 if (dist
< LZMA_NUM_REPS
)
1436 reps
[1] = prevOpt
->reps
[1];
1437 reps
[2] = prevOpt
->reps
[2];
1438 reps
[3] = prevOpt
->reps
[3];
1443 b0
= prevOpt
->reps
[1];
1447 reps
[2] = prevOpt
->reps
[2];
1448 reps
[3] = prevOpt
->reps
[3];
1453 reps
[0] = prevOpt
->reps
[dist
];
1454 reps
[3] = prevOpt
->reps
[dist
^ 1];
1460 reps
[0] = (dist
- LZMA_NUM_REPS
+ 1);
1462 reps
[2] = prevOpt
->reps
[1];
1463 reps
[3] = prevOpt
->reps
[2];
1467 curOpt
->state
= (CState
)state
;
1468 curOpt
->reps
[0] = reps
[0];
1469 curOpt
->reps
[1] = reps
[1];
1470 curOpt
->reps
[2] = reps
[2];
1471 curOpt
->reps
[3] = reps
[3];
1473 data
= p
->matchFinder
.GetPointerToCurrentPos(p
->matchFinderObj
) - 1;
1475 matchByte
= *(data
- reps
[0]);
1477 posState
= (position
& p
->pbMask
);
1480 The order of Price checks:
1484 < REP [ : LIT : REP_0 ]
1485 < MATCH [ : LIT : REP_0 ]
1489 UInt32 curPrice
= curOpt
->price
;
1490 unsigned prob
= p
->isMatch
[state
][posState
];
1491 matchPrice
= curPrice
+ GET_PRICE_1(prob
);
1492 litPrice
= curPrice
+ GET_PRICE_0(prob
);
1495 nextOpt
= &p
->opt
[(size_t)cur
+ 1];
1498 // here we can allow skip_items in p->opt, if we don't check (nextOpt->price < kInfinityPrice)
1500 if ((nextOpt
->price
< kInfinityPrice
1501 // && !IsLitState(state)
1502 && matchByte
== curByte
)
1503 || litPrice
> nextOpt
->price
1508 const CLzmaProb
*probs
= LIT_PROBS(position
, *(data
- 1));
1509 litPrice
+= (!IsLitState(state
) ?
1510 LitEnc_Matched_GetPrice(probs
, curByte
, matchByte
, p
->ProbPrices
) :
1511 LitEnc_GetPrice(probs
, curByte
, p
->ProbPrices
));
1513 if (litPrice
< nextOpt
->price
)
1515 nextOpt
->price
= litPrice
;
1517 MakeAs_Lit(nextOpt
);
1522 repMatchPrice
= matchPrice
+ GET_PRICE_1(p
->isRep
[state
]);
1524 numAvailFull
= p
->numAvail
;
1526 unsigned temp
= kNumOpts
- 1 - cur
;
1527 if (numAvailFull
> temp
)
1528 numAvailFull
= (UInt32
)temp
;
1532 // ---------- SHORT_REP ----------
1533 if (IsLitState(state
)) // 18.new
1534 if (matchByte
== curByte
)
1535 if (repMatchPrice
< nextOpt
->price
) // 18.new
1536 // if (numAvailFull < 2 || data[1] != *(data - reps[0] + 1))
1538 // nextOpt->price >= kInfinityPrice ||
1539 nextOpt
->len
< 2 // we can check nextOpt->len, if skip items are not allowed in p->opt
1540 || (nextOpt
->dist
!= 0
1541 // && nextOpt->extra <= 1 // 17.old
1545 UInt32 shortRepPrice
= repMatchPrice
+ GetPrice_ShortRep(p
, state
, posState
);
1546 // if (shortRepPrice <= nextOpt->price) // 17.old
1547 if (shortRepPrice
< nextOpt
->price
) // 18.new
1549 nextOpt
->price
= shortRepPrice
;
1551 MakeAs_ShortRep(nextOpt
);
1556 if (numAvailFull
< 2)
1558 numAvail
= (numAvailFull
<= p
->numFastBytes
? numAvailFull
: p
->numFastBytes
);
1560 // numAvail <= p->numFastBytes
1562 // ---------- LIT : REP_0 ----------
1565 && litPrice
!= 0 // 18.new
1566 && matchByte
!= curByte
1567 && numAvailFull
> 2)
1569 const Byte
*data2
= data
- reps
[0];
1570 if (data
[1] == data2
[1] && data
[2] == data2
[2])
1573 unsigned limit
= p
->numFastBytes
+ 1;
1574 if (limit
> numAvailFull
)
1575 limit
= numAvailFull
;
1576 for (len
= 3; len
< limit
&& data
[len
] == data2
[len
]; len
++)
1580 unsigned state2
= kLiteralNextStates
[state
];
1581 unsigned posState2
= (position
+ 1) & p
->pbMask
;
1582 UInt32 price
= litPrice
+ GetPrice_Rep_0(p
, state2
, posState2
);
1584 unsigned offset
= cur
+ len
;
1594 // price2 = price + GetPrice_Len_Rep_0(p, len, state2, posState2);
1595 price2
= price
+ GET_PRICE_LEN(&p
->repLenEnc
, posState2
, len
);
1597 opt
= &p
->opt
[offset
];
1599 if (price2
< opt
->price
)
1601 opt
->price
= price2
;
1602 opt
->len
= (UInt32
)len
;
1607 // while (len >= 3);
1613 startLen
= 2; /* speed optimization */
1616 // ---------- REP ----------
1617 unsigned repIndex
= 0; // 17.old
1618 // unsigned repIndex = IsLitState(state) ? 0 : 1; // 18.notused
1619 for (; repIndex
< LZMA_NUM_REPS
; repIndex
++)
1623 const Byte
*data2
= data
- reps
[repIndex
];
1624 if (data
[0] != data2
[0] || data
[1] != data2
[1])
1627 for (len
= 2; len
< numAvail
&& data
[len
] == data2
[len
]; len
++)
1630 // if (len < startLen) continue; // 18.new: speed optimization
1633 unsigned offset
= cur
+ len
;
1638 unsigned len2
= len
;
1639 price
= repMatchPrice
+ GetPrice_PureRep(p
, repIndex
, state
, posState
);
1642 UInt32 price2
= price
+ GET_PRICE_LEN(&p
->repLenEnc
, posState
, len2
);
1643 COptimal
*opt
= &p
->opt
[cur
+ len2
];
1644 if (price2
< opt
->price
)
1646 opt
->price
= price2
;
1647 opt
->len
= (UInt32
)len2
;
1648 opt
->dist
= (UInt32
)repIndex
;
1652 while (--len2
>= 2);
1655 if (repIndex
== 0) startLen
= len
+ 1; // 17.old
1656 // startLen = len + 1; // 18.new
1660 // ---------- REP : LIT : REP_0 ----------
1661 // numFastBytes + 1 + numFastBytes
1663 unsigned len2
= len
+ 1;
1664 unsigned limit
= len2
+ p
->numFastBytes
;
1665 if (limit
> numAvailFull
)
1666 limit
= numAvailFull
;
1670 if (data
[len2
- 2] == data2
[len2
- 2])
1671 if (data
[len2
- 1] == data2
[len2
- 1])
1673 unsigned state2
= kRepNextStates
[state
];
1674 unsigned posState2
= (position
+ len
) & p
->pbMask
;
1675 price
+= GET_PRICE_LEN(&p
->repLenEnc
, posState
, len
)
1676 + GET_PRICE_0(p
->isMatch
[state2
][posState2
])
1677 + LitEnc_Matched_GetPrice(LIT_PROBS(position
+ len
, data
[(size_t)len
- 1]),
1678 data
[len
], data2
[len
], p
->ProbPrices
);
1680 // state2 = kLiteralNextStates[state2];
1681 state2
= kState_LitAfterRep
;
1682 posState2
= (posState2
+ 1) & p
->pbMask
;
1685 price
+= GetPrice_Rep_0(p
, state2
, posState2
);
1687 for (; len2
< limit
&& data
[len2
] == data2
[len2
]; len2
++)
1694 unsigned offset
= cur
+ len
+ len2
;
1703 // price2 = price + GetPrice_Len_Rep_0(p, len2, state2, posState2);
1704 price2
= price
+ GET_PRICE_LEN(&p
->repLenEnc
, posState2
, len2
);
1706 opt
= &p
->opt
[offset
];
1708 if (price2
< opt
->price
)
1710 opt
->price
= price2
;
1711 opt
->len
= (UInt32
)len2
;
1712 opt
->extra
= (CExtra
)(len
+ 1);
1713 opt
->dist
= (UInt32
)repIndex
;
1716 // while (len2 >= 3);
1725 // ---------- MATCH ----------
1726 /* for (unsigned len = 2; len <= newLen; len++) */
1727 if (newLen
> numAvail
)
1730 for (numPairs
= 0; newLen
> matches
[numPairs
]; numPairs
+= 2);
1731 matches
[numPairs
] = (UInt32
)newLen
;
1735 // startLen = 2; /* speed optimization */
1737 if (newLen
>= startLen
)
1739 UInt32 normalMatchPrice
= matchPrice
+ GET_PRICE_0(p
->isRep
[state
]);
1741 unsigned offs
, posSlot
, len
;
1744 unsigned offset
= cur
+ newLen
;
1750 while (startLen
> matches
[offs
])
1752 dist
= matches
[(size_t)offs
+ 1];
1754 // if (dist >= kNumFullDistances)
1755 GetPosSlot2(dist
, posSlot
);
1757 for (len
= /*2*/ startLen
; ; len
++)
1759 UInt32 price
= normalMatchPrice
+ GET_PRICE_LEN(&p
->lenEnc
, posState
, len
);
1762 unsigned lenNorm
= len
- 2;
1763 lenNorm
= GetLenToPosState2(lenNorm
);
1764 if (dist
< kNumFullDistances
)
1765 price
+= p
->distancesPrices
[lenNorm
][dist
& (kNumFullDistances
- 1)];
1767 price
+= p
->posSlotPrices
[lenNorm
][posSlot
] + p
->alignPrices
[dist
& kAlignMask
];
1769 opt
= &p
->opt
[cur
+ len
];
1770 if (price
< opt
->price
)
1773 opt
->len
= (UInt32
)len
;
1774 opt
->dist
= dist
+ LZMA_NUM_REPS
;
1779 if (len
== matches
[offs
])
1781 // if (p->_maxMode) {
1782 // MATCH : LIT : REP_0
1784 const Byte
*data2
= data
- dist
- 1;
1785 unsigned len2
= len
+ 1;
1786 unsigned limit
= len2
+ p
->numFastBytes
;
1787 if (limit
> numAvailFull
)
1788 limit
= numAvailFull
;
1792 if (data
[len2
- 2] == data2
[len2
- 2])
1793 if (data
[len2
- 1] == data2
[len2
- 1])
1795 for (; len2
< limit
&& data
[len2
] == data2
[len2
]; len2
++)
1802 unsigned state2
= kMatchNextStates
[state
];
1803 unsigned posState2
= (position
+ len
) & p
->pbMask
;
1805 price
+= GET_PRICE_0(p
->isMatch
[state2
][posState2
]);
1806 price
+= LitEnc_Matched_GetPrice(LIT_PROBS(position
+ len
, data
[(size_t)len
- 1]),
1807 data
[len
], data2
[len
], p
->ProbPrices
);
1809 // state2 = kLiteralNextStates[state2];
1810 state2
= kState_LitAfterMatch
;
1812 posState2
= (posState2
+ 1) & p
->pbMask
;
1813 price
+= GetPrice_Rep_0(p
, state2
, posState2
);
1815 offset
= cur
+ len
+ len2
;
1824 // price2 = price + GetPrice_Len_Rep_0(p, len2, state2, posState2);
1825 price2
= price
+ GET_PRICE_LEN(&p
->repLenEnc
, posState2
, len2
);
1826 opt
= &p
->opt
[offset
];
1828 if (price2
< opt
->price
)
1830 opt
->price
= price2
;
1831 opt
->len
= (UInt32
)len2
;
1832 opt
->extra
= (CExtra
)(len
+ 1);
1833 opt
->dist
= dist
+ LZMA_NUM_REPS
;
1836 // while (len2 >= 3);
1842 if (offs
== numPairs
)
1844 dist
= matches
[(size_t)offs
+ 1];
1845 // if (dist >= kNumFullDistances)
1846 GetPosSlot2(dist
, posSlot
);
1853 p
->opt
[last
].price
= kInfinityPrice
;
1856 return Backward(p
, cur
);
1861 #define ChangePair(smallDist, bigDist) (((bigDist) >> 7) > (smallDist))
1865 static unsigned GetOptimumFast(CLzmaEnc
*p
)
1867 UInt32 numAvail
, mainDist
;
1868 unsigned mainLen
, numPairs
, repIndex
, repLen
, i
;
1871 if (p
->additionalOffset
== 0)
1872 mainLen
= ReadMatchDistances(p
, &numPairs
);
1875 mainLen
= p
->longestMatchLen
;
1876 numPairs
= p
->numPairs
;
1879 numAvail
= p
->numAvail
;
1880 p
->backRes
= MARK_LIT
;
1883 // if (mainLen < 2 && p->state == 0) return 1; // 18.06.notused
1884 if (numAvail
> LZMA_MATCH_LEN_MAX
)
1885 numAvail
= LZMA_MATCH_LEN_MAX
;
1886 data
= p
->matchFinder
.GetPointerToCurrentPos(p
->matchFinderObj
) - 1;
1887 repLen
= repIndex
= 0;
1889 for (i
= 0; i
< LZMA_NUM_REPS
; i
++)
1892 const Byte
*data2
= data
- p
->reps
[i
];
1893 if (data
[0] != data2
[0] || data
[1] != data2
[1])
1895 for (len
= 2; len
< numAvail
&& data
[len
] == data2
[len
]; len
++)
1897 if (len
>= p
->numFastBytes
)
1899 p
->backRes
= (UInt32
)i
;
1900 MOVE_POS(p
, len
- 1)
1910 if (mainLen
>= p
->numFastBytes
)
1912 p
->backRes
= p
->matches
[(size_t)numPairs
- 1] + LZMA_NUM_REPS
;
1913 MOVE_POS(p
, mainLen
- 1)
1917 mainDist
= 0; /* for GCC */
1921 mainDist
= p
->matches
[(size_t)numPairs
- 1];
1922 while (numPairs
> 2)
1925 if (mainLen
!= p
->matches
[(size_t)numPairs
- 4] + 1)
1927 dist2
= p
->matches
[(size_t)numPairs
- 3];
1928 if (!ChangePair(dist2
, mainDist
))
1934 if (mainLen
== 2 && mainDist
>= 0x80)
1939 if ( repLen
+ 1 >= mainLen
1940 || (repLen
+ 2 >= mainLen
&& mainDist
>= (1 << 9))
1941 || (repLen
+ 3 >= mainLen
&& mainDist
>= (1 << 15)))
1943 p
->backRes
= (UInt32
)repIndex
;
1944 MOVE_POS(p
, repLen
- 1)
1948 if (mainLen
< 2 || numAvail
<= 2)
1952 unsigned len1
= ReadMatchDistances(p
, &p
->numPairs
);
1953 p
->longestMatchLen
= len1
;
1957 UInt32 newDist
= p
->matches
[(size_t)p
->numPairs
- 1];
1958 if ( (len1
>= mainLen
&& newDist
< mainDist
)
1959 || (len1
== mainLen
+ 1 && !ChangePair(mainDist
, newDist
))
1960 || (len1
> mainLen
+ 1)
1961 || (len1
+ 1 >= mainLen
&& mainLen
>= 3 && ChangePair(newDist
, mainDist
)))
1966 data
= p
->matchFinder
.GetPointerToCurrentPos(p
->matchFinderObj
) - 1;
1968 for (i
= 0; i
< LZMA_NUM_REPS
; i
++)
1970 unsigned len
, limit
;
1971 const Byte
*data2
= data
- p
->reps
[i
];
1972 if (data
[0] != data2
[0] || data
[1] != data2
[1])
1974 limit
= mainLen
- 1;
1975 for (len
= 2;; len
++)
1979 if (data
[len
] != data2
[len
])
1984 p
->backRes
= mainDist
+ LZMA_NUM_REPS
;
1987 MOVE_POS(p
, mainLen
- 2)
1995 static void WriteEndMarker(CLzmaEnc
*p
, unsigned posState
)
1998 range
= p
->rc
.range
;
2000 UInt32 ttt
, newBound
;
2001 CLzmaProb
*prob
= &p
->isMatch
[p
->state
][posState
];
2002 RC_BIT_PRE(&p
->rc
, prob
)
2003 RC_BIT_1(&p
->rc
, prob
)
2004 prob
= &p
->isRep
[p
->state
];
2005 RC_BIT_PRE(&p
->rc
, prob
)
2006 RC_BIT_0(&p
->rc
, prob
)
2008 p
->state
= kMatchNextStates
[p
->state
];
2010 p
->rc
.range
= range
;
2011 LenEnc_Encode(&p
->lenProbs
, &p
->rc
, 0, posState
);
2012 range
= p
->rc
.range
;
2015 // RcTree_Encode_PosSlot(&p->rc, p->posSlotEncoder[0], (1 << kNumPosSlotBits) - 1);
2016 CLzmaProb
*probs
= p
->posSlotEncoder
[0];
2020 UInt32 ttt
, newBound
;
2021 RC_BIT_PRE(p
, probs
+ m
)
2022 RC_BIT_1(&p
->rc
, probs
+ m
);
2025 while (m
< (1 << kNumPosSlotBits
));
2028 // RangeEnc_EncodeDirectBits(&p->rc, ((UInt32)1 << (30 - kNumAlignBits)) - 1, 30 - kNumAlignBits); UInt32 range = p->range;
2029 unsigned numBits
= 30 - kNumAlignBits
;
2040 // RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, kAlignMask);
2041 CLzmaProb
*probs
= p
->posAlignEncoder
;
2045 UInt32 ttt
, newBound
;
2046 RC_BIT_PRE(p
, probs
+ m
)
2047 RC_BIT_1(&p
->rc
, probs
+ m
);
2050 while (m
< kAlignTableSize
);
2052 p
->rc
.range
= range
;
2056 static SRes
CheckErrors(CLzmaEnc
*p
)
2058 if (p
->result
!= SZ_OK
)
2060 if (p
->rc
.res
!= SZ_OK
)
2061 p
->result
= SZ_ERROR_WRITE
;
2062 if (p
->matchFinderBase
.result
!= SZ_OK
)
2063 p
->result
= SZ_ERROR_READ
;
2064 if (p
->result
!= SZ_OK
)
2070 MY_NO_INLINE
static SRes
Flush(CLzmaEnc
*p
, UInt32 nowPos
)
2072 /* ReleaseMFStream(); */
2074 if (p
->writeEndMark
)
2075 WriteEndMarker(p
, nowPos
& p
->pbMask
);
2076 RangeEnc_FlushData(&p
->rc
);
2077 RangeEnc_FlushStream(&p
->rc
);
2078 return CheckErrors(p
);
2082 MY_NO_INLINE
static void FillAlignPrices(CLzmaEnc
*p
)
2085 const CProbPrice
*ProbPrices
= p
->ProbPrices
;
2086 const CLzmaProb
*probs
= p
->posAlignEncoder
;
2087 // p->alignPriceCount = 0;
2088 for (i
= 0; i
< kAlignTableSize
/ 2; i
++)
2095 bit
= sym
& 1; sym
>>= 1; price
+= GET_PRICEa(probs
[m
], bit
); m
= (m
<< 1) + bit
;
2096 bit
= sym
& 1; sym
>>= 1; price
+= GET_PRICEa(probs
[m
], bit
); m
= (m
<< 1) + bit
;
2097 bit
= sym
& 1; sym
>>= 1; price
+= GET_PRICEa(probs
[m
], bit
); m
= (m
<< 1) + bit
;
2099 p
->alignPrices
[i
] = price
+ GET_PRICEa_0(prob
);
2100 p
->alignPrices
[i
+ 8] = price
+ GET_PRICEa_1(prob
);
2101 // p->alignPrices[i] = RcTree_ReverseGetPrice(p->posAlignEncoder, kNumAlignBits, i, p->ProbPrices);
2106 MY_NO_INLINE
static void FillDistancesPrices(CLzmaEnc
*p
)
2108 // int y; for (y = 0; y < 100; y++) {
2110 UInt32 tempPrices
[kNumFullDistances
];
2113 const CProbPrice
*ProbPrices
= p
->ProbPrices
;
2114 p
->matchPriceCount
= 0;
2116 for (i
= kStartPosModelIndex
/ 2; i
< kNumFullDistances
/ 2; i
++)
2118 unsigned posSlot
= GetPosSlot1(i
);
2119 unsigned footerBits
= (posSlot
>> 1) - 1;
2120 unsigned base
= ((2 | (posSlot
& 1)) << footerBits
);
2121 const CLzmaProb
*probs
= p
->posEncoders
+ (size_t)base
* 2;
2122 // tempPrices[i] = RcTree_ReverseGetPrice(p->posEncoders + base, footerBits, i - base, p->ProbPrices);
2126 unsigned offset
= (unsigned)1 << footerBits
;
2132 unsigned bit
= sym
& 1;
2134 price
+= GET_PRICEa(probs
[m
], bit
);
2137 while (--footerBits
);
2140 unsigned prob
= probs
[m
];
2141 tempPrices
[base
] = price
+ GET_PRICEa_0(prob
);
2142 tempPrices
[base
+ offset
] = price
+ GET_PRICEa_1(prob
);
2146 for (lps
= 0; lps
< kNumLenToPosStates
; lps
++)
2149 unsigned distTableSize2
= (p
->distTableSize
+ 1) >> 1;
2150 UInt32
*posSlotPrices
= p
->posSlotPrices
[lps
];
2151 const CLzmaProb
*probs
= p
->posSlotEncoder
[lps
];
2153 for (slot
= 0; slot
< distTableSize2
; slot
++)
2155 // posSlotPrices[slot] = RcTree_GetPrice(encoder, kNumPosSlotBits, slot, p->ProbPrices);
2158 unsigned sym
= slot
+ (1 << (kNumPosSlotBits
- 1));
2160 bit
= sym
& 1; sym
>>= 1; price
= GET_PRICEa(probs
[sym
], bit
);
2161 bit
= sym
& 1; sym
>>= 1; price
+= GET_PRICEa(probs
[sym
], bit
);
2162 bit
= sym
& 1; sym
>>= 1; price
+= GET_PRICEa(probs
[sym
], bit
);
2163 bit
= sym
& 1; sym
>>= 1; price
+= GET_PRICEa(probs
[sym
], bit
);
2164 bit
= sym
& 1; sym
>>= 1; price
+= GET_PRICEa(probs
[sym
], bit
);
2165 prob
= probs
[(size_t)slot
+ (1 << (kNumPosSlotBits
- 1))];
2166 posSlotPrices
[(size_t)slot
* 2 ] = price
+ GET_PRICEa_0(prob
);
2167 posSlotPrices
[(size_t)slot
* 2 + 1] = price
+ GET_PRICEa_1(prob
);
2171 UInt32 delta
= ((UInt32
)((kEndPosModelIndex
/ 2 - 1) - kNumAlignBits
) << kNumBitPriceShiftBits
);
2172 for (slot
= kEndPosModelIndex
/ 2; slot
< distTableSize2
; slot
++)
2174 posSlotPrices
[(size_t)slot
* 2 ] += delta
;
2175 posSlotPrices
[(size_t)slot
* 2 + 1] += delta
;
2176 delta
+= ((UInt32
)1 << kNumBitPriceShiftBits
);
2181 UInt32
*dp
= p
->distancesPrices
[lps
];
2183 dp
[0] = posSlotPrices
[0];
2184 dp
[1] = posSlotPrices
[1];
2185 dp
[2] = posSlotPrices
[2];
2186 dp
[3] = posSlotPrices
[3];
2188 for (i
= 4; i
< kNumFullDistances
; i
+= 2)
2190 UInt32 slotPrice
= posSlotPrices
[GetPosSlot1(i
)];
2191 dp
[i
] = slotPrice
+ tempPrices
[i
];
2192 dp
[i
+ 1] = slotPrice
+ tempPrices
[i
+ 1];
2201 void LzmaEnc_Construct(CLzmaEnc
*p
)
2203 RangeEnc_Construct(&p
->rc
);
2204 MatchFinder_Construct(&p
->matchFinderBase
);
2207 MatchFinderMt_Construct(&p
->matchFinderMt
);
2208 p
->matchFinderMt
.MatchFinder
= &p
->matchFinderBase
;
2212 CLzmaEncProps props
;
2213 LzmaEncProps_Init(&props
);
2214 LzmaEnc_SetProps(p
, &props
);
2217 #ifndef LZMA_LOG_BSR
2218 LzmaEnc_FastPosInit(p
->g_FastPos
);
2221 LzmaEnc_InitPriceTables(p
->ProbPrices
);
2223 p
->saveState
.litProbs
= NULL
;
2227 CLzmaEncHandle
LzmaEnc_Create(ISzAllocPtr alloc
)
2230 p
= ISzAlloc_Alloc(alloc
, sizeof(CLzmaEnc
));
2232 LzmaEnc_Construct((CLzmaEnc
*)p
);
2236 void LzmaEnc_FreeLits(CLzmaEnc
*p
, ISzAllocPtr alloc
)
2238 ISzAlloc_Free(alloc
, p
->litProbs
);
2239 ISzAlloc_Free(alloc
, p
->saveState
.litProbs
);
2241 p
->saveState
.litProbs
= NULL
;
2244 void LzmaEnc_Destruct(CLzmaEnc
*p
, ISzAllocPtr alloc
, ISzAllocPtr allocBig
)
2247 MatchFinderMt_Destruct(&p
->matchFinderMt
, allocBig
);
2250 MatchFinder_Free(&p
->matchFinderBase
, allocBig
);
2251 LzmaEnc_FreeLits(p
, alloc
);
2252 RangeEnc_Free(&p
->rc
, alloc
);
2255 void LzmaEnc_Destroy(CLzmaEncHandle p
, ISzAllocPtr alloc
, ISzAllocPtr allocBig
)
2257 LzmaEnc_Destruct((CLzmaEnc
*)p
, alloc
, allocBig
);
2258 ISzAlloc_Free(alloc
, p
);
2262 static SRes
LzmaEnc_CodeOneBlock(CLzmaEnc
*p
, UInt32 maxPackSize
, UInt32 maxUnpackSize
)
2264 UInt32 nowPos32
, startPos32
;
2267 p
->matchFinder
.Init(p
->matchFinderObj
);
2273 RINOK(CheckErrors(p
));
2275 nowPos32
= (UInt32
)p
->nowPos64
;
2276 startPos32
= nowPos32
;
2278 if (p
->nowPos64
== 0)
2282 if (p
->matchFinder
.GetNumAvailableBytes(p
->matchFinderObj
) == 0)
2283 return Flush(p
, nowPos32
);
2284 ReadMatchDistances(p
, &numPairs
);
2285 RangeEnc_EncodeBit_0(&p
->rc
, &p
->isMatch
[kState_Start
][0]);
2286 // p->state = kLiteralNextStates[p->state];
2287 curByte
= *(p
->matchFinder
.GetPointerToCurrentPos(p
->matchFinderObj
) - p
->additionalOffset
);
2288 LitEnc_Encode(&p
->rc
, p
->litProbs
, curByte
);
2289 p
->additionalOffset
--;
2293 if (p
->matchFinder
.GetNumAvailableBytes(p
->matchFinderObj
) != 0)
2298 unsigned len
, posState
;
2299 UInt32 range
, ttt
, newBound
;
2303 len
= GetOptimumFast(p
);
2306 unsigned oci
= p
->optCur
;
2307 if (p
->optEnd
== oci
)
2308 len
= GetOptimum(p
, nowPos32
);
2311 const COptimal
*opt
= &p
->opt
[oci
];
2313 p
->backRes
= opt
->dist
;
2314 p
->optCur
= oci
+ 1;
2318 posState
= (unsigned)nowPos32
& p
->pbMask
;
2319 range
= p
->rc
.range
;
2320 probs
= &p
->isMatch
[p
->state
][posState
];
2322 RC_BIT_PRE(&p
->rc
, probs
)
2327 printf("\n pos = %6X, len = %3u pos = %6u", nowPos32
, len
, dist
);
2330 if (dist
== MARK_LIT
)
2336 RC_BIT_0(&p
->rc
, probs
);
2337 p
->rc
.range
= range
;
2338 data
= p
->matchFinder
.GetPointerToCurrentPos(p
->matchFinderObj
) - p
->additionalOffset
;
2339 probs
= LIT_PROBS(nowPos32
, *(data
- 1));
2342 p
->state
= kLiteralNextStates
[state
];
2343 if (IsLitState(state
))
2344 LitEnc_Encode(&p
->rc
, probs
, curByte
);
2346 LitEnc_EncodeMatched(&p
->rc
, probs
, curByte
, *(data
- p
->reps
[0]));
2350 RC_BIT_1(&p
->rc
, probs
);
2351 probs
= &p
->isRep
[p
->state
];
2352 RC_BIT_PRE(&p
->rc
, probs
)
2354 if (dist
< LZMA_NUM_REPS
)
2356 RC_BIT_1(&p
->rc
, probs
);
2357 probs
= &p
->isRepG0
[p
->state
];
2358 RC_BIT_PRE(&p
->rc
, probs
)
2361 RC_BIT_0(&p
->rc
, probs
);
2362 probs
= &p
->isRep0Long
[p
->state
][posState
];
2363 RC_BIT_PRE(&p
->rc
, probs
)
2366 RC_BIT_1_BASE(&p
->rc
, probs
);
2370 RC_BIT_0_BASE(&p
->rc
, probs
);
2371 p
->state
= kShortRepNextStates
[p
->state
];
2376 RC_BIT_1(&p
->rc
, probs
);
2377 probs
= &p
->isRepG1
[p
->state
];
2378 RC_BIT_PRE(&p
->rc
, probs
)
2381 RC_BIT_0_BASE(&p
->rc
, probs
);
2386 RC_BIT_1(&p
->rc
, probs
);
2387 probs
= &p
->isRepG2
[p
->state
];
2388 RC_BIT_PRE(&p
->rc
, probs
)
2391 RC_BIT_0_BASE(&p
->rc
, probs
);
2396 RC_BIT_1_BASE(&p
->rc
, probs
);
2398 p
->reps
[3] = p
->reps
[2];
2400 p
->reps
[2] = p
->reps
[1];
2402 p
->reps
[1] = p
->reps
[0];
2408 p
->rc
.range
= range
;
2412 LenEnc_Encode(&p
->repLenProbs
, &p
->rc
, len
- LZMA_MATCH_LEN_MIN
, posState
);
2413 --p
->repLenEncCounter
;
2414 p
->state
= kRepNextStates
[p
->state
];
2420 RC_BIT_0(&p
->rc
, probs
);
2421 p
->rc
.range
= range
;
2422 p
->state
= kMatchNextStates
[p
->state
];
2424 LenEnc_Encode(&p
->lenProbs
, &p
->rc
, len
- LZMA_MATCH_LEN_MIN
, posState
);
2425 // --p->lenEnc.counter;
2427 dist
-= LZMA_NUM_REPS
;
2428 p
->reps
[3] = p
->reps
[2];
2429 p
->reps
[2] = p
->reps
[1];
2430 p
->reps
[1] = p
->reps
[0];
2431 p
->reps
[0] = dist
+ 1;
2433 p
->matchPriceCount
++;
2434 GetPosSlot(dist
, posSlot
);
2435 // RcTree_Encode_PosSlot(&p->rc, p->posSlotEncoder[GetLenToPosState(len)], posSlot);
2437 UInt32 sym
= (UInt32
)posSlot
+ (1 << kNumPosSlotBits
);
2438 range
= p
->rc
.range
;
2439 probs
= p
->posSlotEncoder
[GetLenToPosState(len
)];
2442 CLzmaProb
*prob
= probs
+ (sym
>> kNumPosSlotBits
);
2443 UInt32 bit
= (sym
>> (kNumPosSlotBits
- 1)) & 1;
2445 RC_BIT(&p
->rc
, prob
, bit
);
2447 while (sym
< (1 << kNumPosSlotBits
* 2));
2448 p
->rc
.range
= range
;
2451 if (dist
>= kStartPosModelIndex
)
2453 unsigned footerBits
= ((posSlot
>> 1) - 1);
2455 if (dist
< kNumFullDistances
)
2457 unsigned base
= ((2 | (posSlot
& 1)) << footerBits
);
2458 RcTree_ReverseEncode(&p
->rc
, p
->posEncoders
+ base
, footerBits
, (unsigned)(dist
/* - base */));
2462 UInt32 pos2
= (dist
| 0xF) << (32 - footerBits
);
2463 range
= p
->rc
.range
;
2464 // RangeEnc_EncodeDirectBits(&p->rc, posReduced >> kNumAlignBits, footerBits - kNumAlignBits);
2469 p->rc.low += range & (0 - ((dist >> --footerBits) & 1));
2472 while (footerBits > kNumAlignBits);
2477 p
->rc
.low
+= range
& (0 - (pos2
>> 31));
2481 while (pos2
!= 0xF0000000);
2484 // RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, posReduced & kAlignMask);
2489 bit
= dist
& 1; dist
>>= 1; RC_BIT(&p
->rc
, p
->posAlignEncoder
+ m
, bit
); m
= (m
<< 1) + bit
;
2490 bit
= dist
& 1; dist
>>= 1; RC_BIT(&p
->rc
, p
->posAlignEncoder
+ m
, bit
); m
= (m
<< 1) + bit
;
2491 bit
= dist
& 1; dist
>>= 1; RC_BIT(&p
->rc
, p
->posAlignEncoder
+ m
, bit
); m
= (m
<< 1) + bit
;
2492 bit
= dist
& 1; RC_BIT(&p
->rc
, p
->posAlignEncoder
+ m
, bit
);
2493 p
->rc
.range
= range
;
2494 // p->alignPriceCount++;
2501 nowPos32
+= (UInt32
)len
;
2502 p
->additionalOffset
-= len
;
2504 if (p
->additionalOffset
== 0)
2511 if (p->alignPriceCount >= 16) // kAlignTableSize
2513 if (p->matchPriceCount >= 128)
2514 FillDistancesPrices(p);
2515 if (p->lenEnc.counter <= 0)
2516 LenPriceEnc_UpdateTables(&p->lenEnc, 1 << p->pb, &p->lenProbs, p->ProbPrices);
2518 if (p
->matchPriceCount
>= 64)
2521 // { int y; for (y = 0; y < 100; y++) {
2522 FillDistancesPrices(p
);
2524 LenPriceEnc_UpdateTables(&p
->lenEnc
, 1 << p
->pb
, &p
->lenProbs
, p
->ProbPrices
);
2526 if (p
->repLenEncCounter
<= 0)
2528 p
->repLenEncCounter
= REP_LEN_COUNT
;
2529 LenPriceEnc_UpdateTables(&p
->repLenEnc
, 1 << p
->pb
, &p
->repLenProbs
, p
->ProbPrices
);
2533 if (p
->matchFinder
.GetNumAvailableBytes(p
->matchFinderObj
) == 0)
2535 processed
= nowPos32
- startPos32
;
2539 if (processed
+ kNumOpts
+ 300 >= maxUnpackSize
2540 || RangeEnc_GetProcessed_sizet(&p
->rc
) + kPackReserve
>= maxPackSize
)
2543 else if (processed
>= (1 << 17))
2545 p
->nowPos64
+= nowPos32
- startPos32
;
2546 return CheckErrors(p
);
2551 p
->nowPos64
+= nowPos32
- startPos32
;
2552 return Flush(p
, nowPos32
);
2557 #define kBigHashDicLimit ((UInt32)1 << 24)
2559 static SRes
LzmaEnc_Alloc(CLzmaEnc
*p
, UInt32 keepWindowSize
, ISzAllocPtr alloc
, ISzAllocPtr allocBig
)
2561 UInt32 beforeSize
= kNumOpts
;
2562 if (!RangeEnc_Alloc(&p
->rc
, alloc
))
2563 return SZ_ERROR_MEM
;
2566 p
->mtMode
= (p
->multiThread
&& !p
->fastMode
&& (p
->matchFinderBase
.btMode
!= 0));
2570 unsigned lclp
= p
->lc
+ p
->lp
;
2571 if (!p
->litProbs
|| !p
->saveState
.litProbs
|| p
->lclp
!= lclp
)
2573 LzmaEnc_FreeLits(p
, alloc
);
2574 p
->litProbs
= (CLzmaProb
*)ISzAlloc_Alloc(alloc
, ((UInt32
)0x300 << lclp
) * sizeof(CLzmaProb
));
2575 p
->saveState
.litProbs
= (CLzmaProb
*)ISzAlloc_Alloc(alloc
, ((UInt32
)0x300 << lclp
) * sizeof(CLzmaProb
));
2576 if (!p
->litProbs
|| !p
->saveState
.litProbs
)
2578 LzmaEnc_FreeLits(p
, alloc
);
2579 return SZ_ERROR_MEM
;
2585 p
->matchFinderBase
.bigHash
= (Byte
)(p
->dictSize
> kBigHashDicLimit
? 1 : 0);
2587 if (beforeSize
+ p
->dictSize
< keepWindowSize
)
2588 beforeSize
= keepWindowSize
- p
->dictSize
;
2593 RINOK(MatchFinderMt_Create(&p
->matchFinderMt
, p
->dictSize
, beforeSize
, p
->numFastBytes
,
2597 p
->matchFinderObj
= &p
->matchFinderMt
;
2598 p
->matchFinderBase
.bigHash
= (Byte
)(
2599 (p
->dictSize
> kBigHashDicLimit
&& p
->matchFinderBase
.hashMask
>= 0xFFFFFF) ? 1 : 0);
2600 MatchFinderMt_CreateVTable(&p
->matchFinderMt
, &p
->matchFinder
);
2605 if (!MatchFinder_Create(&p
->matchFinderBase
, p
->dictSize
, beforeSize
, p
->numFastBytes
, LZMA_MATCH_LEN_MAX
, allocBig
))
2606 return SZ_ERROR_MEM
;
2607 p
->matchFinderObj
= &p
->matchFinderBase
;
2608 MatchFinder_CreateVTable(&p
->matchFinderBase
, &p
->matchFinder
);
2614 void LzmaEnc_Init(CLzmaEnc
*p
)
2623 RangeEnc_Init(&p
->rc
);
2625 for (i
= 0; i
< (1 << kNumAlignBits
); i
++)
2626 p
->posAlignEncoder
[i
] = kProbInitValue
;
2628 for (i
= 0; i
< kNumStates
; i
++)
2631 for (j
= 0; j
< LZMA_NUM_PB_STATES_MAX
; j
++)
2633 p
->isMatch
[i
][j
] = kProbInitValue
;
2634 p
->isRep0Long
[i
][j
] = kProbInitValue
;
2636 p
->isRep
[i
] = kProbInitValue
;
2637 p
->isRepG0
[i
] = kProbInitValue
;
2638 p
->isRepG1
[i
] = kProbInitValue
;
2639 p
->isRepG2
[i
] = kProbInitValue
;
2643 for (i
= 0; i
< kNumLenToPosStates
; i
++)
2645 CLzmaProb
*probs
= p
->posSlotEncoder
[i
];
2647 for (j
= 0; j
< (1 << kNumPosSlotBits
); j
++)
2648 probs
[j
] = kProbInitValue
;
2652 for (i
= 0; i
< kNumFullDistances
; i
++)
2653 p
->posEncoders
[i
] = kProbInitValue
;
2657 UInt32 num
= (UInt32
)0x300 << (p
->lp
+ p
->lc
);
2659 CLzmaProb
*probs
= p
->litProbs
;
2660 for (k
= 0; k
< num
; k
++)
2661 probs
[k
] = kProbInitValue
;
2665 LenEnc_Init(&p
->lenProbs
);
2666 LenEnc_Init(&p
->repLenProbs
);
2672 for (i
= 0; i
< kNumOpts
; i
++)
2673 p
->opt
[i
].price
= kInfinityPrice
;
2676 p
->additionalOffset
= 0;
2678 p
->pbMask
= (1 << p
->pb
) - 1;
2679 p
->lpMask
= ((UInt32
)0x100 << p
->lp
) - ((unsigned)0x100 >> p
->lc
);
2683 void LzmaEnc_InitPrices(CLzmaEnc
*p
)
2687 FillDistancesPrices(p
);
2691 p
->lenEnc
.tableSize
=
2692 p
->repLenEnc
.tableSize
=
2693 p
->numFastBytes
+ 1 - LZMA_MATCH_LEN_MIN
;
2695 p
->repLenEncCounter
= REP_LEN_COUNT
;
2697 LenPriceEnc_UpdateTables(&p
->lenEnc
, 1 << p
->pb
, &p
->lenProbs
, p
->ProbPrices
);
2698 LenPriceEnc_UpdateTables(&p
->repLenEnc
, 1 << p
->pb
, &p
->repLenProbs
, p
->ProbPrices
);
2701 static SRes
LzmaEnc_AllocAndInit(CLzmaEnc
*p
, UInt32 keepWindowSize
, ISzAllocPtr alloc
, ISzAllocPtr allocBig
)
2704 for (i
= kEndPosModelIndex
/ 2; i
< kDicLogSizeMax
; i
++)
2705 if (p
->dictSize
<= ((UInt32
)1 << i
))
2707 p
->distTableSize
= i
* 2;
2709 p
->finished
= False
;
2711 RINOK(LzmaEnc_Alloc(p
, keepWindowSize
, alloc
, allocBig
));
2713 LzmaEnc_InitPrices(p
);
2718 static SRes
LzmaEnc_Prepare(CLzmaEncHandle pp
, ISeqOutStream
*outStream
, ISeqInStream
*inStream
,
2719 ISzAllocPtr alloc
, ISzAllocPtr allocBig
)
2721 CLzmaEnc
*p
= (CLzmaEnc
*)pp
;
2722 p
->matchFinderBase
.stream
= inStream
;
2724 p
->rc
.outStream
= outStream
;
2725 return LzmaEnc_AllocAndInit(p
, 0, alloc
, allocBig
);
2728 SRes
LzmaEnc_PrepareForLzma2(CLzmaEncHandle pp
,
2729 ISeqInStream
*inStream
, UInt32 keepWindowSize
,
2730 ISzAllocPtr alloc
, ISzAllocPtr allocBig
)
2732 CLzmaEnc
*p
= (CLzmaEnc
*)pp
;
2733 p
->matchFinderBase
.stream
= inStream
;
2735 return LzmaEnc_AllocAndInit(p
, keepWindowSize
, alloc
, allocBig
);
2738 static void LzmaEnc_SetInputBuf(CLzmaEnc
*p
, const Byte
*src
, SizeT srcLen
)
2740 p
->matchFinderBase
.directInput
= 1;
2741 p
->matchFinderBase
.bufferBase
= (Byte
*)src
;
2742 p
->matchFinderBase
.directInputRem
= srcLen
;
2745 SRes
LzmaEnc_MemPrepare(CLzmaEncHandle pp
, const Byte
*src
, SizeT srcLen
,
2746 UInt32 keepWindowSize
, ISzAllocPtr alloc
, ISzAllocPtr allocBig
)
2748 CLzmaEnc
*p
= (CLzmaEnc
*)pp
;
2749 LzmaEnc_SetInputBuf(p
, src
, srcLen
);
2752 LzmaEnc_SetDataSize(pp
, srcLen
);
2753 return LzmaEnc_AllocAndInit(p
, keepWindowSize
, alloc
, allocBig
);
2756 void LzmaEnc_Finish(CLzmaEncHandle pp
)
2759 CLzmaEnc
*p
= (CLzmaEnc
*)pp
;
2761 MatchFinderMt_ReleaseStream(&p
->matchFinderMt
);
2774 } CLzmaEnc_SeqOutStreamBuf
;
2776 static size_t SeqOutStreamBuf_Write(const ISeqOutStream
*pp
, const void *data
, size_t size
)
2778 CLzmaEnc_SeqOutStreamBuf
*p
= CONTAINER_FROM_VTBL(pp
, CLzmaEnc_SeqOutStreamBuf
, vt
);
2784 memcpy(p
->data
, data
, size
);
2791 UInt32
LzmaEnc_GetNumAvailableBytes(CLzmaEncHandle pp
)
2793 const CLzmaEnc
*p
= (CLzmaEnc
*)pp
;
2794 return p
->matchFinder
.GetNumAvailableBytes(p
->matchFinderObj
);
2798 const Byte
*LzmaEnc_GetCurBuf(CLzmaEncHandle pp
)
2800 const CLzmaEnc
*p
= (CLzmaEnc
*)pp
;
2801 return p
->matchFinder
.GetPointerToCurrentPos(p
->matchFinderObj
) - p
->additionalOffset
;
2805 SRes
LzmaEnc_CodeOneMemBlock(CLzmaEncHandle pp
, BoolInt reInit
,
2806 Byte
*dest
, size_t *destLen
, UInt32 desiredPackSize
, UInt32
*unpackSize
)
2808 CLzmaEnc
*p
= (CLzmaEnc
*)pp
;
2811 CLzmaEnc_SeqOutStreamBuf outStream
;
2813 outStream
.vt
.Write
= SeqOutStreamBuf_Write
;
2814 outStream
.data
= dest
;
2815 outStream
.rem
= *destLen
;
2816 outStream
.overflow
= False
;
2818 p
->writeEndMark
= False
;
2819 p
->finished
= False
;
2824 LzmaEnc_InitPrices(p
);
2826 nowPos64
= p
->nowPos64
;
2827 RangeEnc_Init(&p
->rc
);
2828 p
->rc
.outStream
= &outStream
.vt
;
2830 if (desiredPackSize
== 0)
2831 return SZ_ERROR_OUTPUT_EOF
;
2833 res
= LzmaEnc_CodeOneBlock(p
, desiredPackSize
, *unpackSize
);
2835 *unpackSize
= (UInt32
)(p
->nowPos64
- nowPos64
);
2836 *destLen
-= outStream
.rem
;
2837 if (outStream
.overflow
)
2838 return SZ_ERROR_OUTPUT_EOF
;
2844 static SRes
LzmaEnc_Encode2(CLzmaEnc
*p
, ICompressProgress
*progress
)
2849 Byte allocaDummy
[0x300];
2851 allocaDummy
[1] = allocaDummy
[0];
2856 res
= LzmaEnc_CodeOneBlock(p
, 0, 0);
2857 if (res
!= SZ_OK
|| p
->finished
)
2861 res
= ICompressProgress_Progress(progress
, p
->nowPos64
, RangeEnc_GetProcessed(&p
->rc
));
2864 res
= SZ_ERROR_PROGRESS
;
2873 if (res == SZ_OK && !Inline_MatchFinder_IsFinishedOK(&p->matchFinderBase))
2874 res = SZ_ERROR_FAIL;
2882 SRes
LzmaEnc_Encode(CLzmaEncHandle pp
, ISeqOutStream
*outStream
, ISeqInStream
*inStream
, ICompressProgress
*progress
,
2883 ISzAllocPtr alloc
, ISzAllocPtr allocBig
)
2885 RINOK(LzmaEnc_Prepare(pp
, outStream
, inStream
, alloc
, allocBig
));
2886 return LzmaEnc_Encode2((CLzmaEnc
*)pp
, progress
);
2890 SRes
LzmaEnc_WriteProperties(CLzmaEncHandle pp
, Byte
*props
, SizeT
*size
)
2892 CLzmaEnc
*p
= (CLzmaEnc
*)pp
;
2894 UInt32 dictSize
= p
->dictSize
;
2895 if (*size
< LZMA_PROPS_SIZE
)
2896 return SZ_ERROR_PARAM
;
2897 *size
= LZMA_PROPS_SIZE
;
2898 props
[0] = (Byte
)((p
->pb
* 5 + p
->lp
) * 9 + p
->lc
);
2900 if (dictSize
>= ((UInt32
)1 << 22))
2902 UInt32 kDictMask
= ((UInt32
)1 << 20) - 1;
2903 if (dictSize
< (UInt32
)0xFFFFFFFF - kDictMask
)
2904 dictSize
= (dictSize
+ kDictMask
) & ~kDictMask
;
2906 else for (i
= 11; i
<= 30; i
++)
2908 if (dictSize
<= ((UInt32
)2 << i
)) { dictSize
= (2 << i
); break; }
2909 if (dictSize
<= ((UInt32
)3 << i
)) { dictSize
= (3 << i
); break; }
2912 for (i
= 0; i
< 4; i
++)
2913 props
[1 + i
] = (Byte
)(dictSize
>> (8 * i
));
2918 unsigned LzmaEnc_IsWriteEndMark(CLzmaEncHandle pp
)
2920 return ((CLzmaEnc
*)pp
)->writeEndMark
;
2924 SRes
LzmaEnc_MemEncode(CLzmaEncHandle pp
, Byte
*dest
, SizeT
*destLen
, const Byte
*src
, SizeT srcLen
,
2925 int writeEndMark
, ICompressProgress
*progress
, ISzAllocPtr alloc
, ISzAllocPtr allocBig
)
2928 CLzmaEnc
*p
= (CLzmaEnc
*)pp
;
2930 CLzmaEnc_SeqOutStreamBuf outStream
;
2932 outStream
.vt
.Write
= SeqOutStreamBuf_Write
;
2933 outStream
.data
= dest
;
2934 outStream
.rem
= *destLen
;
2935 outStream
.overflow
= False
;
2937 p
->writeEndMark
= writeEndMark
;
2938 p
->rc
.outStream
= &outStream
.vt
;
2940 res
= LzmaEnc_MemPrepare(pp
, src
, srcLen
, 0, alloc
, allocBig
);
2944 res
= LzmaEnc_Encode2(p
, progress
);
2945 if (res
== SZ_OK
&& p
->nowPos64
!= srcLen
)
2946 res
= SZ_ERROR_FAIL
;
2949 *destLen
-= outStream
.rem
;
2950 if (outStream
.overflow
)
2951 return SZ_ERROR_OUTPUT_EOF
;
2956 SRes
LzmaEncode(Byte
*dest
, SizeT
*destLen
, const Byte
*src
, SizeT srcLen
,
2957 const CLzmaEncProps
*props
, Byte
*propsEncoded
, SizeT
*propsSize
, int writeEndMark
,
2958 ICompressProgress
*progress
, ISzAllocPtr alloc
, ISzAllocPtr allocBig
)
2960 CLzmaEnc
*p
= (CLzmaEnc
*)LzmaEnc_Create(alloc
);
2963 return SZ_ERROR_MEM
;
2965 res
= LzmaEnc_SetProps(p
, props
);
2968 res
= LzmaEnc_WriteProperties(p
, propsEncoded
, propsSize
);
2970 res
= LzmaEnc_MemEncode(p
, dest
, destLen
, src
, srcLen
,
2971 writeEndMark
, progress
, alloc
, allocBig
);
2974 LzmaEnc_Destroy(p
, alloc
, allocBig
);