Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

710 lines
16 KiB

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