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.

189 lines
4.6 KiB

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