Ppmd7Dec.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /* Ppmd7Dec.c -- PPMdH Decoder
  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 "Ppmd7.h"
  6. #define kTopValue (1 << 24)
  7. BoolInt Ppmd7z_RangeDec_Init(CPpmd7z_RangeDec *p)
  8. {
  9. unsigned i;
  10. p->Code = 0;
  11. p->Range = 0xFFFFFFFF;
  12. if (IByteIn_Read(p->Stream) != 0)
  13. return False;
  14. for (i = 0; i < 4; i++)
  15. p->Code = (p->Code << 8) | IByteIn_Read(p->Stream);
  16. return (p->Code < 0xFFFFFFFF);
  17. }
  18. #define GET_Ppmd7z_RangeDec CPpmd7z_RangeDec *p = CONTAINER_FROM_VTBL(pp, CPpmd7z_RangeDec, vt);
  19. static UInt32 Range_GetThreshold(const IPpmd7_RangeDec *pp, UInt32 total)
  20. {
  21. GET_Ppmd7z_RangeDec
  22. return p->Code / (p->Range /= total);
  23. }
  24. static void Range_Normalize(CPpmd7z_RangeDec *p)
  25. {
  26. if (p->Range < kTopValue)
  27. {
  28. p->Code = (p->Code << 8) | IByteIn_Read(p->Stream);
  29. p->Range <<= 8;
  30. if (p->Range < kTopValue)
  31. {
  32. p->Code = (p->Code << 8) | IByteIn_Read(p->Stream);
  33. p->Range <<= 8;
  34. }
  35. }
  36. }
  37. static void Range_Decode(const IPpmd7_RangeDec *pp, UInt32 start, UInt32 size)
  38. {
  39. GET_Ppmd7z_RangeDec
  40. p->Code -= start * p->Range;
  41. p->Range *= size;
  42. Range_Normalize(p);
  43. }
  44. static UInt32 Range_DecodeBit(const IPpmd7_RangeDec *pp, UInt32 size0)
  45. {
  46. GET_Ppmd7z_RangeDec
  47. UInt32 newBound = (p->Range >> 14) * size0;
  48. UInt32 symbol;
  49. if (p->Code < newBound)
  50. {
  51. symbol = 0;
  52. p->Range = newBound;
  53. }
  54. else
  55. {
  56. symbol = 1;
  57. p->Code -= newBound;
  58. p->Range -= newBound;
  59. }
  60. Range_Normalize(p);
  61. return symbol;
  62. }
  63. void Ppmd7z_RangeDec_CreateVTable(CPpmd7z_RangeDec *p)
  64. {
  65. p->vt.GetThreshold = Range_GetThreshold;
  66. p->vt.Decode = Range_Decode;
  67. p->vt.DecodeBit = Range_DecodeBit;
  68. }
  69. #define MASK(sym) ((signed char *)charMask)[sym]
  70. int Ppmd7_DecodeSymbol(CPpmd7 *p, const IPpmd7_RangeDec *rc)
  71. {
  72. size_t charMask[256 / sizeof(size_t)];
  73. if (p->MinContext->NumStats != 1)
  74. {
  75. CPpmd_State *s = Ppmd7_GetStats(p, p->MinContext);
  76. unsigned i;
  77. UInt32 count, hiCnt;
  78. if ((count = rc->GetThreshold(rc, p->MinContext->SummFreq)) < (hiCnt = s->Freq))
  79. {
  80. Byte symbol;
  81. rc->Decode(rc, 0, s->Freq);
  82. p->FoundState = s;
  83. symbol = s->Symbol;
  84. Ppmd7_Update1_0(p);
  85. return symbol;
  86. }
  87. p->PrevSuccess = 0;
  88. i = p->MinContext->NumStats - 1;
  89. do
  90. {
  91. if ((hiCnt += (++s)->Freq) > count)
  92. {
  93. Byte symbol;
  94. rc->Decode(rc, hiCnt - s->Freq, s->Freq);
  95. p->FoundState = s;
  96. symbol = s->Symbol;
  97. Ppmd7_Update1(p);
  98. return symbol;
  99. }
  100. }
  101. while (--i);
  102. if (count >= p->MinContext->SummFreq)
  103. return -2;
  104. p->HiBitsFlag = p->HB2Flag[p->FoundState->Symbol];
  105. rc->Decode(rc, hiCnt, p->MinContext->SummFreq - hiCnt);
  106. PPMD_SetAllBitsIn256Bytes(charMask);
  107. MASK(s->Symbol) = 0;
  108. i = p->MinContext->NumStats - 1;
  109. do { MASK((--s)->Symbol) = 0; } while (--i);
  110. }
  111. else
  112. {
  113. UInt16 *prob = Ppmd7_GetBinSumm(p);
  114. if (rc->DecodeBit(rc, *prob) == 0)
  115. {
  116. Byte symbol;
  117. *prob = (UInt16)PPMD_UPDATE_PROB_0(*prob);
  118. symbol = (p->FoundState = Ppmd7Context_OneState(p->MinContext))->Symbol;
  119. Ppmd7_UpdateBin(p);
  120. return symbol;
  121. }
  122. *prob = (UInt16)PPMD_UPDATE_PROB_1(*prob);
  123. p->InitEsc = PPMD7_kExpEscape[*prob >> 10];
  124. PPMD_SetAllBitsIn256Bytes(charMask);
  125. MASK(Ppmd7Context_OneState(p->MinContext)->Symbol) = 0;
  126. p->PrevSuccess = 0;
  127. }
  128. for (;;)
  129. {
  130. CPpmd_State *ps[256], *s;
  131. UInt32 freqSum, count, hiCnt;
  132. CPpmd_See *see;
  133. unsigned i, num, numMasked = p->MinContext->NumStats;
  134. do
  135. {
  136. p->OrderFall++;
  137. if (!p->MinContext->Suffix)
  138. return -1;
  139. p->MinContext = Ppmd7_GetContext(p, p->MinContext->Suffix);
  140. }
  141. while (p->MinContext->NumStats == numMasked);
  142. hiCnt = 0;
  143. s = Ppmd7_GetStats(p, p->MinContext);
  144. i = 0;
  145. num = p->MinContext->NumStats - numMasked;
  146. do
  147. {
  148. int k = (int)(MASK(s->Symbol));
  149. hiCnt += (s->Freq & k);
  150. ps[i] = s++;
  151. i -= k;
  152. }
  153. while (i != num);
  154. see = Ppmd7_MakeEscFreq(p, numMasked, &freqSum);
  155. freqSum += hiCnt;
  156. count = rc->GetThreshold(rc, freqSum);
  157. if (count < hiCnt)
  158. {
  159. Byte symbol;
  160. CPpmd_State **pps = ps;
  161. for (hiCnt = 0; (hiCnt += (*pps)->Freq) <= count; pps++);
  162. s = *pps;
  163. rc->Decode(rc, hiCnt - s->Freq, s->Freq);
  164. Ppmd_See_Update(see);
  165. p->FoundState = s;
  166. symbol = s->Symbol;
  167. Ppmd7_Update2(p);
  168. return symbol;
  169. }
  170. if (count >= freqSum)
  171. return -2;
  172. rc->Decode(rc, hiCnt, freqSum - hiCnt);
  173. see->Summ = (UInt16)(see->Summ + freqSum);
  174. do { MASK(ps[--i]->Symbol) = 0; } while (i != 0);
  175. }
  176. }