forked from FFmpeg/FFmpeg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexr.c
1965 lines (1646 loc) · 64 KB
/
exr.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
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
/*
* OpenEXR (.exr) image decoder
* Copyright (c) 2006 Industrial Light & Magic, a division of Lucas Digital Ltd. LLC
* Copyright (c) 2009 Jimmy Christensen
*
* B44/B44A, Tile, UINT32 added by Jokyo Images support by CNC - French National Center for Cinema
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* OpenEXR decoder
* @author Jimmy Christensen
*
* For more information on the OpenEXR format, visit:
* http://openexr.com/
*
* exr_flt2uint() and exr_halflt2uint() is credited to Reimar Döffinger.
* exr_half2float() is credited to Aaftab Munshi, Dan Ginsburg, Dave Shreiner.
*/
#include <float.h>
#include <zlib.h>
#include "libavutil/avassert.h"
#include "libavutil/common.h"
#include "libavutil/imgutils.h"
#include "libavutil/intfloat.h"
#include "libavutil/avstring.h"
#include "libavutil/opt.h"
#include "libavutil/color_utils.h"
#include "avcodec.h"
#include "bytestream.h"
#if HAVE_BIGENDIAN
#include "bswapdsp.h"
#endif
#include "exrdsp.h"
#include "get_bits.h"
#include "internal.h"
#include "mathops.h"
#include "thread.h"
enum ExrCompr {
EXR_RAW,
EXR_RLE,
EXR_ZIP1,
EXR_ZIP16,
EXR_PIZ,
EXR_PXR24,
EXR_B44,
EXR_B44A,
EXR_DWA,
EXR_DWB,
EXR_UNKN,
};
enum ExrPixelType {
EXR_UINT,
EXR_HALF,
EXR_FLOAT,
EXR_UNKNOWN,
};
enum ExrTileLevelMode {
EXR_TILE_LEVEL_ONE,
EXR_TILE_LEVEL_MIPMAP,
EXR_TILE_LEVEL_RIPMAP,
EXR_TILE_LEVEL_UNKNOWN,
};
enum ExrTileLevelRound {
EXR_TILE_ROUND_UP,
EXR_TILE_ROUND_DOWN,
EXR_TILE_ROUND_UNKNOWN,
};
typedef struct EXRChannel {
int xsub, ysub;
enum ExrPixelType pixel_type;
} EXRChannel;
typedef struct EXRTileAttribute {
int32_t xSize;
int32_t ySize;
enum ExrTileLevelMode level_mode;
enum ExrTileLevelRound level_round;
} EXRTileAttribute;
typedef struct EXRThreadData {
uint8_t *uncompressed_data;
int uncompressed_size;
uint8_t *tmp;
int tmp_size;
uint8_t *bitmap;
uint16_t *lut;
int ysize, xsize;
int channel_line_size;
} EXRThreadData;
typedef struct EXRContext {
AVClass *class;
AVFrame *picture;
AVCodecContext *avctx;
ExrDSPContext dsp;
#if HAVE_BIGENDIAN
BswapDSPContext bbdsp;
#endif
enum ExrCompr compression;
enum ExrPixelType pixel_type;
int channel_offsets[4]; // 0 = red, 1 = green, 2 = blue and 3 = alpha
const AVPixFmtDescriptor *desc;
int w, h;
uint32_t xmax, xmin;
uint32_t ymax, ymin;
uint32_t xdelta, ydelta;
int scan_lines_per_block;
EXRTileAttribute tile_attr; /* header data attribute of tile */
int is_tile; /* 0 if scanline, 1 if tile */
int is_luma;/* 1 if there is an Y plane */
GetByteContext gb;
const uint8_t *buf;
int buf_size;
EXRChannel *channels;
int nb_channels;
int current_channel_offset;
EXRThreadData *thread_data;
const char *layer;
enum AVColorTransferCharacteristic apply_trc_type;
float gamma;
uint16_t gamma_table[65536];
} EXRContext;
/* -15 stored using a single precision bias of 127 */
#define HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP 0x38000000
/* max exponent value in single precision that will be converted
* to Inf or Nan when stored as a half-float */
#define HALF_FLOAT_MAX_BIASED_EXP_AS_SINGLE_FP_EXP 0x47800000
/* 255 is the max exponent biased value */
#define FLOAT_MAX_BIASED_EXP (0xFF << 23)
#define HALF_FLOAT_MAX_BIASED_EXP (0x1F << 10)
/**
* Convert a half float as a uint16_t into a full float.
*
* @param hf half float as uint16_t
*
* @return float value
*/
static union av_intfloat32 exr_half2float(uint16_t hf)
{
unsigned int sign = (unsigned int) (hf >> 15);
unsigned int mantissa = (unsigned int) (hf & ((1 << 10) - 1));
unsigned int exp = (unsigned int) (hf & HALF_FLOAT_MAX_BIASED_EXP);
union av_intfloat32 f;
if (exp == HALF_FLOAT_MAX_BIASED_EXP) {
// we have a half-float NaN or Inf
// half-float NaNs will be converted to a single precision NaN
// half-float Infs will be converted to a single precision Inf
exp = FLOAT_MAX_BIASED_EXP;
if (mantissa)
mantissa = (1 << 23) - 1; // set all bits to indicate a NaN
} else if (exp == 0x0) {
// convert half-float zero/denorm to single precision value
if (mantissa) {
mantissa <<= 1;
exp = HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP;
// check for leading 1 in denorm mantissa
while ((mantissa & (1 << 10))) {
// for every leading 0, decrement single precision exponent by 1
// and shift half-float mantissa value to the left
mantissa <<= 1;
exp -= (1 << 23);
}
// clamp the mantissa to 10 bits
mantissa &= ((1 << 10) - 1);
// shift left to generate single-precision mantissa of 23 bits
mantissa <<= 13;
}
} else {
// shift left to generate single-precision mantissa of 23 bits
mantissa <<= 13;
// generate single precision biased exponent value
exp = (exp << 13) + HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP;
}
f.i = (sign << 31) | exp | mantissa;
return f;
}
/**
* Convert from 32-bit float as uint32_t to uint16_t.
*
* @param v 32-bit float
*
* @return normalized 16-bit unsigned int
*/
static inline uint16_t exr_flt2uint(int32_t v)
{
int32_t exp = v >> 23;
// "HACK": negative values result in exp< 0, so clipping them to 0
// is also handled by this condition, avoids explicit check for sign bit.
if (exp <= 127 + 7 - 24) // we would shift out all bits anyway
return 0;
if (exp >= 127)
return 0xffff;
v &= 0x007fffff;
return (v + (1 << 23)) >> (127 + 7 - exp);
}
/**
* Convert from 16-bit float as uint16_t to uint16_t.
*
* @param v 16-bit float
*
* @return normalized 16-bit unsigned int
*/
static inline uint16_t exr_halflt2uint(uint16_t v)
{
unsigned exp = 14 - (v >> 10);
if (exp >= 14) {
if (exp == 14)
return (v >> 9) & 1;
else
return (v & 0x8000) ? 0 : 0xffff;
}
v <<= 6;
return (v + (1 << 16)) >> (exp + 1);
}
static int zip_uncompress(EXRContext *s, const uint8_t *src, int compressed_size,
int uncompressed_size, EXRThreadData *td)
{
unsigned long dest_len = uncompressed_size;
if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK ||
dest_len != uncompressed_size)
return AVERROR_INVALIDDATA;
av_assert1(uncompressed_size % 2 == 0);
s->dsp.predictor(td->tmp, uncompressed_size);
s->dsp.reorder_pixels(td->uncompressed_data, td->tmp, uncompressed_size);
return 0;
}
static int rle_uncompress(EXRContext *ctx, const uint8_t *src, int compressed_size,
int uncompressed_size, EXRThreadData *td)
{
uint8_t *d = td->tmp;
const int8_t *s = src;
int ssize = compressed_size;
int dsize = uncompressed_size;
uint8_t *dend = d + dsize;
int count;
while (ssize > 0) {
count = *s++;
if (count < 0) {
count = -count;
if ((dsize -= count) < 0 ||
(ssize -= count + 1) < 0)
return AVERROR_INVALIDDATA;
while (count--)
*d++ = *s++;
} else {
count++;
if ((dsize -= count) < 0 ||
(ssize -= 2) < 0)
return AVERROR_INVALIDDATA;
while (count--)
*d++ = *s;
s++;
}
}
if (dend != d)
return AVERROR_INVALIDDATA;
av_assert1(uncompressed_size % 2 == 0);
ctx->dsp.predictor(td->tmp, uncompressed_size);
ctx->dsp.reorder_pixels(td->uncompressed_data, td->tmp, uncompressed_size);
return 0;
}
#define USHORT_RANGE (1 << 16)
#define BITMAP_SIZE (1 << 13)
static uint16_t reverse_lut(const uint8_t *bitmap, uint16_t *lut)
{
int i, k = 0;
for (i = 0; i < USHORT_RANGE; i++)
if ((i == 0) || (bitmap[i >> 3] & (1 << (i & 7))))
lut[k++] = i;
i = k - 1;
memset(lut + k, 0, (USHORT_RANGE - k) * 2);
return i;
}
static void apply_lut(const uint16_t *lut, uint16_t *dst, int dsize)
{
int i;
for (i = 0; i < dsize; ++i)
dst[i] = lut[dst[i]];
}
#define HUF_ENCBITS 16 // literal (value) bit length
#define HUF_DECBITS 14 // decoding bit size (>= 8)
#define HUF_ENCSIZE ((1 << HUF_ENCBITS) + 1) // encoding table size
#define HUF_DECSIZE (1 << HUF_DECBITS) // decoding table size
#define HUF_DECMASK (HUF_DECSIZE - 1)
typedef struct HufDec {
int len;
int lit;
int *p;
} HufDec;
static void huf_canonical_code_table(uint64_t *hcode)
{
uint64_t c, n[59] = { 0 };
int i;
for (i = 0; i < HUF_ENCSIZE; ++i)
n[hcode[i]] += 1;
c = 0;
for (i = 58; i > 0; --i) {
uint64_t nc = ((c + n[i]) >> 1);
n[i] = c;
c = nc;
}
for (i = 0; i < HUF_ENCSIZE; ++i) {
int l = hcode[i];
if (l > 0)
hcode[i] = l | (n[l]++ << 6);
}
}
#define SHORT_ZEROCODE_RUN 59
#define LONG_ZEROCODE_RUN 63
#define SHORTEST_LONG_RUN (2 + LONG_ZEROCODE_RUN - SHORT_ZEROCODE_RUN)
#define LONGEST_LONG_RUN (255 + SHORTEST_LONG_RUN)
static int huf_unpack_enc_table(GetByteContext *gb,
int32_t im, int32_t iM, uint64_t *hcode)
{
GetBitContext gbit;
int ret = init_get_bits8(&gbit, gb->buffer, bytestream2_get_bytes_left(gb));
if (ret < 0)
return ret;
for (; im <= iM; im++) {
uint64_t l = hcode[im] = get_bits(&gbit, 6);
if (l == LONG_ZEROCODE_RUN) {
int zerun = get_bits(&gbit, 8) + SHORTEST_LONG_RUN;
if (im + zerun > iM + 1)
return AVERROR_INVALIDDATA;
while (zerun--)
hcode[im++] = 0;
im--;
} else if (l >= SHORT_ZEROCODE_RUN) {
int zerun = l - SHORT_ZEROCODE_RUN + 2;
if (im + zerun > iM + 1)
return AVERROR_INVALIDDATA;
while (zerun--)
hcode[im++] = 0;
im--;
}
}
bytestream2_skip(gb, (get_bits_count(&gbit) + 7) / 8);
huf_canonical_code_table(hcode);
return 0;
}
static int huf_build_dec_table(const uint64_t *hcode, int im,
int iM, HufDec *hdecod)
{
for (; im <= iM; im++) {
uint64_t c = hcode[im] >> 6;
int i, l = hcode[im] & 63;
if (c >> l)
return AVERROR_INVALIDDATA;
if (l > HUF_DECBITS) {
HufDec *pl = hdecod + (c >> (l - HUF_DECBITS));
if (pl->len)
return AVERROR_INVALIDDATA;
pl->lit++;
pl->p = av_realloc(pl->p, pl->lit * sizeof(int));
if (!pl->p)
return AVERROR(ENOMEM);
pl->p[pl->lit - 1] = im;
} else if (l) {
HufDec *pl = hdecod + (c << (HUF_DECBITS - l));
for (i = 1 << (HUF_DECBITS - l); i > 0; i--, pl++) {
if (pl->len || pl->p)
return AVERROR_INVALIDDATA;
pl->len = l;
pl->lit = im;
}
}
}
return 0;
}
#define get_char(c, lc, gb) \
{ \
c = (c << 8) | bytestream2_get_byte(gb); \
lc += 8; \
}
#define get_code(po, rlc, c, lc, gb, out, oe, outb) \
{ \
if (po == rlc) { \
if (lc < 8) \
get_char(c, lc, gb); \
lc -= 8; \
\
cs = c >> lc; \
\
if (out + cs > oe || out == outb) \
return AVERROR_INVALIDDATA; \
\
s = out[-1]; \
\
while (cs-- > 0) \
*out++ = s; \
} else if (out < oe) { \
*out++ = po; \
} else { \
return AVERROR_INVALIDDATA; \
} \
}
static int huf_decode(const uint64_t *hcode, const HufDec *hdecod,
GetByteContext *gb, int nbits,
int rlc, int no, uint16_t *out)
{
uint64_t c = 0;
uint16_t *outb = out;
uint16_t *oe = out + no;
const uint8_t *ie = gb->buffer + (nbits + 7) / 8; // input byte size
uint8_t cs;
uint16_t s;
int i, lc = 0;
while (gb->buffer < ie) {
get_char(c, lc, gb);
while (lc >= HUF_DECBITS) {
const HufDec pl = hdecod[(c >> (lc - HUF_DECBITS)) & HUF_DECMASK];
if (pl.len) {
lc -= pl.len;
get_code(pl.lit, rlc, c, lc, gb, out, oe, outb);
} else {
int j;
if (!pl.p)
return AVERROR_INVALIDDATA;
for (j = 0; j < pl.lit; j++) {
int l = hcode[pl.p[j]] & 63;
while (lc < l && bytestream2_get_bytes_left(gb) > 0)
get_char(c, lc, gb);
if (lc >= l) {
if ((hcode[pl.p[j]] >> 6) ==
((c >> (lc - l)) & ((1LL << l) - 1))) {
lc -= l;
get_code(pl.p[j], rlc, c, lc, gb, out, oe, outb);
break;
}
}
}
if (j == pl.lit)
return AVERROR_INVALIDDATA;
}
}
}
i = (8 - nbits) & 7;
c >>= i;
lc -= i;
while (lc > 0) {
const HufDec pl = hdecod[(c << (HUF_DECBITS - lc)) & HUF_DECMASK];
if (pl.len && lc >= pl.len) {
lc -= pl.len;
get_code(pl.lit, rlc, c, lc, gb, out, oe, outb);
} else {
return AVERROR_INVALIDDATA;
}
}
if (out - outb != no)
return AVERROR_INVALIDDATA;
return 0;
}
static int huf_uncompress(GetByteContext *gb,
uint16_t *dst, int dst_size)
{
int32_t src_size, im, iM;
uint32_t nBits;
uint64_t *freq;
HufDec *hdec;
int ret, i;
src_size = bytestream2_get_le32(gb);
im = bytestream2_get_le32(gb);
iM = bytestream2_get_le32(gb);
bytestream2_skip(gb, 4);
nBits = bytestream2_get_le32(gb);
if (im < 0 || im >= HUF_ENCSIZE ||
iM < 0 || iM >= HUF_ENCSIZE ||
src_size < 0)
return AVERROR_INVALIDDATA;
bytestream2_skip(gb, 4);
freq = av_mallocz_array(HUF_ENCSIZE, sizeof(*freq));
hdec = av_mallocz_array(HUF_DECSIZE, sizeof(*hdec));
if (!freq || !hdec) {
ret = AVERROR(ENOMEM);
goto fail;
}
if ((ret = huf_unpack_enc_table(gb, im, iM, freq)) < 0)
goto fail;
if (nBits > 8 * bytestream2_get_bytes_left(gb)) {
ret = AVERROR_INVALIDDATA;
goto fail;
}
if ((ret = huf_build_dec_table(freq, im, iM, hdec)) < 0)
goto fail;
ret = huf_decode(freq, hdec, gb, nBits, iM, dst_size, dst);
fail:
for (i = 0; i < HUF_DECSIZE; i++)
if (hdec)
av_freep(&hdec[i].p);
av_free(freq);
av_free(hdec);
return ret;
}
static inline void wdec14(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
{
int16_t ls = l;
int16_t hs = h;
int hi = hs;
int ai = ls + (hi & 1) + (hi >> 1);
int16_t as = ai;
int16_t bs = ai - hi;
*a = as;
*b = bs;
}
#define NBITS 16
#define A_OFFSET (1 << (NBITS - 1))
#define MOD_MASK ((1 << NBITS) - 1)
static inline void wdec16(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
{
int m = l;
int d = h;
int bb = (m - (d >> 1)) & MOD_MASK;
int aa = (d + bb - A_OFFSET) & MOD_MASK;
*b = bb;
*a = aa;
}
static void wav_decode(uint16_t *in, int nx, int ox,
int ny, int oy, uint16_t mx)
{
int w14 = (mx < (1 << 14));
int n = (nx > ny) ? ny : nx;
int p = 1;
int p2;
while (p <= n)
p <<= 1;
p >>= 1;
p2 = p;
p >>= 1;
while (p >= 1) {
uint16_t *py = in;
uint16_t *ey = in + oy * (ny - p2);
uint16_t i00, i01, i10, i11;
int oy1 = oy * p;
int oy2 = oy * p2;
int ox1 = ox * p;
int ox2 = ox * p2;
for (; py <= ey; py += oy2) {
uint16_t *px = py;
uint16_t *ex = py + ox * (nx - p2);
for (; px <= ex; px += ox2) {
uint16_t *p01 = px + ox1;
uint16_t *p10 = px + oy1;
uint16_t *p11 = p10 + ox1;
if (w14) {
wdec14(*px, *p10, &i00, &i10);
wdec14(*p01, *p11, &i01, &i11);
wdec14(i00, i01, px, p01);
wdec14(i10, i11, p10, p11);
} else {
wdec16(*px, *p10, &i00, &i10);
wdec16(*p01, *p11, &i01, &i11);
wdec16(i00, i01, px, p01);
wdec16(i10, i11, p10, p11);
}
}
if (nx & p) {
uint16_t *p10 = px + oy1;
if (w14)
wdec14(*px, *p10, &i00, p10);
else
wdec16(*px, *p10, &i00, p10);
*px = i00;
}
}
if (ny & p) {
uint16_t *px = py;
uint16_t *ex = py + ox * (nx - p2);
for (; px <= ex; px += ox2) {
uint16_t *p01 = px + ox1;
if (w14)
wdec14(*px, *p01, &i00, p01);
else
wdec16(*px, *p01, &i00, p01);
*px = i00;
}
}
p2 = p;
p >>= 1;
}
}
static int piz_uncompress(EXRContext *s, const uint8_t *src, int ssize,
int dsize, EXRThreadData *td)
{
GetByteContext gb;
uint16_t maxval, min_non_zero, max_non_zero;
uint16_t *ptr;
uint16_t *tmp = (uint16_t *)td->tmp;
uint16_t *out;
uint16_t *in;
int ret, i, j;
int pixel_half_size;/* 1 for half, 2 for float and uint32 */
EXRChannel *channel;
int tmp_offset;
if (!td->bitmap)
td->bitmap = av_malloc(BITMAP_SIZE);
if (!td->lut)
td->lut = av_malloc(1 << 17);
if (!td->bitmap || !td->lut) {
av_freep(&td->bitmap);
av_freep(&td->lut);
return AVERROR(ENOMEM);
}
bytestream2_init(&gb, src, ssize);
min_non_zero = bytestream2_get_le16(&gb);
max_non_zero = bytestream2_get_le16(&gb);
if (max_non_zero >= BITMAP_SIZE)
return AVERROR_INVALIDDATA;
memset(td->bitmap, 0, FFMIN(min_non_zero, BITMAP_SIZE));
if (min_non_zero <= max_non_zero)
bytestream2_get_buffer(&gb, td->bitmap + min_non_zero,
max_non_zero - min_non_zero + 1);
memset(td->bitmap + max_non_zero + 1, 0, BITMAP_SIZE - max_non_zero - 1);
maxval = reverse_lut(td->bitmap, td->lut);
ret = huf_uncompress(&gb, tmp, dsize / sizeof(uint16_t));
if (ret)
return ret;
ptr = tmp;
for (i = 0; i < s->nb_channels; i++) {
channel = &s->channels[i];
if (channel->pixel_type == EXR_HALF)
pixel_half_size = 1;
else
pixel_half_size = 2;
for (j = 0; j < pixel_half_size; j++)
wav_decode(ptr + j, td->xsize, pixel_half_size, td->ysize,
td->xsize * pixel_half_size, maxval);
ptr += td->xsize * td->ysize * pixel_half_size;
}
apply_lut(td->lut, tmp, dsize / sizeof(uint16_t));
out = (uint16_t *)td->uncompressed_data;
for (i = 0; i < td->ysize; i++) {
tmp_offset = 0;
for (j = 0; j < s->nb_channels; j++) {
channel = &s->channels[j];
if (channel->pixel_type == EXR_HALF)
pixel_half_size = 1;
else
pixel_half_size = 2;
in = tmp + tmp_offset * td->xsize * td->ysize + i * td->xsize * pixel_half_size;
tmp_offset += pixel_half_size;
#if HAVE_BIGENDIAN
s->bbdsp.bswap16_buf(out, in, td->xsize * pixel_half_size);
#else
memcpy(out, in, td->xsize * 2 * pixel_half_size);
#endif
out += td->xsize * pixel_half_size;
}
}
return 0;
}
static int pxr24_uncompress(EXRContext *s, const uint8_t *src,
int compressed_size, int uncompressed_size,
EXRThreadData *td)
{
unsigned long dest_len, expected_len = 0;
const uint8_t *in = td->tmp;
uint8_t *out;
int c, i, j;
for (i = 0; i < s->nb_channels; i++) {
if (s->channels[i].pixel_type == EXR_FLOAT) {
expected_len += (td->xsize * td->ysize * 3);/* PRX 24 store float in 24 bit instead of 32 */
} else if (s->channels[i].pixel_type == EXR_HALF) {
expected_len += (td->xsize * td->ysize * 2);
} else {//UINT 32
expected_len += (td->xsize * td->ysize * 4);
}
}
dest_len = expected_len;
if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK) {
return AVERROR_INVALIDDATA;
} else if (dest_len != expected_len) {
return AVERROR_INVALIDDATA;
}
out = td->uncompressed_data;
for (i = 0; i < td->ysize; i++)
for (c = 0; c < s->nb_channels; c++) {
EXRChannel *channel = &s->channels[c];
const uint8_t *ptr[4];
uint32_t pixel = 0;
switch (channel->pixel_type) {
case EXR_FLOAT:
ptr[0] = in;
ptr[1] = ptr[0] + td->xsize;
ptr[2] = ptr[1] + td->xsize;
in = ptr[2] + td->xsize;
for (j = 0; j < td->xsize; ++j) {
uint32_t diff = ((unsigned)*(ptr[0]++) << 24) |
(*(ptr[1]++) << 16) |
(*(ptr[2]++) << 8);
pixel += diff;
bytestream_put_le32(&out, pixel);
}
break;
case EXR_HALF:
ptr[0] = in;
ptr[1] = ptr[0] + td->xsize;
in = ptr[1] + td->xsize;
for (j = 0; j < td->xsize; j++) {
uint32_t diff = (*(ptr[0]++) << 8) | *(ptr[1]++);
pixel += diff;
bytestream_put_le16(&out, pixel);
}
break;
case EXR_UINT:
ptr[0] = in;
ptr[1] = ptr[0] + s->xdelta;
ptr[2] = ptr[1] + s->xdelta;
ptr[3] = ptr[2] + s->xdelta;
in = ptr[3] + s->xdelta;
for (j = 0; j < s->xdelta; ++j) {
uint32_t diff = ((uint32_t)*(ptr[0]++) << 24) |
(*(ptr[1]++) << 16) |
(*(ptr[2]++) << 8 ) |
(*(ptr[3]++));
pixel += diff;
bytestream_put_le32(&out, pixel);
}
break;
default:
return AVERROR_INVALIDDATA;
}
}
return 0;
}
static void unpack_14(const uint8_t b[14], uint16_t s[16])
{
unsigned short shift = (b[ 2] >> 2) & 15;
unsigned short bias = (0x20 << shift);
int i;
s[ 0] = (b[0] << 8) | b[1];
s[ 4] = s[ 0] + ((((b[ 2] << 4) | (b[ 3] >> 4)) & 0x3f) << shift) - bias;
s[ 8] = s[ 4] + ((((b[ 3] << 2) | (b[ 4] >> 6)) & 0x3f) << shift) - bias;
s[12] = s[ 8] + ((b[ 4] & 0x3f) << shift) - bias;
s[ 1] = s[ 0] + ((b[ 5] >> 2) << shift) - bias;
s[ 5] = s[ 4] + ((((b[ 5] << 4) | (b[ 6] >> 4)) & 0x3f) << shift) - bias;
s[ 9] = s[ 8] + ((((b[ 6] << 2) | (b[ 7] >> 6)) & 0x3f) << shift) - bias;
s[13] = s[12] + ((b[ 7] & 0x3f) << shift) - bias;
s[ 2] = s[ 1] + ((b[ 8] >> 2) << shift) - bias;
s[ 6] = s[ 5] + ((((b[ 8] << 4) | (b[ 9] >> 4)) & 0x3f) << shift) - bias;
s[10] = s[ 9] + ((((b[ 9] << 2) | (b[10] >> 6)) & 0x3f) << shift) - bias;
s[14] = s[13] + ((b[10] & 0x3f) << shift) - bias;
s[ 3] = s[ 2] + ((b[11] >> 2) << shift) - bias;
s[ 7] = s[ 6] + ((((b[11] << 4) | (b[12] >> 4)) & 0x3f) << shift) - bias;
s[11] = s[10] + ((((b[12] << 2) | (b[13] >> 6)) & 0x3f) << shift) - bias;
s[15] = s[14] + ((b[13] & 0x3f) << shift) - bias;
for (i = 0; i < 16; ++i) {
if (s[i] & 0x8000)
s[i] &= 0x7fff;
else
s[i] = ~s[i];
}
}
static void unpack_3(const uint8_t b[3], uint16_t s[16])
{
int i;
s[0] = (b[0] << 8) | b[1];
if (s[0] & 0x8000)
s[0] &= 0x7fff;
else
s[0] = ~s[0];
for (i = 1; i < 16; i++)
s[i] = s[0];
}
static int b44_uncompress(EXRContext *s, const uint8_t *src, int compressed_size,
int uncompressed_size, EXRThreadData *td) {
const int8_t *sr = src;
int stay_to_uncompress = compressed_size;
int nb_b44_block_w, nb_b44_block_h;
int index_tl_x, index_tl_y, index_out, index_tmp;
uint16_t tmp_buffer[16]; /* B44 use 4x4 half float pixel */
int c, iY, iX, y, x;
int target_channel_offset = 0;
/* calc B44 block count */
nb_b44_block_w = td->xsize / 4;
if ((td->xsize % 4) != 0)
nb_b44_block_w++;
nb_b44_block_h = td->ysize / 4;
if ((td->ysize % 4) != 0)
nb_b44_block_h++;
for (c = 0; c < s->nb_channels; c++) {
if (s->channels[c].pixel_type == EXR_HALF) {/* B44 only compress half float data */
for (iY = 0; iY < nb_b44_block_h; iY++) {
for (iX = 0; iX < nb_b44_block_w; iX++) {/* For each B44 block */
if (stay_to_uncompress < 3) {
av_log(s, AV_LOG_ERROR, "Not enough data for B44A block: %d", stay_to_uncompress);
return AVERROR_INVALIDDATA;
}
if (src[compressed_size - stay_to_uncompress + 2] == 0xfc) { /* B44A block */
unpack_3(sr, tmp_buffer);
sr += 3;
stay_to_uncompress -= 3;
} else {/* B44 Block */
if (stay_to_uncompress < 14) {
av_log(s, AV_LOG_ERROR, "Not enough data for B44 block: %d", stay_to_uncompress);
return AVERROR_INVALIDDATA;
}
unpack_14(sr, tmp_buffer);
sr += 14;
stay_to_uncompress -= 14;
}
/* copy data to uncompress buffer (B44 block can exceed target resolution)*/
index_tl_x = iX * 4;
index_tl_y = iY * 4;
for (y = index_tl_y; y < FFMIN(index_tl_y + 4, td->ysize); y++) {
for (x = index_tl_x; x < FFMIN(index_tl_x + 4, td->xsize); x++) {
index_out = target_channel_offset * td->xsize + y * td->channel_line_size + 2 * x;