1/* Ppmd7.c -- PPMdH codec 22010-03-12 : Igor Pavlov : Public domain 3This code is based on PPMd var.H (2001): Dmitry Shkarin : Public domain */ 4 5#include "Precomp.h" 6 7#include <memory.h> 8 9#include "Ppmd7.h" 10 11const Byte PPMD7_kExpEscape[16] = { 25, 14, 9, 7, 5, 5, 4, 4, 4, 3, 3, 3, 2, 2, 2, 2 }; 12static const UInt16 kInitBinEsc[] = { 0x3CDD, 0x1F3F, 0x59BF, 0x48F3, 0x64A1, 0x5ABC, 0x6632, 0x6051}; 13 14#define MAX_FREQ 124 15#define UNIT_SIZE 12 16 17#define U2B(nu) ((UInt32)(nu) * UNIT_SIZE) 18#define U2I(nu) (p->Units2Indx[(nu) - 1]) 19#define I2U(indx) (p->Indx2Units[indx]) 20 21#ifdef PPMD_32BIT 22 #define REF(ptr) (ptr) 23#else 24 #define REF(ptr) ((UInt32)((Byte *)(ptr) - (p)->Base)) 25#endif 26 27#define STATS_REF(ptr) ((CPpmd_State_Ref)REF(ptr)) 28 29#define CTX(ref) ((CPpmd7_Context *)Ppmd7_GetContext(p, ref)) 30#define STATS(ctx) Ppmd7_GetStats(p, ctx) 31#define ONE_STATE(ctx) Ppmd7Context_OneState(ctx) 32#define SUFFIX(ctx) CTX((ctx)->Suffix) 33 34typedef CPpmd7_Context * CTX_PTR; 35 36struct CPpmd7_Node_; 37 38typedef 39 #ifdef PPMD_32BIT 40 struct CPpmd7_Node_ * 41 #else 42 UInt32 43 #endif 44 CPpmd7_Node_Ref; 45 46typedef struct CPpmd7_Node_ 47{ 48 UInt16 Stamp; /* must be at offset 0 as CPpmd7_Context::NumStats. Stamp=0 means free */ 49 UInt16 NU; 50 CPpmd7_Node_Ref Next; /* must be at offset >= 4 */ 51 CPpmd7_Node_Ref Prev; 52} CPpmd7_Node; 53 54#ifdef PPMD_32BIT 55 #define NODE(ptr) (ptr) 56#else 57 #define NODE(offs) ((CPpmd7_Node *)(p->Base + (offs))) 58#endif 59 60void Ppmd7_Construct(CPpmd7 *p) 61{ 62 unsigned i, k, m; 63 64 p->Base = 0; 65 66 for (i = 0, k = 0; i < PPMD_NUM_INDEXES; i++) 67 { 68 unsigned step = (i >= 12 ? 4 : (i >> 2) + 1); 69 do { p->Units2Indx[k++] = (Byte)i; } while(--step); 70 p->Indx2Units[i] = (Byte)k; 71 } 72 73 p->NS2BSIndx[0] = (0 << 1); 74 p->NS2BSIndx[1] = (1 << 1); 75 memset(p->NS2BSIndx + 2, (2 << 1), 9); 76 memset(p->NS2BSIndx + 11, (3 << 1), 256 - 11); 77 78 for (i = 0; i < 3; i++) 79 p->NS2Indx[i] = (Byte)i; 80 for (m = i, k = 1; i < 256; i++) 81 { 82 p->NS2Indx[i] = (Byte)m; 83 if (--k == 0) 84 k = (++m) - 2; 85 } 86 87 memset(p->HB2Flag, 0, 0x40); 88 memset(p->HB2Flag + 0x40, 8, 0x100 - 0x40); 89} 90 91void Ppmd7_Free(CPpmd7 *p, ISzAlloc *alloc) 92{ 93 alloc->Free(alloc, p->Base); 94 p->Size = 0; 95 p->Base = 0; 96} 97 98Bool Ppmd7_Alloc(CPpmd7 *p, UInt32 size, ISzAlloc *alloc) 99{ 100 if (p->Base == 0 || p->Size != size) 101 { 102 Ppmd7_Free(p, alloc); 103 p->AlignOffset = 104 #ifdef PPMD_32BIT 105 (4 - size) & 3; 106 #else 107 4 - (size & 3); 108 #endif 109 if ((p->Base = (Byte *)alloc->Alloc(alloc, p->AlignOffset + size 110 #ifndef PPMD_32BIT 111 + UNIT_SIZE 112 #endif 113 )) == 0) 114 return False; 115 p->Size = size; 116 } 117 return True; 118} 119 120static void InsertNode(CPpmd7 *p, void *node, unsigned indx) 121{ 122 *((CPpmd_Void_Ref *)node) = p->FreeList[indx]; 123 p->FreeList[indx] = REF(node); 124} 125 126static void *RemoveNode(CPpmd7 *p, unsigned indx) 127{ 128 CPpmd_Void_Ref *node = (CPpmd_Void_Ref *)Ppmd7_GetPtr(p, p->FreeList[indx]); 129 p->FreeList[indx] = *node; 130 return node; 131} 132 133static void SplitBlock(CPpmd7 *p, void *ptr, unsigned oldIndx, unsigned newIndx) 134{ 135 unsigned i, nu = I2U(oldIndx) - I2U(newIndx); 136 ptr = (Byte *)ptr + U2B(I2U(newIndx)); 137 if (I2U(i = U2I(nu)) != nu) 138 { 139 unsigned k = I2U(--i); 140 InsertNode(p, ((Byte *)ptr) + U2B(k), nu - k - 1); 141 } 142 InsertNode(p, ptr, i); 143} 144 145static void GlueFreeBlocks(CPpmd7 *p) 146{ 147 #ifdef PPMD_32BIT 148 CPpmd7_Node headItem; 149 CPpmd7_Node_Ref head = &headItem; 150 #else 151 CPpmd7_Node_Ref head = p->AlignOffset + p->Size; 152 #endif 153 154 CPpmd7_Node_Ref n = head; 155 unsigned i; 156 157 p->GlueCount = 255; 158 159 /* create doubly-linked list of free blocks */ 160 for (i = 0; i < PPMD_NUM_INDEXES; i++) 161 { 162 UInt16 nu = I2U(i); 163 CPpmd7_Node_Ref next = (CPpmd7_Node_Ref)p->FreeList[i]; 164 p->FreeList[i] = 0; 165 while (next != 0) 166 { 167 CPpmd7_Node *node = NODE(next); 168 node->Next = n; 169 n = NODE(n)->Prev = next; 170 next = *(const CPpmd7_Node_Ref *)node; 171 node->Stamp = 0; 172 node->NU = (UInt16)nu; 173 } 174 } 175 NODE(head)->Stamp = 1; 176 NODE(head)->Next = n; 177 NODE(n)->Prev = head; 178 if (p->LoUnit != p->HiUnit) 179 ((CPpmd7_Node *)p->LoUnit)->Stamp = 1; 180 181 /* Glue free blocks */ 182 while (n != head) 183 { 184 CPpmd7_Node *node = NODE(n); 185 UInt32 nu = (UInt32)node->NU; 186 for (;;) 187 { 188 CPpmd7_Node *node2 = NODE(n) + nu; 189 nu += node2->NU; 190 if (node2->Stamp != 0 || nu >= 0x10000) 191 break; 192 NODE(node2->Prev)->Next = node2->Next; 193 NODE(node2->Next)->Prev = node2->Prev; 194 node->NU = (UInt16)nu; 195 } 196 n = node->Next; 197 } 198 199 /* Fill lists of free blocks */ 200 for (n = NODE(head)->Next; n != head;) 201 { 202 CPpmd7_Node *node = NODE(n); 203 unsigned nu; 204 CPpmd7_Node_Ref next = node->Next; 205 for (nu = node->NU; nu > 128; nu -= 128, node += 128) 206 InsertNode(p, node, PPMD_NUM_INDEXES - 1); 207 if (I2U(i = U2I(nu)) != nu) 208 { 209 unsigned k = I2U(--i); 210 InsertNode(p, node + k, nu - k - 1); 211 } 212 InsertNode(p, node, i); 213 n = next; 214 } 215} 216 217static void *AllocUnitsRare(CPpmd7 *p, unsigned indx) 218{ 219 unsigned i; 220 void *retVal; 221 if (p->GlueCount == 0) 222 { 223 GlueFreeBlocks(p); 224 if (p->FreeList[indx] != 0) 225 return RemoveNode(p, indx); 226 } 227 i = indx; 228 do 229 { 230 if (++i == PPMD_NUM_INDEXES) 231 { 232 UInt32 numBytes = U2B(I2U(indx)); 233 p->GlueCount--; 234 return ((UInt32)(p->UnitsStart - p->Text) > numBytes) ? (p->UnitsStart -= numBytes) : (NULL); 235 } 236 } 237 while (p->FreeList[i] == 0); 238 retVal = RemoveNode(p, i); 239 SplitBlock(p, retVal, i, indx); 240 return retVal; 241} 242 243static void *AllocUnits(CPpmd7 *p, unsigned indx) 244{ 245 UInt32 numBytes; 246 if (p->FreeList[indx] != 0) 247 return RemoveNode(p, indx); 248 numBytes = U2B(I2U(indx)); 249 if (numBytes <= (UInt32)(p->HiUnit - p->LoUnit)) 250 { 251 void *retVal = p->LoUnit; 252 p->LoUnit += numBytes; 253 return retVal; 254 } 255 return AllocUnitsRare(p, indx); 256} 257 258#define MyMem12Cpy(dest, src, num) \ 259 { UInt32 *d = (UInt32 *)dest; const UInt32 *s = (const UInt32 *)src; UInt32 n = num; \ 260 do { d[0] = s[0]; d[1] = s[1]; d[2] = s[2]; s += 3; d += 3; } while(--n); } 261 262static void *ShrinkUnits(CPpmd7 *p, void *oldPtr, unsigned oldNU, unsigned newNU) 263{ 264 unsigned i0 = U2I(oldNU); 265 unsigned i1 = U2I(newNU); 266 if (i0 == i1) 267 return oldPtr; 268 if (p->FreeList[i1] != 0) 269 { 270 void *ptr = RemoveNode(p, i1); 271 MyMem12Cpy(ptr, oldPtr, newNU); 272 InsertNode(p, oldPtr, i0); 273 return ptr; 274 } 275 SplitBlock(p, oldPtr, i0, i1); 276 return oldPtr; 277} 278 279#define SUCCESSOR(p) ((CPpmd_Void_Ref)((p)->SuccessorLow | ((UInt32)(p)->SuccessorHigh << 16))) 280 281static void SetSuccessor(CPpmd_State *p, CPpmd_Void_Ref v) 282{ 283 (p)->SuccessorLow = (UInt16)((UInt32)(v) & 0xFFFF); 284 (p)->SuccessorHigh = (UInt16)(((UInt32)(v) >> 16) & 0xFFFF); 285} 286 287static void RestartModel(CPpmd7 *p) 288{ 289 unsigned i, k, m; 290 291 memset(p->FreeList, 0, sizeof(p->FreeList)); 292 p->Text = p->Base + p->AlignOffset; 293 p->HiUnit = p->Text + p->Size; 294 p->LoUnit = p->UnitsStart = p->HiUnit - p->Size / 8 / UNIT_SIZE * 7 * UNIT_SIZE; 295 p->GlueCount = 0; 296 297 p->OrderFall = p->MaxOrder; 298 p->RunLength = p->InitRL = -(Int32)((p->MaxOrder < 12) ? p->MaxOrder : 12) - 1; 299 p->PrevSuccess = 0; 300 301 p->MinContext = p->MaxContext = (CTX_PTR)(p->HiUnit -= UNIT_SIZE); /* AllocContext(p); */ 302 p->MinContext->Suffix = 0; 303 p->MinContext->NumStats = 256; 304 p->MinContext->SummFreq = 256 + 1; 305 p->FoundState = (CPpmd_State *)p->LoUnit; /* AllocUnits(p, PPMD_NUM_INDEXES - 1); */ 306 p->LoUnit += U2B(256 / 2); 307 p->MinContext->Stats = REF(p->FoundState); 308 for (i = 0; i < 256; i++) 309 { 310 CPpmd_State *s = &p->FoundState[i]; 311 s->Symbol = (Byte)i; 312 s->Freq = 1; 313 SetSuccessor(s, 0); 314 } 315 316 for (i = 0; i < 128; i++) 317 for (k = 0; k < 8; k++) 318 { 319 UInt16 *dest = p->BinSumm[i] + k; 320 UInt16 val = (UInt16)(PPMD_BIN_SCALE - kInitBinEsc[k] / (i + 2)); 321 for (m = 0; m < 64; m += 8) 322 dest[m] = val; 323 } 324 325 for (i = 0; i < 25; i++) 326 for (k = 0; k < 16; k++) 327 { 328 CPpmd_See *s = &p->See[i][k]; 329 s->Summ = (UInt16)((5 * i + 10) << (s->Shift = PPMD_PERIOD_BITS - 4)); 330 s->Count = 4; 331 } 332} 333 334void Ppmd7_Init(CPpmd7 *p, unsigned maxOrder) 335{ 336 p->MaxOrder = maxOrder; 337 RestartModel(p); 338 p->DummySee.Shift = PPMD_PERIOD_BITS; 339 p->DummySee.Summ = 0; /* unused */ 340 p->DummySee.Count = 64; /* unused */ 341} 342 343static CTX_PTR CreateSuccessors(CPpmd7 *p, Bool skip) 344{ 345 CPpmd_State upState; 346 CTX_PTR c = p->MinContext; 347 CPpmd_Byte_Ref upBranch = (CPpmd_Byte_Ref)SUCCESSOR(p->FoundState); 348 CPpmd_State *ps[PPMD7_MAX_ORDER]; 349 unsigned numPs = 0; 350 351 if (!skip) 352 ps[numPs++] = p->FoundState; 353 354 while (c->Suffix) 355 { 356 CPpmd_Void_Ref successor; 357 CPpmd_State *s; 358 c = SUFFIX(c); 359 if (c->NumStats != 1) 360 { 361 for (s = STATS(c); s->Symbol != p->FoundState->Symbol; s++); 362 } 363 else 364 s = ONE_STATE(c); 365 successor = SUCCESSOR(s); 366 if (successor != upBranch) 367 { 368 c = CTX(successor); 369 if (numPs == 0) 370 return c; 371 break; 372 } 373 ps[numPs++] = s; 374 } 375 376 upState.Symbol = *(const Byte *)Ppmd7_GetPtr(p, upBranch); 377 SetSuccessor(&upState, upBranch + 1); 378 379 if (c->NumStats == 1) 380 upState.Freq = ONE_STATE(c)->Freq; 381 else 382 { 383 UInt32 cf, s0; 384 CPpmd_State *s; 385 for (s = STATS(c); s->Symbol != upState.Symbol; s++); 386 cf = s->Freq - 1; 387 s0 = c->SummFreq - c->NumStats - cf; 388 upState.Freq = (Byte)(1 + ((2 * cf <= s0) ? (5 * cf > s0) : ((2 * cf + 3 * s0 - 1) / (2 * s0)))); 389 } 390 391 do 392 { 393 /* Create Child */ 394 CTX_PTR c1; /* = AllocContext(p); */ 395 if (p->HiUnit != p->LoUnit) 396 c1 = (CTX_PTR)(p->HiUnit -= UNIT_SIZE); 397 else if (p->FreeList[0] != 0) 398 c1 = (CTX_PTR)RemoveNode(p, 0); 399 else 400 { 401 c1 = (CTX_PTR)AllocUnitsRare(p, 0); 402 if (!c1) 403 return NULL; 404 } 405 c1->NumStats = 1; 406 *ONE_STATE(c1) = upState; 407 c1->Suffix = REF(c); 408 SetSuccessor(ps[--numPs], REF(c1)); 409 c = c1; 410 } 411 while (numPs != 0); 412 413 return c; 414} 415 416static void SwapStates(CPpmd_State *t1, CPpmd_State *t2) 417{ 418 CPpmd_State tmp = *t1; 419 *t1 = *t2; 420 *t2 = tmp; 421} 422 423static void UpdateModel(CPpmd7 *p) 424{ 425 CPpmd_Void_Ref successor, fSuccessor = SUCCESSOR(p->FoundState); 426 CTX_PTR c; 427 unsigned s0, ns; 428 429 if (p->FoundState->Freq < MAX_FREQ / 4 && p->MinContext->Suffix != 0) 430 { 431 c = SUFFIX(p->MinContext); 432 433 if (c->NumStats == 1) 434 { 435 CPpmd_State *s = ONE_STATE(c); 436 if (s->Freq < 32) 437 s->Freq++; 438 } 439 else 440 { 441 CPpmd_State *s = STATS(c); 442 if (s->Symbol != p->FoundState->Symbol) 443 { 444 do { s++; } while (s->Symbol != p->FoundState->Symbol); 445 if (s[0].Freq >= s[-1].Freq) 446 { 447 SwapStates(&s[0], &s[-1]); 448 s--; 449 } 450 } 451 if (s->Freq < MAX_FREQ - 9) 452 { 453 s->Freq += 2; 454 c->SummFreq += 2; 455 } 456 } 457 } 458 459 if (p->OrderFall == 0) 460 { 461 p->MinContext = p->MaxContext = CreateSuccessors(p, True); 462 if (p->MinContext == 0) 463 { 464 RestartModel(p); 465 return; 466 } 467 SetSuccessor(p->FoundState, REF(p->MinContext)); 468 return; 469 } 470 471 *p->Text++ = p->FoundState->Symbol; 472 successor = REF(p->Text); 473 if (p->Text >= p->UnitsStart) 474 { 475 RestartModel(p); 476 return; 477 } 478 479 if (fSuccessor) 480 { 481 if (fSuccessor <= successor) 482 { 483 CTX_PTR cs = CreateSuccessors(p, False); 484 if (cs == NULL) 485 { 486 RestartModel(p); 487 return; 488 } 489 fSuccessor = REF(cs); 490 } 491 if (--p->OrderFall == 0) 492 { 493 successor = fSuccessor; 494 p->Text -= (p->MaxContext != p->MinContext); 495 } 496 } 497 else 498 { 499 SetSuccessor(p->FoundState, successor); 500 fSuccessor = REF(p->MinContext); 501 } 502 503 s0 = p->MinContext->SummFreq - (ns = p->MinContext->NumStats) - (p->FoundState->Freq - 1); 504 505 for (c = p->MaxContext; c != p->MinContext; c = SUFFIX(c)) 506 { 507 unsigned ns1; 508 UInt32 cf, sf; 509 if ((ns1 = c->NumStats) != 1) 510 { 511 if ((ns1 & 1) == 0) 512 { 513 /* Expand for one UNIT */ 514 unsigned oldNU = ns1 >> 1; 515 unsigned i = U2I(oldNU); 516 if (i != U2I(oldNU + 1)) 517 { 518 void *ptr = AllocUnits(p, i + 1); 519 void *oldPtr; 520 if (!ptr) 521 { 522 RestartModel(p); 523 return; 524 } 525 oldPtr = STATS(c); 526 MyMem12Cpy(ptr, oldPtr, oldNU); 527 InsertNode(p, oldPtr, i); 528 c->Stats = STATS_REF(ptr); 529 } 530 } 531 c->SummFreq = (UInt16)(c->SummFreq + (2 * ns1 < ns) + 2 * ((4 * ns1 <= ns) & (c->SummFreq <= 8 * ns1))); 532 } 533 else 534 { 535 CPpmd_State *s = (CPpmd_State*)AllocUnits(p, 0); 536 if (!s) 537 { 538 RestartModel(p); 539 return; 540 } 541 *s = *ONE_STATE(c); 542 c->Stats = REF(s); 543 if (s->Freq < MAX_FREQ / 4 - 1) 544 s->Freq <<= 1; 545 else 546 s->Freq = MAX_FREQ - 4; 547 c->SummFreq = (UInt16)(s->Freq + p->InitEsc + (ns > 3)); 548 } 549 cf = 2 * (UInt32)p->FoundState->Freq * (c->SummFreq + 6); 550 sf = (UInt32)s0 + c->SummFreq; 551 if (cf < 6 * sf) 552 { 553 cf = 1 + (cf > sf) + (cf >= 4 * sf); 554 c->SummFreq += 3; 555 } 556 else 557 { 558 cf = 4 + (cf >= 9 * sf) + (cf >= 12 * sf) + (cf >= 15 * sf); 559 c->SummFreq = (UInt16)(c->SummFreq + cf); 560 } 561 { 562 CPpmd_State *s = STATS(c) + ns1; 563 SetSuccessor(s, successor); 564 s->Symbol = p->FoundState->Symbol; 565 s->Freq = (Byte)cf; 566 c->NumStats = (UInt16)(ns1 + 1); 567 } 568 } 569 p->MaxContext = p->MinContext = CTX(fSuccessor); 570} 571 572static void Rescale(CPpmd7 *p) 573{ 574 unsigned i, adder, sumFreq, escFreq; 575 CPpmd_State *stats = STATS(p->MinContext); 576 CPpmd_State *s = p->FoundState; 577 { 578 CPpmd_State tmp = *s; 579 for (; s != stats; s--) 580 s[0] = s[-1]; 581 *s = tmp; 582 } 583 escFreq = p->MinContext->SummFreq - s->Freq; 584 s->Freq += 4; 585 adder = (p->OrderFall != 0); 586 s->Freq = (Byte)((s->Freq + adder) >> 1); 587 sumFreq = s->Freq; 588 589 i = p->MinContext->NumStats - 1; 590 do 591 { 592 escFreq -= (++s)->Freq; 593 s->Freq = (Byte)((s->Freq + adder) >> 1); 594 sumFreq += s->Freq; 595 if (s[0].Freq > s[-1].Freq) 596 { 597 CPpmd_State *s1 = s; 598 CPpmd_State tmp = *s1; 599 do 600 s1[0] = s1[-1]; 601 while (--s1 != stats && tmp.Freq > s1[-1].Freq); 602 *s1 = tmp; 603 } 604 } 605 while (--i); 606 607 if (s->Freq == 0) 608 { 609 unsigned numStats = p->MinContext->NumStats; 610 unsigned n0, n1; 611 do { i++; } while ((--s)->Freq == 0); 612 escFreq += i; 613 p->MinContext->NumStats = (UInt16)(p->MinContext->NumStats - i); 614 if (p->MinContext->NumStats == 1) 615 { 616 CPpmd_State tmp = *stats; 617 do 618 { 619 tmp.Freq = (Byte)(tmp.Freq - (tmp.Freq >> 1)); 620 escFreq >>= 1; 621 } 622 while (escFreq > 1); 623 InsertNode(p, stats, U2I(((numStats + 1) >> 1))); 624 *(p->FoundState = ONE_STATE(p->MinContext)) = tmp; 625 return; 626 } 627 n0 = (numStats + 1) >> 1; 628 n1 = (p->MinContext->NumStats + 1) >> 1; 629 if (n0 != n1) 630 p->MinContext->Stats = STATS_REF(ShrinkUnits(p, stats, n0, n1)); 631 } 632 p->MinContext->SummFreq = (UInt16)(sumFreq + escFreq - (escFreq >> 1)); 633 p->FoundState = STATS(p->MinContext); 634} 635 636CPpmd_See *Ppmd7_MakeEscFreq(CPpmd7 *p, unsigned numMasked, UInt32 *escFreq) 637{ 638 CPpmd_See *see; 639 unsigned nonMasked = p->MinContext->NumStats - numMasked; 640 if (p->MinContext->NumStats != 256) 641 { 642 see = p->See[p->NS2Indx[nonMasked - 1]] + 643 (nonMasked < (unsigned)SUFFIX(p->MinContext)->NumStats - p->MinContext->NumStats) + 644 2 * (p->MinContext->SummFreq < 11 * p->MinContext->NumStats) + 645 4 * (numMasked > nonMasked) + 646 p->HiBitsFlag; 647 { 648 unsigned r = (see->Summ >> see->Shift); 649 see->Summ = (UInt16)(see->Summ - r); 650 *escFreq = r + (r == 0); 651 } 652 } 653 else 654 { 655 see = &p->DummySee; 656 *escFreq = 1; 657 } 658 return see; 659} 660 661static void NextContext(CPpmd7 *p) 662{ 663 CTX_PTR c = CTX(SUCCESSOR(p->FoundState)); 664 if (p->OrderFall == 0 && (Byte *)c > p->Text) 665 p->MinContext = p->MaxContext = c; 666 else 667 UpdateModel(p); 668} 669 670void Ppmd7_Update1(CPpmd7 *p) 671{ 672 CPpmd_State *s = p->FoundState; 673 s->Freq += 4; 674 p->MinContext->SummFreq += 4; 675 if (s[0].Freq > s[-1].Freq) 676 { 677 SwapStates(&s[0], &s[-1]); 678 p->FoundState = --s; 679 if (s->Freq > MAX_FREQ) 680 Rescale(p); 681 } 682 NextContext(p); 683} 684 685void Ppmd7_Update1_0(CPpmd7 *p) 686{ 687 p->PrevSuccess = (2 * p->FoundState->Freq > p->MinContext->SummFreq); 688 p->RunLength += p->PrevSuccess; 689 p->MinContext->SummFreq += 4; 690 if ((p->FoundState->Freq += 4) > MAX_FREQ) 691 Rescale(p); 692 NextContext(p); 693} 694 695void Ppmd7_UpdateBin(CPpmd7 *p) 696{ 697 p->FoundState->Freq = (Byte)(p->FoundState->Freq + (p->FoundState->Freq < 128 ? 1: 0)); 698 p->PrevSuccess = 1; 699 p->RunLength++; 700 NextContext(p); 701} 702 703void Ppmd7_Update2(CPpmd7 *p) 704{ 705 p->MinContext->SummFreq += 4; 706 if ((p->FoundState->Freq += 4) > MAX_FREQ) 707 Rescale(p); 708 p->RunLength = p->InitRL; 709 UpdateModel(p); 710} 711