PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/ZipPla/SharpCompress/Compressors/PPMd/H/PPMContext.cs

https://bitbucket.org/udaken/zippla-mirror
C# | 568 lines | 484 code | 48 blank | 36 comment | 58 complexity | 66e1c18e03131e7bd324c6d1044ede63 MD5 | raw file
  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 { return 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 unionSize;
  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 + unionSize + 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[] ExpEscape = {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. this.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. this.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 { return 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 = ExpEscape[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. unionSize = Math.Max(FreqData.Size, State.Size);
  518. }
  519. }
  520. }