| 1 |
unit ULZMAEncoder; |
| 2 |
|
| 3 |
{$IFDEF FPC} |
| 4 |
{$MODE Delphi} |
| 5 |
{$ENDIF} |
| 6 |
|
| 7 |
interface |
| 8 |
|
| 9 |
uses UBitTreeEncoder,ULZMABase,ULZBinTree,URangeEncoder,Classes,Math,ULZMACommon; |
| 10 |
|
| 11 |
const EMatchFinderTypeBT2 = 0; |
| 12 |
EMatchFinderTypeBT4 = 1; |
| 13 |
kIfinityPrice:integer = $FFFFFFF; |
| 14 |
kDefaultDictionaryLogSize = 22; |
| 15 |
kNumFastBytesDefault = $20; |
| 16 |
kNumLenSpecSymbols = ULZMABase.kNumLowLenSymbols + ULZMABase.kNumMidLenSymbols; |
| 17 |
kNumOpts = 1 shl 12; |
| 18 |
kPropSize = 5; |
| 19 |
|
| 20 |
type TLZMAEncoder2=class; |
| 21 |
TLZMALiteralEncoder=class; |
| 22 |
TLZMAOptimal=class; |
| 23 |
TLZMALenPriceTableEncoder=class; |
| 24 |
|
| 25 |
TLZMAEncoder=class |
| 26 |
private |
| 27 |
FOnProgress:TLZMAProgress; |
| 28 |
procedure DoProgress(const Action:TLZMAProgressAction;const Value:integer); |
| 29 |
public |
| 30 |
g_FastPos:array [0..1 shl 11-1] of byte; |
| 31 |
_state:integer; |
| 32 |
_previousByte:byte; |
| 33 |
_repDistances:array [0..ULZMABase.kNumRepDistances-1] of integer; |
| 34 |
|
| 35 |
_optimum: array [0..kNumOpts-1] of TLZMAOptimal; |
| 36 |
_matchFinder:TLZBinTree; |
| 37 |
_rangeEncoder:TRangeEncoder; |
| 38 |
|
| 39 |
_isMatch:array [0..ULZMABase.kNumStates shl ULZMABase.kNumPosStatesBitsMax-1]of smallint; |
| 40 |
_isRep:array [0..ULZMABase.kNumStates-1] of smallint; |
| 41 |
_isRepG0:array [0..ULZMABase.kNumStates-1] of smallint; |
| 42 |
_isRepG1:array [0..ULZMABase.kNumStates-1] of smallint; |
| 43 |
_isRepG2:array [0..ULZMABase.kNumStates-1] of smallint; |
| 44 |
_isRep0Long:array [0..ULZMABase.kNumStates shl ULZMABase.kNumPosStatesBitsMax-1]of smallint; |
| 45 |
|
| 46 |
_posSlotEncoder:array [0..ULZMABase.kNumLenToPosStates-1] of TBitTreeEncoder; // kNumPosSlotBits |
| 47 |
|
| 48 |
_posEncoders:array [0..ULZMABase.kNumFullDistances-ULZMABase.kEndPosModelIndex-1]of smallint; |
| 49 |
_posAlignEncoder:TBitTreeEncoder; |
| 50 |
|
| 51 |
_lenEncoder:TLZMALenPriceTableEncoder; |
| 52 |
_repMatchLenEncoder:TLZMALenPriceTableEncoder; |
| 53 |
|
| 54 |
_literalEncoder:TLZMALiteralEncoder; |
| 55 |
|
| 56 |
_matchDistances:array [0..ULZMABase.kMatchMaxLen*2+1] of integer; |
| 57 |
|
| 58 |
_numFastBytes:integer; |
| 59 |
_longestMatchLength:integer; |
| 60 |
_numDistancePairs:integer; |
| 61 |
|
| 62 |
_additionalOffset:integer; |
| 63 |
|
| 64 |
_optimumEndIndex:integer; |
| 65 |
_optimumCurrentIndex:integer; |
| 66 |
|
| 67 |
_longestMatchWasFound:boolean; |
| 68 |
|
| 69 |
_posSlotPrices:array [0..1 shl (ULZMABase.kNumPosSlotBits+ULZMABase.kNumLenToPosStatesBits)-1] of integer; |
| 70 |
_distancesPrices:array [0..ULZMABase.kNumFullDistances shl ULZMABase.kNumLenToPosStatesBits-1] of integer; |
| 71 |
_alignPrices:array [0..ULZMABase.kAlignTableSize-1] of integer; |
| 72 |
_alignPriceCount:integer; |
| 73 |
|
| 74 |
_distTableSize:integer; |
| 75 |
|
| 76 |
_posStateBits:integer; |
| 77 |
_posStateMask:integer; |
| 78 |
_numLiteralPosStateBits:integer; |
| 79 |
_numLiteralContextBits:integer; |
| 80 |
|
| 81 |
_dictionarySize:integer; |
| 82 |
_dictionarySizePrev:integer; |
| 83 |
_numFastBytesPrev:integer; |
| 84 |
|
| 85 |
nowPos64:int64; |
| 86 |
_finished:boolean; |
| 87 |
_inStream:TStream; |
| 88 |
|
| 89 |
_matchFinderType:integer; |
| 90 |
_writeEndMark:boolean; |
| 91 |
|
| 92 |
_needReleaseMFStream:boolean; |
| 93 |
|
| 94 |
reps:array [0..ULZMABase.kNumRepDistances-1]of integer; |
| 95 |
repLens:array [0..ULZMABase.kNumRepDistances-1] of integer; |
| 96 |
backRes:integer; |
| 97 |
processedInSize:int64; |
| 98 |
processedOutSize:int64; |
| 99 |
finished:boolean; |
| 100 |
properties:array [0..kPropSize] of byte; |
| 101 |
tempPrices:array [0..ULZMABase.kNumFullDistances-1]of integer; |
| 102 |
_matchPriceCount:integer; |
| 103 |
constructor Create; |
| 104 |
destructor Destroy;override; |
| 105 |
function GetPosSlot(const pos:integer):integer; |
| 106 |
function GetPosSlot2(const pos:integer):integer; |
| 107 |
procedure BaseInit; |
| 108 |
procedure _Create; |
| 109 |
procedure SetWriteEndMarkerMode(const writeEndMarker:boolean); |
| 110 |
procedure Init; |
| 111 |
function ReadMatchDistances:integer; |
| 112 |
procedure MovePos(const num:integer); |
| 113 |
function GetRepLen1Price(const state,posState:integer):integer; |
| 114 |
function GetPureRepPrice(const repIndex, state, posState:integer):integer; |
| 115 |
function GetRepPrice(const repIndex, len, state, posState:integer):integer; |
| 116 |
function GetPosLenPrice(const pos, len, posState:integer):integer; |
| 117 |
function Backward(cur:integer):integer; |
| 118 |
function GetOptimum(position:integer):integer; |
| 119 |
function ChangePair(const smallDist, bigDist:integer):boolean; |
| 120 |
procedure WriteEndMarker(const posState:integer); |
| 121 |
procedure Flush(const nowPos:integer); |
| 122 |
procedure ReleaseMFStream; |
| 123 |
procedure CodeOneBlock(var inSize,outSize:int64;var finished:boolean); |
| 124 |
procedure FillDistancesPrices; |
| 125 |
procedure FillAlignPrices; |
| 126 |
procedure SetOutStream(const outStream:TStream); |
| 127 |
procedure ReleaseOutStream; |
| 128 |
procedure ReleaseStreams; |
| 129 |
procedure SetStreams(const inStream, outStream:TStream;const inSize, outSize:int64); |
| 130 |
procedure Code(const inStream, outStream:TStream;const inSize, outSize:int64); |
| 131 |
procedure WriteCoderProperties(const outStream:TStream); |
| 132 |
function SetAlgorithm(const algorithm:integer):boolean; |
| 133 |
function SetDictionarySize(dictionarySize:integer):boolean; |
| 134 |
function SeNumFastBytes(const numFastBytes:integer):boolean; |
| 135 |
function SetMatchFinder(const matchFinderIndex:integer):boolean; |
| 136 |
function SetLcLpPb(const lc,lp,pb:integer):boolean; |
| 137 |
procedure SetEndMarkerMode(const endMarkerMode:boolean); |
| 138 |
property OnProgress:TLZMAProgress read FOnProgress write FOnProgress; |
| 139 |
end; |
| 140 |
|
| 141 |
TLZMALiteralEncoder=class |
| 142 |
public |
| 143 |
m_Coders: array of TLZMAEncoder2; |
| 144 |
m_NumPrevBits:integer; |
| 145 |
m_NumPosBits:integer; |
| 146 |
m_PosMask:integer; |
| 147 |
procedure _Create(const numPosBits,numPrevBits:integer); |
| 148 |
destructor Destroy;override; |
| 149 |
procedure Init; |
| 150 |
function GetSubCoder(const pos:integer;const prevByte:byte):TLZMAEncoder2; |
| 151 |
end; |
| 152 |
|
| 153 |
TLZMAEncoder2=class |
| 154 |
public |
| 155 |
m_Encoders: array[0..$300-1] of smallint; |
| 156 |
procedure Init; |
| 157 |
procedure Encode(const rangeEncoder:TRangeEncoder;const symbol:byte); |
| 158 |
procedure EncodeMatched(const rangeEncoder:TRangeEncoder;const matchByte,symbol:byte); |
| 159 |
function GetPrice(const matchMode:boolean;const matchByte,symbol:byte):integer; |
| 160 |
end; |
| 161 |
|
| 162 |
TLZMALenEncoder=class |
| 163 |
public |
| 164 |
_choice:array[0..1] of smallint; |
| 165 |
_lowCoder: array [0..ULZMABase.kNumPosStatesEncodingMax-1] of TBitTreeEncoder; |
| 166 |
_midCoder: array [0..ULZMABase.kNumPosStatesEncodingMax-1] of TBitTreeEncoder; |
| 167 |
_highCoder:TBitTreeEncoder; |
| 168 |
constructor Create; |
| 169 |
destructor Destroy;override; |
| 170 |
procedure Init(const numPosStates:integer); |
| 171 |
procedure Encode(const rangeEncoder:TRangeEncoder;symbol:integer;const posState:integer);virtual; |
| 172 |
procedure SetPrices(const posState,numSymbols:integer;var prices:array of integer;const st:integer); |
| 173 |
end; |
| 174 |
|
| 175 |
TLZMALenPriceTableEncoder=class(TLZMALenEncoder) |
| 176 |
public |
| 177 |
_prices: array [0..ULZMABase.kNumLenSymbols shl ULZMABase.kNumPosStatesBitsEncodingMax-1] of integer; |
| 178 |
_tableSize:integer; |
| 179 |
_counters: array [0..ULZMABase.kNumPosStatesEncodingMax-1] of integer; |
| 180 |
procedure SetTableSize(const tableSize:integer); |
| 181 |
function GetPrice(const symbol,posState:integer):integer; |
| 182 |
procedure UpdateTable(const posState:integer); |
| 183 |
procedure UpdateTables(const numPosStates:integer); |
| 184 |
procedure Encode(const rangeEncoder:TRangeEncoder;symbol:integer;const posState:integer);override; |
| 185 |
end; |
| 186 |
|
| 187 |
TLZMAOptimal=class |
| 188 |
public |
| 189 |
State:integer; |
| 190 |
|
| 191 |
Prev1IsChar:boolean; |
| 192 |
Prev2:boolean; |
| 193 |
|
| 194 |
PosPrev2:integer; |
| 195 |
BackPrev2:integer; |
| 196 |
|
| 197 |
Price:integer; |
| 198 |
PosPrev:integer; |
| 199 |
BackPrev:integer; |
| 200 |
|
| 201 |
Backs0:integer; |
| 202 |
Backs1:integer; |
| 203 |
Backs2:integer; |
| 204 |
Backs3:integer; |
| 205 |
|
| 206 |
procedure MakeAsChar; |
| 207 |
procedure MakeAsShortRep; |
| 208 |
function IsShortRep:boolean; |
| 209 |
end; |
| 210 |
|
| 211 |
implementation |
| 212 |
|
| 213 |
constructor TLZMAEncoder.Create; |
| 214 |
var kFastSlots,c,slotFast,j,k:integer; |
| 215 |
begin |
| 216 |
kFastSlots := 22; |
| 217 |
c := 2; |
| 218 |
g_FastPos[0] := 0; |
| 219 |
g_FastPos[1] := 1; |
| 220 |
for slotFast := 2 to kFastSlots -1 do begin |
| 221 |
k := (1 shl ((slotFast shr 1) - 1)); |
| 222 |
for j := 0 to k -1 do begin |
| 223 |
g_FastPos[c] := slotFast; |
| 224 |
inc(c); |
| 225 |
end; |
| 226 |
end; |
| 227 |
_state := ULZMABase.StateInit(); |
| 228 |
_matchFinder:=nil; |
| 229 |
_rangeEncoder:=TRangeEncoder.Create; |
| 230 |
_posAlignEncoder:=TBitTreeEncoder.Create(ULZMABase.kNumAlignBits); |
| 231 |
_lenEncoder:=TLZMALenPriceTableEncoder.Create; |
| 232 |
_repMatchLenEncoder:=TLZMALenPriceTableEncoder.Create; |
| 233 |
_literalEncoder:=TLZMALiteralEncoder.Create; |
| 234 |
_numFastBytes:= kNumFastBytesDefault; |
| 235 |
_distTableSize:= (kDefaultDictionaryLogSize * 2); |
| 236 |
_posStateBits:= 2; |
| 237 |
_posStateMask:= (4 - 1); |
| 238 |
_numLiteralPosStateBits:= 0; |
| 239 |
_numLiteralContextBits:= 3; |
| 240 |
|
| 241 |
_dictionarySize:= (1 shl kDefaultDictionaryLogSize); |
| 242 |
_dictionarySizePrev:= -1; |
| 243 |
_numFastBytesPrev:= -1; |
| 244 |
_matchFinderType:= EMatchFinderTypeBT4; |
| 245 |
_writeEndMark:= false; |
| 246 |
|
| 247 |
_needReleaseMFStream:= false; |
| 248 |
end; |
| 249 |
|
| 250 |
destructor TLZMAEncoder.Destroy; |
| 251 |
var i:integer; |
| 252 |
begin |
| 253 |
_rangeEncoder.Free; |
| 254 |
_posAlignEncoder.Free; |
| 255 |
_lenEncoder.Free; |
| 256 |
_repMatchLenEncoder.Free; |
| 257 |
_literalEncoder.Free; |
| 258 |
if _matchFinder<>nil then _matchFinder.Free; |
| 259 |
for i := 0 to kNumOpts -1 do |
| 260 |
_optimum[i].Free; |
| 261 |
for i := 0 to ULZMABase.kNumLenToPosStates -1 do |
| 262 |
_posSlotEncoder[i].Free; |
| 263 |
end; |
| 264 |
|
| 265 |
procedure TLZMAEncoder._Create; |
| 266 |
var bt:TLZBinTree; |
| 267 |
numHashBytes,i:integer; |
| 268 |
begin |
| 269 |
if _matchFinder = nil then begin |
| 270 |
bt := TLZBinTree.Create; |
| 271 |
numHashBytes:= 4; |
| 272 |
if _matchFinderType = EMatchFinderTypeBT2 then |
| 273 |
numHashBytes := 2; |
| 274 |
bt.SetType(numHashBytes); |
| 275 |
_matchFinder := bt; |
| 276 |
end; |
| 277 |
_literalEncoder._Create(_numLiteralPosStateBits, _numLiteralContextBits); |
| 278 |
|
| 279 |
if (_dictionarySize = _dictionarySizePrev) and (_numFastBytesPrev = _numFastBytes) then |
| 280 |
exit; |
| 281 |
_matchFinder._Create(_dictionarySize, kNumOpts, _numFastBytes, ULZMABase.kMatchMaxLen + 1); |
| 282 |
_dictionarySizePrev := _dictionarySize; |
| 283 |
_numFastBytesPrev := _numFastBytes; |
| 284 |
|
| 285 |
for i := 0 to kNumOpts -1 do |
| 286 |
_optimum[i]:=TLZMAOptimal.Create; |
| 287 |
for i := 0 to ULZMABase.kNumLenToPosStates -1 do |
| 288 |
_posSlotEncoder[i] :=TBitTreeEncoder.Create(ULZMABase.kNumPosSlotBits); |
| 289 |
end; |
| 290 |
|
| 291 |
function TLZMAEncoder.GetPosSlot(const pos:integer):integer; |
| 292 |
begin |
| 293 |
if (pos < (1 shl 11)) then |
| 294 |
result:=g_FastPos[pos] |
| 295 |
else if (pos < (1 shl 21)) then |
| 296 |
result:=(g_FastPos[pos shr 10] + 20) |
| 297 |
else result:=(g_FastPos[pos shr 20] + 40); |
| 298 |
end; |
| 299 |
|
| 300 |
function TLZMAEncoder.GetPosSlot2(const pos:integer):integer; |
| 301 |
begin |
| 302 |
if (pos < (1 shl 17)) then |
| 303 |
result:=(g_FastPos[pos shr 6] + 12) |
| 304 |
else if (pos < (1 shl 27)) then |
| 305 |
result:=(g_FastPos[pos shr 16] + 32) |
| 306 |
else result:=(g_FastPos[pos shr 26] + 52); |
| 307 |
end; |
| 308 |
|
| 309 |
procedure TLZMAEncoder.BaseInit; |
| 310 |
var i:integer; |
| 311 |
begin |
| 312 |
_state := ulzmaBase.StateInit; |
| 313 |
_previousByte := 0; |
| 314 |
for i := 0 to ULZMABase.kNumRepDistances -1 do |
| 315 |
_repDistances[i] := 0; |
| 316 |
end; |
| 317 |
|
| 318 |
procedure TLZMAEncoder.SetWriteEndMarkerMode(const writeEndMarker:boolean); |
| 319 |
begin |
| 320 |
_writeEndMark := writeEndMarker; |
| 321 |
end; |
| 322 |
|
| 323 |
procedure TLZMAEncoder.Init; |
| 324 |
var i:integer; |
| 325 |
begin |
| 326 |
BaseInit; |
| 327 |
_rangeEncoder.Init; |
| 328 |
|
| 329 |
URangeEncoder.InitBitModels(_isMatch); |
| 330 |
URangeEncoder.InitBitModels(_isRep0Long); |
| 331 |
URangeEncoder.InitBitModels(_isRep); |
| 332 |
URangeEncoder.InitBitModels(_isRepG0); |
| 333 |
URangeEncoder.InitBitModels(_isRepG1); |
| 334 |
URangeEncoder.InitBitModels(_isRepG2); |
| 335 |
URangeEncoder.InitBitModels(_posEncoders); |
| 336 |
|
| 337 |
|
| 338 |
_literalEncoder.Init(); |
| 339 |
for i := 0 to ULZMABase.kNumLenToPosStates -1 do |
| 340 |
_posSlotEncoder[i].Init; |
| 341 |
|
| 342 |
_lenEncoder.Init(1 shl _posStateBits); |
| 343 |
_repMatchLenEncoder.Init(1 shl _posStateBits); |
| 344 |
|
| 345 |
_posAlignEncoder.Init; |
| 346 |
|
| 347 |
_longestMatchWasFound := false; |
| 348 |
_optimumEndIndex := 0; |
| 349 |
_optimumCurrentIndex := 0; |
| 350 |
_additionalOffset := 0; |
| 351 |
end; |
| 352 |
|
| 353 |
function TLZMAEncoder.ReadMatchDistances:integer; |
| 354 |
var lenRes:integer; |
| 355 |
begin |
| 356 |
lenRes := 0; |
| 357 |
_numDistancePairs := _matchFinder.GetMatches(_matchDistances); |
| 358 |
|
| 359 |
if _numDistancePairs > 0 then begin |
| 360 |
lenRes := _matchDistances[_numDistancePairs - 2]; |
| 361 |
if lenRes = _numFastBytes then |
| 362 |
lenRes := lenRes + _matchFinder.GetMatchLen(lenRes - 1, _matchDistances[_numDistancePairs - 1], ULZMABase.kMatchMaxLen - lenRes); |
| 363 |
end; |
| 364 |
inc(_additionalOffset); |
| 365 |
result:=lenRes; |
| 366 |
end; |
| 367 |
|
| 368 |
procedure TLZMAEncoder.MovePos(const num:integer); |
| 369 |
begin |
| 370 |
if num > 0 then begin |
| 371 |
_matchFinder.Skip(num); |
| 372 |
_additionalOffset := _additionalOffset + num; |
| 373 |
end; |
| 374 |
end; |
| 375 |
|
| 376 |
function TLZMAEncoder.GetRepLen1Price(const state,posState:integer):integer; |
| 377 |
begin |
| 378 |
result:=RangeEncoder.GetPrice0(_isRepG0[state]) + |
| 379 |
RangeEncoder.GetPrice0(_isRep0Long[(state shl ULZMABase.kNumPosStatesBitsMax) + posState]); |
| 380 |
end; |
| 381 |
|
| 382 |
function TLZMAEncoder.GetPureRepPrice(const repIndex, state, posState:integer):integer; |
| 383 |
var price:integer; |
| 384 |
begin |
| 385 |
if repIndex = 0 then begin |
| 386 |
price := RangeEncoder.GetPrice0(_isRepG0[state]); |
| 387 |
price := price + RangeEncoder.GetPrice1(_isRep0Long[(state shl ULZMABase.kNumPosStatesBitsMax) + posState]); |
| 388 |
end else begin |
| 389 |
price := RangeEncoder.GetPrice1(_isRepG0[state]); |
| 390 |
if repIndex = 1 then |
| 391 |
price := price + RangeEncoder.GetPrice0(_isRepG1[state]) |
| 392 |
else begin |
| 393 |
price := price + RangeEncoder.GetPrice1(_isRepG1[state]); |
| 394 |
price := price + RangeEncoder.GetPrice(_isRepG2[state], repIndex - 2); |
| 395 |
end; |
| 396 |
end; |
| 397 |
result:=price; |
| 398 |
end; |
| 399 |
|
| 400 |
function TLZMAEncoder.GetRepPrice(const repIndex, len, state, posState:integer):integer; |
| 401 |
var price:integer; |
| 402 |
begin |
| 403 |
price := _repMatchLenEncoder.GetPrice(len - ULZMABase.kMatchMinLen, posState); |
| 404 |
result := price + GetPureRepPrice(repIndex, state, posState); |
| 405 |
end; |
| 406 |
|
| 407 |
function TLZMAEncoder.GetPosLenPrice(const pos, len, posState:integer):integer; |
| 408 |
var price,lenToPosState:integer; |
| 409 |
begin |
| 410 |
lenToPosState := ULZMABase.GetLenToPosState(len); |
| 411 |
if pos < ULZMABase.kNumFullDistances then |
| 412 |
price := _distancesPrices[(lenToPosState * ULZMABase.kNumFullDistances) + pos] |
| 413 |
else price := _posSlotPrices[(lenToPosState shl ULZMABase.kNumPosSlotBits) + GetPosSlot2(pos)] + |
| 414 |
_alignPrices[pos and ULZMABase.kAlignMask]; |
| 415 |
result := price + _lenEncoder.GetPrice(len - ULZMABase.kMatchMinLen, posState); |
| 416 |
end; |
| 417 |
|
| 418 |
function TLZMAEncoder.Backward(cur:integer):integer; |
| 419 |
var posMem,backMem,posPrev,backCur:integer; |
| 420 |
begin |
| 421 |
_optimumEndIndex := cur; |
| 422 |
posMem := _optimum[cur].PosPrev; |
| 423 |
backMem := _optimum[cur].BackPrev; |
| 424 |
repeat |
| 425 |
if _optimum[cur].Prev1IsChar then begin |
| 426 |
_optimum[posMem].MakeAsChar; |
| 427 |
_optimum[posMem].PosPrev := posMem - 1; |
| 428 |
if _optimum[cur].Prev2 then begin |
| 429 |
_optimum[posMem - 1].Prev1IsChar := false; |
| 430 |
_optimum[posMem - 1].PosPrev := _optimum[cur].PosPrev2; |
| 431 |
_optimum[posMem - 1].BackPrev := _optimum[cur].BackPrev2; |
| 432 |
end; |
| 433 |
end; |
| 434 |
posPrev := posMem; |
| 435 |
backCur := backMem; |
| 436 |
|
| 437 |
backMem := _optimum[posPrev].BackPrev; |
| 438 |
posMem := _optimum[posPrev].PosPrev; |
| 439 |
|
| 440 |
_optimum[posPrev].BackPrev := backCur; |
| 441 |
_optimum[posPrev].PosPrev := cur; |
| 442 |
cur := posPrev; |
| 443 |
until not (cur > 0); |
| 444 |
backRes := _optimum[0].BackPrev; |
| 445 |
_optimumCurrentIndex := _optimum[0].PosPrev; |
| 446 |
result:=_optimumCurrentIndex; |
| 447 |
end; |
| 448 |
|
| 449 |
function TLZMAEncoder.GetOptimum(position:integer):integer; |
| 450 |
var lenRes,lenMain,numDistancePairs,numAvailableBytes,repMaxIndex,i:integer; |
| 451 |
matchPrice,repMatchPrice,shortRepPrice,lenEnd,len,repLen,price:integer; |
| 452 |
curAndLenPrice,normalMatchPrice,Offs,distance,cur,newLen:integer; |
| 453 |
posPrev,state,pos,curPrice,curAnd1Price,numAvailableBytesFull:integer; |
| 454 |
lenTest2,t,state2,posStateNext,nextRepMatchPrice,offset:integer; |
| 455 |
startLen,repIndex,lenTest,lenTestTemp,curAndLenCharPrice:integer; |
| 456 |
nextMatchPrice,curBack:integer; |
| 457 |
optimum,opt,nextOptimum:TLZMAOptimal; |
| 458 |
currentByte,matchByte,posState:byte; |
| 459 |
nextIsChar:boolean; |
| 460 |
begin |
| 461 |
if (_optimumEndIndex <> _optimumCurrentIndex) then begin |
| 462 |
lenRes := _optimum[_optimumCurrentIndex].PosPrev - _optimumCurrentIndex; |
| 463 |
backRes := _optimum[_optimumCurrentIndex].BackPrev; |
| 464 |
_optimumCurrentIndex := _optimum[_optimumCurrentIndex].PosPrev; |
| 465 |
result:=lenRes; |
| 466 |
exit; |
| 467 |
end;//if optimumendindex |
| 468 |
_optimumCurrentIndex := 0; |
| 469 |
_optimumEndIndex := 0; |
| 470 |
|
| 471 |
if not _longestMatchWasFound then begin |
| 472 |
lenMain := ReadMatchDistances(); |
| 473 |
end else begin //if not longest |
| 474 |
lenMain := _longestMatchLength; |
| 475 |
_longestMatchWasFound := false; |
| 476 |
end;//if not longest else |
| 477 |
numDistancePairs := _numDistancePairs; |
| 478 |
|
| 479 |
numAvailableBytes := _matchFinder.GetNumAvailableBytes + 1; |
| 480 |
if numAvailableBytes < 2 then begin |
| 481 |
backRes := -1; |
| 482 |
result:=1; |
| 483 |
exit; |
| 484 |
end;//if numavailable |
| 485 |
{if numAvailableBytes > ULZMABase.kMatchMaxLen then |
| 486 |
numAvailableBytes := ULZMABase.kMatchMaxLen;} |
| 487 |
|
| 488 |
repMaxIndex := 0; |
| 489 |
for i := 0 to ULZMABase.kNumRepDistances-1 do begin |
| 490 |
reps[i] := _repDistances[i]; |
| 491 |
repLens[i] := _matchFinder.GetMatchLen(0 - 1, reps[i], ULZMABase.kMatchMaxLen); |
| 492 |
if repLens[i] > repLens[repMaxIndex] then |
| 493 |
repMaxIndex := i; |
| 494 |
end;//for i |
| 495 |
if repLens[repMaxIndex] >= _numFastBytes then begin |
| 496 |
backRes := repMaxIndex; |
| 497 |
lenRes := repLens[repMaxIndex]; |
| 498 |
MovePos(lenRes - 1); |
| 499 |
result:=lenRes; |
| 500 |
exit; |
| 501 |
end;//if replens[] |
| 502 |
|
| 503 |
if lenMain >= _numFastBytes then begin |
| 504 |
backRes := _matchDistances[numDistancePairs - 1] + ULZMABase.kNumRepDistances; |
| 505 |
MovePos(lenMain - 1); |
| 506 |
result:=lenMain; |
| 507 |
exit; |
| 508 |
end;//if lenMain |
| 509 |
|
| 510 |
currentByte := _matchFinder.GetIndexByte(0 - 1); |
| 511 |
matchByte := _matchFinder.GetIndexByte(0 - _repDistances[0] - 1 - 1); |
| 512 |
|
| 513 |
if (lenMain < 2) and (currentByte <> matchByte) and (repLens[repMaxIndex] < 2) then begin |
| 514 |
backRes := -1; |
| 515 |
result:=1; |
| 516 |
exit; |
| 517 |
end;//if lenmain<2 |
| 518 |
|
| 519 |
_optimum[0].State := _state; |
| 520 |
|
| 521 |
posState := (position and _posStateMask); |
| 522 |
|
| 523 |
_optimum[1].Price := RangeEncoder.GetPrice0(_isMatch[(_state shl ULZMABase.kNumPosStatesBitsMax) + posState]) + |
| 524 |
_literalEncoder.GetSubCoder(position, _previousByte).GetPrice(not ULZMABase.StateIsCharState(_state), matchByte, currentByte); |
| 525 |
_optimum[1].MakeAsChar(); |
| 526 |
|
| 527 |
matchPrice := RangeEncoder.GetPrice1(_isMatch[(_state shl ULZMABase.kNumPosStatesBitsMax) + posState]); |
| 528 |
repMatchPrice := matchPrice + RangeEncoder.GetPrice1(_isRep[_state]); |
| 529 |
|
| 530 |
if matchByte = currentByte then begin |
| 531 |
shortRepPrice := repMatchPrice + GetRepLen1Price(_state, posState); |
| 532 |
if shortRepPrice < _optimum[1].Price then begin |
| 533 |
_optimum[1].Price := shortRepPrice; |
| 534 |
_optimum[1].MakeAsShortRep; |
| 535 |
end;//if shortrepprice |
| 536 |
end;//if matchbyte |
| 537 |
|
| 538 |
if lenMain >= repLens[repMaxIndex] then lenEnd:=lenMain |
| 539 |
else lenEnd:=repLens[repMaxIndex]; |
| 540 |
|
| 541 |
if lenEnd < 2 then begin |
| 542 |
backRes := _optimum[1].BackPrev; |
| 543 |
result:=1; |
| 544 |
exit; |
| 545 |
end;//if lenend<2 |
| 546 |
|
| 547 |
_optimum[1].PosPrev := 0; |
| 548 |
|
| 549 |
_optimum[0].Backs0 := reps[0]; |
| 550 |
_optimum[0].Backs1 := reps[1]; |
| 551 |
_optimum[0].Backs2 := reps[2]; |
| 552 |
_optimum[0].Backs3 := reps[3]; |
| 553 |
|
| 554 |
len := lenEnd; |
| 555 |
repeat |
| 556 |
_optimum[len].Price := kIfinityPrice; |
| 557 |
dec(len); |
| 558 |
until not (len >= 2); |
| 559 |
|
| 560 |
for i := 0 to ULZMABase.kNumRepDistances -1 do begin |
| 561 |
repLen := repLens[i]; |
| 562 |
if repLen < 2 then |
| 563 |
continue; |
| 564 |
price := repMatchPrice + GetPureRepPrice(i, _state, posState); |
| 565 |
repeat |
| 566 |
curAndLenPrice := price + _repMatchLenEncoder.GetPrice(repLen - 2, posState); |
| 567 |
optimum := _optimum[repLen]; |
| 568 |
if curAndLenPrice < optimum.Price then begin |
| 569 |
optimum.Price := curAndLenPrice; |
| 570 |
optimum.PosPrev := 0; |
| 571 |
optimum.BackPrev := i; |
| 572 |
optimum.Prev1IsChar := false; |
| 573 |
end;//if curandlenprice |
| 574 |
dec(replen); |
| 575 |
until not (repLen >= 2); |
| 576 |
end;//for i |
| 577 |
|
| 578 |
normalMatchPrice := matchPrice + RangeEncoder.GetPrice0(_isRep[_state]); |
| 579 |
|
| 580 |
if repLens[0] >= 2 then len:=repLens[0] + 1 |
| 581 |
else len:=2; |
| 582 |
|
| 583 |
if len <= lenMain then begin |
| 584 |
offs := 0; |
| 585 |
while len > _matchDistances[offs] do |
| 586 |
offs := offs + 2; |
| 587 |
while (true) do begin |
| 588 |
distance := _matchDistances[offs + 1]; |
| 589 |
curAndLenPrice := normalMatchPrice + GetPosLenPrice(distance, len, posState); |
| 590 |
optimum := _optimum[len]; |
| 591 |
if curAndLenPrice < optimum.Price then begin |
| 592 |
optimum.Price := curAndLenPrice; |
| 593 |
optimum.PosPrev := 0; |
| 594 |
optimum.BackPrev := distance + ULZMABase.kNumRepDistances; |
| 595 |
optimum.Prev1IsChar := false; |
| 596 |
end;//if curlenandprice |
| 597 |
if len = _matchDistances[offs] then begin |
| 598 |
offs := offs + 2; |
| 599 |
if offs = numDistancePairs then |
| 600 |
break; |
| 601 |
end;//if len=_match |
| 602 |
inc(len); |
| 603 |
end;//while (true) |
| 604 |
end;//if len<=lenmain |
| 605 |
|
| 606 |
cur := 0; |
| 607 |
|
| 608 |
while (true) do begin |
| 609 |
inc(cur); |
| 610 |
if cur = lenEnd then begin |
| 611 |
result:=Backward(cur); |
| 612 |
exit; |
| 613 |
end;//if cur=lenEnd |
| 614 |
newLen := ReadMatchDistances; |
| 615 |
numDistancePairs := _numDistancePairs; |
| 616 |
if newLen >= _numFastBytes then begin |
| 617 |
_longestMatchLength := newLen; |
| 618 |
_longestMatchWasFound := true; |
| 619 |
result:=Backward(cur); |
| 620 |
exit; |
| 621 |
end;//if newlen=_numfast |
| 622 |
inc(position); |
| 623 |
posPrev := _optimum[cur].PosPrev; |
| 624 |
if _optimum[cur].Prev1IsChar then begin |
| 625 |
dec(posPrev); |
| 626 |
if _optimum[cur].Prev2 then begin |
| 627 |
state := _optimum[_optimum[cur].PosPrev2].State; |
| 628 |
if _optimum[cur].BackPrev2 < ULZMABase.kNumRepDistances then |
| 629 |
state := ULZMABase.StateUpdateRep(state) |
| 630 |
else state := ULZMABase.StateUpdateMatch(state); |
| 631 |
end//if _optimum[cur].Prev2 |
| 632 |
else state := _optimum[posPrev].State; |
| 633 |
state := ULZMABase.StateUpdateChar(state); |
| 634 |
end//if _optimum[cur].Prev1IsChar |
| 635 |
else state := _optimum[posPrev].State; |
| 636 |
if posPrev = cur - 1 then begin |
| 637 |
if _optimum[cur].IsShortRep then |
| 638 |
state := ULZMABase.StateUpdateShortRep(state) |
| 639 |
else state := ULZMABase.StateUpdateChar(state); |
| 640 |
end //if posPrev = cur - 1 |
| 641 |
else begin |
| 642 |
if _optimum[cur].Prev1IsChar and _optimum[cur].Prev2 then begin |
| 643 |
posPrev := _optimum[cur].PosPrev2; |
| 644 |
pos := _optimum[cur].BackPrev2; |
| 645 |
state := ULZMABase.StateUpdateRep(state); |
| 646 |
end//if _optimum[cur].Prev1IsChar |
| 647 |
else begin |
| 648 |
pos := _optimum[cur].BackPrev; |
| 649 |
if pos < ULZMABase.kNumRepDistances then |
| 650 |
state := ULZMABase.StateUpdateRep(state) |
| 651 |
else state := ULZMABase.StateUpdateMatch(state); |
| 652 |
end;//if else _optimum[cur].Prev1IsChar |
| 653 |
opt := _optimum[posPrev]; |
| 654 |
if pos < ULZMABase.kNumRepDistances then begin |
| 655 |
if pos = 0 then begin |
| 656 |
reps[0] := opt.Backs0; |
| 657 |
reps[1] := opt.Backs1; |
| 658 |
reps[2] := opt.Backs2; |
| 659 |
reps[3] := opt.Backs3; |
| 660 |
end//if pos=0 |
| 661 |
else if pos = 1 then begin |
| 662 |
reps[0] := opt.Backs1; |
| 663 |
reps[1] := opt.Backs0; |
| 664 |
reps[2] := opt.Backs2; |
| 665 |
reps[3] := opt.Backs3; |
| 666 |
end //if pos=1 |
| 667 |
else if pos = 2 then begin |
| 668 |
reps[0] := opt.Backs2; |
| 669 |
reps[1] := opt.Backs0; |
| 670 |
reps[2] := opt.Backs1; |
| 671 |
reps[3] := opt.Backs3; |
| 672 |
end//if pos=2 |
| 673 |
else begin |
| 674 |
reps[0] := opt.Backs3; |
| 675 |
reps[1] := opt.Backs0; |
| 676 |
reps[2] := opt.Backs1; |
| 677 |
reps[3] := opt.Backs2; |
| 678 |
end;//else if pos= |
| 679 |
end// if pos < ULZMABase.kNumRepDistances |
| 680 |
else begin |
| 681 |
reps[0] := (pos - ULZMABase.kNumRepDistances); |
| 682 |
reps[1] := opt.Backs0; |
| 683 |
reps[2] := opt.Backs1; |
| 684 |
reps[3] := opt.Backs2; |
| 685 |
end;//if else pos < ULZMABase.kNumRepDistances |
| 686 |
end;//if else posPrev = cur - 1 |
| 687 |
_optimum[cur].State := state; |
| 688 |
_optimum[cur].Backs0 := reps[0]; |
| 689 |
_optimum[cur].Backs1 := reps[1]; |
| 690 |
_optimum[cur].Backs2 := reps[2]; |
| 691 |
_optimum[cur].Backs3 := reps[3]; |
| 692 |
curPrice := _optimum[cur].Price; |
| 693 |
|
| 694 |
currentByte := _matchFinder.GetIndexByte(0 - 1); |
| 695 |
matchByte := _matchFinder.GetIndexByte(0 - reps[0] - 1 - 1); |
| 696 |
|
| 697 |
posState := (position and _posStateMask); |
| 698 |
|
| 699 |
curAnd1Price := curPrice + |
| 700 |
RangeEncoder.GetPrice0(_isMatch[(state shl ULZMABase.kNumPosStatesBitsMax) + posState]) + |
| 701 |
_literalEncoder.GetSubCoder(position, _matchFinder.GetIndexByte(0 - 2)). |
| 702 |
GetPrice(not ULZMABase.StateIsCharState(state), matchByte, currentByte); |
| 703 |
|
| 704 |
nextOptimum := _optimum[cur + 1]; |
| 705 |
|
| 706 |
nextIsChar := false; |
| 707 |
if curAnd1Price < nextOptimum.Price then begin |
| 708 |
nextOptimum.Price := curAnd1Price; |
| 709 |
nextOptimum.PosPrev := cur; |
| 710 |
nextOptimum.MakeAsChar; |
| 711 |
nextIsChar := true; |
| 712 |
end;//if curand1price |
| 713 |
|
| 714 |
matchPrice := curPrice + RangeEncoder.GetPrice1(_isMatch[(state shl ULZMABase.kNumPosStatesBitsMax) + posState]); |
| 715 |
repMatchPrice := matchPrice + RangeEncoder.GetPrice1(_isRep[state]); |
| 716 |
|
| 717 |
if (matchByte = currentByte) and |
| 718 |
(not ((nextOptimum.PosPrev < cur) and (nextOptimum.BackPrev = 0))) then begin |
| 719 |
shortRepPrice := repMatchPrice + GetRepLen1Price(state, posState); |
| 720 |
if shortRepPrice <= nextOptimum.Price then begin |
| 721 |
nextOptimum.Price := shortRepPrice; |
| 722 |
nextOptimum.PosPrev := cur; |
| 723 |
nextOptimum.MakeAsShortRep; |
| 724 |
nextIsChar := true; |
| 725 |
end;//if shortRepPrice <= nextOptimum.Price |
| 726 |
end;//if (matchByte = currentByte) and |
| 727 |
|
| 728 |
numAvailableBytesFull := _matchFinder.GetNumAvailableBytes + 1; |
| 729 |
numAvailableBytesFull := min(kNumOpts - 1 - cur, numAvailableBytesFull); |
| 730 |
numAvailableBytes := numAvailableBytesFull; |
| 731 |
|
| 732 |
if numAvailableBytes < 2 then |
| 733 |
continue; |
| 734 |
if numAvailableBytes > _numFastBytes then |
| 735 |
numAvailableBytes := _numFastBytes; |
| 736 |
if (not nextIsChar) and (matchByte <> currentByte) then begin |
| 737 |
// try Literal + rep0 |
| 738 |
t := min(numAvailableBytesFull - 1, _numFastBytes); |
| 739 |
lenTest2 := _matchFinder.GetMatchLen(0, reps[0], t); |
| 740 |
if lenTest2 >= 2 then begin |
| 741 |
state2 := ULZMABase.StateUpdateChar(state); |
| 742 |
|
| 743 |
posStateNext := (position + 1) and _posStateMask; |
| 744 |
nextRepMatchPrice := curAnd1Price + |
| 745 |
RangeEncoder.GetPrice1(_isMatch[(state2 shl ULZMABase.kNumPosStatesBitsMax) + posStateNext]) + |
| 746 |
RangeEncoder.GetPrice1(_isRep[state2]); |
| 747 |
begin |
| 748 |
offset := cur + 1 + lenTest2; |
| 749 |
while lenEnd < offset do begin |
| 750 |
inc(lenEnd); |
| 751 |
_optimum[lenEnd].Price := kIfinityPrice; |
| 752 |
end;//while lenend |
| 753 |
curAndLenPrice := nextRepMatchPrice + GetRepPrice( |
| 754 |
0, lenTest2, state2, posStateNext); |
| 755 |
optimum := _optimum[offset]; |
| 756 |
if curAndLenPrice < optimum.Price then begin |
| 757 |
optimum.Price := curAndLenPrice; |
| 758 |
optimum.PosPrev := cur + 1; |
| 759 |
optimum.BackPrev := 0; |
| 760 |
optimum.Prev1IsChar := true; |
| 761 |
optimum.Prev2 := false; |
| 762 |
end;//if curandlenprice |
| 763 |
end;//none |
| 764 |
end;//if lentest |
| 765 |
end;//if not nextischar and ... |
| 766 |
|
| 767 |
startLen := 2; // speed optimization |
| 768 |
|
| 769 |
for repIndex := 0 to ULZMABase.kNumRepDistances -1 do begin |
| 770 |
lenTest := _matchFinder.GetMatchLen(0 - 1, reps[repIndex], numAvailableBytes); |
| 771 |
if lenTest < 2 then |
| 772 |
continue; |
| 773 |
lenTestTemp := lenTest; |
| 774 |
repeat |
| 775 |
while lenEnd < cur + lenTest do begin |
| 776 |
inc(lenEnd); |
| 777 |
_optimum[lenEnd].Price := kIfinityPrice; |
| 778 |
end;//while lenEnd |
| 779 |
curAndLenPrice := repMatchPrice + GetRepPrice(repIndex, lenTest, state, posState); |
| 780 |
optimum := _optimum[cur + lenTest]; |
| 781 |
if curAndLenPrice < optimum.Price then begin |
| 782 |
optimum.Price := curAndLenPrice; |
| 783 |
optimum.PosPrev := cur; |
| 784 |
optimum.BackPrev := repIndex; |
| 785 |
optimum.Prev1IsChar := false; |
| 786 |
end;//if curandlen |
| 787 |
dec(lenTest); |
| 788 |
until not (lenTest >= 2); |
| 789 |
lenTest := lenTestTemp; |
| 790 |
|
| 791 |
if repIndex = 0 then |
| 792 |
startLen := lenTest + 1; |
| 793 |
|
| 794 |
// if (_maxMode) |
| 795 |
if lenTest < numAvailableBytesFull then begin |
| 796 |
t := min(numAvailableBytesFull - 1 - lenTest, _numFastBytes); |
| 797 |
lenTest2 := _matchFinder.GetMatchLen(lenTest, reps[repIndex], t); |
| 798 |
if lenTest2 >= 2 then begin |
| 799 |
state2 := ULZMABase.StateUpdateRep(state); |
| 800 |
|
| 801 |
posStateNext := (position + lenTest) and _posStateMask; |
| 802 |
curAndLenCharPrice := |
| 803 |
repMatchPrice + GetRepPrice(repIndex, lenTest, state, posState) + |
| 804 |
RangeEncoder.GetPrice0(_isMatch[(state2 shl ULZMABase.kNumPosStatesBitsMax) + posStateNext]) + |
| 805 |
_literalEncoder.GetSubCoder(position + lenTest, |
| 806 |
_matchFinder.GetIndexByte(lenTest - 1 - 1)).GetPrice(true, |
| 807 |
_matchFinder.GetIndexByte(lenTest - 1 - (reps[repIndex] + 1)), |
| 808 |
_matchFinder.GetIndexByte(lenTest - 1)); |
| 809 |
state2 := ULZMABase.StateUpdateChar(state2); |
| 810 |
posStateNext := (position + lenTest + 1) and _posStateMask; |
| 811 |
nextMatchPrice := curAndLenCharPrice + RangeEncoder.GetPrice1(_isMatch[(state2 shl ULZMABase.kNumPosStatesBitsMax) + posStateNext]); |
| 812 |
nextRepMatchPrice := nextMatchPrice + RangeEncoder.GetPrice1(_isRep[state2]); |
| 813 |
|
| 814 |
// for(; lenTest2 >= 2; lenTest2--) |
| 815 |
begin |
| 816 |
offset := lenTest + 1 + lenTest2; |
| 817 |
while lenEnd < cur + offset do begin |
| 818 |
inc(lenEnd); |
| 819 |
_optimum[lenEnd].Price := kIfinityPrice; |
| 820 |
end;//while lenEnd |
| 821 |
curAndLenPrice := nextRepMatchPrice + GetRepPrice(0, lenTest2, state2, posStateNext); |
| 822 |
optimum := _optimum[cur + offset]; |
| 823 |
if curAndLenPrice < optimum.Price then begin |
| 824 |
optimum.Price := curAndLenPrice; |
| 825 |
optimum.PosPrev := cur + lenTest + 1; |
| 826 |
optimum.BackPrev := 0; |
| 827 |
optimum.Prev1IsChar := true; |
| 828 |
optimum.Prev2 := true; |
| 829 |
optimum.PosPrev2 := cur; |
| 830 |
optimum.BackPrev2 := repIndex; |
| 831 |
end;//if curAndLenPrice < optimum.Price |
| 832 |
end;//none |
| 833 |
end;//if lenTest2 >= 2 |
| 834 |
end;//if lenTest < numAvailableBytesFull |
| 835 |
end;//for repIndex |
| 836 |
|
| 837 |
if newLen > numAvailableBytes then begin |
| 838 |
newLen := numAvailableBytes; |
| 839 |
numDistancePairs := 0; |
| 840 |
while newLen > _matchDistances[numDistancePairs] do |
| 841 |
numDistancePairs := numDistancePairs + 2; |
| 842 |
_matchDistances[numDistancePairs] := newLen; |
| 843 |
numDistancePairs := numDistancePairs + 2; |
| 844 |
end;//if newLen > numAvailableBytes |
| 845 |
if newLen >= startLen then begin |
| 846 |
normalMatchPrice := matchPrice + RangeEncoder.GetPrice0(_isRep[state]); |
| 847 |
while lenEnd < cur + newLen do begin |
| 848 |
inc(lenEnd); |
| 849 |
_optimum[lenEnd].Price := kIfinityPrice; |
| 850 |
end;//while lenEnd |
| 851 |
|
| 852 |
offs := 0; |
| 853 |
while startLen > _matchDistances[offs] do |
| 854 |
offs := offs + 2; |
| 855 |
|
| 856 |
lenTest := startLen; |
| 857 |
while (true) do begin |
| 858 |
curBack := _matchDistances[offs + 1]; |
| 859 |
curAndLenPrice := normalMatchPrice + GetPosLenPrice(curBack, lenTest, posState); |
| 860 |
optimum := _optimum[cur + lenTest]; |
| 861 |
if curAndLenPrice < optimum.Price then begin |
| 862 |
optimum.Price := curAndLenPrice; |
| 863 |
optimum.PosPrev := cur; |
| 864 |
optimum.BackPrev := curBack + ULZMABase.kNumRepDistances; |
| 865 |
optimum.Prev1IsChar := false; |
| 866 |
end;//if curAndLenPrice < optimum.Price |
| 867 |
|
| 868 |
if lenTest = _matchDistances[offs] then begin |
| 869 |
if lenTest < numAvailableBytesFull then begin |
| 870 |
t := min(numAvailableBytesFull - 1 - lenTest, _numFastBytes); |
| 871 |
lenTest2 := _matchFinder.GetMatchLen(lenTest, curBack, t); |
| 872 |
if lenTest2 >= 2 then begin |
| 873 |
state2 := ULZMABase.StateUpdateMatch(state); |
| 874 |
|
| 875 |
posStateNext := (position + lenTest) and _posStateMask; |
| 876 |
curAndLenCharPrice := curAndLenPrice + |
| 877 |
RangeEncoder.GetPrice0(_isMatch[(state2 shl ULZMABase.kNumPosStatesBitsMax) + posStateNext]) + |
| 878 |
_literalEncoder.GetSubCoder(position + lenTest, |
| 879 |
_matchFinder.GetIndexByte(lenTest - 1 - 1)). |
| 880 |
GetPrice(true, |
| 881 |
_matchFinder.GetIndexByte(lenTest - (curBack + 1) - 1), |
| 882 |
_matchFinder.GetIndexByte(lenTest - 1)); |
| 883 |
state2 := ULZMABase.StateUpdateChar(state2); |
| 884 |
posStateNext := (position + lenTest + 1) and _posStateMask; |
| 885 |
nextMatchPrice := curAndLenCharPrice + RangeEncoder.GetPrice1(_isMatch[(state2 shl ULZMABase.kNumPosStatesBitsMax) + posStateNext]); |
| 886 |
nextRepMatchPrice := nextMatchPrice + RangeEncoder.GetPrice1(_isRep[state2]); |
| 887 |
|
| 888 |
offset := lenTest + 1 + lenTest2; |
| 889 |
while lenEnd < cur + offset do begin |
| 890 |
inc(lenEnd); |
| 891 |
_optimum[lenEnd].Price := kIfinityPrice; |
| 892 |
end;//while lenEnd |
| 893 |
curAndLenPrice := nextRepMatchPrice + GetRepPrice(0, lenTest2, state2, posStateNext); |
| 894 |
optimum := _optimum[cur + offset]; |
| 895 |
if curAndLenPrice < optimum.Price then begin |
| 896 |
optimum.Price := curAndLenPrice; |
| 897 |
optimum.PosPrev := cur + lenTest + 1; |
| 898 |
optimum.BackPrev := 0; |
| 899 |
optimum.Prev1IsChar := true; |
| 900 |
optimum.Prev2 := true; |
| 901 |
optimum.PosPrev2 := cur; |
| 902 |
optimum.BackPrev2 := curBack + ULZMABase.kNumRepDistances; |
| 903 |
end;//if curAndLenPrice < optimum.Price |
| 904 |
end;//if lenTest2 >= 2 |
| 905 |
end;//lenTest < numAvailableBytesFull |
| 906 |
offs :=offs + 2; |
| 907 |
if offs = numDistancePairs then |
| 908 |
break; |
| 909 |
end;//if lenTest = _matchDistances[offs] |
| 910 |
inc(lenTest); |
| 911 |
end;//while(true) |
| 912 |
end;//if newLen >= startLen |
| 913 |
end;//while (true) |
| 914 |
end; |
| 915 |
|
| 916 |
function TLZMAEncoder.ChangePair(const smallDist, bigDist:integer):boolean; |
| 917 |
var kDif:integer; |
| 918 |
begin |
| 919 |
kDif := 7; |
| 920 |
result:= (smallDist < (1 shl (32 - kDif))) and (bigDist >= (smallDist shl kDif)); |
| 921 |
end; |
| 922 |
|
| 923 |
procedure TLZMAEncoder.WriteEndMarker(const posState:integer); |
| 924 |
var len,posSlot,lenToPosState,footerBits,posReduced:integer; |
| 925 |
begin |
| 926 |
if not _writeEndMark then |
| 927 |
exit; |
| 928 |
|
| 929 |
_rangeEncoder.Encode(_isMatch, (_state shl ULZMABase.kNumPosStatesBitsMax) + posState, 1); |
| 930 |
_rangeEncoder.Encode(_isRep, _state, 0); |
| 931 |
_state := ULZMABase.StateUpdateMatch(_state); |
| 932 |
len := ULZMABase.kMatchMinLen; |
| 933 |
_lenEncoder.Encode(_rangeEncoder, len - ULZMABase.kMatchMinLen, posState); |
| 934 |
posSlot := (1 shl ULZMABase.kNumPosSlotBits) - 1; |
| 935 |
lenToPosState := ULZMABase.GetLenToPosState(len); |
| 936 |
_posSlotEncoder[lenToPosState].Encode(_rangeEncoder, posSlot); |
| 937 |
footerBits := 30; |
| 938 |
posReduced := (1 shl footerBits) - 1; |
| 939 |
_rangeEncoder.EncodeDirectBits(posReduced shr ULZMABase.kNumAlignBits, footerBits - ULZMABase.kNumAlignBits); |
| 940 |
_posAlignEncoder.ReverseEncode(_rangeEncoder, posReduced and ULZMABase.kAlignMask); |
| 941 |
end; |
| 942 |
|
| 943 |
procedure TLZMAEncoder.Flush(const nowPos:integer); |
| 944 |
begin |
| 945 |
ReleaseMFStream; |
| 946 |
WriteEndMarker(nowPos and _posStateMask); |
| 947 |
_rangeEncoder.FlushData(); |
| 948 |
_rangeEncoder.FlushStream(); |
| 949 |
end; |
| 950 |
|
| 951 |
procedure TLZMAEncoder.CodeOneBlock(var inSize,outSize:int64;var finished:boolean); |
| 952 |
var progressPosValuePrev:int64; |
| 953 |
posState,len,pos,complexState,distance,i,posSlot,lenToPosState:integer; |
| 954 |
footerBits,baseVal,posReduced:integer; |
| 955 |
curByte,matchByte:byte; |
| 956 |
subcoder:TLZMAEncoder2; |
| 957 |
begin |
| 958 |
inSize := 0; |
| 959 |
outSize := 0; |
| 960 |
finished := true; |
| 961 |
|
| 962 |
if _inStream <>nil then begin |
| 963 |
_matchFinder.SetStream(_inStream); |
| 964 |
_matchFinder.Init; |
| 965 |
_needReleaseMFStream := true; |
| 966 |
_inStream := nil; |
| 967 |
end; |
| 968 |
|
| 969 |
if _finished then |
| 970 |
exit; |
| 971 |
_finished := true; |
| 972 |
|
| 973 |
progressPosValuePrev := nowPos64; |
| 974 |
if nowPos64 = 0 then begin |
| 975 |
if _matchFinder.GetNumAvailableBytes = 0 then begin |
| 976 |
Flush(nowPos64); |
| 977 |
exit; |
| 978 |
end; |
| 979 |
|
| 980 |
ReadMatchDistances; |
| 981 |
posState := integer(nowPos64) and _posStateMask; |
| 982 |
_rangeEncoder.Encode(_isMatch, (_state shl ULZMABase.kNumPosStatesBitsMax) + posState, 0); |
| 983 |
_state := ULZMABase.StateUpdateChar(_state); |
| 984 |
curByte := _matchFinder.GetIndexByte(0 - _additionalOffset); |
| 985 |
_literalEncoder.GetSubCoder(integer(nowPos64), _previousByte).Encode(_rangeEncoder, curByte); |
| 986 |
_previousByte := curByte; |
| 987 |
dec(_additionalOffset); |
| 988 |
inc(nowPos64); |
| 989 |
end; |
| 990 |
if _matchFinder.GetNumAvailableBytes = 0 then begin |
| 991 |
Flush(integer(nowPos64)); |
| 992 |
exit; |
| 993 |
end; |
| 994 |
while true do begin |
| 995 |
len := GetOptimum(integer(nowPos64)); |
| 996 |
pos := backRes; |
| 997 |
posState := integer(nowPos64) and _posStateMask; |
| 998 |
complexState := (_state shl ULZMABase.kNumPosStatesBitsMax) + posState; |
| 999 |
if (len = 1) and (pos = -1) then begin |
| 1000 |
_rangeEncoder.Encode(_isMatch, complexState, 0); |
| 1001 |
curByte := _matchFinder.GetIndexByte(0 - _additionalOffset); |
| 1002 |
subCoder := _literalEncoder.GetSubCoder(integer(nowPos64), _previousByte); |
| 1003 |
if not ULZMABase.StateIsCharState(_state) then begin |
| 1004 |
matchByte := _matchFinder.GetIndexByte(0 - _repDistances[0] - 1 - _additionalOffset); |
| 1005 |
subCoder.EncodeMatched(_rangeEncoder, matchByte, curByte); |
| 1006 |
end else subCoder.Encode(_rangeEncoder, curByte); |
| 1007 |
_previousByte := curByte; |
| 1008 |
_state := ULZMABase.StateUpdateChar(_state); |
| 1009 |
end else begin |
| 1010 |
_rangeEncoder.Encode(_isMatch, complexState, 1); |
| 1011 |
if pos < ULZMABase.kNumRepDistances then begin |
| 1012 |
_rangeEncoder.Encode(_isRep, _state, 1); |
| 1013 |
if pos = 0 then begin |
| 1014 |
_rangeEncoder.Encode(_isRepG0, _state, 0); |
| 1015 |
if len = 1 then |
| 1016 |
_rangeEncoder.Encode(_isRep0Long, complexState, 0) |
| 1017 |
else _rangeEncoder.Encode(_isRep0Long, complexState, 1); |
| 1018 |
end else begin |
| 1019 |
_rangeEncoder.Encode(_isRepG0, _state, 1); |
| 1020 |
if pos = 1 then |
| 1021 |
_rangeEncoder.Encode(_isRepG1, _state, 0) |
| 1022 |
else begin |
| 1023 |
_rangeEncoder.Encode(_isRepG1, _state, 1); |
| 1024 |
_rangeEncoder.Encode(_isRepG2, _state, pos - 2); |
| 1025 |
end; |
| 1026 |
end; |
| 1027 |
if len = 1 then |
| 1028 |
_state := ULZMABase.StateUpdateShortRep(_state) |
| 1029 |
else begin |
| 1030 |
_repMatchLenEncoder.Encode(_rangeEncoder, len - ULZMABase.kMatchMinLen, posState); |
| 1031 |
_state := ULZMABase.StateUpdateRep(_state); |
| 1032 |
end; |
| 1033 |
distance := _repDistances[pos]; |
| 1034 |
if pos <> 0 then begin |
| 1035 |
for i := pos downto 1 do |
| 1036 |
_repDistances[i] := _repDistances[i - 1]; |
| 1037 |
_repDistances[0] := distance; |
| 1038 |
end; |
| 1039 |
end else begin |
| 1040 |
_rangeEncoder.Encode(_isRep, _state, 0); |
| 1041 |
_state := ULZMABase.StateUpdateMatch(_state); |
| 1042 |
_lenEncoder.Encode(_rangeEncoder, len - ULZMABase.kMatchMinLen, posState); |
| 1043 |
pos := pos - ULZMABase.kNumRepDistances; |
| 1044 |
posSlot := GetPosSlot(pos); |
| 1045 |
lenToPosState := ULZMABase.GetLenToPosState(len); |
| 1046 |
_posSlotEncoder[lenToPosState].Encode(_rangeEncoder, posSlot); |
| 1047 |
|
| 1048 |
if posSlot >= ULZMABase.kStartPosModelIndex then begin |
| 1049 |
footerBits := integer((posSlot shr 1) - 1); |
| 1050 |
baseVal := ((2 or (posSlot and 1)) shl footerBits); |
| 1051 |
posReduced := pos - baseVal; |
| 1052 |
|
| 1053 |
if posSlot < ULZMABase.kEndPosModelIndex then |
| 1054 |
UBitTreeEncoder.ReverseEncode(_posEncoders, |
| 1055 |
baseVal - posSlot - 1, _rangeEncoder, footerBits, posReduced) |
| 1056 |
else begin |
| 1057 |
_rangeEncoder.EncodeDirectBits(posReduced shr ULZMABase.kNumAlignBits, footerBits - ULZMABase.kNumAlignBits); |
| 1058 |
_posAlignEncoder.ReverseEncode(_rangeEncoder, posReduced and ULZMABase.kAlignMask); |
| 1059 |
inc(_alignPriceCount); |
| 1060 |
end; |
| 1061 |
end; |
| 1062 |
distance := pos; |
| 1063 |
for i := ULZMABase.kNumRepDistances - 1 downto 1 do |
| 1064 |
_repDistances[i] := _repDistances[i - 1]; |
| 1065 |
_repDistances[0] := distance; |
| 1066 |
inc(_matchPriceCount); |
| 1067 |
end; |
| 1068 |
_previousByte := _matchFinder.GetIndexByte(len - 1 - _additionalOffset); |
| 1069 |
end; |
| 1070 |
_additionalOffset := _additionalOffset - len; |
| 1071 |
nowPos64 := nowPos64 + len; |
| 1072 |
if _additionalOffset = 0 then begin |
| 1073 |
// if (!_fastMode) |
| 1074 |
if _matchPriceCount >= (1 shl 7) then |
| 1075 |
FillDistancesPrices; |
| 1076 |
if _alignPriceCount >= ULZMABase.kAlignTableSize then |
| 1077 |
FillAlignPrices; |
| 1078 |
inSize := nowPos64; |
| 1079 |
outSize := _rangeEncoder.GetProcessedSizeAdd; |
| 1080 |
if _matchFinder.GetNumAvailableBytes = 0 then begin |
| 1081 |
Flush(integer(nowPos64)); |
| 1082 |
exit; |
| 1083 |
end; |
| 1084 |
|
| 1085 |
if (nowPos64 - progressPosValuePrev >= (1 shl 12)) then begin |
| 1086 |
_finished := false; |
| 1087 |
finished := false; |
| 1088 |
exit; |
| 1089 |
end; |
| 1090 |
end; |
| 1091 |
end; |
| 1092 |
end; |
| 1093 |
|
| 1094 |
procedure TLZMAEncoder.ReleaseMFStream; |
| 1095 |
begin |
| 1096 |
if (_matchFinder <>nil) and _needReleaseMFStream then begin |
| 1097 |
_matchFinder.ReleaseStream; |
| 1098 |
_needReleaseMFStream := false; |
| 1099 |
end; |
| 1100 |
end; |
| 1101 |
|
| 1102 |
procedure TLZMAEncoder.SetOutStream(const outStream:TStream); |
| 1103 |
begin |
| 1104 |
_rangeEncoder.SetStream(outStream); |
| 1105 |
end; |
| 1106 |
|
| 1107 |
procedure TLZMAEncoder.ReleaseOutStream; |
| 1108 |
begin |
| 1109 |
_rangeEncoder.ReleaseStream; |
| 1110 |
end; |
| 1111 |
|
| 1112 |
procedure TLZMAEncoder.ReleaseStreams; |
| 1113 |
begin |
| 1114 |
ReleaseMFStream; |
| 1115 |
ReleaseOutStream; |
| 1116 |
end; |
| 1117 |
|
| 1118 |
procedure TLZMAEncoder.SetStreams(const inStream, outStream:TStream;const inSize, outSize:int64); |
| 1119 |
begin |
| 1120 |
_inStream := inStream; |
| 1121 |
_finished := false; |
| 1122 |
_Create(); |
| 1123 |
SetOutStream(outStream); |
| 1124 |
Init(); |
| 1125 |
|
| 1126 |
// if (!_fastMode) |
| 1127 |
FillDistancesPrices; |
| 1128 |
FillAlignPrices; |
| 1129 |
|
| 1130 |
_lenEncoder.SetTableSize(_numFastBytes + 1 - ULZMABase.kMatchMinLen); |
| 1131 |
_lenEncoder.UpdateTables(1 shl _posStateBits); |
| 1132 |
_repMatchLenEncoder.SetTableSize(_numFastBytes + 1 - ULZMABase.kMatchMinLen); |
| 1133 |
_repMatchLenEncoder.UpdateTables(1 shl _posStateBits); |
| 1134 |
|
| 1135 |
nowPos64 := 0; |
| 1136 |
end; |
| 1137 |
|
| 1138 |
procedure TLZMAEncoder.Code(const inStream, outStream:TStream;const inSize, outSize:int64); |
| 1139 |
var lpos:int64; |
| 1140 |
progint:int64; |
| 1141 |
inputsize:int64; |
| 1142 |
begin |
| 1143 |
if insize=-1 then |
| 1144 |
inputsize:=instream.Size-instream.Position |
| 1145 |
else inputsize:=insize; |
| 1146 |
progint:=inputsize div CodeProgressInterval; |
| 1147 |
lpos:=progint; |
| 1148 |
|
| 1149 |
_needReleaseMFStream := false; |
| 1150 |
DoProgress(LPAMax,inputsize); |
| 1151 |
try |
| 1152 |
SetStreams(inStream, outStream, inSize, outSize); |
| 1153 |
while true do begin |
| 1154 |
CodeOneBlock(processedInSize, processedOutSize, finished); |
| 1155 |
if finished then begin |
| 1156 |
DoProgress(LPAPos,inputsize); |
| 1157 |
exit; |
| 1158 |
end; |
| 1159 |
if (processedInSize>=lpos) then begin |
| 1160 |
DoProgress(LPAPos,processedInSize); |
| 1161 |
lpos:=lpos+progint; |
| 1162 |
end; |
| 1163 |
end; |
| 1164 |
finally |
| 1165 |
ReleaseStreams(); |
| 1166 |
end; |
| 1167 |
end; |
| 1168 |
|
| 1169 |
procedure TLZMAEncoder.WriteCoderProperties(const outStream:TStream); |
| 1170 |
var i:integer; |
| 1171 |
begin |
| 1172 |
properties[0] := (_posStateBits * 5 + _numLiteralPosStateBits) * 9 + _numLiteralContextBits; |
| 1173 |
for i := 0 to 3 do |
| 1174 |
properties[1 + i] := byte(_dictionarySize shr (8 * i)); |
| 1175 |
outStream.write(properties, kPropSize); |
| 1176 |
end; |
| 1177 |
|
| 1178 |
procedure TLZMAEncoder.FillDistancesPrices; |
| 1179 |
var i,posSlot,footerBits,baseVal,lenToPosState,st,st2:integer; |
| 1180 |
encoder:TBitTreeEncoder; |
| 1181 |
begin |
| 1182 |
for i := ULZMABase.kStartPosModelIndex to ULZMABase.kNumFullDistances -1 do begin |
| 1183 |
posSlot := GetPosSlot(i); |
| 1184 |
footerBits := integer((posSlot shr 1) - 1); |
| 1185 |
baseVal := (2 or (posSlot and 1)) shl footerBits; |
| 1186 |
tempPrices[i] := ReverseGetPrice(_posEncoders, |
| 1187 |
baseVal - posSlot - 1, footerBits, i - baseVal); |
| 1188 |
end; |
| 1189 |
|
| 1190 |
for lenToPosState := 0 to ULZMABase.kNumLenToPosStates -1 do begin |
| 1191 |
encoder := _posSlotEncoder[lenToPosState]; |
| 1192 |
|
| 1193 |
st := (lenToPosState shl ULZMABase.kNumPosSlotBits); |
| 1194 |
for posSlot := 0 to _distTableSize -1 do |
| 1195 |
_posSlotPrices[st + posSlot] := encoder.GetPrice(posSlot); |
| 1196 |
for posSlot := ULZMABase.kEndPosModelIndex to _distTableSize -1 do |
| 1197 |
_posSlotPrices[st + posSlot] := _posSlotPrices[st + posSlot] + ((((posSlot shr 1) - 1) - ULZMABase.kNumAlignBits) shl kNumBitPriceShiftBits); |
| 1198 |
|
| 1199 |
st2 := lenToPosState * ULZMABase.kNumFullDistances; |
| 1200 |
for i := 0 to ULZMABase.kStartPosModelIndex -1 do |
| 1201 |
_distancesPrices[st2 + i] := _posSlotPrices[st + i]; |
| 1202 |
for i := ULZMABase.kStartPosModelIndex to ULZMABase.kNumFullDistances-1 do |
| 1203 |
_distancesPrices[st2 + i] := _posSlotPrices[st + GetPosSlot(i)] + tempPrices[i]; |
| 1204 |
end; |
| 1205 |
_matchPriceCount := 0; |
| 1206 |
end; |
| 1207 |
|
| 1208 |
procedure TLZMAEncoder.FillAlignPrices; |
| 1209 |
var i:integer; |
| 1210 |
begin |
| 1211 |
for i := 0 to ULZMABase.kAlignTableSize -1 do |
| 1212 |
_alignPrices[i] := _posAlignEncoder.ReverseGetPrice(i); |
| 1213 |
_alignPriceCount := 0; |
| 1214 |
end; |
| 1215 |
|
| 1216 |
function TLZMAEncoder.SetAlgorithm(const algorithm:integer):boolean; |
| 1217 |
begin |
| 1218 |
{ |
| 1219 |
_fastMode = (algorithm == 0); |
| 1220 |
_maxMode = (algorithm >= 2); |
| 1221 |
} |
| 1222 |
result:=true; |
| 1223 |
end; |
| 1224 |
|
| 1225 |
function TLZMAEncoder.SetDictionarySize(dictionarySize:integer):boolean; |
| 1226 |
var kDicLogSizeMaxCompress,dicLogSize:integer; |
| 1227 |
begin |
| 1228 |
kDicLogSizeMaxCompress := 29; |
| 1229 |
if (dictionarySize < (1 shl ULZMABase.kDicLogSizeMin)) or (dictionarySize > (1 shl kDicLogSizeMaxCompress)) then begin |
| 1230 |
result:=false; |
| 1231 |
exit; |
| 1232 |
end; |
| 1233 |
_dictionarySize := dictionarySize; |
| 1234 |
dicLogSize := 0; |
| 1235 |
while dictionarySize > (1 shl dicLogSize) do |
| 1236 |
inc(dicLogSize); |
| 1237 |
_distTableSize := dicLogSize * 2; |
| 1238 |
result:=true; |
| 1239 |
end; |
| 1240 |
|
| 1241 |
function TLZMAEncoder.SeNumFastBytes(const numFastBytes:integer):boolean; |
| 1242 |
begin |
| 1243 |
if (numFastBytes < 5) or (numFastBytes > ULZMABase.kMatchMaxLen) then begin |
| 1244 |
result:=false; |
| 1245 |
exit; |
| 1246 |
end; |
| 1247 |
_numFastBytes := numFastBytes; |
| 1248 |
result:=true; |
| 1249 |
end; |
| 1250 |
|
| 1251 |
function TLZMAEncoder.SetMatchFinder(const matchFinderIndex:integer):boolean; |
| 1252 |
var matchFinderIndexPrev:integer; |
| 1253 |
begin |
| 1254 |
if (matchFinderIndex < 0) or (matchFinderIndex > 2) then begin |
| 1255 |
result:=false; |
| 1256 |
exit; |
| 1257 |
end; |
| 1258 |
matchFinderIndexPrev := _matchFinderType; |
| 1259 |
_matchFinderType := matchFinderIndex; |
| 1260 |
if (_matchFinder <> nil) and (matchFinderIndexPrev <> _matchFinderType) then begin |
| 1261 |
_dictionarySizePrev := -1; |
| 1262 |
_matchFinder := nil; |
| 1263 |
end; |
| 1264 |
result:=true; |
| 1265 |
end; |
| 1266 |
|
| 1267 |
function TLZMAEncoder.SetLcLpPb(const lc,lp,pb:integer):boolean; |
| 1268 |
begin |
| 1269 |
if (lp < 0) or (lp > ULZMABase.kNumLitPosStatesBitsEncodingMax) or |
| 1270 |
(lc < 0) or (lc > ULZMABase.kNumLitContextBitsMax) or |
| 1271 |
(pb < 0) or (pb > ULZMABase.kNumPosStatesBitsEncodingMax) then begin |
| 1272 |
result:=false; |
| 1273 |
exit; |
| 1274 |
end; |
| 1275 |
_numLiteralPosStateBits := lp; |
| 1276 |
_numLiteralContextBits := lc; |
| 1277 |
_posStateBits := pb; |
| 1278 |
_posStateMask := ((1) shl _posStateBits) - 1; |
| 1279 |
result:=true; |
| 1280 |
end; |
| 1281 |
|
| 1282 |
procedure TLZMAEncoder.SetEndMarkerMode(const endMarkerMode:boolean); |
| 1283 |
begin |
| 1284 |
_writeEndMark := endMarkerMode; |
| 1285 |
end; |
| 1286 |
|
| 1287 |
procedure TLZMAEncoder2.Init; |
| 1288 |
begin |
| 1289 |
URangeEncoder.InitBitModels(m_Encoders); |
| 1290 |
end; |
| 1291 |
|
| 1292 |
procedure TLZMAEncoder2.Encode(const rangeEncoder:TRangeEncoder;const symbol:byte); |
| 1293 |
var context:integer; |
| 1294 |
bit,i:integer; |
| 1295 |
begin |
| 1296 |
context := 1; |
| 1297 |
for i := 7 downto 0 do begin |
| 1298 |
bit := ((symbol shr i) and 1); |
| 1299 |
rangeEncoder.Encode(m_Encoders, context, bit); |
| 1300 |
context := (context shl 1) or bit; |
| 1301 |
end; |
| 1302 |
end; |
| 1303 |
|
| 1304 |
procedure TLZMAEncoder2.EncodeMatched(const rangeEncoder:TRangeEncoder;const matchByte,symbol:byte); |
| 1305 |
var context,i,bit,state,matchbit:integer; |
| 1306 |
same:boolean; |
| 1307 |
begin |
| 1308 |
context := 1; |
| 1309 |
same := true; |
| 1310 |
for i := 7 downto 0 do begin |
| 1311 |
bit := ((symbol shr i) and 1); |
| 1312 |
state := context; |
| 1313 |
if same then begin |
| 1314 |
matchBit := ((matchByte shr i) and 1); |
| 1315 |
state :=state + ((1 + matchBit) shl 8); |
| 1316 |
same := (matchBit = bit); |
| 1317 |
end; |
| 1318 |
rangeEncoder.Encode(m_Encoders, state, bit); |
| 1319 |
context := (context shl 1) or bit; |
| 1320 |
end; |
| 1321 |
end; |
| 1322 |
|
| 1323 |
function TLZMAEncoder2.GetPrice(const matchMode:boolean;const matchByte,symbol:byte):integer; |
| 1324 |
var price,context,i,matchbit,bit:integer; |
| 1325 |
begin |
| 1326 |
price := 0; |
| 1327 |
context := 1; |
| 1328 |
i := 7; |
| 1329 |
if matchMode then |
| 1330 |
while i>=0 do begin |
| 1331 |
matchBit := (matchByte shr i) and 1; |
| 1332 |
bit := (symbol shr i) and 1; |
| 1333 |
price := price + RangeEncoder.GetPrice(m_Encoders[((1 + matchBit) shl 8) + context], bit); |
| 1334 |
context := (context shl 1) or bit; |
| 1335 |
if (matchBit <> bit) then begin |
| 1336 |
dec(i); |
| 1337 |
break; |
| 1338 |
end; |
| 1339 |
dec(i); |
| 1340 |
end; |
| 1341 |
while i>=0 do begin |
| 1342 |
bit := (symbol shr i) and 1; |
| 1343 |
price := price + RangeEncoder.GetPrice(m_Encoders[context], bit); |
| 1344 |
context := (context shl 1) or bit; |
| 1345 |
dec(i); |
| 1346 |
end; |
| 1347 |
result:=price; |
| 1348 |
end; |
| 1349 |
|
| 1350 |
procedure TLZMALiteralEncoder._Create(const numPosBits,numPrevBits:integer); |
| 1351 |
var numstates:integer; |
| 1352 |
i:integer; |
| 1353 |
begin |
| 1354 |
if (length(m_Coders)<>0) and (m_NumPrevBits = numPrevBits) and (m_NumPosBits = numPosBits) then |
| 1355 |
exit; |
| 1356 |
m_NumPosBits := numPosBits; |
| 1357 |
m_PosMask := (1 shl numPosBits) - 1; |
| 1358 |
m_NumPrevBits := numPrevBits; |
| 1359 |
numStates := 1 shl (m_NumPrevBits + m_NumPosBits); |
| 1360 |
setlength(m_coders,numStates); |
| 1361 |
for i := 0 to numStates-1 do |
| 1362 |
m_Coders[i] := TLZMAEncoder2.Create; |
| 1363 |
end; |
| 1364 |
|
| 1365 |
destructor TLZMALiteralEncoder.Destroy; |
| 1366 |
var i:integer; |
| 1367 |
begin |
| 1368 |
for i:=low(m_Coders) to high(m_Coders) do |
| 1369 |
if m_Coders[i]<>nil then m_Coders[i].Free; |
| 1370 |
inherited; |
| 1371 |
end; |
| 1372 |
|
| 1373 |
procedure TLZMALiteralEncoder.Init; |
| 1374 |
var numstates,i:integer; |
| 1375 |
begin |
| 1376 |
numStates := 1 shl (m_NumPrevBits + m_NumPosBits); |
| 1377 |
for i := 0 to numStates-1 do |
| 1378 |
m_Coders[i].Init; |
| 1379 |
end; |
| 1380 |
|
| 1381 |
function TLZMALiteralEncoder.GetSubCoder(const pos:integer;const prevByte:byte):TLZMAEncoder2; |
| 1382 |
begin |
| 1383 |
result:=m_Coders[((pos and m_PosMask) shl m_NumPrevBits) + ((prevByte and $FF) shr (8 - m_NumPrevBits))]; |
| 1384 |
end; |
| 1385 |
|
| 1386 |
constructor TLZMALenEncoder.Create; |
| 1387 |
var posState:integer; |
| 1388 |
begin |
| 1389 |
_highCoder := TBitTreeEncoder.Create(ULZMABase.kNumHighLenBits); |
| 1390 |
for posState := 0 to ULZMABase.kNumPosStatesEncodingMax-1 do begin |
| 1391 |
_lowCoder[posState] := TBitTreeEncoder.Create(ULZMABase.kNumLowLenBits); |
| 1392 |
_midCoder[posState] := TBitTreeEncoder.Create(ULZMABase.kNumMidLenBits); |
| 1393 |
end; |
| 1394 |
end; |
| 1395 |
|
| 1396 |
destructor TLZMALenEncoder.Destroy; |
| 1397 |
var posState:integer; |
| 1398 |
begin |
| 1399 |
_highCoder.Free; |
| 1400 |
for posState := 0 to ULZMABase.kNumPosStatesEncodingMax-1 do begin |
| 1401 |
_lowCoder[posState].Free; |
| 1402 |
_midCoder[posState].Free; |
| 1403 |
end; |
| 1404 |
inherited; |
| 1405 |
end; |
| 1406 |
|
| 1407 |
procedure TLZMALenEncoder.Init(const numPosStates:integer); |
| 1408 |
var posState:integer; |
| 1409 |
begin |
| 1410 |
URangeEncoder.InitBitModels(_choice); |
| 1411 |
|
| 1412 |
for posState := 0 to numPosStates -1 do begin |
| 1413 |
_lowCoder[posState].Init; |
| 1414 |
_midCoder[posState].Init; |
| 1415 |
end; |
| 1416 |
_highCoder.Init; |
| 1417 |
end; |
| 1418 |
|
| 1419 |
procedure TLZMALenEncoder.Encode(const rangeEncoder:TRangeEncoder;symbol:integer;const posState:integer); |
| 1420 |
begin |
| 1421 |
if (symbol < ULZMABase.kNumLowLenSymbols) then begin |
| 1422 |
rangeEncoder.Encode(_choice, 0, 0); |
| 1423 |
_lowCoder[posState].Encode(rangeEncoder, symbol); |
| 1424 |
end else begin |
| 1425 |
symbol := symbol - ULZMABase.kNumLowLenSymbols; |
| 1426 |
rangeEncoder.Encode(_choice, 0, 1); |
| 1427 |
if symbol < ULZMABase.kNumMidLenSymbols then begin |
| 1428 |
rangeEncoder.Encode(_choice, 1, 0); |
| 1429 |
_midCoder[posState].Encode(rangeEncoder, symbol); |
| 1430 |
end else begin |
| 1431 |
rangeEncoder.Encode(_choice, 1, 1); |
| 1432 |
_highCoder.Encode(rangeEncoder, symbol - ULZMABase.kNumMidLenSymbols); |
| 1433 |
end; |
| 1434 |
end; |
| 1435 |
end; |
| 1436 |
|
| 1437 |
procedure TLZMALenEncoder.SetPrices(const posState,numSymbols:integer;var prices:array of integer;const st:integer); |
| 1438 |
var a0,a1,b0,b1,i:integer; |
| 1439 |
begin |
| 1440 |
a0 := RangeEncoder.GetPrice0(_choice[0]); |
| 1441 |
a1 := RangeEncoder.GetPrice1(_choice[0]); |
| 1442 |
b0 := a1 + RangeEncoder.GetPrice0(_choice[1]); |
| 1443 |
b1 := a1 + RangeEncoder.GetPrice1(_choice[1]); |
| 1444 |
i:=0; |
| 1445 |
while i<ULZMABase.kNumLowLenSymbols do begin |
| 1446 |
if i >= numSymbols then |
| 1447 |
exit; |
| 1448 |
prices[st + i] := a0 + _lowCoder[posState].GetPrice(i); |
| 1449 |
inc(i); |
| 1450 |
end; |
| 1451 |
while i < ULZMABase.kNumLowLenSymbols + ULZMABase.kNumMidLenSymbols do begin |
| 1452 |
if i >= numSymbols then |
| 1453 |
exit; |
| 1454 |
prices[st + i] := b0 + _midCoder[posState].GetPrice(i - ULZMABase.kNumLowLenSymbols); |
| 1455 |
inc(i); |
| 1456 |
end; |
| 1457 |
while i < numSymbols do begin |
| 1458 |
prices[st + i] := b1 + _highCoder.GetPrice(i - ULZMABase.kNumLowLenSymbols - ULZMABase.kNumMidLenSymbols); |
| 1459 |
inc(i); |
| 1460 |
end; |
| 1461 |
end; |
| 1462 |
|
| 1463 |
procedure TLZMALenPriceTableEncoder.SetTableSize(const tableSize:integer); |
| 1464 |
begin |
| 1465 |
_tableSize := tableSize; |
| 1466 |
end; |
| 1467 |
|
| 1468 |
function TLZMALenPriceTableEncoder.GetPrice(const symbol,posState:integer):integer; |
| 1469 |
begin |
| 1470 |
result:=_prices[posState * ULZMABase.kNumLenSymbols + symbol] |
| 1471 |
end; |
| 1472 |
|
| 1473 |
procedure TLZMALenPriceTableEncoder.UpdateTable(const posState:integer); |
| 1474 |
begin |
| 1475 |
SetPrices(posState, _tableSize, _prices, posState * ULZMABase.kNumLenSymbols); |
| 1476 |
_counters[posState] := _tableSize; |
| 1477 |
end; |
| 1478 |
|
| 1479 |
procedure TLZMALenPriceTableEncoder.UpdateTables(const numPosStates:integer); |
| 1480 |
var posState:integer; |
| 1481 |
begin |
| 1482 |
for posState := 0 to numPosStates -1 do |
| 1483 |
UpdateTable(posState); |
| 1484 |
end; |
| 1485 |
|
| 1486 |
procedure TLZMALenPriceTableEncoder.Encode(const rangeEncoder:TRangeEncoder;symbol:integer;const posState:integer); |
| 1487 |
begin |
| 1488 |
inherited Encode(rangeEncoder, symbol, posState); |
| 1489 |
dec(_counters[posState]); |
| 1490 |
if (_counters[posState] = 0) then |
| 1491 |
UpdateTable(posState); |
| 1492 |
end; |
| 1493 |
|
| 1494 |
procedure TLZMAOptimal.MakeAsChar; |
| 1495 |
begin |
| 1496 |
BackPrev := -1; |
| 1497 |
Prev1IsChar := false; |
| 1498 |
end; |
| 1499 |
|
| 1500 |
procedure TLZMAOptimal.MakeAsShortRep; |
| 1501 |
begin |
| 1502 |
BackPrev := 0; |
| 1503 |
Prev1IsChar := false; |
| 1504 |
end; |
| 1505 |
|
| 1506 |
function TLZMAOptimal.IsShortRep:boolean; |
| 1507 |
begin |
| 1508 |
result:=BackPrev = 0; |
| 1509 |
end; |
| 1510 |
|
| 1511 |
procedure TLZMAEncoder.DoProgress(const Action:TLZMAProgressAction;const Value:integer); |
| 1512 |
begin |
| 1513 |
if assigned(fonprogress) then |
| 1514 |
fonprogress(action,value); |
| 1515 |
end; |
| 1516 |
|
| 1517 |
end. |