1 /* Ppmd7Enc.c -- PPMdH Encoder
2 2010-03-12 : Igor Pavlov : Public domain
3 This code is based on PPMd var.H (2001): Dmitry Shkarin : Public domain */
4
5 #include "Ppmd7.h"
6
7 #define kTopValue (1 << 24)
8
Ppmd7z_RangeEnc_Init(CPpmd7z_RangeEnc * p)9 void Ppmd7z_RangeEnc_Init(CPpmd7z_RangeEnc *p)
10 {
11 p->Low = 0;
12 p->Range = 0xFFFFFFFF;
13 p->Cache = 0;
14 p->CacheSize = 1;
15 }
16
RangeEnc_ShiftLow(CPpmd7z_RangeEnc * p)17 static void RangeEnc_ShiftLow(CPpmd7z_RangeEnc *p)
18 {
19 if ((UInt32)p->Low < (UInt32)0xFF000000 || (unsigned)(p->Low >> 32) != 0)
20 {
21 Byte temp = p->Cache;
22 do
23 {
24 p->Stream->Write(p->Stream, (Byte)(temp + (Byte)(p->Low >> 32)));
25 temp = 0xFF;
26 }
27 while(--p->CacheSize != 0);
28 p->Cache = (Byte)((UInt32)p->Low >> 24);
29 }
30 p->CacheSize++;
31 p->Low = (UInt32)p->Low << 8;
32 }
33
RangeEnc_Encode(CPpmd7z_RangeEnc * p,UInt32 start,UInt32 size,UInt32 total)34 static void RangeEnc_Encode(CPpmd7z_RangeEnc *p, UInt32 start, UInt32 size, UInt32 total)
35 {
36 p->Low += start * (p->Range /= total);
37 p->Range *= size;
38 while (p->Range < kTopValue)
39 {
40 p->Range <<= 8;
41 RangeEnc_ShiftLow(p);
42 }
43 }
44
RangeEnc_EncodeBit_0(CPpmd7z_RangeEnc * p,UInt32 size0)45 static void RangeEnc_EncodeBit_0(CPpmd7z_RangeEnc *p, UInt32 size0)
46 {
47 p->Range = (p->Range >> 14) * size0;
48 while (p->Range < kTopValue)
49 {
50 p->Range <<= 8;
51 RangeEnc_ShiftLow(p);
52 }
53 }
54
RangeEnc_EncodeBit_1(CPpmd7z_RangeEnc * p,UInt32 size0)55 static void RangeEnc_EncodeBit_1(CPpmd7z_RangeEnc *p, UInt32 size0)
56 {
57 UInt32 newBound = (p->Range >> 14) * size0;
58 p->Low += newBound;
59 p->Range -= newBound;
60 while (p->Range < kTopValue)
61 {
62 p->Range <<= 8;
63 RangeEnc_ShiftLow(p);
64 }
65 }
66
Ppmd7z_RangeEnc_FlushData(CPpmd7z_RangeEnc * p)67 void Ppmd7z_RangeEnc_FlushData(CPpmd7z_RangeEnc *p)
68 {
69 unsigned i;
70 for (i = 0; i < 5; i++)
71 RangeEnc_ShiftLow(p);
72 }
73
74
75 #define MASK(sym) ((signed char *)charMask)[sym]
76
Ppmd7_EncodeSymbol(CPpmd7 * p,CPpmd7z_RangeEnc * rc,int symbol)77 void Ppmd7_EncodeSymbol(CPpmd7 *p, CPpmd7z_RangeEnc *rc, int symbol)
78 {
79 size_t charMask[256 / sizeof(size_t)];
80 if (p->MinContext->NumStats != 1)
81 {
82 CPpmd_State *s = Ppmd7_GetStats(p, p->MinContext);
83 UInt32 sum;
84 unsigned i;
85 if (s->Symbol == symbol)
86 {
87 RangeEnc_Encode(rc, 0, s->Freq, p->MinContext->SummFreq);
88 p->FoundState = s;
89 Ppmd7_Update1_0(p);
90 return;
91 }
92 p->PrevSuccess = 0;
93 sum = s->Freq;
94 i = p->MinContext->NumStats - 1;
95 do
96 {
97 if ((++s)->Symbol == symbol)
98 {
99 RangeEnc_Encode(rc, sum, s->Freq, p->MinContext->SummFreq);
100 p->FoundState = s;
101 Ppmd7_Update1(p);
102 return;
103 }
104 sum += s->Freq;
105 }
106 while (--i);
107
108 p->HiBitsFlag = p->HB2Flag[p->FoundState->Symbol];
109 PPMD_SetAllBitsIn256Bytes(charMask);
110 MASK(s->Symbol) = 0;
111 i = p->MinContext->NumStats - 1;
112 do { MASK((--s)->Symbol) = 0; } while (--i);
113 RangeEnc_Encode(rc, sum, p->MinContext->SummFreq - sum, p->MinContext->SummFreq);
114 }
115 else
116 {
117 UInt16 *prob = Ppmd7_GetBinSumm(p);
118 CPpmd_State *s = Ppmd7Context_OneState(p->MinContext);
119 if (s->Symbol == symbol)
120 {
121 RangeEnc_EncodeBit_0(rc, *prob);
122 *prob = (UInt16)PPMD_UPDATE_PROB_0(*prob);
123 p->FoundState = s;
124 Ppmd7_UpdateBin(p);
125 return;
126 }
127 else
128 {
129 RangeEnc_EncodeBit_1(rc, *prob);
130 *prob = (UInt16)PPMD_UPDATE_PROB_1(*prob);
131 p->InitEsc = PPMD7_kExpEscape[*prob >> 10];
132 PPMD_SetAllBitsIn256Bytes(charMask);
133 MASK(s->Symbol) = 0;
134 p->PrevSuccess = 0;
135 }
136 }
137 for (;;)
138 {
139 UInt32 escFreq;
140 CPpmd_See *see;
141 CPpmd_State *s;
142 UInt32 sum;
143 unsigned i, numMasked = p->MinContext->NumStats;
144 do
145 {
146 p->OrderFall++;
147 if (!p->MinContext->Suffix)
148 return; /* EndMarker (symbol = -1) */
149 p->MinContext = Ppmd7_GetContext(p, p->MinContext->Suffix);
150 }
151 while (p->MinContext->NumStats == numMasked);
152
153 see = Ppmd7_MakeEscFreq(p, numMasked, &escFreq);
154 s = Ppmd7_GetStats(p, p->MinContext);
155 sum = 0;
156 i = p->MinContext->NumStats;
157 do
158 {
159 int cur = s->Symbol;
160 if (cur == symbol)
161 {
162 UInt32 low = sum;
163 CPpmd_State *s1 = s;
164 do
165 {
166 sum += (s->Freq & (int)(MASK(s->Symbol)));
167 s++;
168 }
169 while (--i);
170 RangeEnc_Encode(rc, low, s1->Freq, sum + escFreq);
171 Ppmd_See_Update(see);
172 p->FoundState = s1;
173 Ppmd7_Update2(p);
174 return;
175 }
176 sum += (s->Freq & (int)(MASK(cur)));
177 MASK(cur) = 0;
178 s++;
179 }
180 while (--i);
181
182 RangeEnc_Encode(rc, sum, escFreq, sum + escFreq);
183 see->Summ = (UInt16)(see->Summ + sum + escFreq);
184 }
185 }
186