1 /* LzmaEnc.c -- LZMA Encoder
2 2018-04-29 : 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 void LzmaEncProps_Init(CLzmaEncProps
*p
)
44 p
->dictSize
= p
->mc
= 0;
45 p
->reduceSize
= (UInt64
)(Int64
)-1;
46 p
->lc
= p
->lp
= p
->pb
= p
->algo
= p
->fb
= p
->btMode
= p
->numHashBytes
= p
->numThreads
= -1;
50 void LzmaEncProps_Normalize(CLzmaEncProps
*p
)
53 if (level
< 0) level
= 5;
56 if (p
->dictSize
== 0) p
->dictSize
= (level
<= 5 ? (1 << (level
* 2 + 14)) : (level
<= 7 ? (1 << 25) : (1 << 26)));
57 if (p
->dictSize
> p
->reduceSize
)
60 UInt32 reduceSize
= (UInt32
)p
->reduceSize
;
61 for (i
= 11; i
<= 30; i
++)
63 if (reduceSize
<= ((UInt32
)2 << i
)) { p
->dictSize
= ((UInt32
)2 << i
); break; }
64 if (reduceSize
<= ((UInt32
)3 << i
)) { p
->dictSize
= ((UInt32
)3 << i
); break; }
68 if (p
->lc
< 0) p
->lc
= 3;
69 if (p
->lp
< 0) p
->lp
= 0;
70 if (p
->pb
< 0) p
->pb
= 2;
72 if (p
->algo
< 0) p
->algo
= (level
< 5 ? 0 : 1);
73 if (p
->fb
< 0) p
->fb
= (level
< 7 ? 32 : 64);
74 if (p
->btMode
< 0) p
->btMode
= (p
->algo
== 0 ? 0 : 1);
75 if (p
->numHashBytes
< 0) p
->numHashBytes
= 4;
76 if (p
->mc
== 0) p
->mc
= (16 + (p
->fb
>> 1)) >> (p
->btMode
? 0 : 1);
78 if (p
->numThreads
< 0)
81 ((p
->btMode
&& p
->algo
) ? 2 : 1);
87 UInt32
LzmaEncProps_GetDictSize(const CLzmaEncProps
*props2
)
89 CLzmaEncProps props
= *props2
;
90 LzmaEncProps_Normalize(&props
);
91 return props
.dictSize
;
94 #if (_MSC_VER >= 1400)
95 /* BSR code is fast for some new CPUs */
96 /* #define LZMA_LOG_BSR */
101 #define kDicLogSizeMaxCompress 32
103 #define BSR2_RET(pos, res) { unsigned long zz; _BitScanReverse(&zz, (pos)); res = (zz + zz) + ((pos >> (zz - 1)) & 1); }
105 static unsigned GetPosSlot1(UInt32 pos
)
111 #define GetPosSlot2(pos, res) { BSR2_RET(pos, res); }
112 #define GetPosSlot(pos, res) { if (pos < 2) res = pos; else BSR2_RET(pos, res); }
116 #define kNumLogBits (9 + sizeof(size_t) / 2)
117 /* #define kNumLogBits (11 + sizeof(size_t) / 8 * 3) */
119 #define kDicLogSizeMaxCompress ((kNumLogBits - 1) * 2 + 7)
121 static void LzmaEnc_FastPosInit(Byte
*g_FastPos
)
128 for (slot
= 2; slot
< kNumLogBits
* 2; slot
++)
130 size_t k
= ((size_t)1 << ((slot
>> 1) - 1));
132 for (j
= 0; j
< k
; j
++)
133 g_FastPos
[j
] = (Byte
)slot
;
138 /* we can use ((limit - pos) >> 31) only if (pos < ((UInt32)1 << 31)) */
140 #define BSR2_RET(pos, res) { unsigned zz = 6 + ((kNumLogBits - 1) & \
141 (0 - (((((UInt32)1 << (kNumLogBits + 6)) - 1) - pos) >> 31))); \
142 res = p->g_FastPos[pos >> zz] + (zz * 2); }
146 #define BSR2_RET(pos, res) { unsigned zz = 6 + ((kNumLogBits - 1) & \
147 (0 - (((((UInt32)1 << (kNumLogBits)) - 1) - (pos >> 6)) >> 31))); \
148 res = p->g_FastPos[pos >> zz] + (zz * 2); }
151 #define BSR2_RET(pos, res) { unsigned zz = (pos < (1 << (kNumLogBits + 6))) ? 6 : 6 + kNumLogBits - 1; \
152 res = p->g_FastPos[pos >> zz] + (zz * 2); }
155 #define BSR2_RET(pos, res) { res = (pos < (1 << (kNumLogBits + 6))) ? \
156 p->g_FastPos[pos >> 6] + 12 : \
157 p->g_FastPos[pos >> (6 + kNumLogBits - 1)] + (6 + (kNumLogBits - 1)) * 2; }
160 #define GetPosSlot1(pos) p->g_FastPos[pos]
161 #define GetPosSlot2(pos, res) { BSR2_RET(pos, res); }
162 #define GetPosSlot(pos, res) { if (pos < kNumFullDistances) res = p->g_FastPos[pos & (kNumFullDistances - 1)]; else BSR2_RET(pos, res); }
167 #define LZMA_NUM_REPS 4
169 typedef UInt16 CState
;
170 typedef UInt16 CExtra
;
179 // > 1 : MATCH (extra-1) : LIT : REP0 (len)
182 UInt32 reps
[LZMA_NUM_REPS
];
186 #define kNumOpts (1 << 12)
187 #define kPackReserve (1 + kNumOpts * 2)
189 #define kNumLenToPosStates 4
190 #define kNumPosSlotBits 6
191 #define kDicLogSizeMin 0
192 #define kDicLogSizeMax 32
193 #define kDistTableSizeMax (kDicLogSizeMax * 2)
195 #define kNumAlignBits 4
196 #define kAlignTableSize (1 << kNumAlignBits)
197 #define kAlignMask (kAlignTableSize - 1)
199 #define kStartPosModelIndex 4
200 #define kEndPosModelIndex 14
201 #define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
211 #define LZMA_PB_MAX 4
212 #define LZMA_LC_MAX 8
213 #define LZMA_LP_MAX 4
215 #define LZMA_NUM_PB_STATES_MAX (1 << LZMA_PB_MAX)
217 #define kLenNumLowBits 3
218 #define kLenNumLowSymbols (1 << kLenNumLowBits)
219 #define kLenNumHighBits 8
220 #define kLenNumHighSymbols (1 << kLenNumHighBits)
221 #define kLenNumSymbolsTotal (kLenNumLowSymbols * 2 + kLenNumHighSymbols)
223 #define LZMA_MATCH_LEN_MIN 2
224 #define LZMA_MATCH_LEN_MAX (LZMA_MATCH_LEN_MIN + kLenNumSymbolsTotal - 1)
226 #define kNumStates 12
231 CLzmaProb low
[LZMA_NUM_PB_STATES_MAX
<< (kLenNumLowBits
+ 1)];
232 CLzmaProb high
[kLenNumHighSymbols
];
239 unsigned counters
[LZMA_NUM_PB_STATES_MAX
];
240 UInt32 prices
[LZMA_NUM_PB_STATES_MAX
][kLenNumSymbolsTotal
];
253 ISeqOutStream
*outStream
;
264 UInt32 reps
[LZMA_NUM_REPS
];
266 CLzmaProb posAlignEncoder
[1 << kNumAlignBits
];
267 CLzmaProb isRep
[kNumStates
];
268 CLzmaProb isRepG0
[kNumStates
];
269 CLzmaProb isRepG1
[kNumStates
];
270 CLzmaProb isRepG2
[kNumStates
];
271 CLzmaProb isMatch
[kNumStates
][LZMA_NUM_PB_STATES_MAX
];
272 CLzmaProb isRep0Long
[kNumStates
][LZMA_NUM_PB_STATES_MAX
];
274 CLzmaProb posSlotEncoder
[kNumLenToPosStates
][1 << kNumPosSlotBits
];
275 CLzmaProb posEncoders
[kNumFullDistances
];
283 typedef UInt32 CProbPrice
;
288 void *matchFinderObj
;
289 IMatchFinder matchFinder
;
294 unsigned longestMatchLen
;
299 unsigned numFastBytes
;
300 unsigned additionalOffset
;
301 UInt32 reps
[LZMA_NUM_REPS
];
302 unsigned lpMask
, pbMask
;
319 unsigned matchPriceCount
;
320 unsigned alignPriceCount
;
322 unsigned distTableSize
;
329 // begin of CMatchFinderMt is used in LZ thread
330 CMatchFinderMt matchFinderMt
;
331 // end of CMatchFinderMt is used in BT and HASH threads
334 CMatchFinder matchFinderBase
;
341 CProbPrice ProbPrices
[kBitModelTotal
>> kNumMoveReducingBits
];
343 UInt32 matches
[LZMA_MATCH_LEN_MAX
* 2 + 2 + 1];
345 UInt32 alignPrices
[kAlignTableSize
];
346 UInt32 posSlotPrices
[kNumLenToPosStates
][kDistTableSizeMax
];
347 UInt32 distancesPrices
[kNumLenToPosStates
][kNumFullDistances
];
349 CLzmaProb posAlignEncoder
[1 << kNumAlignBits
];
350 CLzmaProb isRep
[kNumStates
];
351 CLzmaProb isRepG0
[kNumStates
];
352 CLzmaProb isRepG1
[kNumStates
];
353 CLzmaProb isRepG2
[kNumStates
];
354 CLzmaProb isMatch
[kNumStates
][LZMA_NUM_PB_STATES_MAX
];
355 CLzmaProb isRep0Long
[kNumStates
][LZMA_NUM_PB_STATES_MAX
];
356 CLzmaProb posSlotEncoder
[kNumLenToPosStates
][1 << kNumPosSlotBits
];
357 CLzmaProb posEncoders
[kNumFullDistances
];
363 Byte g_FastPos
[1 << kNumLogBits
];
367 CLenPriceEnc repLenEnc
;
369 COptimal opt
[kNumOpts
];
371 CSaveState saveState
;
380 #define COPY_ARR(dest, src, arr) memcpy(dest->arr, src->arr, sizeof(src->arr));
382 void LzmaEnc_SaveState(CLzmaEncHandle pp
)
384 CLzmaEnc
*p
= (CLzmaEnc
*)pp
;
385 CSaveState
*dest
= &p
->saveState
;
387 dest
->state
= p
->state
;
389 dest
->lenProbs
= p
->lenProbs
;
390 dest
->repLenProbs
= p
->repLenProbs
;
392 COPY_ARR(dest
, p
, reps
);
394 COPY_ARR(dest
, p
, posAlignEncoder
);
395 COPY_ARR(dest
, p
, isRep
);
396 COPY_ARR(dest
, p
, isRepG0
);
397 COPY_ARR(dest
, p
, isRepG1
);
398 COPY_ARR(dest
, p
, isRepG2
);
399 COPY_ARR(dest
, p
, isMatch
);
400 COPY_ARR(dest
, p
, isRep0Long
);
401 COPY_ARR(dest
, p
, posSlotEncoder
);
402 COPY_ARR(dest
, p
, posEncoders
);
404 memcpy(dest
->litProbs
, p
->litProbs
, ((UInt32
)0x300 << p
->lclp
) * sizeof(CLzmaProb
));
408 void LzmaEnc_RestoreState(CLzmaEncHandle pp
)
410 CLzmaEnc
*dest
= (CLzmaEnc
*)pp
;
411 const CSaveState
*p
= &dest
->saveState
;
413 dest
->state
= p
->state
;
415 dest
->lenProbs
= p
->lenProbs
;
416 dest
->repLenProbs
= p
->repLenProbs
;
418 COPY_ARR(dest
, p
, reps
);
420 COPY_ARR(dest
, p
, posAlignEncoder
);
421 COPY_ARR(dest
, p
, isRep
);
422 COPY_ARR(dest
, p
, isRepG0
);
423 COPY_ARR(dest
, p
, isRepG1
);
424 COPY_ARR(dest
, p
, isRepG2
);
425 COPY_ARR(dest
, p
, isMatch
);
426 COPY_ARR(dest
, p
, isRep0Long
);
427 COPY_ARR(dest
, p
, posSlotEncoder
);
428 COPY_ARR(dest
, p
, posEncoders
);
430 memcpy(dest
->litProbs
, p
->litProbs
, ((UInt32
)0x300 << dest
->lclp
) * sizeof(CLzmaProb
));
435 SRes
LzmaEnc_SetProps(CLzmaEncHandle pp
, const CLzmaEncProps
*props2
)
437 CLzmaEnc
*p
= (CLzmaEnc
*)pp
;
438 CLzmaEncProps props
= *props2
;
439 LzmaEncProps_Normalize(&props
);
441 if (props
.lc
> LZMA_LC_MAX
442 || props
.lp
> LZMA_LP_MAX
443 || props
.pb
> LZMA_PB_MAX
444 || props
.dictSize
> ((UInt64
)1 << kDicLogSizeMaxCompress
)
445 || props
.dictSize
> kLzmaMaxHistorySize
)
446 return SZ_ERROR_PARAM
;
448 p
->dictSize
= props
.dictSize
;
450 unsigned fb
= props
.fb
;
453 if (fb
> LZMA_MATCH_LEN_MAX
)
454 fb
= LZMA_MATCH_LEN_MAX
;
455 p
->numFastBytes
= fb
;
460 p
->fastMode
= (props
.algo
== 0);
461 p
->matchFinderBase
.btMode
= (Byte
)(props
.btMode
? 1 : 0);
463 unsigned numHashBytes
= 4;
466 if (props
.numHashBytes
< 2)
468 else if (props
.numHashBytes
< 4)
469 numHashBytes
= props
.numHashBytes
;
471 p
->matchFinderBase
.numHashBytes
= numHashBytes
;
474 p
->matchFinderBase
.cutValue
= props
.mc
;
476 p
->writeEndMark
= props
.writeEndMark
;
480 if (newMultiThread != _multiThread)
482 ReleaseMatchFinder();
483 _multiThread = newMultiThread;
486 p
->multiThread
= (props
.numThreads
> 1);
493 void LzmaEnc_SetDataSize(CLzmaEncHandle pp
, UInt64 expectedDataSiize
)
495 CLzmaEnc
*p
= (CLzmaEnc
*)pp
;
496 p
->matchFinderBase
.expectedDataSize
= expectedDataSiize
;
500 #define kState_Start 0
501 #define kState_LitAfterMatch 4
502 #define kState_LitAfterRep 5
503 #define kState_MatchAfterLit 7
504 #define kState_RepAfterLit 8
506 static const Byte kLiteralNextStates
[kNumStates
] = {0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5};
507 static const Byte kMatchNextStates
[kNumStates
] = {7, 7, 7, 7, 7, 7, 7, 10, 10, 10, 10, 10};
508 static const Byte kRepNextStates
[kNumStates
] = {8, 8, 8, 8, 8, 8, 8, 11, 11, 11, 11, 11};
509 static const Byte kShortRepNextStates
[kNumStates
]= {9, 9, 9, 9, 9, 9, 9, 11, 11, 11, 11, 11};
511 #define IsLitState(s) ((s) < 7)
512 #define GetLenToPosState2(len) (((len) < kNumLenToPosStates - 1) ? (len) : kNumLenToPosStates - 1)
513 #define GetLenToPosState(len) (((len) < kNumLenToPosStates + 1) ? (len) - 2 : kNumLenToPosStates - 1)
515 #define kInfinityPrice (1 << 30)
517 static void RangeEnc_Construct(CRangeEnc
*p
)
523 #define RangeEnc_GetProcessed(p) ((p)->processed + ((p)->buf - (p)->bufBase) + (p)->cacheSize)
524 #define RangeEnc_GetProcessed_sizet(p) ((size_t)(p)->processed + ((p)->buf - (p)->bufBase) + (size_t)(p)->cacheSize)
526 #define RC_BUF_SIZE (1 << 16)
528 static int RangeEnc_Alloc(CRangeEnc
*p
, ISzAllocPtr alloc
)
532 p
->bufBase
= (Byte
*)ISzAlloc_Alloc(alloc
, RC_BUF_SIZE
);
535 p
->bufLim
= p
->bufBase
+ RC_BUF_SIZE
;
540 static void RangeEnc_Free(CRangeEnc
*p
, ISzAllocPtr alloc
)
542 ISzAlloc_Free(alloc
, p
->bufBase
);
546 static void RangeEnc_Init(CRangeEnc
*p
)
549 p
->range
= 0xFFFFFFFF;
560 MY_NO_INLINE
static void RangeEnc_FlushStream(CRangeEnc
*p
)
565 num
= p
->buf
- p
->bufBase
;
566 if (num
!= ISeqOutStream_Write(p
->outStream
, p
->bufBase
, num
))
567 p
->res
= SZ_ERROR_WRITE
;
572 MY_NO_INLINE
static void MY_FAST_CALL
RangeEnc_ShiftLow(CRangeEnc
*p
)
574 UInt32 low
= (UInt32
)p
->low
;
575 unsigned high
= (unsigned)(p
->low
>> 32);
576 p
->low
= (UInt32
)(low
<< 8);
577 if (low
< (UInt32
)0xFF000000 || high
!= 0)
581 *buf
++ = (Byte
)(p
->cache
+ high
);
582 p
->cache
= (unsigned)(low
>> 24);
584 if (buf
== p
->bufLim
)
585 RangeEnc_FlushStream(p
);
586 if (p
->cacheSize
== 0)
593 *buf
++ = (Byte
)(high
);
595 if (buf
== p
->bufLim
)
596 RangeEnc_FlushStream(p
);
597 if (--p
->cacheSize
== 0)
604 static void RangeEnc_FlushData(CRangeEnc
*p
)
607 for (i
= 0; i
< 5; i
++)
608 RangeEnc_ShiftLow(p
);
611 #define RC_NORM(p) if (range < kTopValue) { range <<= 8; RangeEnc_ShiftLow(p); }
613 #define RC_BIT_PRE(p, prob) \
615 newBound = (range >> kNumBitModelTotalBits) * ttt;
617 // #define _LZMA_ENC_USE_BRANCH
619 #ifdef _LZMA_ENC_USE_BRANCH
621 #define RC_BIT(p, prob, symbol) { \
622 RC_BIT_PRE(p, prob) \
623 if (symbol == 0) { range = newBound; ttt += (kBitModelTotal - ttt) >> kNumMoveBits; } \
624 else { (p)->low += newBound; range -= newBound; ttt -= ttt >> kNumMoveBits; } \
625 *(prob) = (CLzmaProb)ttt; \
631 #define RC_BIT(p, prob, symbol) { \
633 RC_BIT_PRE(p, prob) \
634 mask = 0 - (UInt32)symbol; \
639 mask = (UInt32)symbol - 1; \
640 range += newBound & mask; \
641 mask &= (kBitModelTotal - ((1 << kNumMoveBits) - 1)); \
642 mask += ((1 << kNumMoveBits) - 1); \
643 ttt += (Int32)(mask - ttt) >> kNumMoveBits; \
644 *(prob) = (CLzmaProb)ttt; \
653 #define RC_BIT_0_BASE(p, prob) \
654 range = newBound; *(prob) = (CLzmaProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits));
656 #define RC_BIT_1_BASE(p, prob) \
657 range -= newBound; (p)->low += newBound; *(prob) = (CLzmaProb)(ttt - (ttt >> kNumMoveBits)); \
659 #define RC_BIT_0(p, prob) \
660 RC_BIT_0_BASE(p, prob) \
663 #define RC_BIT_1(p, prob) \
664 RC_BIT_1_BASE(p, prob) \
667 static void RangeEnc_EncodeBit_0(CRangeEnc
*p
, CLzmaProb
*prob
)
669 UInt32 range
, ttt
, newBound
;
676 static void LitEnc_Encode(CRangeEnc
*p
, CLzmaProb
*probs
, UInt32 symbol
)
678 UInt32 range
= p
->range
;
682 UInt32 ttt
, newBound
;
683 // RangeEnc_EncodeBit(p, probs + (symbol >> 8), (symbol >> 7) & 1);
684 CLzmaProb
*prob
= probs
+ (symbol
>> 8);
685 UInt32 bit
= (symbol
>> 7) & 1;
687 RC_BIT(p
, prob
, bit
);
689 while (symbol
< 0x10000);
693 static void LitEnc_EncodeMatched(CRangeEnc
*p
, CLzmaProb
*probs
, UInt32 symbol
, UInt32 matchByte
)
695 UInt32 range
= p
->range
;
700 UInt32 ttt
, newBound
;
704 // RangeEnc_EncodeBit(p, probs + (offs + (matchByte & offs) + (symbol >> 8)), (symbol >> 7) & 1);
705 prob
= probs
+ (offs
+ (matchByte
& offs
) + (symbol
>> 8));
706 bit
= (symbol
>> 7) & 1;
708 offs
&= ~(matchByte
^ symbol
);
709 RC_BIT(p
, prob
, bit
);
711 while (symbol
< 0x10000);
717 static void LzmaEnc_InitPriceTables(CProbPrice
*ProbPrices
)
720 for (i
= 0; i
< (kBitModelTotal
>> kNumMoveReducingBits
); i
++)
722 const unsigned kCyclesBits
= kNumBitPriceShiftBits
;
723 UInt32 w
= (i
<< kNumMoveReducingBits
) + (1 << (kNumMoveReducingBits
- 1));
724 unsigned bitCount
= 0;
726 for (j
= 0; j
< kCyclesBits
; j
++)
730 while (w
>= ((UInt32
)1 << 16))
736 ProbPrices
[i
] = (CProbPrice
)((kNumBitModelTotalBits
<< kCyclesBits
) - 15 - bitCount
);
737 // printf("\n%3d: %5d", i, ProbPrices[i]);
742 #define GET_PRICE(prob, symbol) \
743 p->ProbPrices[((prob) ^ (unsigned)(((-(int)(symbol))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits];
745 #define GET_PRICEa(prob, symbol) \
746 ProbPrices[((prob) ^ (unsigned)((-((int)(symbol))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits];
748 #define GET_PRICE_0(prob) p->ProbPrices[(prob) >> kNumMoveReducingBits]
749 #define GET_PRICE_1(prob) p->ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]
751 #define GET_PRICEa_0(prob) ProbPrices[(prob) >> kNumMoveReducingBits]
752 #define GET_PRICEa_1(prob) ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]
755 static UInt32
LitEnc_GetPrice(const CLzmaProb
*probs
, UInt32 symbol
, const CProbPrice
*ProbPrices
)
761 unsigned bit
= symbol
& 1;
763 price
+= GET_PRICEa(probs
[symbol
], bit
);
770 static UInt32
LitEnc_Matched_GetPrice(const CLzmaProb
*probs
, UInt32 symbol
, UInt32 matchByte
, const CProbPrice
*ProbPrices
)
778 price
+= GET_PRICEa(probs
[offs
+ (matchByte
& offs
) + (symbol
>> 8)], (symbol
>> 7) & 1);
780 offs
&= ~(matchByte
^ symbol
);
782 while (symbol
< 0x10000);
787 static void RcTree_ReverseEncode(CRangeEnc
*rc
, CLzmaProb
*probs
, unsigned numBits
, UInt32 symbol
)
789 UInt32 range
= rc
->range
;
793 UInt32 ttt
, newBound
;
794 unsigned bit
= symbol
& 1;
795 // RangeEnc_EncodeBit(rc, probs + m, bit);
797 RC_BIT(rc
, probs
+ m
, bit
);
806 static void LenEnc_Init(CLenEnc
*p
)
809 for (i
= 0; i
< (LZMA_NUM_PB_STATES_MAX
<< (kLenNumLowBits
+ 1)); i
++)
810 p
->low
[i
] = kProbInitValue
;
811 for (i
= 0; i
< kLenNumHighSymbols
; i
++)
812 p
->high
[i
] = kProbInitValue
;
815 static void LenEnc_Encode(CLenEnc
*p
, CRangeEnc
*rc
, unsigned symbol
, unsigned posState
)
817 UInt32 range
, ttt
, newBound
;
818 CLzmaProb
*probs
= p
->low
;
820 RC_BIT_PRE(rc
, probs
);
821 if (symbol
>= kLenNumLowSymbols
)
824 probs
+= kLenNumLowSymbols
;
825 RC_BIT_PRE(rc
, probs
);
826 if (symbol
>= kLenNumLowSymbols
* 2)
830 // RcTree_Encode(rc, p->high, kLenNumHighBits, symbol - kLenNumLowSymbols * 2);
831 LitEnc_Encode(rc
, p
->high
, symbol
- kLenNumLowSymbols
* 2);
834 symbol
-= kLenNumLowSymbols
;
837 // RcTree_Encode(rc, probs + (posState << kLenNumLowBits), kLenNumLowBits, symbol);
842 probs
+= (posState
<< (1 + kLenNumLowBits
));
843 bit
= (symbol
>> 2) ; RC_BIT(rc
, probs
+ 1, bit
); m
= (1 << 1) + bit
;
844 bit
= (symbol
>> 1) & 1; RC_BIT(rc
, probs
+ m
, bit
); m
= (m
<< 1) + bit
;
845 bit
= symbol
& 1; RC_BIT(rc
, probs
+ m
, bit
);
850 static void SetPrices_3(const CLzmaProb
*probs
, UInt32 startPrice
, UInt32
*prices
, const CProbPrice
*ProbPrices
)
853 for (i
= 0; i
< 8; i
+= 2)
855 UInt32 price
= startPrice
;
857 price
+= GET_PRICEa(probs
[1 ], (i
>> 2));
858 price
+= GET_PRICEa(probs
[2 + (i
>> 2)], (i
>> 1) & 1);
859 prob
= probs
[4 + (i
>> 1)];
860 prices
[i
] = price
+ GET_PRICEa_0(prob
);
861 prices
[i
+ 1] = price
+ GET_PRICEa_1(prob
);
866 MY_NO_INLINE
static void MY_FAST_CALL
LenPriceEnc_UpdateTable(
867 CLenPriceEnc
*p
, unsigned posState
,
869 const CProbPrice
*ProbPrices
)
871 // int y; for (y = 0; y < 100; y++) {
873 unsigned i
, numSymbols
;
875 UInt32
*prices
= p
->prices
[posState
];
877 const CLzmaProb
*probs
= enc
->low
+ (posState
<< (1 + kLenNumLowBits
));
878 SetPrices_3(probs
, GET_PRICEa_0(enc
->low
[0]), prices
, ProbPrices
);
879 a
= GET_PRICEa_1(enc
->low
[0]);
880 SetPrices_3(probs
+ kLenNumLowSymbols
, a
+ GET_PRICEa_0(enc
->low
[kLenNumLowSymbols
]), prices
+ kLenNumLowSymbols
, ProbPrices
);
881 a
+= GET_PRICEa_1(enc
->low
[kLenNumLowSymbols
]);
883 numSymbols
= p
->tableSize
;
884 p
->counters
[posState
] = numSymbols
;
885 for (i
= kLenNumLowSymbols
* 2; i
< numSymbols
; i
+= 1)
888 // RcTree_GetPrice(enc->high, kLenNumHighBits, i - kLenNumLowSymbols * 2, ProbPrices);
889 LitEnc_GetPrice(enc
->high
, i
- kLenNumLowSymbols
* 2, ProbPrices
);
891 unsigned sym = (i - kLenNumLowSymbols * 2) >> 1;
892 UInt32 price = a + RcTree_GetPrice(enc->high, kLenNumHighBits - 1, sym, ProbPrices);
893 UInt32 prob = enc->high[(1 << 7) + sym];
894 prices[i ] = price + GET_PRICEa_0(prob);
895 prices[i + 1] = price + GET_PRICEa_1(prob);
901 static void LenPriceEnc_UpdateTables(CLenPriceEnc
*p
, unsigned numPosStates
,
903 const CProbPrice
*ProbPrices
)
906 for (posState
= 0; posState
< numPosStates
; posState
++)
907 LenPriceEnc_UpdateTable(p
, posState
, enc
, ProbPrices
);
913 g_STAT_OFFSET += num;
914 printf("\n MovePos %u", num);
918 #define MOVE_POS(p, num) { \
919 p->additionalOffset += (num); \
920 p->matchFinder.Skip(p->matchFinderObj, (num)); }
923 static unsigned ReadMatchDistances(CLzmaEnc
*p
, unsigned *numPairsRes
)
927 p
->additionalOffset
++;
928 p
->numAvail
= p
->matchFinder
.GetNumAvailableBytes(p
->matchFinderObj
);
929 numPairs
= p
->matchFinder
.GetMatches(p
->matchFinderObj
, p
->matches
);
930 *numPairsRes
= numPairs
;
933 printf("\n i = %u numPairs = %u ", g_STAT_OFFSET
, numPairs
/ 2);
937 for (i
= 0; i
< numPairs
; i
+= 2)
938 printf("%2u %6u | ", p
->matches
[i
], p
->matches
[i
+ 1]);
945 unsigned len
= p
->matches
[(size_t)numPairs
- 2];
946 if (len
!= p
->numFastBytes
)
949 UInt32 numAvail
= p
->numAvail
;
950 if (numAvail
> LZMA_MATCH_LEN_MAX
)
951 numAvail
= LZMA_MATCH_LEN_MAX
;
953 const Byte
*p1
= p
->matchFinder
.GetPointerToCurrentPos(p
->matchFinderObj
) - 1;
954 const Byte
*p2
= p1
+ len
;
955 ptrdiff_t dif
= (ptrdiff_t)-1 - p
->matches
[(size_t)numPairs
- 1];
956 const Byte
*lim
= p1
+ numAvail
;
957 for (; p2
!= lim
&& *p2
== p2
[dif
]; p2
++);
958 return (unsigned)(p2
- p1
);
964 #define MARK_LIT ((UInt32)(Int32)-1)
966 #define MakeAs_Lit(p) { (p)->dist = MARK_LIT; (p)->extra = 0; }
967 #define MakeAs_ShortRep(p) { (p)->dist = 0; (p)->extra = 0; }
968 #define IsShortRep(p) ((p)->dist == 0)
971 #define GetPrice_ShortRep(p, state, posState) \
972 ( GET_PRICE_0(p->isRepG0[state]) + GET_PRICE_0(p->isRep0Long[state][posState]))
974 #define GetPrice_Rep_0(p, state, posState) ( \
975 GET_PRICE_1(p->isMatch[state][posState]) \
976 + GET_PRICE_1(p->isRep0Long[state][posState])) \
977 + GET_PRICE_1(p->isRep[state]) \
978 + GET_PRICE_0(p->isRepG0[state])
981 static UInt32
GetPrice_PureRep(const CLzmaEnc
*p
, unsigned repIndex
, size_t state
, size_t posState
)
984 UInt32 prob
= p
->isRepG0
[state
];
987 price
= GET_PRICE_0(prob
);
988 price
+= GET_PRICE_1(p
->isRep0Long
[state
][posState
]);
992 price
= GET_PRICE_1(prob
);
993 prob
= p
->isRepG1
[state
];
995 price
+= GET_PRICE_0(prob
);
998 price
+= GET_PRICE_1(prob
);
999 price
+= GET_PRICE(p
->isRepG2
[state
], repIndex
- 2);
1006 static unsigned Backward(CLzmaEnc
*p
, unsigned cur
)
1008 unsigned wr
= cur
+ 1;
1013 UInt32 dist
= p
->opt
[cur
].dist
;
1014 UInt32 len
= p
->opt
[cur
].len
;
1015 UInt32 extra
= p
->opt
[cur
].extra
;
1021 p
->opt
[wr
].len
= len
;
1026 p
->opt
[wr
].dist
= dist
;
1031 p
->opt
[wr
].dist
= 0;
1034 p
->opt
[wr
].dist
= MARK_LIT
;
1047 p
->opt
[wr
].dist
= dist
;
1048 p
->opt
[wr
].len
= len
;
1054 #define LIT_PROBS(pos, prevByte) \
1055 (p->litProbs + (UInt32)3 * (((((pos) << 8) + (prevByte)) & p->lpMask) << p->lc))
1058 static unsigned GetOptimum(CLzmaEnc
*p
, UInt32 position
)
1061 UInt32 reps
[LZMA_NUM_REPS
];
1062 unsigned repLens
[LZMA_NUM_REPS
];
1067 unsigned numPairs
, mainLen
, repMaxIndex
, i
, posState
;
1068 UInt32 matchPrice
, repMatchPrice
;
1070 Byte curByte
, matchByte
;
1072 p
->optCur
= p
->optEnd
= 0;
1074 if (p
->additionalOffset
== 0)
1075 mainLen
= ReadMatchDistances(p
, &numPairs
);
1078 mainLen
= p
->longestMatchLen
;
1079 numPairs
= p
->numPairs
;
1082 numAvail
= p
->numAvail
;
1085 p
->backRes
= MARK_LIT
;
1088 if (numAvail
> LZMA_MATCH_LEN_MAX
)
1089 numAvail
= LZMA_MATCH_LEN_MAX
;
1091 data
= p
->matchFinder
.GetPointerToCurrentPos(p
->matchFinderObj
) - 1;
1094 for (i
= 0; i
< LZMA_NUM_REPS
; i
++)
1098 reps
[i
] = p
->reps
[i
];
1099 data2
= data
- reps
[i
];
1100 if (data
[0] != data2
[0] || data
[1] != data2
[1])
1105 for (len
= 2; len
< numAvail
&& data
[len
] == data2
[len
]; len
++);
1107 if (len
> repLens
[repMaxIndex
])
1111 if (repLens
[repMaxIndex
] >= p
->numFastBytes
)
1114 p
->backRes
= repMaxIndex
;
1115 len
= repLens
[repMaxIndex
];
1116 MOVE_POS(p
, len
- 1)
1120 matches
= p
->matches
;
1122 if (mainLen
>= p
->numFastBytes
)
1124 p
->backRes
= matches
[(size_t)numPairs
- 1] + LZMA_NUM_REPS
;
1125 MOVE_POS(p
, mainLen
- 1)
1130 matchByte
= *(data
- reps
[0]);
1132 if (mainLen
< 2 && curByte
!= matchByte
&& repLens
[repMaxIndex
] < 2)
1134 p
->backRes
= MARK_LIT
;
1138 p
->opt
[0].state
= (CState
)p
->state
;
1140 posState
= (position
& p
->pbMask
);
1143 const CLzmaProb
*probs
= LIT_PROBS(position
, *(data
- 1));
1144 p
->opt
[1].price
= GET_PRICE_0(p
->isMatch
[p
->state
][posState
]) +
1145 (!IsLitState(p
->state
) ?
1146 LitEnc_Matched_GetPrice(probs
, curByte
, matchByte
, p
->ProbPrices
) :
1147 LitEnc_GetPrice(probs
, curByte
, p
->ProbPrices
));
1150 MakeAs_Lit(&p
->opt
[1]);
1152 matchPrice
= GET_PRICE_1(p
->isMatch
[p
->state
][posState
]);
1153 repMatchPrice
= matchPrice
+ GET_PRICE_1(p
->isRep
[p
->state
]);
1155 if (matchByte
== curByte
)
1157 UInt32 shortRepPrice
= repMatchPrice
+ GetPrice_ShortRep(p
, p
->state
, posState
);
1158 if (shortRepPrice
< p
->opt
[1].price
)
1160 p
->opt
[1].price
= shortRepPrice
;
1161 MakeAs_ShortRep(&p
->opt
[1]);
1165 last
= (mainLen
>= repLens
[repMaxIndex
] ? mainLen
: repLens
[repMaxIndex
]);
1169 p
->backRes
= p
->opt
[1].dist
;
1175 p
->opt
[0].reps
[0] = reps
[0];
1176 p
->opt
[0].reps
[1] = reps
[1];
1177 p
->opt
[0].reps
[2] = reps
[2];
1178 p
->opt
[0].reps
[3] = reps
[3];
1181 unsigned len
= last
;
1183 p
->opt
[len
--].price
= kInfinityPrice
;
1187 // ---------- REP ----------
1189 for (i
= 0; i
< LZMA_NUM_REPS
; i
++)
1191 unsigned repLen
= repLens
[i
];
1195 price
= repMatchPrice
+ GetPrice_PureRep(p
, i
, p
->state
, posState
);
1198 UInt32 price2
= price
+ p
->repLenEnc
.prices
[posState
][(size_t)repLen
- 2];
1199 COptimal
*opt
= &p
->opt
[repLen
];
1200 if (price2
< opt
->price
)
1202 opt
->price
= price2
;
1208 while (--repLen
>= 2);
1212 // ---------- MATCH ----------
1214 unsigned len
= ((repLens
[0] >= 2) ? repLens
[0] + 1 : 2);
1218 UInt32 normalMatchPrice
= matchPrice
+ GET_PRICE_0(p
->isRep
[p
->state
]);
1220 while (len
> matches
[offs
])
1226 UInt32 dist
= matches
[(size_t)offs
+ 1];
1227 UInt32 price2
= normalMatchPrice
+ p
->lenEnc
.prices
[posState
][(size_t)len
- LZMA_MATCH_LEN_MIN
];
1228 unsigned lenToPosState
= GetLenToPosState(len
);
1230 if (dist
< kNumFullDistances
)
1231 price2
+= p
->distancesPrices
[lenToPosState
][dist
& (kNumFullDistances
- 1)];
1235 GetPosSlot2(dist
, slot
);
1236 price2
+= p
->alignPrices
[dist
& kAlignMask
];
1237 price2
+= p
->posSlotPrices
[lenToPosState
][slot
];
1242 if (price2
< opt
->price
)
1244 opt
->price
= price2
;
1246 opt
->dist
= dist
+ LZMA_NUM_REPS
;
1250 if (len
== matches
[offs
])
1253 if (offs
== numPairs
)
1264 /* if (position >= 0) */
1267 printf("\n pos = %4X", position
);
1268 for (i
= cur
; i
<= last
; i
++)
1269 printf("\nprice[%4X] = %u", position
- cur
+ i
, p
->opt
[i
].price
);
1276 // ---------- Optimal Parsing ----------
1280 UInt32 numAvail
, numAvailFull
;
1281 unsigned newLen
, numPairs
, prev
, state
, posState
, startLen
;
1282 UInt32 curPrice
, litPrice
, matchPrice
, repMatchPrice
;
1284 Byte curByte
, matchByte
;
1286 COptimal
*curOpt
, *nextOpt
;
1289 return Backward(p
, cur
);
1291 newLen
= ReadMatchDistances(p
, &numPairs
);
1293 if (newLen
>= p
->numFastBytes
)
1295 p
->numPairs
= numPairs
;
1296 p
->longestMatchLen
= newLen
;
1297 return Backward(p
, cur
);
1300 curOpt
= &p
->opt
[cur
];
1301 prev
= cur
- curOpt
->len
;
1303 if (curOpt
->len
== 1)
1305 state
= p
->opt
[prev
].state
;
1306 if (IsShortRep(curOpt
))
1307 state
= kShortRepNextStates
[state
];
1309 state
= kLiteralNextStates
[state
];
1313 const COptimal
*prevOpt
;
1315 UInt32 dist
= curOpt
->dist
;
1319 prev
-= curOpt
->extra
;
1320 state
= kState_RepAfterLit
;
1321 if (curOpt
->extra
== 1)
1322 state
= (dist
< LZMA_NUM_REPS
) ? kState_RepAfterLit
: kState_MatchAfterLit
;
1326 state
= p
->opt
[prev
].state
;
1327 if (dist
< LZMA_NUM_REPS
)
1328 state
= kRepNextStates
[state
];
1330 state
= kMatchNextStates
[state
];
1333 prevOpt
= &p
->opt
[prev
];
1334 b0
= prevOpt
->reps
[0];
1336 if (dist
< LZMA_NUM_REPS
)
1341 reps
[1] = prevOpt
->reps
[1];
1342 reps
[2] = prevOpt
->reps
[2];
1343 reps
[3] = prevOpt
->reps
[3];
1348 b0
= prevOpt
->reps
[1];
1352 reps
[2] = prevOpt
->reps
[2];
1353 reps
[3] = prevOpt
->reps
[3];
1358 reps
[0] = prevOpt
->reps
[dist
];
1359 reps
[3] = prevOpt
->reps
[dist
^ 1];
1365 reps
[0] = (dist
- LZMA_NUM_REPS
+ 1);
1367 reps
[2] = prevOpt
->reps
[1];
1368 reps
[3] = prevOpt
->reps
[2];
1372 curOpt
->state
= (CState
)state
;
1373 curOpt
->reps
[0] = reps
[0];
1374 curOpt
->reps
[1] = reps
[1];
1375 curOpt
->reps
[2] = reps
[2];
1376 curOpt
->reps
[3] = reps
[3];
1378 data
= p
->matchFinder
.GetPointerToCurrentPos(p
->matchFinderObj
) - 1;
1380 matchByte
= *(data
- reps
[0]);
1383 posState
= (position
& p
->pbMask
);
1386 The order of Price checks:
1390 < REP [ : LIT : REP_0 ]
1391 < MATCH [ : LIT : REP_0 ]
1394 curPrice
= curOpt
->price
;
1395 litPrice
= curPrice
+ GET_PRICE_0(p
->isMatch
[state
][posState
]);
1397 nextOpt
= &p
->opt
[(size_t)cur
+ 1];
1400 // if (litPrice >= nextOpt->price) litPrice = 0; else // 18.new
1402 const CLzmaProb
*probs
= LIT_PROBS(position
, *(data
- 1));
1403 litPrice
+= (!IsLitState(state
) ?
1404 LitEnc_Matched_GetPrice(probs
, curByte
, matchByte
, p
->ProbPrices
) :
1405 LitEnc_GetPrice(probs
, curByte
, p
->ProbPrices
));
1407 if (litPrice
< nextOpt
->price
)
1409 nextOpt
->price
= litPrice
;
1411 MakeAs_Lit(nextOpt
);
1416 matchPrice
= curPrice
+ GET_PRICE_1(p
->isMatch
[state
][posState
]);
1417 repMatchPrice
= matchPrice
+ GET_PRICE_1(p
->isRep
[state
]);
1419 // ---------- SHORT_REP ----------
1420 // if (IsLitState(state)) // 18.new
1421 if (matchByte
== curByte
)
1422 // if (repMatchPrice < nextOpt->price) // 18.new
1423 if (nextOpt
->len
< 2
1424 || (nextOpt
->dist
!= 0
1425 && nextOpt
->extra
<= 1 // 17.old
1428 UInt32 shortRepPrice
= repMatchPrice
+ GetPrice_ShortRep(p
, state
, posState
);
1429 if (shortRepPrice
<= nextOpt
->price
) // 17.old
1430 // if (shortRepPrice < nextOpt->price) // 18.new
1432 nextOpt
->price
= shortRepPrice
;
1434 MakeAs_ShortRep(nextOpt
);
1439 numAvailFull
= p
->numAvail
;
1441 UInt32 temp
= kNumOpts
- 1 - cur
;
1442 if (numAvailFull
> temp
)
1443 numAvailFull
= temp
;
1446 if (numAvailFull
< 2)
1448 numAvail
= (numAvailFull
<= p
->numFastBytes
? numAvailFull
: p
->numFastBytes
);
1450 // numAvail <= p->numFastBytes
1452 // ---------- LIT : REP_0 ----------
1455 // litPrice != 0 && // 18.new
1457 && matchByte
!= curByte
1458 && numAvailFull
> 2)
1460 const Byte
*data2
= data
- reps
[0];
1461 if (data
[1] == data2
[1] && data
[2] == data2
[2])
1464 unsigned limit
= p
->numFastBytes
+ 1;
1465 if (limit
> numAvailFull
)
1466 limit
= numAvailFull
;
1467 for (len
= 3; len
< limit
&& data
[len
] == data2
[len
]; len
++);
1470 unsigned state2
= kLiteralNextStates
[state
];
1471 unsigned posState2
= (position
+ 1) & p
->pbMask
;
1472 UInt32 price
= litPrice
+ GetPrice_Rep_0(p
, state2
, posState2
);
1474 unsigned offset
= cur
+ len
;
1475 while (last
< offset
)
1476 p
->opt
[++last
].price
= kInfinityPrice
;
1483 // price2 = price + GetPrice_Len_Rep_0(p, len, state2, posState2);
1484 price2
= price
+ p
->repLenEnc
.prices
[posState2
][len
- LZMA_MATCH_LEN_MIN
];
1486 opt
= &p
->opt
[offset
];
1488 if (price2
< opt
->price
)
1490 opt
->price
= price2
;
1496 // while (len >= 3);
1502 startLen
= 2; /* speed optimization */
1504 // ---------- REP ----------
1505 unsigned repIndex
= 0; // 17.old
1506 // unsigned repIndex = IsLitState(state) ? 0 : 1; // 18.notused
1507 for (; repIndex
< LZMA_NUM_REPS
; repIndex
++)
1511 const Byte
*data2
= data
- reps
[repIndex
];
1512 if (data
[0] != data2
[0] || data
[1] != data2
[1])
1515 for (len
= 2; len
< numAvail
&& data
[len
] == data2
[len
]; len
++);
1517 // if (len < startLen) continue; // 18.new: speed optimization
1519 while (last
< cur
+ len
)
1520 p
->opt
[++last
].price
= kInfinityPrice
;
1522 unsigned len2
= len
;
1523 price
= repMatchPrice
+ GetPrice_PureRep(p
, repIndex
, state
, posState
);
1526 UInt32 price2
= price
+ p
->repLenEnc
.prices
[posState
][(size_t)len2
- 2];
1527 COptimal
*opt
= &p
->opt
[cur
+ len2
];
1528 if (price2
< opt
->price
)
1530 opt
->price
= price2
;
1532 opt
->dist
= repIndex
;
1536 while (--len2
>= 2);
1539 if (repIndex
== 0) startLen
= len
+ 1; // 17.old
1540 // startLen = len + 1; // 18.new
1544 // ---------- REP : LIT : REP_0 ----------
1545 // numFastBytes + 1 + numFastBytes
1547 unsigned len2
= len
+ 1;
1548 unsigned limit
= len2
+ p
->numFastBytes
;
1549 if (limit
> numAvailFull
)
1550 limit
= numAvailFull
;
1552 for (; len2
< limit
&& data
[len2
] == data2
[len2
]; len2
++);
1557 unsigned state2
= kRepNextStates
[state
];
1558 unsigned posState2
= (position
+ len
) & p
->pbMask
;
1560 p
->repLenEnc
.prices
[posState
][(size_t)len
- 2]
1561 + GET_PRICE_0(p
->isMatch
[state2
][posState2
])
1562 + LitEnc_Matched_GetPrice(LIT_PROBS(position
+ len
, data
[(size_t)len
- 1]),
1563 data
[len
], data2
[len
], p
->ProbPrices
);
1565 // state2 = kLiteralNextStates[state2];
1566 state2
= kState_LitAfterRep
;
1567 posState2
= (posState2
+ 1) & p
->pbMask
;
1570 price
+= GetPrice_Rep_0(p
, state2
, posState2
);
1572 unsigned offset
= cur
+ len
+ len2
;
1573 while (last
< offset
)
1574 p
->opt
[++last
].price
= kInfinityPrice
;
1580 // price2 = price + GetPrice_Len_Rep_0(p, len2, state2, posState2);
1581 price2
= price
+ p
->repLenEnc
.prices
[posState2
][len2
- LZMA_MATCH_LEN_MIN
];
1583 opt
= &p
->opt
[offset
];
1585 if (price2
< opt
->price
)
1587 opt
->price
= price2
;
1589 opt
->extra
= (CExtra
)(len
+ 1);
1590 opt
->dist
= repIndex
;
1593 // while (len2 >= 3);
1601 // ---------- MATCH ----------
1602 /* for (unsigned len = 2; len <= newLen; len++) */
1603 if (newLen
> numAvail
)
1606 for (numPairs
= 0; newLen
> matches
[numPairs
]; numPairs
+= 2);
1607 matches
[numPairs
] = newLen
;
1611 if (newLen
>= startLen
)
1613 UInt32 normalMatchPrice
= matchPrice
+ GET_PRICE_0(p
->isRep
[state
]);
1615 unsigned offs
, posSlot
, len
;
1616 while (last
< cur
+ newLen
)
1617 p
->opt
[++last
].price
= kInfinityPrice
;
1620 while (startLen
> matches
[offs
])
1622 dist
= matches
[(size_t)offs
+ 1];
1624 // if (dist >= kNumFullDistances)
1625 GetPosSlot2(dist
, posSlot
);
1627 for (len
= /*2*/ startLen
; ; len
++)
1629 UInt32 price
= normalMatchPrice
+ p
->lenEnc
.prices
[posState
][(size_t)len
- LZMA_MATCH_LEN_MIN
];
1632 unsigned lenToPosState
= len
- 2; lenToPosState
= GetLenToPosState2(lenToPosState
);
1633 if (dist
< kNumFullDistances
)
1634 price
+= p
->distancesPrices
[lenToPosState
][dist
& (kNumFullDistances
- 1)];
1636 price
+= p
->posSlotPrices
[lenToPosState
][posSlot
] + p
->alignPrices
[dist
& kAlignMask
];
1638 opt
= &p
->opt
[cur
+ len
];
1639 if (price
< opt
->price
)
1643 opt
->dist
= dist
+ LZMA_NUM_REPS
;
1648 if (/*_maxMode && */ len
== matches
[offs
])
1650 // MATCH : LIT : REP_0
1652 const Byte
*data2
= data
- dist
- 1;
1653 unsigned len2
= len
+ 1;
1654 unsigned limit
= len2
+ p
->numFastBytes
;
1655 if (limit
> numAvailFull
)
1656 limit
= numAvailFull
;
1658 for (; len2
< limit
&& data
[len2
] == data2
[len2
]; len2
++);
1664 unsigned state2
= kMatchNextStates
[state
];
1665 unsigned posState2
= (position
+ len
) & p
->pbMask
;
1667 price
+= GET_PRICE_0(p
->isMatch
[state2
][posState2
]);
1668 price
+= LitEnc_Matched_GetPrice(LIT_PROBS(position
+ len
, data
[(size_t)len
- 1]),
1669 data
[len
], data2
[len
], p
->ProbPrices
);
1671 // state2 = kLiteralNextStates[state2];
1672 state2
= kState_LitAfterMatch
;
1674 posState2
= (posState2
+ 1) & p
->pbMask
;
1675 price
+= GetPrice_Rep_0(p
, state2
, posState2
);
1677 offset
= cur
+ len
+ len2
;
1678 while (last
< offset
)
1679 p
->opt
[++last
].price
= kInfinityPrice
;
1685 // price2 = price + GetPrice_Len_Rep_0(p, len2, state2, posState2);
1686 price2
= price
+ p
->repLenEnc
.prices
[posState2
][len2
- LZMA_MATCH_LEN_MIN
];
1687 opt
= &p
->opt
[offset
];
1689 if (price2
< opt
->price
)
1691 opt
->price
= price2
;
1693 opt
->extra
= (CExtra
)(len
+ 1);
1694 opt
->dist
= dist
+ LZMA_NUM_REPS
;
1697 // while (len2 >= 3);
1701 if (offs
== numPairs
)
1703 dist
= matches
[(size_t)offs
+ 1];
1704 // if (dist >= kNumFullDistances)
1705 GetPosSlot2(dist
, posSlot
);
1714 #define ChangePair(smallDist, bigDist) (((bigDist) >> 7) > (smallDist))
1718 static unsigned GetOptimumFast(CLzmaEnc
*p
)
1720 UInt32 numAvail
, mainDist
;
1721 unsigned mainLen
, numPairs
, repIndex
, repLen
, i
;
1724 if (p
->additionalOffset
== 0)
1725 mainLen
= ReadMatchDistances(p
, &numPairs
);
1728 mainLen
= p
->longestMatchLen
;
1729 numPairs
= p
->numPairs
;
1732 numAvail
= p
->numAvail
;
1733 p
->backRes
= MARK_LIT
;
1736 if (numAvail
> LZMA_MATCH_LEN_MAX
)
1737 numAvail
= LZMA_MATCH_LEN_MAX
;
1738 data
= p
->matchFinder
.GetPointerToCurrentPos(p
->matchFinderObj
) - 1;
1739 repLen
= repIndex
= 0;
1741 for (i
= 0; i
< LZMA_NUM_REPS
; i
++)
1744 const Byte
*data2
= data
- p
->reps
[i
];
1745 if (data
[0] != data2
[0] || data
[1] != data2
[1])
1747 for (len
= 2; len
< numAvail
&& data
[len
] == data2
[len
]; len
++);
1748 if (len
>= p
->numFastBytes
)
1751 MOVE_POS(p
, len
- 1)
1761 if (mainLen
>= p
->numFastBytes
)
1763 p
->backRes
= p
->matches
[(size_t)numPairs
- 1] + LZMA_NUM_REPS
;
1764 MOVE_POS(p
, mainLen
- 1)
1768 mainDist
= 0; /* for GCC */
1772 mainDist
= p
->matches
[(size_t)numPairs
- 1];
1773 while (numPairs
> 2)
1776 if (mainLen
!= p
->matches
[(size_t)numPairs
- 4] + 1)
1778 dist2
= p
->matches
[(size_t)numPairs
- 3];
1779 if (!ChangePair(dist2
, mainDist
))
1785 if (mainLen
== 2 && mainDist
>= 0x80)
1790 if ( repLen
+ 1 >= mainLen
1791 || (repLen
+ 2 >= mainLen
&& mainDist
>= (1 << 9))
1792 || (repLen
+ 3 >= mainLen
&& mainDist
>= (1 << 15)))
1794 p
->backRes
= repIndex
;
1795 MOVE_POS(p
, repLen
- 1)
1799 if (mainLen
< 2 || numAvail
<= 2)
1803 unsigned len1
= ReadMatchDistances(p
, &p
->numPairs
);
1804 p
->longestMatchLen
= len1
;
1808 UInt32 newDist
= p
->matches
[(size_t)p
->numPairs
- 1];
1809 if ( (len1
>= mainLen
&& newDist
< mainDist
)
1810 || (len1
== mainLen
+ 1 && !ChangePair(mainDist
, newDist
))
1811 || (len1
> mainLen
+ 1)
1812 || (len1
+ 1 >= mainLen
&& mainLen
>= 3 && ChangePair(newDist
, mainDist
)))
1817 data
= p
->matchFinder
.GetPointerToCurrentPos(p
->matchFinderObj
) - 1;
1819 for (i
= 0; i
< LZMA_NUM_REPS
; i
++)
1821 unsigned len
, limit
;
1822 const Byte
*data2
= data
- p
->reps
[i
];
1823 if (data
[0] != data2
[0] || data
[1] != data2
[1])
1825 limit
= mainLen
- 1;
1826 for (len
= 2;; len
++)
1830 if (data
[len
] != data2
[len
])
1835 p
->backRes
= mainDist
+ LZMA_NUM_REPS
;
1838 MOVE_POS(p
, mainLen
- 2)
1846 static void WriteEndMarker(CLzmaEnc
*p
, unsigned posState
)
1849 range
= p
->rc
.range
;
1851 UInt32 ttt
, newBound
;
1852 CLzmaProb
*prob
= &p
->isMatch
[p
->state
][posState
];
1853 RC_BIT_PRE(&p
->rc
, prob
)
1854 RC_BIT_1(&p
->rc
, prob
)
1855 prob
= &p
->isRep
[p
->state
];
1856 RC_BIT_PRE(&p
->rc
, prob
)
1857 RC_BIT_0(&p
->rc
, prob
)
1859 p
->state
= kMatchNextStates
[p
->state
];
1861 p
->rc
.range
= range
;
1862 LenEnc_Encode(&p
->lenProbs
, &p
->rc
, 0, posState
);
1863 range
= p
->rc
.range
;
1866 // RcTree_Encode_PosSlot(&p->rc, p->posSlotEncoder[0], (1 << kNumPosSlotBits) - 1);
1867 CLzmaProb
*probs
= p
->posSlotEncoder
[0];
1871 UInt32 ttt
, newBound
;
1872 RC_BIT_PRE(p
, probs
+ m
)
1873 RC_BIT_1(&p
->rc
, probs
+ m
);
1876 while (m
< (1 << kNumPosSlotBits
));
1879 // RangeEnc_EncodeDirectBits(&p->rc, ((UInt32)1 << (30 - kNumAlignBits)) - 1, 30 - kNumAlignBits); UInt32 range = p->range;
1880 unsigned numBits
= 30 - kNumAlignBits
;
1891 // RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, kAlignMask);
1892 CLzmaProb
*probs
= p
->posAlignEncoder
;
1896 UInt32 ttt
, newBound
;
1897 RC_BIT_PRE(p
, probs
+ m
)
1898 RC_BIT_1(&p
->rc
, probs
+ m
);
1901 while (m
< kAlignTableSize
);
1903 p
->rc
.range
= range
;
1907 static SRes
CheckErrors(CLzmaEnc
*p
)
1909 if (p
->result
!= SZ_OK
)
1911 if (p
->rc
.res
!= SZ_OK
)
1912 p
->result
= SZ_ERROR_WRITE
;
1913 if (p
->matchFinderBase
.result
!= SZ_OK
)
1914 p
->result
= SZ_ERROR_READ
;
1915 if (p
->result
!= SZ_OK
)
1921 MY_NO_INLINE
static SRes
Flush(CLzmaEnc
*p
, UInt32 nowPos
)
1923 /* ReleaseMFStream(); */
1925 if (p
->writeEndMark
)
1926 WriteEndMarker(p
, nowPos
& p
->pbMask
);
1927 RangeEnc_FlushData(&p
->rc
);
1928 RangeEnc_FlushStream(&p
->rc
);
1929 return CheckErrors(p
);
1934 static void FillAlignPrices(CLzmaEnc
*p
)
1937 const CProbPrice
*ProbPrices
= p
->ProbPrices
;
1938 const CLzmaProb
*probs
= p
->posAlignEncoder
;
1939 p
->alignPriceCount
= 0;
1940 for (i
= 0; i
< kAlignTableSize
/ 2; i
++)
1943 unsigned symbol
= i
;
1947 bit
= symbol
& 1; symbol
>>= 1; price
+= GET_PRICEa(probs
[m
], bit
); m
= (m
<< 1) + bit
;
1948 bit
= symbol
& 1; symbol
>>= 1; price
+= GET_PRICEa(probs
[m
], bit
); m
= (m
<< 1) + bit
;
1949 bit
= symbol
& 1; symbol
>>= 1; price
+= GET_PRICEa(probs
[m
], bit
); m
= (m
<< 1) + bit
;
1951 p
->alignPrices
[i
] = price
+ GET_PRICEa_0(prob
);
1952 p
->alignPrices
[i
+ 8] = price
+ GET_PRICEa_1(prob
);
1953 // p->alignPrices[i] = RcTree_ReverseGetPrice(p->posAlignEncoder, kNumAlignBits, i, p->ProbPrices);
1958 static void FillDistancesPrices(CLzmaEnc
*p
)
1960 UInt32 tempPrices
[kNumFullDistances
];
1961 unsigned i
, lenToPosState
;
1963 const CProbPrice
*ProbPrices
= p
->ProbPrices
;
1964 p
->matchPriceCount
= 0;
1966 for (i
= kStartPosModelIndex
; i
< kNumFullDistances
; i
++)
1968 unsigned posSlot
= GetPosSlot1(i
);
1969 unsigned footerBits
= ((posSlot
>> 1) - 1);
1970 unsigned base
= ((2 | (posSlot
& 1)) << footerBits
);
1971 // tempPrices[i] = RcTree_ReverseGetPrice(p->posEncoders + base, footerBits, i - base, p->ProbPrices);
1973 const CLzmaProb
*probs
= p
->posEncoders
+ base
;
1976 unsigned symbol
= i
- base
;
1979 unsigned bit
= symbol
& 1;
1981 price
+= GET_PRICEa(probs
[m
], bit
);
1984 while (--footerBits
);
1985 tempPrices
[i
] = price
;
1988 for (lenToPosState
= 0; lenToPosState
< kNumLenToPosStates
; lenToPosState
++)
1991 const CLzmaProb
*encoder
= p
->posSlotEncoder
[lenToPosState
];
1992 UInt32
*posSlotPrices
= p
->posSlotPrices
[lenToPosState
];
1993 unsigned distTableSize
= p
->distTableSize
;
1994 const CLzmaProb
*probs
= encoder
;
1995 for (posSlot
= 0; posSlot
< distTableSize
; posSlot
+= 2)
1997 // posSlotPrices[posSlot] = RcTree_GetPrice(encoder, kNumPosSlotBits, posSlot, p->ProbPrices);
2000 unsigned symbol
= (posSlot
>> 1) + (1 << (kNumPosSlotBits
- 1));
2002 bit
= symbol
& 1; symbol
>>= 1; price
+= GET_PRICEa(probs
[symbol
], bit
);
2003 bit
= symbol
& 1; symbol
>>= 1; price
+= GET_PRICEa(probs
[symbol
], bit
);
2004 bit
= symbol
& 1; symbol
>>= 1; price
+= GET_PRICEa(probs
[symbol
], bit
);
2005 bit
= symbol
& 1; symbol
>>= 1; price
+= GET_PRICEa(probs
[symbol
], bit
);
2006 bit
= symbol
& 1; symbol
>>= 1; price
+= GET_PRICEa(probs
[symbol
], bit
);
2007 prob
= probs
[(posSlot
>> 1) + (1 << (kNumPosSlotBits
- 1))];
2008 posSlotPrices
[posSlot
] = price
+ GET_PRICEa_0(prob
);
2009 posSlotPrices
[posSlot
+ 1] = price
+ GET_PRICEa_1(prob
);
2011 for (posSlot
= kEndPosModelIndex
; posSlot
< distTableSize
; posSlot
++)
2012 posSlotPrices
[posSlot
] += ((UInt32
)(((posSlot
>> 1) - 1) - kNumAlignBits
) << kNumBitPriceShiftBits
);
2015 UInt32
*distancesPrices
= p
->distancesPrices
[lenToPosState
];
2017 distancesPrices
[0] = posSlotPrices
[0];
2018 distancesPrices
[1] = posSlotPrices
[1];
2019 distancesPrices
[2] = posSlotPrices
[2];
2020 distancesPrices
[3] = posSlotPrices
[3];
2022 for (i
= 4; i
< kNumFullDistances
; i
+= 2)
2024 UInt32 slotPrice
= posSlotPrices
[GetPosSlot1(i
)];
2025 distancesPrices
[i
] = slotPrice
+ tempPrices
[i
];
2026 distancesPrices
[i
+ 1] = slotPrice
+ tempPrices
[i
+ 1];
2034 void LzmaEnc_Construct(CLzmaEnc
*p
)
2036 RangeEnc_Construct(&p
->rc
);
2037 MatchFinder_Construct(&p
->matchFinderBase
);
2040 MatchFinderMt_Construct(&p
->matchFinderMt
);
2041 p
->matchFinderMt
.MatchFinder
= &p
->matchFinderBase
;
2045 CLzmaEncProps props
;
2046 LzmaEncProps_Init(&props
);
2047 LzmaEnc_SetProps(p
, &props
);
2050 #ifndef LZMA_LOG_BSR
2051 LzmaEnc_FastPosInit(p
->g_FastPos
);
2054 LzmaEnc_InitPriceTables(p
->ProbPrices
);
2056 p
->saveState
.litProbs
= NULL
;
2060 CLzmaEncHandle
LzmaEnc_Create(ISzAllocPtr alloc
)
2063 p
= ISzAlloc_Alloc(alloc
, sizeof(CLzmaEnc
));
2065 LzmaEnc_Construct((CLzmaEnc
*)p
);
2069 void LzmaEnc_FreeLits(CLzmaEnc
*p
, ISzAllocPtr alloc
)
2071 ISzAlloc_Free(alloc
, p
->litProbs
);
2072 ISzAlloc_Free(alloc
, p
->saveState
.litProbs
);
2074 p
->saveState
.litProbs
= NULL
;
2077 void LzmaEnc_Destruct(CLzmaEnc
*p
, ISzAllocPtr alloc
, ISzAllocPtr allocBig
)
2080 MatchFinderMt_Destruct(&p
->matchFinderMt
, allocBig
);
2083 MatchFinder_Free(&p
->matchFinderBase
, allocBig
);
2084 LzmaEnc_FreeLits(p
, alloc
);
2085 RangeEnc_Free(&p
->rc
, alloc
);
2088 void LzmaEnc_Destroy(CLzmaEncHandle p
, ISzAllocPtr alloc
, ISzAllocPtr allocBig
)
2090 LzmaEnc_Destruct((CLzmaEnc
*)p
, alloc
, allocBig
);
2091 ISzAlloc_Free(alloc
, p
);
2095 static SRes
LzmaEnc_CodeOneBlock(CLzmaEnc
*p
, UInt32 maxPackSize
, UInt32 maxUnpackSize
)
2097 UInt32 nowPos32
, startPos32
;
2100 p
->matchFinder
.Init(p
->matchFinderObj
);
2106 RINOK(CheckErrors(p
));
2108 nowPos32
= (UInt32
)p
->nowPos64
;
2109 startPos32
= nowPos32
;
2111 if (p
->nowPos64
== 0)
2115 if (p
->matchFinder
.GetNumAvailableBytes(p
->matchFinderObj
) == 0)
2116 return Flush(p
, nowPos32
);
2117 ReadMatchDistances(p
, &numPairs
);
2118 RangeEnc_EncodeBit_0(&p
->rc
, &p
->isMatch
[kState_Start
][0]);
2119 // p->state = kLiteralNextStates[p->state];
2120 curByte
= *(p
->matchFinder
.GetPointerToCurrentPos(p
->matchFinderObj
) - p
->additionalOffset
);
2121 LitEnc_Encode(&p
->rc
, p
->litProbs
, curByte
);
2122 p
->additionalOffset
--;
2126 if (p
->matchFinder
.GetNumAvailableBytes(p
->matchFinderObj
) != 0)
2131 unsigned len
, posState
;
2132 UInt32 range
, ttt
, newBound
;
2136 len
= GetOptimumFast(p
);
2139 unsigned oci
= p
->optCur
;
2140 if (p
->optEnd
== oci
)
2141 len
= GetOptimum(p
, nowPos32
);
2144 const COptimal
*opt
= &p
->opt
[oci
];
2146 p
->backRes
= opt
->dist
;
2147 p
->optCur
= oci
+ 1;
2151 posState
= (unsigned)nowPos32
& p
->pbMask
;
2152 range
= p
->rc
.range
;
2153 probs
= &p
->isMatch
[p
->state
][posState
];
2155 RC_BIT_PRE(&p
->rc
, probs
)
2160 printf("\n pos = %6X, len = %3u pos = %6u", nowPos32
, len
, dist
);
2163 if (dist
== MARK_LIT
)
2169 RC_BIT_0(&p
->rc
, probs
);
2170 p
->rc
.range
= range
;
2171 data
= p
->matchFinder
.GetPointerToCurrentPos(p
->matchFinderObj
) - p
->additionalOffset
;
2172 probs
= LIT_PROBS(nowPos32
, *(data
- 1));
2175 p
->state
= kLiteralNextStates
[state
];
2176 if (IsLitState(state
))
2177 LitEnc_Encode(&p
->rc
, probs
, curByte
);
2179 LitEnc_EncodeMatched(&p
->rc
, probs
, curByte
, *(data
- p
->reps
[0]));
2183 RC_BIT_1(&p
->rc
, probs
);
2184 probs
= &p
->isRep
[p
->state
];
2185 RC_BIT_PRE(&p
->rc
, probs
)
2187 if (dist
< LZMA_NUM_REPS
)
2189 RC_BIT_1(&p
->rc
, probs
);
2190 probs
= &p
->isRepG0
[p
->state
];
2191 RC_BIT_PRE(&p
->rc
, probs
)
2194 RC_BIT_0(&p
->rc
, probs
);
2195 probs
= &p
->isRep0Long
[p
->state
][posState
];
2196 RC_BIT_PRE(&p
->rc
, probs
)
2199 RC_BIT_1_BASE(&p
->rc
, probs
);
2203 RC_BIT_0_BASE(&p
->rc
, probs
);
2204 p
->state
= kShortRepNextStates
[p
->state
];
2209 RC_BIT_1(&p
->rc
, probs
);
2210 probs
= &p
->isRepG1
[p
->state
];
2211 RC_BIT_PRE(&p
->rc
, probs
)
2214 RC_BIT_0_BASE(&p
->rc
, probs
);
2219 RC_BIT_1(&p
->rc
, probs
);
2220 probs
= &p
->isRepG2
[p
->state
];
2221 RC_BIT_PRE(&p
->rc
, probs
)
2224 RC_BIT_0_BASE(&p
->rc
, probs
);
2229 RC_BIT_1_BASE(&p
->rc
, probs
);
2231 p
->reps
[3] = p
->reps
[2];
2233 p
->reps
[2] = p
->reps
[1];
2235 p
->reps
[1] = p
->reps
[0];
2241 p
->rc
.range
= range
;
2245 LenEnc_Encode(&p
->repLenProbs
, &p
->rc
, len
- LZMA_MATCH_LEN_MIN
, posState
);
2247 if (--p
->repLenEnc
.counters
[posState
] == 0)
2248 LenPriceEnc_UpdateTable(&p
->repLenEnc
, posState
, &p
->repLenProbs
, p
->ProbPrices
);
2250 p
->state
= kRepNextStates
[p
->state
];
2256 RC_BIT_0(&p
->rc
, probs
);
2257 p
->rc
.range
= range
;
2258 p
->state
= kMatchNextStates
[p
->state
];
2260 LenEnc_Encode(&p
->lenProbs
, &p
->rc
, len
- LZMA_MATCH_LEN_MIN
, posState
);
2262 if (--p
->lenEnc
.counters
[posState
] == 0)
2263 LenPriceEnc_UpdateTable(&p
->lenEnc
, posState
, &p
->lenProbs
, p
->ProbPrices
);
2265 dist
-= LZMA_NUM_REPS
;
2266 p
->reps
[3] = p
->reps
[2];
2267 p
->reps
[2] = p
->reps
[1];
2268 p
->reps
[1] = p
->reps
[0];
2269 p
->reps
[0] = dist
+ 1;
2271 p
->matchPriceCount
++;
2272 GetPosSlot(dist
, posSlot
);
2273 // RcTree_Encode_PosSlot(&p->rc, p->posSlotEncoder[GetLenToPosState(len)], posSlot);
2275 UInt32 symbol
= posSlot
+ (1 << kNumPosSlotBits
);
2276 range
= p
->rc
.range
;
2277 probs
= p
->posSlotEncoder
[GetLenToPosState(len
)];
2280 CLzmaProb
*prob
= probs
+ (symbol
>> kNumPosSlotBits
);
2281 UInt32 bit
= (symbol
>> (kNumPosSlotBits
- 1)) & 1;
2283 RC_BIT(&p
->rc
, prob
, bit
);
2285 while (symbol
< (1 << kNumPosSlotBits
* 2));
2286 p
->rc
.range
= range
;
2289 if (dist
>= kStartPosModelIndex
)
2291 unsigned footerBits
= ((posSlot
>> 1) - 1);
2293 if (dist
< kNumFullDistances
)
2295 unsigned base
= ((2 | (posSlot
& 1)) << footerBits
);
2296 RcTree_ReverseEncode(&p
->rc
, p
->posEncoders
+ base
, footerBits
, dist
- base
);
2300 UInt32 pos2
= (dist
| 0xF) << (32 - footerBits
);
2301 range
= p
->rc
.range
;
2302 // RangeEnc_EncodeDirectBits(&p->rc, posReduced >> kNumAlignBits, footerBits - kNumAlignBits);
2307 p->rc.low += range & (0 - ((dist >> --footerBits) & 1));
2310 while (footerBits > kNumAlignBits);
2315 p
->rc
.low
+= range
& (0 - (pos2
>> 31));
2319 while (pos2
!= 0xF0000000);
2322 // RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, posReduced & kAlignMask);
2327 bit
= dist
& 1; dist
>>= 1; RC_BIT(&p
->rc
, p
->posAlignEncoder
+ m
, bit
); m
= (m
<< 1) + bit
;
2328 bit
= dist
& 1; dist
>>= 1; RC_BIT(&p
->rc
, p
->posAlignEncoder
+ m
, bit
); m
= (m
<< 1) + bit
;
2329 bit
= dist
& 1; dist
>>= 1; RC_BIT(&p
->rc
, p
->posAlignEncoder
+ m
, bit
); m
= (m
<< 1) + bit
;
2330 bit
= dist
& 1; RC_BIT(&p
->rc
, p
->posAlignEncoder
+ m
, bit
);
2331 p
->rc
.range
= range
;
2332 p
->alignPriceCount
++;
2340 p
->additionalOffset
-= len
;
2342 if (p
->additionalOffset
== 0)
2348 if (p
->matchPriceCount
>= (1 << 7))
2349 FillDistancesPrices(p
);
2350 if (p
->alignPriceCount
>= kAlignTableSize
)
2354 if (p
->matchFinder
.GetNumAvailableBytes(p
->matchFinderObj
) == 0)
2356 processed
= nowPos32
- startPos32
;
2360 if (processed
+ kNumOpts
+ 300 >= maxUnpackSize
2361 || RangeEnc_GetProcessed_sizet(&p
->rc
) + kPackReserve
>= maxPackSize
)
2364 else if (processed
>= (1 << 17))
2366 p
->nowPos64
+= nowPos32
- startPos32
;
2367 return CheckErrors(p
);
2372 p
->nowPos64
+= nowPos32
- startPos32
;
2373 return Flush(p
, nowPos32
);
2378 #define kBigHashDicLimit ((UInt32)1 << 24)
2380 static SRes
LzmaEnc_Alloc(CLzmaEnc
*p
, UInt32 keepWindowSize
, ISzAllocPtr alloc
, ISzAllocPtr allocBig
)
2382 UInt32 beforeSize
= kNumOpts
;
2383 if (!RangeEnc_Alloc(&p
->rc
, alloc
))
2384 return SZ_ERROR_MEM
;
2387 p
->mtMode
= (p
->multiThread
&& !p
->fastMode
&& (p
->matchFinderBase
.btMode
!= 0));
2391 unsigned lclp
= p
->lc
+ p
->lp
;
2392 if (!p
->litProbs
|| !p
->saveState
.litProbs
|| p
->lclp
!= lclp
)
2394 LzmaEnc_FreeLits(p
, alloc
);
2395 p
->litProbs
= (CLzmaProb
*)ISzAlloc_Alloc(alloc
, ((UInt32
)0x300 << lclp
) * sizeof(CLzmaProb
));
2396 p
->saveState
.litProbs
= (CLzmaProb
*)ISzAlloc_Alloc(alloc
, ((UInt32
)0x300 << lclp
) * sizeof(CLzmaProb
));
2397 if (!p
->litProbs
|| !p
->saveState
.litProbs
)
2399 LzmaEnc_FreeLits(p
, alloc
);
2400 return SZ_ERROR_MEM
;
2406 p
->matchFinderBase
.bigHash
= (Byte
)(p
->dictSize
> kBigHashDicLimit
? 1 : 0);
2408 if (beforeSize
+ p
->dictSize
< keepWindowSize
)
2409 beforeSize
= keepWindowSize
- p
->dictSize
;
2414 RINOK(MatchFinderMt_Create(&p
->matchFinderMt
, p
->dictSize
, beforeSize
, p
->numFastBytes
,
2418 p
->matchFinderObj
= &p
->matchFinderMt
;
2419 p
->matchFinderBase
.bigHash
= (Byte
)(
2420 (p
->dictSize
> kBigHashDicLimit
&& p
->matchFinderBase
.hashMask
>= 0xFFFFFF) ? 1 : 0);
2421 MatchFinderMt_CreateVTable(&p
->matchFinderMt
, &p
->matchFinder
);
2426 if (!MatchFinder_Create(&p
->matchFinderBase
, p
->dictSize
, beforeSize
, p
->numFastBytes
, LZMA_MATCH_LEN_MAX
, allocBig
))
2427 return SZ_ERROR_MEM
;
2428 p
->matchFinderObj
= &p
->matchFinderBase
;
2429 MatchFinder_CreateVTable(&p
->matchFinderBase
, &p
->matchFinder
);
2435 void LzmaEnc_Init(CLzmaEnc
*p
)
2444 RangeEnc_Init(&p
->rc
);
2446 for (i
= 0; i
< (1 << kNumAlignBits
); i
++)
2447 p
->posAlignEncoder
[i
] = kProbInitValue
;
2449 for (i
= 0; i
< kNumStates
; i
++)
2452 for (j
= 0; j
< LZMA_NUM_PB_STATES_MAX
; j
++)
2454 p
->isMatch
[i
][j
] = kProbInitValue
;
2455 p
->isRep0Long
[i
][j
] = kProbInitValue
;
2457 p
->isRep
[i
] = kProbInitValue
;
2458 p
->isRepG0
[i
] = kProbInitValue
;
2459 p
->isRepG1
[i
] = kProbInitValue
;
2460 p
->isRepG2
[i
] = kProbInitValue
;
2464 for (i
= 0; i
< kNumLenToPosStates
; i
++)
2466 CLzmaProb
*probs
= p
->posSlotEncoder
[i
];
2468 for (j
= 0; j
< (1 << kNumPosSlotBits
); j
++)
2469 probs
[j
] = kProbInitValue
;
2473 for (i
= 0; i
< kNumFullDistances
; i
++)
2474 p
->posEncoders
[i
] = kProbInitValue
;
2478 UInt32 num
= (UInt32
)0x300 << (p
->lp
+ p
->lc
);
2480 CLzmaProb
*probs
= p
->litProbs
;
2481 for (k
= 0; k
< num
; k
++)
2482 probs
[k
] = kProbInitValue
;
2486 LenEnc_Init(&p
->lenProbs
);
2487 LenEnc_Init(&p
->repLenProbs
);
2491 p
->additionalOffset
= 0;
2493 p
->pbMask
= (1 << p
->pb
) - 1;
2494 p
->lpMask
= ((UInt32
)0x100 << p
->lp
) - ((unsigned)0x100 >> p
->lc
);
2497 void LzmaEnc_InitPrices(CLzmaEnc
*p
)
2501 FillDistancesPrices(p
);
2505 p
->lenEnc
.tableSize
=
2506 p
->repLenEnc
.tableSize
=
2507 p
->numFastBytes
+ 1 - LZMA_MATCH_LEN_MIN
;
2508 LenPriceEnc_UpdateTables(&p
->lenEnc
, 1 << p
->pb
, &p
->lenProbs
, p
->ProbPrices
);
2509 LenPriceEnc_UpdateTables(&p
->repLenEnc
, 1 << p
->pb
, &p
->repLenProbs
, p
->ProbPrices
);
2512 static SRes
LzmaEnc_AllocAndInit(CLzmaEnc
*p
, UInt32 keepWindowSize
, ISzAllocPtr alloc
, ISzAllocPtr allocBig
)
2515 for (i
= kEndPosModelIndex
/ 2; i
< kDicLogSizeMax
; i
++)
2516 if (p
->dictSize
<= ((UInt32
)1 << i
))
2518 p
->distTableSize
= i
* 2;
2520 p
->finished
= False
;
2522 RINOK(LzmaEnc_Alloc(p
, keepWindowSize
, alloc
, allocBig
));
2524 LzmaEnc_InitPrices(p
);
2529 static SRes
LzmaEnc_Prepare(CLzmaEncHandle pp
, ISeqOutStream
*outStream
, ISeqInStream
*inStream
,
2530 ISzAllocPtr alloc
, ISzAllocPtr allocBig
)
2532 CLzmaEnc
*p
= (CLzmaEnc
*)pp
;
2533 p
->matchFinderBase
.stream
= inStream
;
2535 p
->rc
.outStream
= outStream
;
2536 return LzmaEnc_AllocAndInit(p
, 0, alloc
, allocBig
);
2539 SRes
LzmaEnc_PrepareForLzma2(CLzmaEncHandle pp
,
2540 ISeqInStream
*inStream
, UInt32 keepWindowSize
,
2541 ISzAllocPtr alloc
, ISzAllocPtr allocBig
)
2543 CLzmaEnc
*p
= (CLzmaEnc
*)pp
;
2544 p
->matchFinderBase
.stream
= inStream
;
2546 return LzmaEnc_AllocAndInit(p
, keepWindowSize
, alloc
, allocBig
);
2549 static void LzmaEnc_SetInputBuf(CLzmaEnc
*p
, const Byte
*src
, SizeT srcLen
)
2551 p
->matchFinderBase
.directInput
= 1;
2552 p
->matchFinderBase
.bufferBase
= (Byte
*)src
;
2553 p
->matchFinderBase
.directInputRem
= srcLen
;
2556 SRes
LzmaEnc_MemPrepare(CLzmaEncHandle pp
, const Byte
*src
, SizeT srcLen
,
2557 UInt32 keepWindowSize
, ISzAllocPtr alloc
, ISzAllocPtr allocBig
)
2559 CLzmaEnc
*p
= (CLzmaEnc
*)pp
;
2560 LzmaEnc_SetInputBuf(p
, src
, srcLen
);
2563 LzmaEnc_SetDataSize(pp
, srcLen
);
2564 return LzmaEnc_AllocAndInit(p
, keepWindowSize
, alloc
, allocBig
);
2567 void LzmaEnc_Finish(CLzmaEncHandle pp
)
2570 CLzmaEnc
*p
= (CLzmaEnc
*)pp
;
2572 MatchFinderMt_ReleaseStream(&p
->matchFinderMt
);
2585 } CLzmaEnc_SeqOutStreamBuf
;
2587 static size_t SeqOutStreamBuf_Write(const ISeqOutStream
*pp
, const void *data
, size_t size
)
2589 CLzmaEnc_SeqOutStreamBuf
*p
= CONTAINER_FROM_VTBL(pp
, CLzmaEnc_SeqOutStreamBuf
, vt
);
2595 memcpy(p
->data
, data
, size
);
2602 UInt32
LzmaEnc_GetNumAvailableBytes(CLzmaEncHandle pp
)
2604 const CLzmaEnc
*p
= (CLzmaEnc
*)pp
;
2605 return p
->matchFinder
.GetNumAvailableBytes(p
->matchFinderObj
);
2609 const Byte
*LzmaEnc_GetCurBuf(CLzmaEncHandle pp
)
2611 const CLzmaEnc
*p
= (CLzmaEnc
*)pp
;
2612 return p
->matchFinder
.GetPointerToCurrentPos(p
->matchFinderObj
) - p
->additionalOffset
;
2616 SRes
LzmaEnc_CodeOneMemBlock(CLzmaEncHandle pp
, Bool reInit
,
2617 Byte
*dest
, size_t *destLen
, UInt32 desiredPackSize
, UInt32
*unpackSize
)
2619 CLzmaEnc
*p
= (CLzmaEnc
*)pp
;
2622 CLzmaEnc_SeqOutStreamBuf outStream
;
2624 outStream
.vt
.Write
= SeqOutStreamBuf_Write
;
2625 outStream
.data
= dest
;
2626 outStream
.rem
= *destLen
;
2627 outStream
.overflow
= False
;
2629 p
->writeEndMark
= False
;
2630 p
->finished
= False
;
2635 LzmaEnc_InitPrices(p
);
2637 nowPos64
= p
->nowPos64
;
2638 RangeEnc_Init(&p
->rc
);
2639 p
->rc
.outStream
= &outStream
.vt
;
2641 if (desiredPackSize
== 0)
2642 return SZ_ERROR_OUTPUT_EOF
;
2644 res
= LzmaEnc_CodeOneBlock(p
, desiredPackSize
, *unpackSize
);
2646 *unpackSize
= (UInt32
)(p
->nowPos64
- nowPos64
);
2647 *destLen
-= outStream
.rem
;
2648 if (outStream
.overflow
)
2649 return SZ_ERROR_OUTPUT_EOF
;
2655 static SRes
LzmaEnc_Encode2(CLzmaEnc
*p
, ICompressProgress
*progress
)
2660 Byte allocaDummy
[0x300];
2662 allocaDummy
[1] = allocaDummy
[0];
2667 res
= LzmaEnc_CodeOneBlock(p
, 0, 0);
2668 if (res
!= SZ_OK
|| p
->finished
)
2672 res
= ICompressProgress_Progress(progress
, p
->nowPos64
, RangeEnc_GetProcessed(&p
->rc
));
2675 res
= SZ_ERROR_PROGRESS
;
2684 if (res == SZ_OK && !Inline_MatchFinder_IsFinishedOK(&p->matchFinderBase))
2685 res = SZ_ERROR_FAIL;
2693 SRes
LzmaEnc_Encode(CLzmaEncHandle pp
, ISeqOutStream
*outStream
, ISeqInStream
*inStream
, ICompressProgress
*progress
,
2694 ISzAllocPtr alloc
, ISzAllocPtr allocBig
)
2696 RINOK(LzmaEnc_Prepare(pp
, outStream
, inStream
, alloc
, allocBig
));
2697 return LzmaEnc_Encode2((CLzmaEnc
*)pp
, progress
);
2701 SRes
LzmaEnc_WriteProperties(CLzmaEncHandle pp
, Byte
*props
, SizeT
*size
)
2703 CLzmaEnc
*p
= (CLzmaEnc
*)pp
;
2705 UInt32 dictSize
= p
->dictSize
;
2706 if (*size
< LZMA_PROPS_SIZE
)
2707 return SZ_ERROR_PARAM
;
2708 *size
= LZMA_PROPS_SIZE
;
2709 props
[0] = (Byte
)((p
->pb
* 5 + p
->lp
) * 9 + p
->lc
);
2711 if (dictSize
>= ((UInt32
)1 << 22))
2713 UInt32 kDictMask
= ((UInt32
)1 << 20) - 1;
2714 if (dictSize
< (UInt32
)0xFFFFFFFF - kDictMask
)
2715 dictSize
= (dictSize
+ kDictMask
) & ~kDictMask
;
2717 else for (i
= 11; i
<= 30; i
++)
2719 if (dictSize
<= ((UInt32
)2 << i
)) { dictSize
= (2 << i
); break; }
2720 if (dictSize
<= ((UInt32
)3 << i
)) { dictSize
= (3 << i
); break; }
2723 for (i
= 0; i
< 4; i
++)
2724 props
[1 + i
] = (Byte
)(dictSize
>> (8 * i
));
2729 unsigned LzmaEnc_IsWriteEndMark(CLzmaEncHandle pp
)
2731 return ((CLzmaEnc
*)pp
)->writeEndMark
;
2735 SRes
LzmaEnc_MemEncode(CLzmaEncHandle pp
, Byte
*dest
, SizeT
*destLen
, const Byte
*src
, SizeT srcLen
,
2736 int writeEndMark
, ICompressProgress
*progress
, ISzAllocPtr alloc
, ISzAllocPtr allocBig
)
2739 CLzmaEnc
*p
= (CLzmaEnc
*)pp
;
2741 CLzmaEnc_SeqOutStreamBuf outStream
;
2743 outStream
.vt
.Write
= SeqOutStreamBuf_Write
;
2744 outStream
.data
= dest
;
2745 outStream
.rem
= *destLen
;
2746 outStream
.overflow
= False
;
2748 p
->writeEndMark
= writeEndMark
;
2749 p
->rc
.outStream
= &outStream
.vt
;
2751 res
= LzmaEnc_MemPrepare(pp
, src
, srcLen
, 0, alloc
, allocBig
);
2755 res
= LzmaEnc_Encode2(p
, progress
);
2756 if (res
== SZ_OK
&& p
->nowPos64
!= srcLen
)
2757 res
= SZ_ERROR_FAIL
;
2760 *destLen
-= outStream
.rem
;
2761 if (outStream
.overflow
)
2762 return SZ_ERROR_OUTPUT_EOF
;
2767 SRes
LzmaEncode(Byte
*dest
, SizeT
*destLen
, const Byte
*src
, SizeT srcLen
,
2768 const CLzmaEncProps
*props
, Byte
*propsEncoded
, SizeT
*propsSize
, int writeEndMark
,
2769 ICompressProgress
*progress
, ISzAllocPtr alloc
, ISzAllocPtr allocBig
)
2771 CLzmaEnc
*p
= (CLzmaEnc
*)LzmaEnc_Create(alloc
);
2774 return SZ_ERROR_MEM
;
2776 res
= LzmaEnc_SetProps(p
, props
);
2779 res
= LzmaEnc_WriteProperties(p
, propsEncoded
, propsSize
);
2781 res
= LzmaEnc_MemEncode(p
, dest
, destLen
, src
, srcLen
,
2782 writeEndMark
, progress
, alloc
, allocBig
);
2785 LzmaEnc_Destroy(p
, alloc
, allocBig
);