1 /* Ppmd8Dec.c -- PPMdI Decoder
2 2017-04-03 : Igor Pavlov : Public domain
3 This code is based on:
4 PPMd var.I (2002): Dmitry Shkarin : Public domain
5 Carryless rangecoder (1999): Dmitry Subbotin : Public domain */
6
7 #include "Precomp.h"
8
9 #include "Ppmd8.h"
10
11 #define kTop (1 << 24)
12 #define kBot (1 << 15)
13
Ppmd8_RangeDec_Init(CPpmd8 * p)14 Bool Ppmd8_RangeDec_Init(CPpmd8 *p)
15 {
16 unsigned i;
17 p->Low = 0;
18 p->Range = 0xFFFFFFFF;
19 p->Code = 0;
20 for (i = 0; i < 4; i++)
21 p->Code = (p->Code << 8) | IByteIn_Read(p->Stream.In);
22 return (p->Code < 0xFFFFFFFF);
23 }
24
RangeDec_GetThreshold(CPpmd8 * p,UInt32 total)25 static UInt32 RangeDec_GetThreshold(CPpmd8 *p, UInt32 total)
26 {
27 return p->Code / (p->Range /= total);
28 }
29
RangeDec_Decode(CPpmd8 * p,UInt32 start,UInt32 size)30 static void RangeDec_Decode(CPpmd8 *p, UInt32 start, UInt32 size)
31 {
32 start *= p->Range;
33 p->Low += start;
34 p->Code -= start;
35 p->Range *= size;
36
37 while ((p->Low ^ (p->Low + p->Range)) < kTop ||
38 (p->Range < kBot && ((p->Range = (0 - p->Low) & (kBot - 1)), 1)))
39 {
40 p->Code = (p->Code << 8) | IByteIn_Read(p->Stream.In);
41 p->Range <<= 8;
42 p->Low <<= 8;
43 }
44 }
45
46 #define MASK(sym) ((signed char *)charMask)[sym]
47
Ppmd8_DecodeSymbol(CPpmd8 * p)48 int Ppmd8_DecodeSymbol(CPpmd8 *p)
49 {
50 size_t charMask[256 / sizeof(size_t)];
51 if (p->MinContext->NumStats != 0)
52 {
53 CPpmd_State *s = Ppmd8_GetStats(p, p->MinContext);
54 unsigned i;
55 UInt32 count, hiCnt;
56 if ((count = RangeDec_GetThreshold(p, p->MinContext->SummFreq)) < (hiCnt = s->Freq))
57 {
58 Byte symbol;
59 RangeDec_Decode(p, 0, s->Freq);
60 p->FoundState = s;
61 symbol = s->Symbol;
62 Ppmd8_Update1_0(p);
63 return symbol;
64 }
65 p->PrevSuccess = 0;
66 i = p->MinContext->NumStats;
67 do
68 {
69 if ((hiCnt += (++s)->Freq) > count)
70 {
71 Byte symbol;
72 RangeDec_Decode(p, hiCnt - s->Freq, s->Freq);
73 p->FoundState = s;
74 symbol = s->Symbol;
75 Ppmd8_Update1(p);
76 return symbol;
77 }
78 }
79 while (--i);
80 if (count >= p->MinContext->SummFreq)
81 return -2;
82 RangeDec_Decode(p, hiCnt, p->MinContext->SummFreq - hiCnt);
83 PPMD_SetAllBitsIn256Bytes(charMask);
84 MASK(s->Symbol) = 0;
85 i = p->MinContext->NumStats;
86 do { MASK((--s)->Symbol) = 0; } while (--i);
87 }
88 else
89 {
90 UInt16 *prob = Ppmd8_GetBinSumm(p);
91 if (((p->Code / (p->Range >>= 14)) < *prob))
92 {
93 Byte symbol;
94 RangeDec_Decode(p, 0, *prob);
95 *prob = (UInt16)PPMD_UPDATE_PROB_0(*prob);
96 symbol = (p->FoundState = Ppmd8Context_OneState(p->MinContext))->Symbol;
97 Ppmd8_UpdateBin(p);
98 return symbol;
99 }
100 RangeDec_Decode(p, *prob, (1 << 14) - *prob);
101 *prob = (UInt16)PPMD_UPDATE_PROB_1(*prob);
102 p->InitEsc = PPMD8_kExpEscape[*prob >> 10];
103 PPMD_SetAllBitsIn256Bytes(charMask);
104 MASK(Ppmd8Context_OneState(p->MinContext)->Symbol) = 0;
105 p->PrevSuccess = 0;
106 }
107 for (;;)
108 {
109 CPpmd_State *ps[256], *s;
110 UInt32 freqSum, count, hiCnt;
111 CPpmd_See *see;
112 unsigned i, num, numMasked = p->MinContext->NumStats;
113 do
114 {
115 p->OrderFall++;
116 if (!p->MinContext->Suffix)
117 return -1;
118 p->MinContext = Ppmd8_GetContext(p, p->MinContext->Suffix);
119 }
120 while (p->MinContext->NumStats == numMasked);
121 hiCnt = 0;
122 s = Ppmd8_GetStats(p, p->MinContext);
123 i = 0;
124 num = p->MinContext->NumStats - numMasked;
125 do
126 {
127 int k = (int)(MASK(s->Symbol));
128 hiCnt += (s->Freq & k);
129 ps[i] = s++;
130 i -= k;
131 }
132 while (i != num);
133
134 see = Ppmd8_MakeEscFreq(p, numMasked, &freqSum);
135 freqSum += hiCnt;
136 count = RangeDec_GetThreshold(p, freqSum);
137
138 if (count < hiCnt)
139 {
140 Byte symbol;
141 CPpmd_State **pps = ps;
142 for (hiCnt = 0; (hiCnt += (*pps)->Freq) <= count; pps++);
143 s = *pps;
144 RangeDec_Decode(p, hiCnt - s->Freq, s->Freq);
145 Ppmd_See_Update(see);
146 p->FoundState = s;
147 symbol = s->Symbol;
148 Ppmd8_Update2(p);
149 return symbol;
150 }
151 if (count >= freqSum)
152 return -2;
153 RangeDec_Decode(p, hiCnt, freqSum - hiCnt);
154 see->Summ = (UInt16)(see->Summ + freqSum);
155 do { MASK(ps[--i]->Symbol) = 0; } while (i != 0);
156 }
157 }
158