Ppmd7.c 17 KB

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