PPMContext.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. using System;
  2. using System.Text;
  3. using SharpCompress.Converters;
  4. namespace SharpCompress.Compressors.PPMd.H
  5. {
  6. internal class PpmContext : Pointer
  7. {
  8. internal FreqData FreqData
  9. {
  10. get => _freqData;
  11. set
  12. {
  13. _freqData.SummFreq = value.SummFreq;
  14. _freqData.SetStats(value.GetStats());
  15. }
  16. }
  17. public virtual int NumStats
  18. {
  19. get
  20. {
  21. if (Memory != null)
  22. {
  23. _numStats = DataConverter.LittleEndian.GetInt16(Memory, Address) & 0xffff;
  24. }
  25. return _numStats;
  26. }
  27. set
  28. {
  29. _numStats = value & 0xffff;
  30. if (Memory != null)
  31. {
  32. DataConverter.LittleEndian.PutBytes(Memory, Address, (short)value);
  33. }
  34. }
  35. }
  36. //UPGRADE_NOTE: Final was removed from the declaration of 'unionSize '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
  37. //UPGRADE_NOTE: The initialization of 'unionSize' was moved to static method 'SharpCompress.Unpack.PPM.PPMContext'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1005'"
  38. private static readonly int UNION_SIZE;
  39. //UPGRADE_NOTE: Final was removed from the declaration of 'size '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
  40. public static readonly int SIZE = 2 + UNION_SIZE + 4; // 12
  41. // ushort NumStats;
  42. private int _numStats; // determines if feqData or onstate is used
  43. // (1==onestate)
  44. //UPGRADE_NOTE: Final was removed from the declaration of 'freqData '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
  45. private readonly FreqData _freqData; // -\
  46. // |-> union
  47. //UPGRADE_NOTE: Final was removed from the declaration of 'oneState '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
  48. private readonly State _oneState; // -/
  49. private int _suffix; // pointer ppmcontext
  50. //UPGRADE_NOTE: Final was removed from the declaration of 'ExpEscape'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
  51. public static readonly int[] EXP_ESCAPE = {25, 14, 9, 7, 5, 5, 4, 4, 4, 3, 3, 3, 2, 2, 2, 2};
  52. // Temp fields
  53. //UPGRADE_NOTE: Final was removed from the declaration of 'tempState1 '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
  54. private readonly State _tempState1 = new State(null);
  55. //UPGRADE_NOTE: Final was removed from the declaration of 'tempState2 '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
  56. private readonly State _tempState2 = new State(null);
  57. //UPGRADE_NOTE: Final was removed from the declaration of 'tempState3 '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
  58. private readonly State _tempState3 = new State(null);
  59. //UPGRADE_NOTE: Final was removed from the declaration of 'tempState4 '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
  60. private readonly State _tempState4 = new State(null);
  61. //UPGRADE_NOTE: Final was removed from the declaration of 'tempState5 '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
  62. private readonly State _tempState5 = new State(null);
  63. private PpmContext _tempPpmContext;
  64. //UPGRADE_NOTE: Final was removed from the declaration of 'ps '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
  65. internal int[] _ps = new int[256];
  66. public PpmContext(byte[] memory)
  67. : base(memory)
  68. {
  69. _oneState = new State(memory);
  70. _freqData = new FreqData(memory);
  71. }
  72. internal PpmContext Initialize(byte[] mem)
  73. {
  74. _oneState.Initialize(mem);
  75. _freqData.Initialize(mem);
  76. return base.Initialize<PpmContext>(mem);
  77. }
  78. internal State GetOneState()
  79. {
  80. return _oneState;
  81. }
  82. internal void SetOneState(StateRef oneState)
  83. {
  84. _oneState.SetValues(oneState);
  85. }
  86. internal int GetSuffix()
  87. {
  88. if (Memory != null)
  89. {
  90. _suffix = DataConverter.LittleEndian.GetInt32(Memory, Address + 8);
  91. }
  92. return _suffix;
  93. }
  94. internal void SetSuffix(PpmContext suffix)
  95. {
  96. SetSuffix(suffix.Address);
  97. }
  98. internal void SetSuffix(int suffix)
  99. {
  100. _suffix = suffix;
  101. if (Memory != null)
  102. {
  103. DataConverter.LittleEndian.PutBytes(Memory, Address + 8, suffix);
  104. }
  105. }
  106. internal override int Address
  107. {
  108. get => base.Address;
  109. set
  110. {
  111. base.Address = value;
  112. _oneState.Address = value + 2;
  113. _freqData.Address = value + 2;
  114. }
  115. }
  116. private PpmContext GetTempPpmContext(byte[] memory)
  117. {
  118. if (_tempPpmContext == null)
  119. {
  120. _tempPpmContext = new PpmContext(null);
  121. }
  122. return _tempPpmContext.Initialize(memory);
  123. }
  124. internal int CreateChild(ModelPpm model, State pStats, StateRef firstState)
  125. {
  126. PpmContext pc = GetTempPpmContext(model.SubAlloc.Heap);
  127. pc.Address = model.SubAlloc.AllocContext();
  128. if (pc != null)
  129. {
  130. pc.NumStats = 1;
  131. pc.SetOneState(firstState);
  132. pc.SetSuffix(this);
  133. pStats.SetSuccessor(pc);
  134. }
  135. return pc.Address;
  136. }
  137. internal void Rescale(ModelPpm model)
  138. {
  139. int oldNs = NumStats, i = NumStats - 1, adder, escFreq;
  140. // STATE* p1, * p;
  141. State p1 = new State(model.Heap);
  142. State p = new State(model.Heap);
  143. State temp = new State(model.Heap);
  144. for (p.Address = model.FoundState.Address; p.Address != _freqData.GetStats(); p.DecrementAddress())
  145. {
  146. temp.Address = p.Address - State.SIZE;
  147. State.PpmdSwap(p, temp);
  148. }
  149. temp.Address = _freqData.GetStats();
  150. temp.IncrementFreq(4);
  151. _freqData.IncrementSummFreq(4);
  152. escFreq = _freqData.SummFreq - p.Freq;
  153. adder = (model.OrderFall != 0) ? 1 : 0;
  154. p.Freq = Utility.URShift((p.Freq + adder), 1);
  155. _freqData.SummFreq = p.Freq;
  156. do
  157. {
  158. p.IncrementAddress();
  159. escFreq -= p.Freq;
  160. p.Freq = Utility.URShift((p.Freq + adder), 1);
  161. _freqData.IncrementSummFreq(p.Freq);
  162. temp.Address = p.Address - State.SIZE;
  163. if (p.Freq > temp.Freq)
  164. {
  165. p1.Address = p.Address;
  166. StateRef tmp = new StateRef();
  167. tmp.Values = p1;
  168. State temp2 = new State(model.Heap);
  169. State temp3 = new State(model.Heap);
  170. do
  171. {
  172. // p1[0]=p1[-1];
  173. temp2.Address = p1.Address - State.SIZE;
  174. p1.SetValues(temp2);
  175. p1.DecrementAddress();
  176. temp3.Address = p1.Address - State.SIZE;
  177. }
  178. while (p1.Address != _freqData.GetStats() && tmp.Freq > temp3.Freq);
  179. p1.SetValues(tmp);
  180. }
  181. }
  182. while (--i != 0);
  183. if (p.Freq == 0)
  184. {
  185. do
  186. {
  187. i++;
  188. p.DecrementAddress();
  189. }
  190. while (p.Freq == 0);
  191. escFreq += i;
  192. NumStats = NumStats - i;
  193. if (NumStats == 1)
  194. {
  195. StateRef tmp = new StateRef();
  196. temp.Address = _freqData.GetStats();
  197. tmp.Values = temp;
  198. // STATE tmp=*U.Stats;
  199. do
  200. {
  201. // tmp.Freq-=(tmp.Freq >> 1)
  202. tmp.DecrementFreq(Utility.URShift(tmp.Freq, 1));
  203. escFreq = Utility.URShift(escFreq, 1);
  204. }
  205. while (escFreq > 1);
  206. model.SubAlloc.FreeUnits(_freqData.GetStats(), Utility.URShift((oldNs + 1), 1));
  207. _oneState.SetValues(tmp);
  208. model.FoundState.Address = _oneState.Address;
  209. return;
  210. }
  211. }
  212. escFreq -= Utility.URShift(escFreq, 1);
  213. _freqData.IncrementSummFreq(escFreq);
  214. int n0 = Utility.URShift((oldNs + 1), 1), n1 = Utility.URShift((NumStats + 1), 1);
  215. if (n0 != n1)
  216. {
  217. _freqData.SetStats(model.SubAlloc.ShrinkUnits(_freqData.GetStats(), n0, n1));
  218. }
  219. model.FoundState.Address = _freqData.GetStats();
  220. }
  221. internal int GetArrayIndex(ModelPpm model, State rs)
  222. {
  223. PpmContext tempSuffix = GetTempPpmContext(model.SubAlloc.Heap);
  224. tempSuffix.Address = GetSuffix();
  225. int ret = 0;
  226. ret += model.PrevSuccess;
  227. ret += model.GetNs2BsIndx()[tempSuffix.NumStats - 1];
  228. ret += model.HiBitsFlag + 2 * model.GetHb2Flag()[rs.Symbol];
  229. ret += ((Utility.URShift(model.RunLength, 26)) & 0x20);
  230. return ret;
  231. }
  232. internal int GetMean(int summ, int shift, int round)
  233. {
  234. return (Utility.URShift((summ + (1 << (shift - round))), (shift)));
  235. }
  236. internal void DecodeBinSymbol(ModelPpm model)
  237. {
  238. State rs = _tempState1.Initialize(model.Heap);
  239. rs.Address = _oneState.Address; // State&
  240. model.HiBitsFlag = model.GetHb2Flag()[model.FoundState.Symbol];
  241. int off1 = rs.Freq - 1;
  242. int off2 = GetArrayIndex(model, rs);
  243. int bs = model.BinSumm[off1][off2];
  244. if (model.Coder.GetCurrentShiftCount(ModelPpm.TOT_BITS) < bs)
  245. {
  246. model.FoundState.Address = rs.Address;
  247. rs.IncrementFreq((rs.Freq < 128) ? 1 : 0);
  248. model.Coder.SubRange.LowCount = 0;
  249. model.Coder.SubRange.HighCount = bs;
  250. bs = ((bs + ModelPpm.INTERVAL - GetMean(bs, ModelPpm.PERIOD_BITS, 2)) & 0xffff);
  251. model.BinSumm[off1][off2] = bs;
  252. model.PrevSuccess = 1;
  253. model.IncRunLength(1);
  254. }
  255. else
  256. {
  257. model.Coder.SubRange.LowCount = bs;
  258. bs = (bs - GetMean(bs, ModelPpm.PERIOD_BITS, 2)) & 0xFFFF;
  259. model.BinSumm[off1][off2] = bs;
  260. model.Coder.SubRange.HighCount = ModelPpm.BIN_SCALE;
  261. model.InitEsc = EXP_ESCAPE[Utility.URShift(bs, 10)];
  262. model.NumMasked = 1;
  263. model.CharMask[rs.Symbol] = model.EscCount;
  264. model.PrevSuccess = 0;
  265. model.FoundState.Address = 0;
  266. }
  267. //int a = 0;//TODO just 4 debugging
  268. }
  269. // public static void ppmdSwap(ModelPPM model, StatePtr state1, StatePtr state2)
  270. // {
  271. // byte[] bytes = model.getSubAlloc().getHeap();
  272. // int p1 = state1.Address;
  273. // int p2 = state2.Address;
  274. //
  275. // for (int i = 0; i < StatePtr.size; i++) {
  276. // byte temp = bytes[p1+i];
  277. // bytes[p1+i] = bytes[p2+i];
  278. // bytes[p2+i] = temp;
  279. // }
  280. // state1.Address=p1);
  281. // state2.Address=p2);
  282. // }
  283. internal void Update1(ModelPpm model, int p)
  284. {
  285. model.FoundState.Address = p;
  286. model.FoundState.IncrementFreq(4);
  287. _freqData.IncrementSummFreq(4);
  288. State p0 = _tempState3.Initialize(model.Heap);
  289. State p1 = _tempState4.Initialize(model.Heap);
  290. p0.Address = p;
  291. p1.Address = p - State.SIZE;
  292. if (p0.Freq > p1.Freq)
  293. {
  294. State.PpmdSwap(p0, p1);
  295. model.FoundState.Address = p1.Address;
  296. if (p1.Freq > ModelPpm.MAX_FREQ)
  297. {
  298. Rescale(model);
  299. }
  300. }
  301. }
  302. internal void update1_0(ModelPpm model, int p)
  303. {
  304. model.FoundState.Address = p;
  305. model.PrevSuccess = 2 * model.FoundState.Freq > _freqData.SummFreq ? 1 : 0;
  306. model.IncRunLength(model.PrevSuccess);
  307. _freqData.IncrementSummFreq(4);
  308. model.FoundState.IncrementFreq(4);
  309. if (model.FoundState.Freq > ModelPpm.MAX_FREQ)
  310. {
  311. Rescale(model);
  312. }
  313. }
  314. internal bool DecodeSymbol2(ModelPpm model)
  315. {
  316. long count;
  317. int hiCnt, i = NumStats - model.NumMasked;
  318. See2Context psee2C = MakeEscFreq2(model, i);
  319. RangeCoder coder = model.Coder;
  320. // STATE* ps[256], ** pps=ps, * p=U.Stats-1;
  321. State p = _tempState1.Initialize(model.Heap);
  322. State temp = _tempState2.Initialize(model.Heap);
  323. p.Address = _freqData.GetStats() - State.SIZE;
  324. int pps = 0;
  325. hiCnt = 0;
  326. do
  327. {
  328. do
  329. {
  330. p.IncrementAddress(); // p++;
  331. }
  332. while (model.CharMask[p.Symbol] == model.EscCount);
  333. hiCnt += p.Freq;
  334. _ps[pps++] = p.Address;
  335. }
  336. while (--i != 0);
  337. coder.SubRange.IncScale(hiCnt);
  338. count = coder.CurrentCount;
  339. if (count >= coder.SubRange.Scale)
  340. {
  341. return false;
  342. }
  343. pps = 0;
  344. p.Address = _ps[pps];
  345. if (count < hiCnt)
  346. {
  347. hiCnt = 0;
  348. while ((hiCnt += p.Freq) <= count)
  349. {
  350. p.Address = _ps[++pps]; // p=*++pps;
  351. }
  352. coder.SubRange.HighCount = hiCnt;
  353. coder.SubRange.LowCount = hiCnt - p.Freq;
  354. psee2C.Update();
  355. Update2(model, p.Address);
  356. }
  357. else
  358. {
  359. coder.SubRange.LowCount = hiCnt;
  360. coder.SubRange.HighCount = coder.SubRange.Scale;
  361. i = NumStats - model.NumMasked; // ->NumMasked;
  362. pps--;
  363. do
  364. {
  365. temp.Address = _ps[++pps]; // (*++pps)
  366. model.CharMask[temp.Symbol] = model.EscCount;
  367. }
  368. while (--i != 0);
  369. psee2C.IncSumm((int)coder.SubRange.Scale);
  370. model.NumMasked = NumStats;
  371. }
  372. return (true);
  373. }
  374. internal void Update2(ModelPpm model, int p)
  375. {
  376. State temp = _tempState5.Initialize(model.Heap);
  377. temp.Address = p;
  378. model.FoundState.Address = p;
  379. model.FoundState.IncrementFreq(4);
  380. _freqData.IncrementSummFreq(4);
  381. if (temp.Freq > ModelPpm.MAX_FREQ)
  382. {
  383. Rescale(model);
  384. }
  385. model.IncEscCount(1);
  386. model.RunLength = model.InitRl;
  387. }
  388. private See2Context MakeEscFreq2(ModelPpm model, int diff)
  389. {
  390. See2Context psee2C;
  391. int numStats = NumStats;
  392. if (numStats != 256)
  393. {
  394. PpmContext suff = GetTempPpmContext(model.Heap);
  395. suff.Address = GetSuffix();
  396. int idx1 = model.GetNs2Indx()[diff - 1];
  397. int idx2 = 0;
  398. idx2 += ((diff < suff.NumStats - numStats) ? 1 : 0);
  399. idx2 += 2 * ((_freqData.SummFreq < 11 * numStats) ? 1 : 0);
  400. idx2 += 4 * ((model.NumMasked > diff) ? 1 : 0);
  401. idx2 += model.HiBitsFlag;
  402. psee2C = model.GetSee2Cont()[idx1][idx2];
  403. model.Coder.SubRange.Scale = psee2C.Mean;
  404. }
  405. else
  406. {
  407. psee2C = model.DummySee2Cont;
  408. model.Coder.SubRange.Scale = 1;
  409. }
  410. return psee2C;
  411. }
  412. internal See2Context MakeEscFreq(ModelPpm model, int numMasked, out int escFreq)
  413. {
  414. See2Context psee2C;
  415. int numStats = NumStats;
  416. int nonMasked = numStats - numMasked;
  417. if (numStats != 256)
  418. {
  419. PpmContext suff = GetTempPpmContext(model.Heap);
  420. suff.Address = GetSuffix();
  421. int idx1 = model.GetNs2Indx()[nonMasked - 1];
  422. int idx2 = 0;
  423. idx2 += ((nonMasked < suff.NumStats - numStats) ? 1 : 0);
  424. idx2 += 2 * ((_freqData.SummFreq < 11 * numStats) ? 1 : 0);
  425. idx2 += 4 * ((numMasked > nonMasked) ? 1 : 0);
  426. idx2 += model.HiBitsFlag;
  427. psee2C = model.GetSee2Cont()[idx1][idx2];
  428. escFreq = psee2C.Mean;
  429. }
  430. else
  431. {
  432. psee2C = model.DummySee2Cont;
  433. escFreq = 1;
  434. }
  435. return psee2C;
  436. }
  437. internal bool DecodeSymbol1(ModelPpm model)
  438. {
  439. RangeCoder coder = model.Coder;
  440. coder.SubRange.Scale = _freqData.SummFreq;
  441. State p = new State(model.Heap);
  442. p.Address = _freqData.GetStats();
  443. int i, hiCnt;
  444. long count = coder.CurrentCount;
  445. if (count >= coder.SubRange.Scale)
  446. {
  447. return false;
  448. }
  449. if (count < (hiCnt = p.Freq))
  450. {
  451. coder.SubRange.HighCount = hiCnt;
  452. model.PrevSuccess = (2 * hiCnt > coder.SubRange.Scale) ? 1 : 0;
  453. model.IncRunLength(model.PrevSuccess);
  454. hiCnt += 4;
  455. model.FoundState.Address = p.Address;
  456. model.FoundState.Freq = hiCnt;
  457. _freqData.IncrementSummFreq(4);
  458. if (hiCnt > ModelPpm.MAX_FREQ)
  459. {
  460. Rescale(model);
  461. }
  462. coder.SubRange.LowCount = 0;
  463. return true;
  464. }
  465. if (model.FoundState.Address == 0)
  466. {
  467. return (false);
  468. }
  469. model.PrevSuccess = 0;
  470. int numStats = NumStats;
  471. i = numStats - 1;
  472. while ((hiCnt += p.IncrementAddress().Freq) <= count)
  473. {
  474. if (--i == 0)
  475. {
  476. model.HiBitsFlag = model.GetHb2Flag()[model.FoundState.Symbol];
  477. coder.SubRange.LowCount = hiCnt;
  478. model.CharMask[p.Symbol] = model.EscCount;
  479. model.NumMasked = numStats;
  480. i = numStats - 1;
  481. model.FoundState.Address = 0;
  482. do
  483. {
  484. model.CharMask[p.DecrementAddress().Symbol] = model.EscCount;
  485. }
  486. while (--i != 0);
  487. coder.SubRange.HighCount = coder.SubRange.Scale;
  488. return (true);
  489. }
  490. }
  491. coder.SubRange.LowCount = hiCnt - p.Freq;
  492. coder.SubRange.HighCount = hiCnt;
  493. Update1(model, p.Address);
  494. return (true);
  495. }
  496. public override String ToString()
  497. {
  498. StringBuilder buffer = new StringBuilder();
  499. buffer.Append("PPMContext[");
  500. buffer.Append("\n Address=");
  501. buffer.Append(Address);
  502. buffer.Append("\n size=");
  503. buffer.Append(SIZE);
  504. buffer.Append("\n numStats=");
  505. buffer.Append(NumStats);
  506. buffer.Append("\n Suffix=");
  507. buffer.Append(GetSuffix());
  508. buffer.Append("\n freqData=");
  509. buffer.Append(_freqData);
  510. buffer.Append("\n oneState=");
  511. buffer.Append(_oneState);
  512. buffer.Append("\n]");
  513. return buffer.ToString();
  514. }
  515. static PpmContext()
  516. {
  517. UNION_SIZE = Math.Max(FreqData.SIZE, State.SIZE);
  518. }
  519. }
  520. }