-
Notifications
You must be signed in to change notification settings - Fork 693
Expand file tree
/
Copy pathcompression.c
More file actions
567 lines (494 loc) · 17.6 KB
/
Copy pathcompression.c
File metadata and controls
567 lines (494 loc) · 17.6 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
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/*
** SPDX-License-Identifier: BSD-3-Clause
** Copyright Contributors to the OpenEXR Project.
*/
#include "openexr_compression.h"
#include "openexr_base.h"
#include "internal_memory.h"
#include "internal_structs.h"
#include "internal_compress.h"
#include "internal_decompress.h"
#include "internal_coding.h"
#include "internal_coding.h"
#include "internal_file.h"
#include "internal_huf.h"
#include "OpenEXRConfigInternal.h"
#if OPENEXR_USE_INTERNAL_DEFLATE
# include "../../../external/deflate/lib/lib_common.h"
# include "../../../external/deflate/lib/arm/cpu_features.c"
# include "../../../external/deflate/lib/x86/cpu_features.c"
# include "../../../external/deflate/lib/utils.c"
# include "../../../external/deflate/lib/deflate_compress.c"
# undef BITBUF_NBITS
# include "../../../external/deflate/lib/deflate_decompress.c"
# undef BITBUF_NBITS
# include "../../../external/deflate/lib/adler32.c"
# include "../../../external/deflate/lib/zlib_compress.c"
# include "../../../external/deflate/lib/zlib_decompress.c"
#else
# include <libdeflate.h>
#endif
#include <string.h>
#if ( \
LIBDEFLATE_VERSION_MAJOR > 1 || \
(LIBDEFLATE_VERSION_MAJOR == 1 && LIBDEFLATE_VERSION_MINOR > 18))
# define EXR_USE_CONFIG_DEFLATE_STRUCT 1
#endif
/* value Aras found to be better trade off of speed vs size */
#define EXR_DEFAULT_ZLIB_COMPRESS_LEVEL 4
/**************************************/
size_t
exr_compress_max_buffer_size (size_t in_bytes)
{
size_t r, extra;
r = libdeflate_zlib_compress_bound (NULL, in_bytes);
/*
* lib deflate has a message about needing a 9 byte boundary
* but is unclear if it actually adds that or not
* (see the comment on libdeflate_deflate_compress)
*/
if (r > (SIZE_MAX - 9)) return (size_t) (SIZE_MAX);
r += 9;
/*
* old library had uiAdd( uiAdd( in, ceil(in * 0.01) ), 100 )
*/
extra = (r * (size_t) 130);
if (extra < r) return (size_t) (SIZE_MAX);
extra /= (size_t) 128;
if (extra > (SIZE_MAX - 100)) return (size_t) (SIZE_MAX);
if (extra > r) r = extra;
/*
* in case huf is larger than zlib
*/
extra = in_bytes + internal_exr_huf_compress_spare_bytes ();
if (r < extra) r = extra;
extra = in_bytes + internal_exr_huf_decompress_spare_bytes ();
if (r < extra) r = extra;
/* make sure there is some small 2 pages worth of buffer */
if (8192 > r) r = 8192;
return r;
}
/**************************************/
exr_result_t
exr_compress_buffer (
exr_const_context_t ctxt,
int level,
const void* in,
size_t in_bytes,
void* out,
size_t out_bytes_avail,
size_t* actual_out)
{
struct libdeflate_compressor* comp;
#ifdef EXR_USE_CONFIG_DEFLATE_STRUCT
struct libdeflate_options opt = {
.sizeof_options = sizeof (struct libdeflate_options),
.malloc_func = ctxt ? ctxt->alloc_fn : internal_exr_alloc,
.free_func = ctxt ? ctxt->free_fn : internal_exr_free};
#else
libdeflate_set_memory_allocator (
ctxt ? ctxt->alloc_fn : internal_exr_alloc,
ctxt ? ctxt->free_fn : internal_exr_free);
#endif
if (level < 0)
{
exr_get_default_zip_compression_level (&level);
/* truly unset anywhere */
if (level < 0) level = EXR_DEFAULT_ZLIB_COMPRESS_LEVEL;
}
#ifdef EXR_USE_CONFIG_DEFLATE_STRUCT
comp = libdeflate_alloc_compressor_ex (level, &opt);
#else
comp = libdeflate_alloc_compressor (level);
#endif
if (comp)
{
size_t outsz;
outsz =
libdeflate_zlib_compress (comp, in, in_bytes, out, out_bytes_avail);
libdeflate_free_compressor (comp);
if (outsz != 0)
{
if (actual_out) *actual_out = outsz;
return EXR_ERR_SUCCESS;
}
return EXR_ERR_OUT_OF_MEMORY;
}
return EXR_ERR_OUT_OF_MEMORY;
}
/**************************************/
exr_result_t
exr_uncompress_buffer (
exr_const_context_t ctxt,
const void* in,
size_t in_bytes,
void* out,
size_t out_bytes_avail,
size_t* actual_out)
{
struct libdeflate_decompressor* decomp;
enum libdeflate_result res;
size_t actual_in_bytes;
#ifdef EXR_USE_CONFIG_DEFLATE_STRUCT
struct libdeflate_options opt = {
.sizeof_options = sizeof (struct libdeflate_options),
.malloc_func = ctxt ? ctxt->alloc_fn : internal_exr_alloc,
.free_func = ctxt ? ctxt->free_fn : internal_exr_free};
#endif
// if (in_bytes == out_bytes_avail)
// {
// if (actual_out) *actual_out = in_bytes;
// if (in != out)
// memcpy(out, in, in_bytes);
//
// return EXR_ERR_SUCCESS;
// }
#ifdef EXR_USE_CONFIG_DEFLATE_STRUCT
decomp = libdeflate_alloc_decompressor_ex (&opt);
#else
libdeflate_set_memory_allocator (
ctxt ? ctxt->alloc_fn : internal_exr_alloc,
ctxt ? ctxt->free_fn : internal_exr_free);
decomp = libdeflate_alloc_decompressor ();
#endif
if (decomp)
{
res = libdeflate_zlib_decompress_ex (
decomp,
in,
in_bytes,
out,
out_bytes_avail,
&actual_in_bytes,
actual_out);
libdeflate_free_decompressor (decomp);
if (res == LIBDEFLATE_SUCCESS)
{
if (in_bytes == actual_in_bytes) return EXR_ERR_SUCCESS;
/* it's an error to not consume the full buffer, right? */
}
else if (res == LIBDEFLATE_INSUFFICIENT_SPACE)
{
return EXR_ERR_OUT_OF_MEMORY;
}
else if (res == LIBDEFLATE_SHORT_OUTPUT)
{
/* Decompression succeeded; *actual_out is the byte count. This is
* not an error when out_bytes_avail exceeds the true uncompressed
* size (e.g. PXR24/ZIP use padded scratch buffers). Callers that
* need an exact payload size must compare *actual_out (see e.g.
* undo_pxr24_impl). */
return EXR_ERR_SUCCESS;
}
return EXR_ERR_CORRUPT_CHUNK;
}
return EXR_ERR_OUT_OF_MEMORY;
}
/**************************************/
/**************************************/
size_t
exr_rle_compress_buffer (size_t in_bytes, const void* in, void* out, size_t out_avail)
{
return internal_rle_compress (out, out_avail, in, in_bytes);
}
size_t
exr_rle_uncompress_buffer (size_t in_bytes, size_t max_len, const void* in, void* out)
{
return internal_rle_decompress (out, max_len, in, in_bytes);
}
/**************************************/
/**************************************/
int exr_compression_lines_per_chunk (exr_compression_t comptype)
{
int linePerChunk = -1;
switch (comptype)
{
case EXR_COMPRESSION_NONE:
case EXR_COMPRESSION_RLE:
case EXR_COMPRESSION_ZIPS: linePerChunk = 1; break;
case EXR_COMPRESSION_ZIP:
case EXR_COMPRESSION_PXR24: linePerChunk = 16; break;
case EXR_COMPRESSION_PIZ:
case EXR_COMPRESSION_B44:
case EXR_COMPRESSION_B44A:
case EXR_COMPRESSION_HTJ2K32:
case EXR_COMPRESSION_DWAA: linePerChunk = 32; break;
case EXR_COMPRESSION_DWAB: linePerChunk = 256; break;
case EXR_COMPRESSION_HTJ2K256: linePerChunk = 256; break;
case EXR_COMPRESSION_LAST_TYPE:
default:
/* ERROR CONDITION */
break;
}
return linePerChunk;
}
/**************************************/
exr_result_t
exr_compress_chunk (exr_encode_pipeline_t* encode)
{
exr_result_t rv;
exr_context_t ctxt;
exr_priv_part_t part;
size_t maxbytes;
if (!encode) return EXR_ERR_MISSING_CONTEXT_ARG;
ctxt = (exr_context_t) encode->context;
if (!ctxt) return EXR_ERR_MISSING_CONTEXT_ARG;
/* TODO: Double check need for a lock? */
if (encode->part_index < 0 || encode->part_index >= ctxt->num_parts)
return ctxt->print_error (
ctxt,
EXR_ERR_ARGUMENT_OUT_OF_RANGE,
"Part index (%d) out of range",
encode->part_index);
part = ctxt->parts[encode->part_index];
maxbytes = encode->chunk.unpacked_size;
if (encode->packed_bytes > maxbytes)
maxbytes = encode->packed_bytes;
rv = internal_encode_alloc_buffer (
encode,
EXR_TRANSCODE_BUFFER_COMPRESSED,
&(encode->compressed_buffer),
&(encode->compressed_alloc_size),
exr_compress_max_buffer_size (maxbytes));
if (rv != EXR_ERR_SUCCESS)
return ctxt->print_error (
ctxt,
rv,
"error allocating buffer %zu",
exr_compress_max_buffer_size (maxbytes));
//return rv;
if (encode->sample_count_table)
{
uint64_t sampsize =
(((uint64_t) encode->chunk.width) *
((uint64_t) encode->chunk.height));
sampsize *= sizeof (int32_t);
if (part->comp_type == EXR_COMPRESSION_NONE)
{
internal_encode_free_buffer (
encode,
EXR_TRANSCODE_BUFFER_PACKED_SAMPLES,
&(encode->packed_sample_count_table),
&(encode->packed_sample_count_alloc_size));
encode->packed_sample_count_table = encode->sample_count_table;
encode->packed_sample_count_alloc_size = 0;
encode->packed_sample_count_bytes = sampsize;
}
else
{
void *pb;
size_t pbb, pas;
pb = encode->packed_buffer;
pbb = encode->packed_bytes;
pas = encode->packed_alloc_size;
rv = internal_encode_alloc_buffer (
encode,
EXR_TRANSCODE_BUFFER_PACKED_SAMPLES,
&(encode->packed_sample_count_table),
&(encode->packed_sample_count_alloc_size),
exr_compress_max_buffer_size (sampsize));
if (rv != EXR_ERR_SUCCESS)
return rv;
encode->packed_buffer = encode->packed_sample_count_table;
encode->packed_bytes = sampsize;
encode->packed_alloc_size = encode->packed_sample_count_alloc_size;
switch (part->comp_type)
{
case EXR_COMPRESSION_NONE: rv = EXR_ERR_INVALID_ARGUMENT; break;
case EXR_COMPRESSION_RLE: rv = internal_exr_apply_rle (encode); break;
case EXR_COMPRESSION_ZIP:
case EXR_COMPRESSION_ZIPS: rv = internal_exr_apply_zip (encode); break;
default:
rv = EXR_ERR_INVALID_ARGUMENT;
break;
}
encode->packed_buffer = pb;
encode->packed_bytes = pbb;
encode->packed_alloc_size = pas;
if (rv != EXR_ERR_SUCCESS)
return ctxt->print_error (
ctxt,
rv,
"Unable to compress sample table");
}
}
switch (part->comp_type)
{
case EXR_COMPRESSION_NONE:
return ctxt->report_error (
ctxt,
EXR_ERR_INVALID_ARGUMENT,
"no compression set but still trying to compress");
case EXR_COMPRESSION_RLE: rv = internal_exr_apply_rle (encode); break;
case EXR_COMPRESSION_ZIP:
case EXR_COMPRESSION_ZIPS: rv = internal_exr_apply_zip (encode); break;
case EXR_COMPRESSION_PIZ: rv = internal_exr_apply_piz (encode); break;
case EXR_COMPRESSION_PXR24:
rv = internal_exr_apply_pxr24 (encode);
break;
case EXR_COMPRESSION_B44: rv = internal_exr_apply_b44 (encode); break;
case EXR_COMPRESSION_B44A: rv = internal_exr_apply_b44a (encode); break;
case EXR_COMPRESSION_DWAA: rv = internal_exr_apply_dwaa (encode); break;
case EXR_COMPRESSION_DWAB: rv = internal_exr_apply_dwab (encode); break;
case EXR_COMPRESSION_HTJ2K32:
case EXR_COMPRESSION_HTJ2K256:
rv = internal_exr_apply_ht (encode); break;
case EXR_COMPRESSION_LAST_TYPE:
default:
return ctxt->print_error (
ctxt,
EXR_ERR_INVALID_ARGUMENT,
"Compression technique 0x%02X invalid",
(int) part->comp_type);
}
return rv;
}
/**************************************/
/**************************************/
static exr_result_t
decompress_data (
exr_const_context_t ctxt,
const exr_compression_t ctype,
exr_decode_pipeline_t* decode,
void* packbufptr,
size_t packsz,
void* unpackbufptr,
size_t unpacksz)
{
exr_result_t rv;
if (packsz == 0) return EXR_ERR_SUCCESS;
if (packsz == unpacksz)
{
if (unpackbufptr != packbufptr)
memcpy (unpackbufptr, packbufptr, unpacksz);
return EXR_ERR_SUCCESS;
}
switch (ctype)
{
case EXR_COMPRESSION_NONE:
return ctxt->report_error (
ctxt,
EXR_ERR_INVALID_ARGUMENT,
"no compression set but still trying to decompress");
case EXR_COMPRESSION_RLE:
rv = internal_exr_undo_rle (
decode, packbufptr, packsz, unpackbufptr, unpacksz);
break;
case EXR_COMPRESSION_ZIP:
case EXR_COMPRESSION_ZIPS:
rv = internal_exr_undo_zip (
decode, packbufptr, packsz, unpackbufptr, unpacksz);
break;
case EXR_COMPRESSION_PIZ:
rv = internal_exr_undo_piz (
decode, packbufptr, packsz, unpackbufptr, unpacksz);
break;
case EXR_COMPRESSION_PXR24:
rv = internal_exr_undo_pxr24 (
decode, packbufptr, packsz, unpackbufptr, unpacksz);
break;
case EXR_COMPRESSION_B44:
rv = internal_exr_undo_b44 (
decode, packbufptr, packsz, unpackbufptr, unpacksz);
break;
case EXR_COMPRESSION_B44A:
rv = internal_exr_undo_b44a (
decode, packbufptr, packsz, unpackbufptr, unpacksz);
break;
case EXR_COMPRESSION_DWAA:
rv = internal_exr_undo_dwaa (
decode, packbufptr, packsz, unpackbufptr, unpacksz);
break;
case EXR_COMPRESSION_DWAB:
rv = internal_exr_undo_dwab (
decode, packbufptr, packsz, unpackbufptr, unpacksz);
break;
case EXR_COMPRESSION_HTJ2K256:
case EXR_COMPRESSION_HTJ2K32:
rv = internal_exr_undo_ht (
decode, packbufptr, packsz, unpackbufptr, unpacksz);
break;
case EXR_COMPRESSION_LAST_TYPE:
default:
return ctxt->print_error (
ctxt,
EXR_ERR_INVALID_ARGUMENT,
"Compression technique 0x%02X invalid",
ctype);
}
return rv;
}
exr_result_t
exr_uncompress_chunk (exr_decode_pipeline_t* decode)
{
exr_result_t rv = EXR_ERR_SUCCESS;
exr_context_t ctxt;
exr_priv_part_t part;
if (!decode) return EXR_ERR_MISSING_CONTEXT_ARG;
decode->bytes_decompressed = 0;
ctxt = (exr_context_t)decode->context;
if (!ctxt) return EXR_ERR_MISSING_CONTEXT_ARG;
/* TODO: Double check need for a lock? */
if (decode->part_index < 0 || decode->part_index >= ctxt->num_parts)
return ctxt->print_error (
ctxt,
EXR_ERR_ARGUMENT_OUT_OF_RANGE,
"Part index (%d) out of range",
decode->part_index);
part = ctxt->parts[decode->part_index];
// if (decode->chunk.unpacked_size != part->unpacked_size_per_chunk)
// return ctxt->print_error (
// ctxt,
// EXR_ERR_ARGUMENT_OUT_OF_RANGE,
// "Memory not sufficient to unpack a full part, expect %" PRIu64 ", given %" PRIu64,
// part->unpacked_size_per_chunk,
// decode->chunk.unpacked_size);
if (decode->packed_sample_count_table)
{
uint64_t sampsize =
(((uint64_t) decode->chunk.width) *
((uint64_t) decode->chunk.height));
sampsize *= sizeof (int32_t);
rv = decompress_data (
ctxt,
part->comp_type,
decode,
decode->packed_sample_count_table,
decode->chunk.sample_count_table_size,
decode->sample_count_table,
sampsize);
if (rv != EXR_ERR_SUCCESS)
{
return ctxt->print_error (
ctxt,
rv,
"Unable to decompress sample table %" PRIu64 " -> %" PRIu64,
decode->chunk.sample_count_table_size,
(uint64_t) sampsize);
}
}
if ((decode->decode_flags & EXR_DECODE_SAMPLE_DATA_ONLY)) return rv;
if (rv == EXR_ERR_SUCCESS &&
decode->chunk.packed_size > 0 &&
decode->chunk.unpacked_size > 0)
rv = decompress_data (
ctxt,
part->comp_type,
decode,
decode->packed_buffer,
decode->chunk.packed_size,
decode->unpacked_buffer,
decode->chunk.unpacked_size);
if (rv != EXR_ERR_SUCCESS)
{
return ctxt->print_error (
ctxt,
rv,
"Unable to decompress w %d image data %" PRIu64 " -> %" PRIu64 ", got %" PRIu64,
(int)part->comp_type,
decode->chunk.packed_size,
decode->chunk.unpacked_size,
decode->bytes_decompressed);
}
return rv;
}