-
Notifications
You must be signed in to change notification settings - Fork 630
/
Copy pathImfZip.cpp
382 lines (306 loc) · 9.18 KB
/
ImfZip.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
//
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) Contributors to the OpenEXR Project.
//
#include "ImfZip.h"
#include "Iex.h"
#include "ImfCheckedArithmetic.h"
#include "ImfNamespace.h"
#include "ImfSimd.h"
#include "ImfSystemSpecific.h"
#include <openexr_compression.h>
OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_ENTER
Zip::Zip (size_t maxRawSize, int level)
: _maxRawSize (maxRawSize), _tmpBuffer (0), _zipLevel (level)
{
_tmpBuffer = new char[_maxRawSize];
}
Zip::Zip (size_t maxScanLineSize, size_t numScanLines, int level)
: _maxRawSize (0), _tmpBuffer (0), _zipLevel (level)
{
_maxRawSize = uiMult (maxScanLineSize, numScanLines);
_tmpBuffer = new char[_maxRawSize];
}
Zip::~Zip ()
{
if (_tmpBuffer) delete[] _tmpBuffer;
}
size_t
Zip::maxRawSize ()
{
return _maxRawSize;
}
size_t
Zip::maxCompressedSize ()
{
return exr_compress_max_buffer_size (_maxRawSize);
}
int
Zip::compress (const char* raw, int rawSize, char* compressed)
{
//
// Reorder the pixel data.
//
{
char* t1 = _tmpBuffer;
char* t2 = _tmpBuffer + (rawSize + 1) / 2;
const char* stop = raw + rawSize;
while (true)
{
if (raw < stop)
*(t1++) = *(raw++);
else
break;
if (raw < stop)
*(t2++) = *(raw++);
else
break;
}
}
//
// Predictor.
//
{
unsigned char* t = (unsigned char*) _tmpBuffer + 1;
unsigned char* stop = (unsigned char*) _tmpBuffer + rawSize;
int p = t[-1];
while (t < stop)
{
int d = int (t[0]) - p + (128 + 256);
p = t[0];
t[0] = d;
++t;
}
}
//
// Compress the data using zlib
//
size_t outSize;
if (EXR_ERR_SUCCESS != exr_compress_buffer (
nullptr,
_zipLevel,
_tmpBuffer,
rawSize,
compressed,
maxCompressedSize (),
&outSize))
{
throw IEX_NAMESPACE::BaseExc ("Data compression failed.");
}
return outSize;
}
namespace
{
#ifdef IMF_HAVE_SSE4_1
void
reconstruct_sse41 (char* buf, size_t outSize)
{
static const size_t bytesPerChunk = sizeof (__m128i);
const size_t vOutSize = outSize / bytesPerChunk;
const __m128i c = _mm_set1_epi8 (-128);
const __m128i shuffleMask = _mm_set1_epi8 (15);
// The first element doesn't have its high bit flipped during compression,
// so it must not be flipped here. To make the SIMD loop nice and
// uniform, we pre-flip the bit so that the loop will unflip it again.
buf[0] += -128;
__m128i* vBuf = reinterpret_cast<__m128i*> (buf);
__m128i vPrev = _mm_setzero_si128 ();
for (size_t i = 0; i < vOutSize; ++i)
{
__m128i d = _mm_add_epi8 (_mm_loadu_si128 (vBuf), c);
// Compute the prefix sum of elements.
d = _mm_add_epi8 (d, _mm_slli_si128 (d, 1));
d = _mm_add_epi8 (d, _mm_slli_si128 (d, 2));
d = _mm_add_epi8 (d, _mm_slli_si128 (d, 4));
d = _mm_add_epi8 (d, _mm_slli_si128 (d, 8));
d = _mm_add_epi8 (d, vPrev);
_mm_storeu_si128 (vBuf++, d);
// Broadcast the high byte in our result to all lanes of the prev
// value for the next iteration.
vPrev = _mm_shuffle_epi8 (d, shuffleMask);
}
unsigned char prev = _mm_extract_epi8 (vPrev, 15);
for (size_t i = vOutSize * bytesPerChunk; i < outSize; ++i)
{
unsigned char d = prev + buf[i] - 128;
buf[i] = d;
prev = d;
}
}
#endif
#ifdef IMF_HAVE_NEON_AARCH64
void
reconstruct_neon (char* buf, size_t outSize)
{
static const size_t bytesPerChunk = sizeof (uint8x16_t);
const size_t vOutSize = outSize / bytesPerChunk;
const uint8x16_t c = vdupq_n_u8 (-128);
const uint8x16_t shuffleMask = vdupq_n_u8 (15);
// The first element doesn't have its high bit flipped during compression,
// so it must not be flipped here. To make the SIMD loop nice and
// uniform, we pre-flip the bit so that the loop will unflip it again.
buf[0] += -128;
unsigned char* vBuf = reinterpret_cast<unsigned char*> (buf);
uint8x16_t vZero = vdupq_n_u8 (0);
uint8x16_t vPrev = vdupq_n_u8 (0);
for (size_t i = 0; i < vOutSize; ++i)
{
uint8x16_t d = vaddq_u8 (vld1q_u8 (vBuf), c);
// Compute the prefix sum of elements.
d = vaddq_u8 (d, vextq_u8 (vZero, d, 16 - 1));
d = vaddq_u8 (d, vextq_u8 (vZero, d, 16 - 2));
d = vaddq_u8 (d, vextq_u8 (vZero, d, 16 - 4));
d = vaddq_u8 (d, vextq_u8 (vZero, d, 16 - 8));
d = vaddq_u8 (d, vPrev);
vst1q_u8 (vBuf, d);
vBuf += sizeof (uint8x16_t);
// Broadcast the high byte in our result to all lanes of the prev
// value for the next iteration.
vPrev = vqtbl1q_u8 (d, shuffleMask);
}
unsigned char prev = vgetq_lane_u8 (vPrev, 15);
for (size_t i = vOutSize * bytesPerChunk; i < outSize; ++i)
{
unsigned char d = prev + buf[i] - 128;
buf[i] = d;
prev = d;
}
}
#endif
void
reconstruct_scalar (char* buf, size_t outSize)
{
unsigned char* t = (unsigned char*) buf + 1;
unsigned char* stop = (unsigned char*) buf + outSize;
while (t < stop)
{
int d = int (t[-1]) + int (t[0]) - 128;
t[0] = d;
++t;
}
}
#ifdef IMF_HAVE_SSE2
void
interleave_sse2 (const char* source, size_t outSize, char* out)
{
static const size_t bytesPerChunk = 2 * sizeof (__m128i);
const size_t vOutSize = outSize / bytesPerChunk;
const __m128i* v1 = reinterpret_cast<const __m128i*> (source);
const __m128i* v2 =
reinterpret_cast<const __m128i*> (source + (outSize + 1) / 2);
__m128i* vOut = reinterpret_cast<__m128i*> (out);
for (size_t i = 0; i < vOutSize; ++i)
{
__m128i a = _mm_loadu_si128 (v1++);
__m128i b = _mm_loadu_si128 (v2++);
__m128i lo = _mm_unpacklo_epi8 (a, b);
__m128i hi = _mm_unpackhi_epi8 (a, b);
_mm_storeu_si128 (vOut++, lo);
_mm_storeu_si128 (vOut++, hi);
}
const char* t1 = reinterpret_cast<const char*> (v1);
const char* t2 = reinterpret_cast<const char*> (v2);
char* sOut = reinterpret_cast<char*> (vOut);
for (size_t i = vOutSize * bytesPerChunk; i < outSize; ++i)
{
*(sOut++) = (i % 2 == 0) ? *(t1++) : *(t2++);
}
}
#endif
#ifdef IMF_HAVE_NEON_AARCH64
void
interleave_neon (const char* source, size_t outSize, char* out)
{
static const size_t bytesPerChunk = 2 * sizeof (uint8x16_t);
const size_t vOutSize = outSize / bytesPerChunk;
const unsigned char* v1 = reinterpret_cast<const unsigned char*> (source);
const unsigned char* v2 =
reinterpret_cast<const unsigned char*> (source + (outSize + 1) / 2);
unsigned char* vOut = reinterpret_cast<unsigned char*> (out);
for (size_t i = 0; i < vOutSize; ++i)
{
uint8x16_t a = vld1q_u8 (v1); v1 += sizeof (uint8x16_t);
uint8x16_t b = vld1q_u8 (v2); v2 += sizeof (uint8x16_t);
uint8x16_t lo = vzip1q_u8 (a, b);
uint8x16_t hi = vzip2q_u8 (a, b);
vst1q_u8 (vOut, lo); vOut += sizeof (uint8x16_t);
vst1q_u8 (vOut, hi); vOut += sizeof (uint8x16_t);
}
const char* t1 = reinterpret_cast<const char*> (v1);
const char* t2 = reinterpret_cast<const char*> (v2);
char* sOut = reinterpret_cast<char*> (vOut);
for (size_t i = vOutSize * bytesPerChunk; i < outSize; ++i)
{
*(sOut++) = (i % 2 == 0) ? *(t1++) : *(t2++);
}
}
#endif
void
interleave_scalar (const char* source, size_t outSize, char* out)
{
const char* t1 = source;
const char* t2 = source + (outSize + 1) / 2;
char* s = out;
char* const stop = s + outSize;
while (true)
{
if (s < stop)
*(s++) = *(t1++);
else
break;
if (s < stop)
*(s++) = *(t2++);
else
break;
}
}
auto reconstruct = reconstruct_scalar;
auto interleave = interleave_scalar;
} // namespace
int
Zip::uncompress (const char* compressed, int compressedSize, char* raw)
{
size_t outSize = 0;
if (EXR_ERR_SUCCESS != exr_uncompress_buffer (
nullptr,
compressed,
(size_t)compressedSize,
_tmpBuffer,
_maxRawSize,
&outSize))
{
throw IEX_NAMESPACE::InputExc ("Data decompression failed.");
}
if (outSize == 0) { return static_cast<int> (outSize); }
//
// Predictor.
//
reconstruct (_tmpBuffer, outSize);
//
// Reorder the pixel data.
//
interleave (_tmpBuffer, outSize, raw);
return outSize;
}
void
Zip::initializeFuncs ()
{
CpuId cpuId;
#ifdef IMF_HAVE_SSE4_1
if (cpuId.sse4_1)
{
reconstruct = reconstruct_sse41;
}
#endif
#ifdef IMF_HAVE_SSE2
if (cpuId.sse2)
{
interleave = interleave_sse2;
}
#endif
#ifdef IMF_HAVE_NEON_AARCH64
reconstruct = reconstruct_neon;
interleave = interleave_neon;
#endif
}
OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_EXIT