-
Notifications
You must be signed in to change notification settings - Fork 699
Expand file tree
/
Copy pathinternal_dwa_compressor.h
More file actions
1807 lines (1472 loc) · 56.2 KB
/
Copy pathinternal_dwa_compressor.h
File metadata and controls
1807 lines (1472 loc) · 56.2 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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) Contributors to the OpenEXR Project.
//
#ifndef IMF_INTERNAL_DWA_HELPERS_H_HAS_BEEN_INCLUDED
# error "only include internal_dwa_helpers.h"
#endif
/**************************************/
typedef struct _DwaCompressor
{
exr_encode_pipeline_t* _encode;
exr_decode_pipeline_t* _decode;
AcCompression _acCompression;
int _numScanLines;
int _min[2], _max[2];
int _numChannels;
int _numCscChannelSets;
ChannelData* _channelData;
CscChannelSet* _cscChannelSets;
void* _channel_mem;
Classifier* _channelRules;
size_t _channelRuleCount;
uint8_t* _packedAcBuffer;
uint64_t _packedAcBufferSize;
uint8_t* _packedDcBuffer;
uint64_t _packedDcBufferSize;
uint8_t* _rleBuffer;
uint64_t _rleBufferSize;
uint8_t* _planarUncBuffer[NUM_COMPRESSOR_SCHEMES];
uint64_t _planarUncBufferSize[NUM_COMPRESSOR_SCHEMES];
exr_memory_allocation_func_t alloc_fn;
exr_memory_free_func_t free_fn;
int _zipLevel;
float _dwaCompressionLevel;
} DwaCompressor;
static exr_result_t DwaCompressor_construct (
DwaCompressor* me,
AcCompression acCompression,
exr_encode_pipeline_t* encode,
exr_decode_pipeline_t* decode);
static void DwaCompressor_destroy (DwaCompressor* me);
static exr_result_t DwaCompressor_compress (DwaCompressor* me);
static exr_result_t DwaCompressor_uncompress (
DwaCompressor* me,
const uint8_t* inPtr,
uint64_t iSize,
void* uncompressed_data,
uint64_t uncompressed_size);
static exr_result_t
DwaCompressor_initializeBuffers (DwaCompressor* me, size_t*);
static exr_result_t DwaCompressor_writeRelevantChannelRules (
DwaCompressor* me, uint8_t** outPtr, uint64_t nAvail, uint64_t* nWritten);
static exr_result_t DwaCompressor_readChannelRules (
DwaCompressor* me,
const uint8_t** inPtr,
uint64_t* nAvail,
uint64_t* outRuleSize);
//
// Populate our cached version of the channel data with
// data from the real channel list. We want to
// copy over attributes, determine compression schemes
// relevant for the channel type, and find sets of
// channels to be compressed from Y'CbCr data instead
// of R'G'B'.
//
static exr_result_t DwaCompressor_classifyChannels (DwaCompressor* me);
//
// Compute various buffer pointers for each channel
//
static exr_result_t DwaCompressor_setupChannelData (DwaCompressor* me);
/**************************************/
exr_result_t
DwaCompressor_construct (
DwaCompressor* me,
AcCompression acCompression,
exr_encode_pipeline_t* encode,
exr_decode_pipeline_t* decode)
{
exr_result_t rv = EXR_ERR_SUCCESS;
initializeFuncs ();
exrcore_ensure_dwa_tables();
memset (me, 0, sizeof (DwaCompressor));
me->_acCompression = acCompression;
me->_encode = encode;
me->_decode = decode;
if (encode)
{
exr_const_context_t pctxt = encode->context;
me->alloc_fn = pctxt ? pctxt->alloc_fn : internal_exr_alloc;
me->free_fn = pctxt ? pctxt->free_fn : internal_exr_free;
me->_channelData = internal_exr_alloc_aligned (
me->alloc_fn,
&(me->_channel_mem),
sizeof (ChannelData) * (size_t) encode->channel_count,
_SSE_ALIGNMENT);
if (!me->_channelData) return EXR_ERR_OUT_OF_MEMORY;
memset (
me->_channelData,
0,
sizeof (ChannelData) * (size_t) encode->channel_count);
me->_numChannels = encode->channel_count;
for (int c = 0; c < encode->channel_count; ++c)
{
me->_channelData[c].chan = encode->channels + c;
me->_channelData[c].compression = UNKNOWN;
DctCoderChannelData_construct (
&(me->_channelData[c]._dctData),
me->_channelData[c].chan->data_type);
}
// DWAA should be 32, DWAB should be 256
me->_numScanLines = encode->chunk.height;
me->_min[0] = encode->chunk.start_x;
me->_min[1] = encode->chunk.start_y;
me->_max[0] = me->_min[0] + encode->chunk.width - 1;
me->_max[1] = me->_min[1] + encode->chunk.height - 1;
rv = exr_get_zip_compression_level (
encode->context, encode->part_index, &(me->_zipLevel));
if (rv != EXR_ERR_SUCCESS) return rv;
rv = exr_get_dwa_compression_level (
encode->context, encode->part_index, &(me->_dwaCompressionLevel));
if (rv != EXR_ERR_SUCCESS) return rv;
}
else
{
exr_const_context_t pctxt = decode->context;
me->alloc_fn = pctxt ? pctxt->alloc_fn : internal_exr_alloc;
me->free_fn = pctxt ? pctxt->free_fn : internal_exr_free;
me->_channelData = internal_exr_alloc_aligned (
me->alloc_fn,
&(me->_channel_mem),
sizeof (ChannelData) * (size_t) decode->channel_count,
_SSE_ALIGNMENT);
if (!me->_channelData) return EXR_ERR_OUT_OF_MEMORY;
memset (
me->_channelData,
0,
sizeof (ChannelData) * (size_t) decode->channel_count);
me->_numChannels = decode->channel_count;
for (int c = 0; c < decode->channel_count; ++c)
{
me->_channelData[c].chan = decode->channels + c;
me->_channelData[c].compression = UNKNOWN;
}
me->_numScanLines = decode->chunk.height;
me->_min[0] = decode->chunk.start_x;
me->_min[1] = decode->chunk.start_y;
me->_max[0] = me->_min[0] + decode->chunk.width - 1;
me->_max[1] = me->_min[1] + decode->chunk.height - 1;
}
return rv;
}
/**************************************/
static void
DwaCompressor_destroy (DwaCompressor* me)
{
if (me->_packedAcBuffer) me->free_fn (me->_packedAcBuffer);
if (me->_packedDcBuffer) me->free_fn (me->_packedDcBuffer);
if (me->_rleBuffer) me->free_fn (me->_rleBuffer);
if (me->_channel_mem)
{
for (int c = 0; c < me->_numChannels; ++c)
DctCoderChannelData_destroy (
me->free_fn, &(me->_channelData[c]._dctData));
me->free_fn (me->_channel_mem);
}
if (me->_cscChannelSets) me->free_fn (me->_cscChannelSets);
if (me->_channelRules != sLegacyChannelRules &&
me->_channelRules != sDefaultChannelRules)
{
for (size_t i = 0; i < me->_channelRuleCount; ++i)
Classifier_destroy (me->free_fn, &(me->_channelRules[i]));
me->free_fn (me->_channelRules);
}
for (int i = 0; i < NUM_COMPRESSOR_SCHEMES; ++i)
{
if (me->_planarUncBuffer[i]) me->free_fn (me->_planarUncBuffer[i]);
}
}
/**************************************/
exr_result_t
DwaCompressor_compress (DwaCompressor* me)
{
exr_result_t rv;
uint8_t* outPtr;
uint64_t* sizes;
size_t outBufferSize = 0;
uint64_t dataBytes, nWritten = 0;
uint64_t nAvail;
uint64_t fileVersion = 2;
uint64_t* version;
uint64_t* unknownUncompressedSize;
uint64_t* unknownCompressedSize;
uint64_t* acCompressedSize;
uint64_t* dcCompressedSize;
uint64_t* rleCompressedSize;
uint64_t* rleUncompressedSize;
uint64_t* rleRawSize;
uint64_t* totalAcUncompressedCount;
uint64_t* totalDcUncompressedCount;
uint64_t* acCompression;
uint8_t* packedAcEnd;
uint8_t* packedDcEnd;
uint8_t* outDataPtr;
uint8_t* inDataPtr;
// Starting with DWA v2, we write the channel
// classification rules into the file
me->_channelRules = sDefaultChannelRules;
me->_channelRuleCount = sizeof (sDefaultChannelRules) / sizeof (Classifier);
rv = DwaCompressor_initializeBuffers (me, &outBufferSize);
nAvail = me->_encode->compressed_alloc_size;
if (nAvail < (NUM_SIZES_SINGLE * sizeof (uint64_t)))
return EXR_ERR_OUT_OF_MEMORY;
rv = internal_encode_alloc_buffer (
me->_encode,
EXR_TRANSCODE_BUFFER_SCRATCH1,
&(me->_encode->compressed_buffer),
&(me->_encode->compressed_alloc_size),
outBufferSize);
if (rv != EXR_ERR_SUCCESS) return rv;
nAvail = outBufferSize;
sizes = (uint64_t*) me->_encode->compressed_buffer;
//
// Zero all the numbers in the chunk header
//
// memset (sizes, 0, NUM_SIZES_SINGLE * sizeof (uint64_t));
memset (sizes, 0, me->_encode->compressed_alloc_size);
#define OBIDX(x) (uint64_t*) (sizes + x)
version = OBIDX (VERSION);
unknownUncompressedSize = OBIDX (UNKNOWN_UNCOMPRESSED_SIZE);
unknownCompressedSize = OBIDX (UNKNOWN_COMPRESSED_SIZE);
acCompressedSize = OBIDX (AC_COMPRESSED_SIZE);
dcCompressedSize = OBIDX (DC_COMPRESSED_SIZE);
rleCompressedSize = OBIDX (RLE_COMPRESSED_SIZE);
rleUncompressedSize = OBIDX (RLE_UNCOMPRESSED_SIZE);
rleRawSize = OBIDX (RLE_RAW_SIZE);
totalAcUncompressedCount = OBIDX (AC_UNCOMPRESSED_COUNT);
totalDcUncompressedCount = OBIDX (DC_UNCOMPRESSED_COUNT);
acCompression = OBIDX (AC_COMPRESSION);
packedAcEnd = NULL;
packedDcEnd = NULL;
// Now write in the channel rules...
outPtr = (uint8_t*) (sizes + NUM_SIZES_SINGLE);
if (rv == EXR_ERR_SUCCESS && fileVersion >= 2)
{
rv = DwaCompressor_writeRelevantChannelRules (
me, &outPtr, nAvail, &nWritten);
}
// post add this so we have a 0 value for the relevant channel
// rules to fill up
nWritten += NUM_SIZES_SINGLE * sizeof (uint64_t);
if (rv != EXR_ERR_SUCCESS || nWritten >= me->_encode->compressed_alloc_size)
return EXR_ERR_OUT_OF_MEMORY;
outDataPtr = outPtr;
//
// We might not be dealing with any color data, in which
// case the AC buffer size will be 0, and dereferencing
// a vector will not be a good thing to do.
//
if (me->_packedAcBuffer) packedAcEnd = me->_packedAcBuffer;
if (me->_packedDcBuffer) packedDcEnd = me->_packedDcBuffer;
//
// Setup the AC compression strategy and the version in the data block,
// then write the relevant channel classification rules if needed
//
*version = fileVersion;
*acCompression = me->_acCompression;
rv = DwaCompressor_setupChannelData (me);
if (rv != EXR_ERR_SUCCESS) return rv;
//
// Determine the start of each row in the input buffer
// Channels are interleaved by scanline
//
for (int c = 0; c < me->_numChannels; ++c)
{
me->_channelData[c].processed = 0;
}
inDataPtr = me->_encode->packed_buffer;
for (int y = me->_min[1]; y <= me->_max[1]; ++y)
{
for (int c = 0; c < me->_numChannels; ++c)
{
ChannelData* cd = &(me->_channelData[c]);
exr_coding_channel_info_t* chan = cd->chan;
if ((y % chan->y_samples) != 0) continue;
rv = DctCoderChannelData_push_row (
me->alloc_fn, me->free_fn, &(cd->_dctData), inDataPtr);
if (rv != EXR_ERR_SUCCESS) return rv;
inDataPtr += chan->width * chan->bytes_per_element;
}
}
//
// Make a pass over all our CSC sets and try to encode them first
//
for (int csc = 0; csc < me->_numCscChannelSets; ++csc)
{
LossyDctEncoder enc;
CscChannelSet* cset = &(me->_cscChannelSets[csc]);
rv = LossyDctEncoderCsc_construct (
&enc,
me->_dwaCompressionLevel / 100000.f,
&(me->_channelData[cset->idx[0]]._dctData),
&(me->_channelData[cset->idx[1]]._dctData),
&(me->_channelData[cset->idx[2]]._dctData),
packedAcEnd,
packedDcEnd,
exrcore_dwaToNonLinearTable,
me->_channelData[cset->idx[0]].chan->width,
me->_channelData[cset->idx[0]].chan->height);
if (rv == EXR_ERR_SUCCESS)
rv = LossyDctEncoder_execute (me->alloc_fn, me->free_fn, &enc);
*totalAcUncompressedCount = *totalAcUncompressedCount + enc._numAcComp;
*totalDcUncompressedCount = *totalDcUncompressedCount + enc._numDcComp;
packedAcEnd += enc._numAcComp * sizeof (uint16_t);
packedDcEnd += enc._numDcComp * sizeof (uint16_t);
me->_channelData[cset->idx[0]].processed = 1;
me->_channelData[cset->idx[1]].processed = 1;
me->_channelData[cset->idx[2]].processed = 1;
if (rv != EXR_ERR_SUCCESS) return rv;
}
for (int chan = 0; chan < me->_numChannels; ++chan)
{
ChannelData* cd = &(me->_channelData[chan]);
exr_coding_channel_info_t* pchan = cd->chan;
if (cd->processed) continue;
switch (cd->compression)
{
case LOSSY_DCT:
//
// For LOSSY_DCT, treat this just like the CSC'd case,
// but only operate on one channel
//
{
LossyDctEncoder enc;
const unsigned short* nonlinearLut = NULL;
if (!pchan->p_linear)
nonlinearLut = exrcore_dwaToNonLinearTable;
rv = LossyDctEncoder_construct (
&enc,
me->_dwaCompressionLevel / 100000.f,
&(cd->_dctData),
packedAcEnd,
packedDcEnd,
nonlinearLut,
pchan->width,
pchan->height);
if (rv == EXR_ERR_SUCCESS)
rv = LossyDctEncoder_execute (
me->alloc_fn, me->free_fn, &enc);
*totalAcUncompressedCount =
*totalAcUncompressedCount + enc._numAcComp;
*totalDcUncompressedCount =
*totalDcUncompressedCount + enc._numDcComp;
packedAcEnd += enc._numAcComp * sizeof (uint16_t);
packedDcEnd += enc._numDcComp * sizeof (uint16_t);
if (rv != EXR_ERR_SUCCESS) return rv;
}
break;
case RLE: {
//
// For RLE, bash the bytes up so that the first bytes of each
// pixel are contiguous, as are the second bytes, and so on.
//
DctCoderChannelData* dcd = &(cd->_dctData);
for (size_t y = 0; y < dcd->_size; ++y)
{
const uint8_t* row = dcd->_rows[y];
for (int x = 0; x < pchan->width; ++x)
{
for (int byte = 0; byte < pchan->bytes_per_element;
++byte)
{
*cd->planarUncRleEnd[byte]++ = *row++;
}
}
*rleRawSize += (uint64_t) pchan->width *
(uint64_t) pchan->bytes_per_element;
}
break;
}
case UNKNOWN:
//
// Otherwise, just copy data over verbatim
//
{
size_t scanlineSize = (size_t) pchan->width *
(size_t) pchan->bytes_per_element;
DctCoderChannelData* dcd = &(cd->_dctData);
for (size_t y = 0; y < dcd->_size; ++y)
{
memcpy (
cd->planarUncBufferEnd,
dcd->_rows[y],
scanlineSize);
cd->planarUncBufferEnd += scanlineSize;
}
*unknownUncompressedSize += cd->planarUncSize;
}
break;
case NUM_COMPRESSOR_SCHEMES:
default: return EXR_ERR_INVALID_ARGUMENT;
}
cd->processed = DWA_CLASSIFIER_TRUE;
}
//
// Pack the Unknown data into the output buffer first. Instead of
// just copying it uncompressed, try zlib compression at least.
//
if (*unknownUncompressedSize > 0)
{
size_t outSize;
rv = exr_compress_buffer (
me->_encode->context,
9, // TODO: use default??? the old call to zlib had 9 hardcoded
me->_planarUncBuffer[UNKNOWN],
*unknownUncompressedSize,
outDataPtr,
exr_compress_max_buffer_size (*unknownUncompressedSize),
&outSize);
if (rv != EXR_ERR_SUCCESS) return rv;
outDataPtr += outSize;
*unknownCompressedSize = outSize;
nWritten += outSize;
}
//
// Now, pack all the Lossy DCT coefficients into our output
// buffer, with Huffman encoding.
//
// Also, record the compressed size and the number of
// uncompressed componentns we have.
//
if (*totalAcUncompressedCount > 0)
{
switch (me->_acCompression)
{
case STATIC_HUFFMAN: {
size_t outDataSize =
outBufferSize -
(size_t) ((uintptr_t) outDataPtr - (uintptr_t) sizes);
rv = internal_huf_compress (
acCompressedSize,
outDataPtr,
outDataSize,
(const uint16_t*) me->_packedAcBuffer,
*totalAcUncompressedCount,
me->_encode->scratch_buffer_1,
me->_encode->scratch_alloc_size_1);
if (rv != EXR_ERR_SUCCESS)
{
if (rv == EXR_ERR_ARGUMENT_OUT_OF_RANGE)
{
memcpy (
me->_encode->compressed_buffer,
me->_encode->packed_buffer,
me->_encode->packed_alloc_size);
me->_encode->compressed_bytes =
me->_encode->packed_alloc_size;
return EXR_ERR_SUCCESS;
}
return rv;
}
break;
}
case DEFLATE: {
size_t sourceLen =
*totalAcUncompressedCount * sizeof (uint16_t);
size_t destLen;
rv = exr_compress_buffer (
me->_encode->context,
9, // TODO: use default??? the old call to zlib had 9 hardcoded
me->_packedAcBuffer,
sourceLen,
outDataPtr,
exr_compress_max_buffer_size (sourceLen),
&destLen);
if (rv != EXR_ERR_SUCCESS) return rv;
*acCompressedSize = destLen;
break;
}
default:
return EXR_ERR_INVALID_ARGUMENT;
//assert (false);
}
outDataPtr += *acCompressedSize;
nWritten += *acCompressedSize;
}
//
// Handle the DC components separately
//
if (*totalDcUncompressedCount > 0)
{
size_t compBytes;
size_t uncompBytes = *totalDcUncompressedCount * sizeof (uint16_t);
rv = internal_encode_alloc_buffer (
me->_encode,
EXR_TRANSCODE_BUFFER_SCRATCH1,
&(me->_encode->scratch_buffer_1),
&(me->_encode->scratch_alloc_size_1),
uncompBytes);
if (rv != EXR_ERR_SUCCESS) return rv;
internal_zip_deconstruct_bytes (
me->_encode->scratch_buffer_1, me->_packedDcBuffer, uncompBytes);
rv = exr_compress_buffer (
me->_encode->context,
me->_zipLevel,
me->_encode->scratch_buffer_1,
uncompBytes,
outDataPtr,
exr_compress_max_buffer_size (uncompBytes),
&compBytes);
if (rv != EXR_ERR_SUCCESS) return rv;
*dcCompressedSize = compBytes;
outDataPtr += compBytes;
nWritten += compBytes;
}
//
// If we have RLE data, first RLE encode it and set the uncompressed
// size. Then, deflate the results and set the compressed size.
//
if (*rleRawSize > 0)
{
size_t compBytes;
*rleUncompressedSize = internal_rle_compress (
me->_rleBuffer,
me->_rleBufferSize,
me->_planarUncBuffer[RLE],
*rleRawSize);
rv = exr_compress_buffer (
me->_encode->context,
9, // TODO: use default??? the old call to zlib had 9 hardcoded
me->_rleBuffer,
*rleUncompressedSize,
outDataPtr,
exr_compress_max_buffer_size (*rleUncompressedSize),
&compBytes);
if (rv != EXR_ERR_SUCCESS) return rv;
*rleCompressedSize = compBytes;
outDataPtr += compBytes;
nWritten += compBytes;
}
//
// Flip the counters to XDR format
//
priv_from_native64 (sizes, NUM_SIZES_SINGLE);
dataBytes =
(uintptr_t) outDataPtr - (uintptr_t) me->_encode->compressed_buffer;
if (nWritten != dataBytes) { return EXR_ERR_CORRUPT_CHUNK; }
if (nWritten >= me->_encode->packed_bytes)
{
memcpy (
me->_encode->compressed_buffer,
me->_encode->packed_buffer,
me->_encode->packed_bytes);
me->_encode->compressed_bytes = me->_encode->packed_bytes;
}
else { me->_encode->compressed_bytes = nWritten; }
return rv;
}
/**************************************/
static exr_result_t
validate_size(DwaCompressor* me, CompressorScheme compression,
uint64_t compressedSize, uint64_t uncompressedSize,
uint64_t extraSize)
{
uint64_t requiredSize = 0;
for (int c = 0; c < me->_numChannels; ++c)
{
if (me->_channelData[c].compression == compression)
{
uint64_t chanSize = (uint64_t) me->_channelData[c].planarUncSize;
/* guard against wraparound when accumulating attacker-influenced
* per-channel sizes; a wrap would understate requiredSize and
* defeat the validation below */
if (chanSize > UINT64_MAX - requiredSize)
return EXR_ERR_CORRUPT_CHUNK;
requiredSize += chanSize;
}
}
if (requiredSize > 0)
{
if (uncompressedSize < requiredSize || compressedSize == 0)
{
return EXR_ERR_CORRUPT_CHUNK;
}
}
else if (uncompressedSize > 0 || compressedSize > 0 || extraSize > 0)
{
return EXR_ERR_CORRUPT_CHUNK;
}
return EXR_ERR_SUCCESS;
}
exr_result_t
DwaCompressor_uncompress (
DwaCompressor* me,
const uint8_t* inPtr,
uint64_t iSize,
void* uncompressed_data,
uint64_t uncompressed_size)
{
uint64_t headerSize = NUM_SIZES_SINGLE * sizeof (uint64_t);
exr_result_t rv = EXR_ERR_SUCCESS;
uint64_t counters[NUM_SIZES_SINGLE];
uint64_t version;
uint64_t unknownUncompressedSize;
uint64_t unknownCompressedSize;
uint64_t acCompressedSize;
uint64_t dcCompressedSize;
uint64_t rleCompressedSize;
uint64_t rleUncompressedSize;
uint64_t rleRawSize;
uint64_t totalAcUncompressedCount;
uint64_t totalDcUncompressedCount;
uint64_t acCompression;
size_t outBufferSize;
uint64_t compressedSize;
const uint8_t* dataPtr;
uint64_t dataLeft;
uint8_t* outBufferEnd;
uint8_t* packedAcBufferEnd;
uint8_t* packedDcBufferEnd;
const uint8_t* dataPtrEnd;
const uint8_t* compressedUnknownBuf;
const uint8_t* compressedAcBuf;
const uint8_t* compressedDcBuf;
const uint8_t* compressedRleBuf;
if (iSize < headerSize) return EXR_ERR_CORRUPT_CHUNK;
//
// Flip the counters from XDR to NATIVE
//
memset (uncompressed_data, 0, uncompressed_size);
memcpy (counters, inPtr, headerSize);
priv_to_native64 (counters, NUM_SIZES_SINGLE);
//
// Unwind all the counter info
//
version = counters[VERSION];
unknownUncompressedSize = counters[UNKNOWN_UNCOMPRESSED_SIZE];
unknownCompressedSize = counters[UNKNOWN_COMPRESSED_SIZE];
acCompressedSize = counters[AC_COMPRESSED_SIZE];
dcCompressedSize = counters[DC_COMPRESSED_SIZE];
rleCompressedSize = counters[RLE_COMPRESSED_SIZE];
rleUncompressedSize = counters[RLE_UNCOMPRESSED_SIZE];
rleRawSize = counters[RLE_RAW_SIZE];
totalAcUncompressedCount = counters[AC_UNCOMPRESSED_COUNT];
totalDcUncompressedCount = counters[DC_UNCOMPRESSED_COUNT];
acCompression = counters[AC_COMPRESSION];
compressedSize = unknownCompressedSize + acCompressedSize +
dcCompressedSize + rleCompressedSize;
dataPtrEnd = inPtr + iSize;
dataPtr = inPtr + headerSize;
dataLeft = iSize - headerSize;
/* Both the sum and individual sizes are checked in case of overflow. */
if (iSize < (headerSize + compressedSize) ||
iSize < unknownCompressedSize || iSize < acCompressedSize ||
iSize < dcCompressedSize || iSize < rleCompressedSize)
{
return EXR_ERR_CORRUPT_CHUNK;
}
/* check for overflow conditions in the unc sizes, corrupt file no
need to check the rleUncompressedSize, the zipped rle data will
be checked below */
if (unknownUncompressedSize > uncompressed_size ||
rleRawSize > uncompressed_size ||
(unknownUncompressedSize + rleRawSize) > uncompressed_size)
{
return EXR_ERR_CORRUPT_CHUNK;
}
if ((int64_t) unknownUncompressedSize < 0 ||
(int64_t) unknownCompressedSize < 0 || (int64_t) acCompressedSize < 0 ||
(int64_t) dcCompressedSize < 0 || (int64_t) rleCompressedSize < 0 ||
(int64_t) rleUncompressedSize < 0 || (int64_t) rleRawSize < 0 ||
(int64_t) totalAcUncompressedCount < 0 ||
(int64_t) totalDcUncompressedCount < 0)
{
return EXR_ERR_CORRUPT_CHUNK;
}
if (version < 2)
{
me->_channelRules = sLegacyChannelRules;
me->_channelRuleCount =
sizeof (sLegacyChannelRules) / sizeof (Classifier);
}
else
{
uint64_t ruleSize;
rv =
DwaCompressor_readChannelRules (me, &dataPtr, &dataLeft, &ruleSize);
headerSize += ruleSize;
}
if (rv != EXR_ERR_SUCCESS) return rv;
outBufferSize = 0;
rv = DwaCompressor_initializeBuffers (me, &outBufferSize);
if (rv != EXR_ERR_SUCCESS) return rv;
//
// Allocate _outBuffer, if we haven't done so already
//
// the C++ classes used to have one buffer size for compress / uncompress
// but here we want to do zero-ish copy...
outBufferEnd = me->_decode->unpacked_buffer;
outBufferSize = me->_decode->unpacked_alloc_size;
//
// Find the start of the RLE packed AC components and
// the DC components for each channel. This will be handy
// if you want to decode the channels in parallel later on.
//
packedAcBufferEnd = NULL;
if (me->_packedAcBuffer) packedAcBufferEnd = me->_packedAcBuffer;
packedDcBufferEnd = NULL;
if (me->_packedDcBuffer) packedDcBufferEnd = me->_packedDcBuffer;
//
// UNKNOWN data is packed first, followed by the
// Huffman-compressed AC, then the DC values,
// and then the zlib compressed RLE data.
//
compressedUnknownBuf = dataPtr;
compressedAcBuf =
compressedUnknownBuf + (ptrdiff_t) (unknownCompressedSize);
compressedDcBuf = compressedAcBuf + (ptrdiff_t) (acCompressedSize);
compressedRleBuf = compressedDcBuf + (ptrdiff_t) (dcCompressedSize);
if (compressedUnknownBuf > dataPtrEnd || dataPtr > compressedAcBuf ||
compressedAcBuf > dataPtrEnd || dataPtr > compressedDcBuf ||
compressedDcBuf > dataPtrEnd || dataPtr > compressedRleBuf ||
compressedRleBuf > dataPtrEnd ||
(compressedRleBuf + rleCompressedSize) > dataPtrEnd)
{
return EXR_ERR_CORRUPT_CHUNK;
}
//
// Sanity check that the version is something we expect. Right now,
// we can decode version 0, 1, and 2. v1 adds 'end of block' symbols
// to the AC RLE. v2 adds channel classification rules at the
// start of the data block.
//
if (version > 2) { return EXR_ERR_BAD_CHUNK_LEADER; }
rv = DwaCompressor_setupChannelData (me);
if (rv != EXR_ERR_SUCCESS) { return rv; }
rv = validate_size(me, UNKNOWN, unknownCompressedSize, unknownUncompressedSize, 0);
if (rv != EXR_ERR_SUCCESS) { return rv; }
rv = validate_size(me, RLE, rleCompressedSize, rleRawSize, rleUncompressedSize);
if (rv != EXR_ERR_SUCCESS) { return rv; }
//
// Uncompress the UNKNOWN data into _planarUncBuffer[UNKNOWN]
//
if (unknownCompressedSize > 0)
{
if (unknownUncompressedSize > me->_planarUncBufferSize[UNKNOWN])
{
return EXR_ERR_CORRUPT_CHUNK;
}
if (EXR_ERR_SUCCESS != exr_uncompress_buffer (
me->_decode->context,
compressedUnknownBuf,
unknownCompressedSize,
me->_planarUncBuffer[UNKNOWN],
unknownUncompressedSize,
NULL))
{
return EXR_ERR_CORRUPT_CHUNK;
}
}
//
// Uncompress the AC data into _packedAcBuffer
//
if (acCompressedSize > 0)
{
if (!me->_packedAcBuffer ||
totalAcUncompressedCount * sizeof (uint16_t) >
me->_packedAcBufferSize)
{
return EXR_ERR_CORRUPT_CHUNK;
}
//
// Don't trust the user to get it right, look in the file.
//
switch (acCompression)
{
case STATIC_HUFFMAN:
rv = internal_huf_decompress (
me->_decode,
compressedAcBuf,
acCompressedSize,
(uint16_t*) me->_packedAcBuffer,
totalAcUncompressedCount,
me->_decode->scratch_buffer_1,
me->_decode->scratch_alloc_size_1);
if (rv != EXR_ERR_SUCCESS) { return rv; }
break;
case DEFLATE: {
size_t destLen;
rv = exr_uncompress_buffer (
me->_decode->context,
compressedAcBuf,
acCompressedSize,
me->_packedAcBuffer,
totalAcUncompressedCount * sizeof (uint16_t),
&destLen);
if (rv != EXR_ERR_SUCCESS) return rv;
if (totalAcUncompressedCount * sizeof (uint16_t) != destLen)
{
return EXR_ERR_CORRUPT_CHUNK;
}
}
break;
default: return EXR_ERR_CORRUPT_CHUNK; break;
}
}
//
// Uncompress the DC data into _packedDcBuffer
//
if (dcCompressedSize > 0)
{
size_t destLen;
size_t uncompBytes = totalDcUncompressedCount * sizeof (uint16_t);
if (uncompBytes > me->_packedDcBufferSize)
{
return EXR_ERR_CORRUPT_CHUNK;