This repository has been archived by the owner on Sep 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compact.cpp
327 lines (277 loc) · 7.05 KB
/
compact.cpp
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <limits.h>
#include <mmintrin.h>
#include <emmintrin.h>
#include <omp.h>
#include "yeti.h"
#include "compact.h"
#include "zerorle.h"
#include "golomb.h"
void InterframeDecode(BYTE* __restrict dest, const BYTE* __restrict src1, const BYTE* __restrict src2, const size_t len)
{
__m128i* mxSrc1 = (__m128i*) src1;
__m128i* mxSrc2 = (__m128i*) src2;
__m128i* mxDest = (__m128i*) dest;
const int end = len / 16;
#pragma omp parallel for
for(int i = 0; i < end; i++)
{
mxDest[i] = _mm_add_epi8(mxSrc1[i], mxSrc2[i]);
}
}
bool InterframeEncode(BYTE* __restrict dest, const BYTE* __restrict src1, const BYTE* __restrict src2, const size_t len, const unsigned __int64 minDelta)
{
unsigned __int64 sad = 0;
__m128i* mxDest = (__m128i*) dest;
__m128i* mxSrc1 = (__m128i*) src1;
__m128i* mxSrc2 = (__m128i*) src2;
const int end = len / 16;
#pragma omp parallel for reduction(+: sad)
for(int i = 0; i < end; i++)
{
__m128i mx1 = _mm_load_si128(mxSrc1 + i);
__m128i mx2 = _mm_load_si128(mxSrc2 + i);
__m128i mxs = _mm_sad_epu8(mx1, mx2);
sad += _mm_extract_epi16(mxs, 0) + _mm_extract_epi16(mxs, 4);
mxDest[i] = _mm_sub_epi8(mx1, mx2);
}
return sad < minDelta;
}
// scale the byte probabilities so the cumulative
// probability is equal to a power of 2
void CompressClass::ScaleBitProbability(size_t length)
{
assert(length > 0);
assert(length < 0x80000000);
unsigned int temp = 1;
while(temp < length)
{
temp <<= 1;
}
if ( temp != length )
{
const double factor = (int)temp / ((double)(int)length);
unsigned int newlen = 0;
for(int i = 1; i < 257; i++)
{
m_probRanges[i] = (unsigned int)(((int)m_probRanges[i]) * factor);
newlen += m_probRanges[i];
}
newlen = temp - newlen;
assert(newlen < 0x80000000);
unsigned int b = 0;
while(newlen)
{
if(m_probRanges[b+1])
{
m_probRanges[b+1]++;
--newlen;
}
++b;
b &= 0x7f;
}
}
unsigned int a = 0;
for(a = 0; temp; a++)
{
temp >>= 1;
}
m_scale = a - 1;
for(a = 1; a < 257; a++)
{
m_probRanges[a] += m_probRanges[a-1];
}
}
// read the byte frequency header
size_t CompressClass::ReadBitProbability(const BYTE* in)
{
size_t length = 0;
ZeroMemory(m_probRanges, sizeof(m_probRanges));
const unsigned int skip = GolombDecode(in, &m_probRanges[1], 256);
assert(skip);
for(unsigned int a = 1; a < 257; a++)
{
length += m_probRanges[a];
}
assert(length);
ScaleBitProbability(length);
return skip;
}
// Determine the frequency of each byte in a byte stream; the frequencies are then scaled
// so the total is a power of 2. This allows binary shifts to be used instead of some
// multiply and divides in the compression/decompression routines
size_t CompressClass::CalcBitProbability(const BYTE* const in, size_t length, BYTE* out)
{
unsigned int table2[256];
ZeroMemory(m_probRanges, 257 * sizeof(unsigned int));
ZeroMemory(table2, sizeof(table2));
unsigned int a=0;
for (a = 0; a < (length&(~1)); a+=2)
{
m_probRanges[in[a]+1]++;
table2[in[a+1]]++;
}
if ( a < length )
{
m_probRanges[in[a]+1]++;
}
for (a = 0; a < 256; a++)
{
m_probRanges[a+1] += table2[a];
}
// Clamp prob_ranges total to 1<<19 to ensure the range coder has enough precision to work with;
// Larger totals reduce compression due to the reduced precision of the range variable, and
// totals larger than 1<<22 can crash the range coder. Lower clamp values reduce the decoder speed
// slightly on affected video.
const int clamp_size = 1<<19;
if ( out && length > clamp_size )
{
double factor = clamp_size;
factor /= length;
double temp[256];
for (int a = 0; a < 256; a++)
{
temp[a] = ((int)m_probRanges[a+1]) * factor;
}
unsigned int total=0;
for (int a = 0; a < 256; a++)
{
int scaled_val = (int)temp[a];
scaled_val += (scaled_val == 0 && m_probRanges[a+1]);
total += scaled_val;
m_probRanges[a+1] = scaled_val;
}
int adjust = total < clamp_size? 1 : -1;
int a = 0;
while (total != clamp_size)
{
if (m_probRanges[a+1] > 1)
{
m_probRanges[a+1] += adjust;
total += adjust;
}
++a;
a&=255;
}
length = clamp_size;
}
size_t size = GolombEncode(&m_probRanges[1], out, 256);
ScaleBitProbability(length);
return size;
}
size_t CompressClass::Compact(BYTE* in, BYTE* out, const size_t length)
{
size_t bytes_used = 0;
BYTE* const buffer_1 = m_buffer;
BYTE* const buffer_2 = m_buffer + ALIGN_ROUND(length + HALF(length) + 16, 16);
short rle = 0;
size_t size = TestAndRLE(in, buffer_1, buffer_2, length, rle);
out[0] = rle;
if ( rle )
{
if(rle == -1)
{ // solid run of 0s, only 1st byte is needed
out[0] = 0xFF;
out[1] = in[0];
bytes_used = 2;
}
else
{
BYTE* b2 = ( rle == 1 ) ? buffer_1 : buffer_2;
*(UINT32*)(out+1)=size;
size_t skip = CalcBitProbability(b2, size, out + 5);
skip += RangeEncode(b2, out + 5 + skip, size) + 5;
if ( size < skip ) // RLE size is less than range compressed size
{
out[0]+=4;
memcpy(out+1,b2,size);
skip=size+1;
}
bytes_used = skip;
}
}
else
{
size_t skip = CalcBitProbability(in, length, out + 1);
skip += RangeEncode(in, out + skip + 1, length) +1;
bytes_used=skip;
}
assert(bytes_used >= 2);
assert(rle <=3 && rle >= -1);
assert(out[0] == rle || rle == -1 || out[0]== rle + 4 );
return bytes_used;
}
void CompressClass::Uncompact(const BYTE* in, BYTE* out, const size_t length)
{
#ifdef _DEBUG
try
{
#endif
BYTE rle = in[0];
if(rle && (rle < 8 || rle == 0xFF))
{
if(rle < 4)
{
size_t skip = ReadBitProbability(in + 5);
if(!skip)
{
return;
}
Decode_And_DeRLE(in + 4 + skip + 1, out, length, in[0]);
}
else
{
if(rle == 0xFF) // solid run of 0s, only need to set 1 byte
{
//MessageBox (HWND_DESKTOP, "ZerosOut", "Info", MB_OK);
ZeroMemory(out, length);
out[0] = in[1];
}
else
{
rle -= 4;
if (rle)
deRLE(in+1, out, length, rle);
else // uncompressed length is smallest...
memcpy((void*)(in+1), out, length);
}
}
}
else
{
assert(*(int*)(in+1));
size_t skip = ReadBitProbability(in + 1);
assert(skip);
Decode_And_DeRLE(in + skip + 1, out, length, 0);
}
#ifdef _DEBUG
}
catch(...)
{
MessageBox(HWND_DESKTOP, "Uncompact Failed", "Error", MB_OK | MB_ICONEXCLAMATION);
}
#endif
}
bool CompressClass::InitCompressBuffers(const size_t length)
{
m_buffer = (BYTE*)ALIGNED_MALLOC(m_buffer, length + HALF(length) + length * 5/4 + 32, 8, "Compress::buffer");
if (!m_buffer)
{
FreeCompressBuffers();
return false;
}
return true;
}
void CompressClass::FreeCompressBuffers()
{
ALIGNED_FREE( m_buffer,"Compress::buffer");
}
CompressClass::CompressClass()
{
m_buffer = NULL;
}
CompressClass::~CompressClass()
{
FreeCompressBuffers();
}