-
Notifications
You must be signed in to change notification settings - Fork 607
/
internal_zip.c
372 lines (321 loc) · 10.2 KB
/
internal_zip.c
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
/*
** SPDX-License-Identifier: BSD-3-Clause
** Copyright Contributors to the OpenEXR Project.
*/
#include "internal_compress.h"
#include "internal_decompress.h"
#include "internal_coding.h"
#include "internal_structs.h"
#include <limits.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <zlib.h>
#if defined __SSE2__ || (_MSC_VER >= 1300 && (_M_IX86 || _M_X64))
# define IMF_HAVE_SSE2 1
# include <emmintrin.h>
# include <mmintrin.h>
#endif
#if defined __SSE4_1__
# define IMF_HAVE_SSE4_1 1
# include <smmintrin.h>
#endif
#if defined(__ARM_NEON)
# define IMF_HAVE_NEON 1
# include <arm_neon.h>
#endif
/**************************************/
#ifdef IMF_HAVE_SSE4_1
static void
reconstruct (uint8_t* buf, uint64_t outSize)
{
static const uint64_t bytesPerChunk = sizeof (__m128i);
const uint64_t vOutSize = outSize / bytesPerChunk;
const __m128i c = _mm_set1_epi8 (-128);
const __m128i shuffleMask = _mm_set1_epi8 (15);
__m128i * vBuf, vPrev;
uint8_t prev;
/*
* 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;
vBuf = (__m128i*) buf;
vPrev = _mm_setzero_si128 ();
for (uint64_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);
}
prev = _mm_extract_epi8 (vPrev, 15);
for (uint64_t i = vOutSize * bytesPerChunk; i < outSize; ++i)
{
uint8_t d = prev + buf[i] - 128;
buf[i] = d;
prev = d;
}
}
#elif defined(IMF_HAVE_NEON)
static void
reconstruct (uint8_t* buf, uint64_t outSize)
{
static const uint64_t bytesPerChunk = sizeof (uint8x16_t);
const uint64_t vOutSize = outSize / bytesPerChunk;
const uint8x16_t c = vdupq_n_u8 (-128);
const uint8x16_t shuffleMask = vdupq_n_u8 (15);
const uint8x16_t zero = vdupq_n_u8 (0);
uint8_t * vBuf;
uint8x16_t vPrev;
uint8_t prev;
/*
* 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;
vBuf = buf;
vPrev = vdupq_n_u8 (0);
for (uint64_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 (zero, d, 16 - 1));
d = vaddq_u8 (d, vextq_u8 (zero, d, 16 - 2));
d = vaddq_u8 (d, vextq_u8 (zero, d, 16 - 4));
d = vaddq_u8 (d, vextq_u8 (zero, 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);
}
prev = vgetq_lane_u8 (vPrev, 15);
for (uint64_t i = vOutSize * bytesPerChunk; i < outSize; ++i)
{
uint8_t d = prev + buf[i] - 128;
buf[i] = d;
prev = d;
}
}
#else
static void
reconstruct (uint8_t* buf, uint64_t sz)
{
uint8_t* t = buf + 1;
uint8_t* stop = buf + sz;
while (t < stop)
{
int d = (int) (t[-1]) + (int) (t[0]) - 128;
t[0] = (uint8_t) d;
++t;
}
}
#endif
/**************************************/
#ifdef IMF_HAVE_SSE2
static void
interleave (uint8_t* out, const uint8_t* source, uint64_t outSize)
{
static const uint64_t bytesPerChunk = 2 * sizeof (__m128i);
const uint64_t vOutSize = outSize / bytesPerChunk;
const __m128i* v1 = (const __m128i*) source;
const __m128i* v2 = (const __m128i*) (source + (outSize + 1) / 2);
__m128i* vOut = (__m128i*) out;
const uint8_t * t1, *t2;
uint8_t* sOut;
for (uint64_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);
}
t1 = (const uint8_t*) v1;
t2 = (const uint8_t*) v2;
sOut = (uint8_t*) vOut;
for (uint64_t i = vOutSize * bytesPerChunk; i < outSize; ++i)
*(sOut++) = (i % 2 == 0) ? *(t1++) : *(t2++);
}
#elif defined(IMF_HAVE_NEON)
static void
interleave (uint8_t* out, const uint8_t* source, uint64_t outSize)
{
static const uint64_t bytesPerChunk = 2 * sizeof (uint8x16_t);
const uint64_t vOutSize = outSize / bytesPerChunk;
const uint8_t* v1 = source;
const uint8_t* v2 = source + (outSize + 1) / 2;
for (uint64_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 (out, lo); out += sizeof (uint8x16_t);
vst1q_u8 (out, hi); out += sizeof (uint8x16_t);
}
for (uint64_t i = vOutSize * bytesPerChunk; i < outSize; ++i)
*(out++) = (i % 2 == 0) ? *(v1++) : *(v2++);
}
#else
static void
interleave (uint8_t* out, const uint8_t* source, uint64_t outSize)
{
const uint8_t* t1 = source;
const uint8_t* t2 = source + (outSize + 1) / 2;
uint8_t* s = out;
uint8_t* const stop = s + outSize;
while (true)
{
if (s < stop)
*(s++) = *(t1++);
else
break;
if (s < stop)
*(s++) = *(t2++);
else
break;
}
}
#endif
/**************************************/
static exr_result_t
undo_zip_impl (
const void* compressed_data,
uint64_t comp_buf_size,
void* uncompressed_data,
uint64_t uncompressed_size,
void* scratch_data,
uint64_t scratch_size)
{
uLong outSize = (uLong) scratch_size;
int rstat;
if (scratch_size < uncompressed_size) return EXR_ERR_INVALID_ARGUMENT;
rstat = uncompress (
(Bytef*) scratch_data,
&outSize,
(const Bytef*) compressed_data,
(uLong) comp_buf_size);
if (rstat == Z_OK)
{
if (outSize == uncompressed_size)
{
reconstruct (scratch_data, outSize);
interleave (uncompressed_data, scratch_data, outSize);
rstat = EXR_ERR_SUCCESS;
}
else
{
rstat = EXR_ERR_CORRUPT_CHUNK;
}
}
else
{
rstat = EXR_ERR_CORRUPT_CHUNK;
}
return (exr_result_t) rstat;
}
/**************************************/
exr_result_t
internal_exr_undo_zip (
exr_decode_pipeline_t* decode,
const void* compressed_data,
uint64_t comp_buf_size,
void* uncompressed_data,
uint64_t uncompressed_size)
{
exr_result_t rv;
uint64_t scratchbufsz = uncompressed_size;
if ( comp_buf_size > scratchbufsz )
scratchbufsz = comp_buf_size;
rv = internal_decode_alloc_buffer (
decode,
EXR_TRANSCODE_BUFFER_SCRATCH1,
&(decode->scratch_buffer_1),
&(decode->scratch_alloc_size_1),
scratchbufsz);
if (rv != EXR_ERR_SUCCESS) return rv;
return undo_zip_impl (
compressed_data,
comp_buf_size,
uncompressed_data,
uncompressed_size,
decode->scratch_buffer_1,
decode->scratch_alloc_size_1);
}
/**************************************/
static exr_result_t
apply_zip_impl (exr_encode_pipeline_t* encode)
{
uint8_t* t1 = encode->scratch_buffer_1;
uint8_t* t2 = t1 + (encode->packed_bytes + 1) / 2;
const uint8_t* raw = encode->packed_buffer;
const uint8_t* stop = raw + encode->packed_bytes;
int p, level;
uLong compbufsz = (uLong) encode->compressed_alloc_size;
exr_result_t rv = EXR_ERR_SUCCESS;
rv = exr_get_zip_compression_level (
encode->context, encode->part_index, &level);
if (rv != EXR_ERR_SUCCESS) return rv;
/* reorder */
while (raw < stop)
{
*(t1++) = *(raw++);
if (raw < stop) *(t2++) = *(raw++);
}
/* reorder */
t1 = encode->scratch_buffer_1;
t2 = t1 + encode->packed_bytes;
t1++;
p = (int) t1[-1];
while (t1 < t2)
{
int d = (int) (t1[0]) - p + (128 + 256);
p = (int) t1[0];
t1[0] = (uint8_t) d;
++t1;
}
if (Z_OK != compress2 (
(Bytef*) encode->compressed_buffer,
&compbufsz,
(const Bytef*) encode->scratch_buffer_1,
(uLong) encode->packed_bytes,
level))
{
return EXR_ERR_CORRUPT_CHUNK;
}
if (compbufsz > encode->packed_bytes)
{
memcpy (
encode->compressed_buffer,
encode->packed_buffer,
encode->packed_bytes);
compbufsz = encode->packed_bytes;
}
encode->compressed_bytes = compbufsz;
return EXR_ERR_SUCCESS;
}
exr_result_t
internal_exr_apply_zip (exr_encode_pipeline_t* encode)
{
exr_result_t rv;
rv = internal_encode_alloc_buffer (
encode,
EXR_TRANSCODE_BUFFER_SCRATCH1,
&(encode->scratch_buffer_1),
&(encode->scratch_alloc_size_1),
encode->packed_bytes);
if (rv != EXR_ERR_SUCCESS) return rv;
return apply_zip_impl (encode);
}