forked from farbrausch/fr_public
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrangecoder.cpp
More file actions
232 lines (188 loc) · 4.3 KB
/
Copy pathrangecoder.cpp
File metadata and controls
232 lines (188 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Written by Fabian "ryg" Giesen.
// I hereby place this code in the public domain.
#include "_types.hpp"
#include "rangecoder.hpp"
#include "debuginfo.hpp"
#include <cmath>
// ---- helpers
extern "C" void __fastcall modelInitASM();
extern "C" sInt __fastcall modelASM(sInt bit);
static sU32 sMulShift12(sU32 a,sU32 b)
{
__asm
{
mov eax, [a];
mul [b];
shrd eax, edx, 12;
}
}
// ---- encoder
extern "C" sU8 *bufPtr,*startBufPtr;
sU8 *bufPtr,*startBufPtr;
static sU8 *outPtr;
static sInt ffNum,cache;
static sU64 low;
static sU32 range;
static sBool firstByte;
static sU32 byteCount;
static void shiftLow()
{
sU32 carry = sU32(low >> 32);
if(low < 0xff000000 || carry == 1)
{
if(!firstByte)
*outPtr++ = cache + carry;
else
firstByte = false;
for(;ffNum;ffNum--)
*outPtr++ = 0xff + carry;
cache = sInt((low >> 24) & 0xff);
}
else
ffNum++;
low = (low << 8) & 0xffffffff;
byteCount++;
}
static void codeBit(sU32 prob,sInt bit)
{
// adjust bound
sU32 newBound = sMulShift12(range,prob);
if(bit)
range = newBound;
else
{
low += newBound;
range -= newBound;
}
// renormalize
while(range < 0x01000000)
{
range <<= 8;
shiftLow();
}
}
sU32 RangecoderPack(const sU8 *in,sU32 inSize,sU8 *out,PackerCallback cb,DebugInfo* info)
{
sU32 prob = 2048;
sU32 zeroProb = 1;
outPtr = out;
ffNum = 0;
low = 0;
range = ~0U;
cache = 0;
firstByte = true;
modelInitASM();
bufPtr = startBufPtr = (sU8 *) in;
sF64 lastSize = 0.0;
sInt curSymbol = 0;
for(sInt pos=0;pos<inSize;pos++)
{
// write zero tag bit every 8k
if((pos & 8191) == 0)
{
// check whether we want to execute the callback first
if(cb)
{
__asm emms;
cb(pos,inSize,sU32(outPtr - out));
}
// >8k still left?
sInt isZero = (inSize - pos) > 8192;
// check whether it's really all zeroes
if(isZero)
{
for(sInt i=0;i<8192;i++)
{
if(bufPtr[i] != 0)
{
isZero = 0;
break;
}
}
}
codeBit(zeroProb,isZero);
zeroProb = (zeroProb + (isZero ? 4096 : 1)) >> 1;
if(isZero)
{
bufPtr += 8192;
pos += 8192-1;
continue;
}
}
for(sInt i=0;i<8;i++)
{
sInt bit = (*bufPtr >> (7-i)) & 1;
codeBit(prob,bit);
// update model
if(i == 7)
bufPtr++;
prob = modelASM(bit);
}
// update sizes
double fracSize = -log(sF64(range)/sF64(0xFFFFFFFF))/log(2.0)/8.0;
double curSize = byteCount + fracSize;
double byteSize = curSize - lastSize;
if (pos < (info->Symbols[curSymbol].sourcePos+info->Symbols[curSymbol].Size)) // we don't count padding for symbols
info->Symbols[curSymbol].PackedSize += byteSize;
info->Files[info->Symbols[curSymbol].FileNum].PackedSize += byteSize;
info->Files[info->Symbols[curSymbol].FileNum].Size += 1;
info->NameSps[info->Symbols[curSymbol].NameSpNum].PackedSize += byteSize;
info->NameSps[info->Symbols[curSymbol].NameSpNum].Size += 1;
lastSize = curSize;
// move to next symbol if applicable
if (curSymbol < info->Symbols.Count-1 && pos>=info->Symbols[curSymbol+1].sourcePos)
curSymbol++;
}
for(sInt i=0;i<5;i++) // these bytes are not counted correctly
shiftLow();
__asm emms;
sU32 finalSize = sU32(outPtr - out);
if(cb)
cb(inSize,inSize,finalSize);
return finalSize;
}
// ---- decoder
sU32 RangecoderDepack(sU8 *out,const sU8 *in,sInt outSize)
{
// initialize ari
sU32 code=0,range=~0U;
sU32 prob=2048;
for(sInt i=0;i<4;i++)
code = (code << 8) | *in++;
modelInitASM();
bufPtr = startBufPtr = out;
while(outSize)
{
sInt byte = 0;
for(sInt i=0;i<8;i++)
{
sInt bit;
sU32 bound = sMulShift12(range,prob);
// decode bit
if(code < bound)
{
range = bound;
bit = 1;
}
else
{
code -= bound;
range -= bound;
bit = 0;
}
byte += byte+bit;
if(i == 7)
*bufPtr++ = byte;
// renormalize
while(range < 0x01000000)
{
code = (code << 8) | *in++;
range <<= 8;
}
// update model
prob = modelASM(bit);
}
outSize--;
}
__asm emms;
}