1 /*
2 * OpenEXR (.exr) image decoder
3 * Copyright (c) 2006 Industrial Light & Magic, a division of Lucas Digital Ltd. LLC
4 * Copyright (c) 2009 Jimmy Christensen
5 *
6 * B44/B44A, Tile, UINT32 added by Jokyo Images support by CNC - French National Center for Cinema
7 *
8 * This file is part of FFmpeg.
9 *
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25 /**
26 * @file
27 * OpenEXR decoder
28 * @author Jimmy Christensen
29 *
30 * For more information on the OpenEXR format, visit:
31 * http://openexr.com/
32 *
33 * exr_half2float() is credited to Aaftab Munshi, Dan Ginsburg, Dave Shreiner.
34 */
35
36 #include <float.h>
37 #include <zlib.h>
38
39 #include "libavutil/avassert.h"
40 #include "libavutil/common.h"
41 #include "libavutil/imgutils.h"
42 #include "libavutil/intfloat.h"
43 #include "libavutil/avstring.h"
44 #include "libavutil/opt.h"
45 #include "libavutil/color_utils.h"
46
47 #include "avcodec.h"
48 #include "bytestream.h"
49
50 #if HAVE_BIGENDIAN
51 #include "bswapdsp.h"
52 #endif
53
54 #include "exrdsp.h"
55 #include "get_bits.h"
56 #include "internal.h"
57 #include "mathops.h"
58 #include "thread.h"
59
60 enum ExrCompr {
61 EXR_RAW,
62 EXR_RLE,
63 EXR_ZIP1,
64 EXR_ZIP16,
65 EXR_PIZ,
66 EXR_PXR24,
67 EXR_B44,
68 EXR_B44A,
69 EXR_DWA,
70 EXR_DWB,
71 EXR_UNKN,
72 };
73
74 enum ExrPixelType {
75 EXR_UINT,
76 EXR_HALF,
77 EXR_FLOAT,
78 EXR_UNKNOWN,
79 };
80
81 enum ExrTileLevelMode {
82 EXR_TILE_LEVEL_ONE,
83 EXR_TILE_LEVEL_MIPMAP,
84 EXR_TILE_LEVEL_RIPMAP,
85 EXR_TILE_LEVEL_UNKNOWN,
86 };
87
88 enum ExrTileLevelRound {
89 EXR_TILE_ROUND_UP,
90 EXR_TILE_ROUND_DOWN,
91 EXR_TILE_ROUND_UNKNOWN,
92 };
93
94 typedef struct EXRChannel {
95 int xsub, ysub;
96 enum ExrPixelType pixel_type;
97 } EXRChannel;
98
99 typedef struct EXRTileAttribute {
100 int32_t xSize;
101 int32_t ySize;
102 enum ExrTileLevelMode level_mode;
103 enum ExrTileLevelRound level_round;
104 } EXRTileAttribute;
105
106 typedef struct EXRThreadData {
107 uint8_t *uncompressed_data;
108 int uncompressed_size;
109
110 uint8_t *tmp;
111 int tmp_size;
112
113 uint8_t *bitmap;
114 uint16_t *lut;
115
116 int ysize, xsize;
117
118 int channel_line_size;
119 } EXRThreadData;
120
121 typedef struct EXRContext {
122 AVClass *class;
123 AVFrame *picture;
124 AVCodecContext *avctx;
125 ExrDSPContext dsp;
126
127 #if HAVE_BIGENDIAN
128 BswapDSPContext bbdsp;
129 #endif
130
131 enum ExrCompr compression;
132 enum ExrPixelType pixel_type;
133 int channel_offsets[4]; // 0 = red, 1 = green, 2 = blue and 3 = alpha
134 const AVPixFmtDescriptor *desc;
135
136 int w, h;
137 uint32_t xmax, xmin;
138 uint32_t ymax, ymin;
139 uint32_t xdelta, ydelta;
140
141 int scan_lines_per_block;
142
143 EXRTileAttribute tile_attr; /* header data attribute of tile */
144 int is_tile; /* 0 if scanline, 1 if tile */
145
146 int is_luma;/* 1 if there is an Y plane */
147
148 GetByteContext gb;
149 const uint8_t *buf;
150 int buf_size;
151
152 EXRChannel *channels;
153 int nb_channels;
154 int current_channel_offset;
155
156 EXRThreadData *thread_data;
157
158 const char *layer;
159
160 enum AVColorTransferCharacteristic apply_trc_type;
161 float gamma;
162 union av_intfloat32 gamma_table[65536];
163 } EXRContext;
164
165 /* -15 stored using a single precision bias of 127 */
166 #define HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP 0x38000000
167
168 /* max exponent value in single precision that will be converted
169 * to Inf or Nan when stored as a half-float */
170 #define HALF_FLOAT_MAX_BIASED_EXP_AS_SINGLE_FP_EXP 0x47800000
171
172 /* 255 is the max exponent biased value */
173 #define FLOAT_MAX_BIASED_EXP (0xFF << 23)
174
175 #define HALF_FLOAT_MAX_BIASED_EXP (0x1F << 10)
176
177 /**
178 * Convert a half float as a uint16_t into a full float.
179 *
180 * @param hf half float as uint16_t
181 *
182 * @return float value
183 */
exr_half2float(uint16_t hf)184 static union av_intfloat32 exr_half2float(uint16_t hf)
185 {
186 unsigned int sign = (unsigned int) (hf >> 15);
187 unsigned int mantissa = (unsigned int) (hf & ((1 << 10) - 1));
188 unsigned int exp = (unsigned int) (hf & HALF_FLOAT_MAX_BIASED_EXP);
189 union av_intfloat32 f;
190
191 if (exp == HALF_FLOAT_MAX_BIASED_EXP) {
192 // we have a half-float NaN or Inf
193 // half-float NaNs will be converted to a single precision NaN
194 // half-float Infs will be converted to a single precision Inf
195 exp = FLOAT_MAX_BIASED_EXP;
196 if (mantissa)
197 mantissa = (1 << 23) - 1; // set all bits to indicate a NaN
198 } else if (exp == 0x0) {
199 // convert half-float zero/denorm to single precision value
200 if (mantissa) {
201 mantissa <<= 1;
202 exp = HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP;
203 // check for leading 1 in denorm mantissa
204 while ((mantissa & (1 << 10))) {
205 // for every leading 0, decrement single precision exponent by 1
206 // and shift half-float mantissa value to the left
207 mantissa <<= 1;
208 exp -= (1 << 23);
209 }
210 // clamp the mantissa to 10 bits
211 mantissa &= ((1 << 10) - 1);
212 // shift left to generate single-precision mantissa of 23 bits
213 mantissa <<= 13;
214 }
215 } else {
216 // shift left to generate single-precision mantissa of 23 bits
217 mantissa <<= 13;
218 // generate single precision biased exponent value
219 exp = (exp << 13) + HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP;
220 }
221
222 f.i = (sign << 31) | exp | mantissa;
223
224 return f;
225 }
226
zip_uncompress(EXRContext * s,const uint8_t * src,int compressed_size,int uncompressed_size,EXRThreadData * td)227 static int zip_uncompress(EXRContext *s, const uint8_t *src, int compressed_size,
228 int uncompressed_size, EXRThreadData *td)
229 {
230 unsigned long dest_len = uncompressed_size;
231
232 if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK ||
233 dest_len != uncompressed_size)
234 return AVERROR_INVALIDDATA;
235
236 av_assert1(uncompressed_size % 2 == 0);
237
238 s->dsp.predictor(td->tmp, uncompressed_size);
239 s->dsp.reorder_pixels(td->uncompressed_data, td->tmp, uncompressed_size);
240
241 return 0;
242 }
243
rle_uncompress(EXRContext * ctx,const uint8_t * src,int compressed_size,int uncompressed_size,EXRThreadData * td)244 static int rle_uncompress(EXRContext *ctx, const uint8_t *src, int compressed_size,
245 int uncompressed_size, EXRThreadData *td)
246 {
247 uint8_t *d = td->tmp;
248 const int8_t *s = src;
249 int ssize = compressed_size;
250 int dsize = uncompressed_size;
251 uint8_t *dend = d + dsize;
252 int count;
253
254 while (ssize > 0) {
255 count = *s++;
256
257 if (count < 0) {
258 count = -count;
259
260 if ((dsize -= count) < 0 ||
261 (ssize -= count + 1) < 0)
262 return AVERROR_INVALIDDATA;
263
264 while (count--)
265 *d++ = *s++;
266 } else {
267 count++;
268
269 if ((dsize -= count) < 0 ||
270 (ssize -= 2) < 0)
271 return AVERROR_INVALIDDATA;
272
273 while (count--)
274 *d++ = *s;
275
276 s++;
277 }
278 }
279
280 if (dend != d)
281 return AVERROR_INVALIDDATA;
282
283 av_assert1(uncompressed_size % 2 == 0);
284
285 ctx->dsp.predictor(td->tmp, uncompressed_size);
286 ctx->dsp.reorder_pixels(td->uncompressed_data, td->tmp, uncompressed_size);
287
288 return 0;
289 }
290
291 #define USHORT_RANGE (1 << 16)
292 #define BITMAP_SIZE (1 << 13)
293
reverse_lut(const uint8_t * bitmap,uint16_t * lut)294 static uint16_t reverse_lut(const uint8_t *bitmap, uint16_t *lut)
295 {
296 int i, k = 0;
297
298 for (i = 0; i < USHORT_RANGE; i++)
299 if ((i == 0) || (bitmap[i >> 3] & (1 << (i & 7))))
300 lut[k++] = i;
301
302 i = k - 1;
303
304 memset(lut + k, 0, (USHORT_RANGE - k) * 2);
305
306 return i;
307 }
308
apply_lut(const uint16_t * lut,uint16_t * dst,int dsize)309 static void apply_lut(const uint16_t *lut, uint16_t *dst, int dsize)
310 {
311 int i;
312
313 for (i = 0; i < dsize; ++i)
314 dst[i] = lut[dst[i]];
315 }
316
317 #define HUF_ENCBITS 16 // literal (value) bit length
318 #define HUF_DECBITS 14 // decoding bit size (>= 8)
319
320 #define HUF_ENCSIZE ((1 << HUF_ENCBITS) + 1) // encoding table size
321 #define HUF_DECSIZE (1 << HUF_DECBITS) // decoding table size
322 #define HUF_DECMASK (HUF_DECSIZE - 1)
323
324 typedef struct HufDec {
325 int len;
326 int lit;
327 int *p;
328 } HufDec;
329
huf_canonical_code_table(uint64_t * hcode)330 static void huf_canonical_code_table(uint64_t *hcode)
331 {
332 uint64_t c, n[59] = { 0 };
333 int i;
334
335 for (i = 0; i < HUF_ENCSIZE; ++i)
336 n[hcode[i]] += 1;
337
338 c = 0;
339 for (i = 58; i > 0; --i) {
340 uint64_t nc = ((c + n[i]) >> 1);
341 n[i] = c;
342 c = nc;
343 }
344
345 for (i = 0; i < HUF_ENCSIZE; ++i) {
346 int l = hcode[i];
347
348 if (l > 0)
349 hcode[i] = l | (n[l]++ << 6);
350 }
351 }
352
353 #define SHORT_ZEROCODE_RUN 59
354 #define LONG_ZEROCODE_RUN 63
355 #define SHORTEST_LONG_RUN (2 + LONG_ZEROCODE_RUN - SHORT_ZEROCODE_RUN)
356 #define LONGEST_LONG_RUN (255 + SHORTEST_LONG_RUN)
357
huf_unpack_enc_table(GetByteContext * gb,int32_t im,int32_t iM,uint64_t * hcode)358 static int huf_unpack_enc_table(GetByteContext *gb,
359 int32_t im, int32_t iM, uint64_t *hcode)
360 {
361 GetBitContext gbit;
362 int ret = init_get_bits8(&gbit, gb->buffer, bytestream2_get_bytes_left(gb));
363 if (ret < 0)
364 return ret;
365
366 for (; im <= iM; im++) {
367 uint64_t l = hcode[im] = get_bits(&gbit, 6);
368
369 if (l == LONG_ZEROCODE_RUN) {
370 int zerun = get_bits(&gbit, 8) + SHORTEST_LONG_RUN;
371
372 if (im + zerun > iM + 1)
373 return AVERROR_INVALIDDATA;
374
375 while (zerun--)
376 hcode[im++] = 0;
377
378 im--;
379 } else if (l >= SHORT_ZEROCODE_RUN) {
380 int zerun = l - SHORT_ZEROCODE_RUN + 2;
381
382 if (im + zerun > iM + 1)
383 return AVERROR_INVALIDDATA;
384
385 while (zerun--)
386 hcode[im++] = 0;
387
388 im--;
389 }
390 }
391
392 bytestream2_skip(gb, (get_bits_count(&gbit) + 7) / 8);
393 huf_canonical_code_table(hcode);
394
395 return 0;
396 }
397
huf_build_dec_table(const uint64_t * hcode,int im,int iM,HufDec * hdecod)398 static int huf_build_dec_table(const uint64_t *hcode, int im,
399 int iM, HufDec *hdecod)
400 {
401 for (; im <= iM; im++) {
402 uint64_t c = hcode[im] >> 6;
403 int i, l = hcode[im] & 63;
404
405 if (c >> l)
406 return AVERROR_INVALIDDATA;
407
408 if (l > HUF_DECBITS) {
409 HufDec *pl = hdecod + (c >> (l - HUF_DECBITS));
410 if (pl->len)
411 return AVERROR_INVALIDDATA;
412
413 pl->lit++;
414
415 pl->p = av_realloc(pl->p, pl->lit * sizeof(int));
416 if (!pl->p)
417 return AVERROR(ENOMEM);
418
419 pl->p[pl->lit - 1] = im;
420 } else if (l) {
421 HufDec *pl = hdecod + (c << (HUF_DECBITS - l));
422
423 for (i = 1 << (HUF_DECBITS - l); i > 0; i--, pl++) {
424 if (pl->len || pl->p)
425 return AVERROR_INVALIDDATA;
426 pl->len = l;
427 pl->lit = im;
428 }
429 }
430 }
431
432 return 0;
433 }
434
435 #define get_char(c, lc, gb) \
436 { \
437 c = (c << 8) | bytestream2_get_byte(gb); \
438 lc += 8; \
439 }
440
441 #define get_code(po, rlc, c, lc, gb, out, oe, outb) \
442 { \
443 if (po == rlc) { \
444 if (lc < 8) \
445 get_char(c, lc, gb); \
446 lc -= 8; \
447 \
448 cs = c >> lc; \
449 \
450 if (out + cs > oe || out == outb) \
451 return AVERROR_INVALIDDATA; \
452 \
453 s = out[-1]; \
454 \
455 while (cs-- > 0) \
456 *out++ = s; \
457 } else if (out < oe) { \
458 *out++ = po; \
459 } else { \
460 return AVERROR_INVALIDDATA; \
461 } \
462 }
463
huf_decode(const uint64_t * hcode,const HufDec * hdecod,GetByteContext * gb,int nbits,int rlc,int no,uint16_t * out)464 static int huf_decode(const uint64_t *hcode, const HufDec *hdecod,
465 GetByteContext *gb, int nbits,
466 int rlc, int no, uint16_t *out)
467 {
468 uint64_t c = 0;
469 uint16_t *outb = out;
470 uint16_t *oe = out + no;
471 const uint8_t *ie = gb->buffer + (nbits + 7) / 8; // input byte size
472 uint8_t cs;
473 uint16_t s;
474 int i, lc = 0;
475
476 while (gb->buffer < ie) {
477 get_char(c, lc, gb);
478
479 while (lc >= HUF_DECBITS) {
480 const HufDec pl = hdecod[(c >> (lc - HUF_DECBITS)) & HUF_DECMASK];
481
482 if (pl.len) {
483 lc -= pl.len;
484 get_code(pl.lit, rlc, c, lc, gb, out, oe, outb);
485 } else {
486 int j;
487
488 if (!pl.p)
489 return AVERROR_INVALIDDATA;
490
491 for (j = 0; j < pl.lit; j++) {
492 int l = hcode[pl.p[j]] & 63;
493
494 while (lc < l && bytestream2_get_bytes_left(gb) > 0)
495 get_char(c, lc, gb);
496
497 if (lc >= l) {
498 if ((hcode[pl.p[j]] >> 6) ==
499 ((c >> (lc - l)) & ((1LL << l) - 1))) {
500 lc -= l;
501 get_code(pl.p[j], rlc, c, lc, gb, out, oe, outb);
502 break;
503 }
504 }
505 }
506
507 if (j == pl.lit)
508 return AVERROR_INVALIDDATA;
509 }
510 }
511 }
512
513 i = (8 - nbits) & 7;
514 c >>= i;
515 lc -= i;
516
517 while (lc > 0) {
518 const HufDec pl = hdecod[(c << (HUF_DECBITS - lc)) & HUF_DECMASK];
519
520 if (pl.len && lc >= pl.len) {
521 lc -= pl.len;
522 get_code(pl.lit, rlc, c, lc, gb, out, oe, outb);
523 } else {
524 return AVERROR_INVALIDDATA;
525 }
526 }
527
528 if (out - outb != no)
529 return AVERROR_INVALIDDATA;
530 return 0;
531 }
532
huf_uncompress(GetByteContext * gb,uint16_t * dst,int dst_size)533 static int huf_uncompress(GetByteContext *gb,
534 uint16_t *dst, int dst_size)
535 {
536 int32_t src_size, im, iM;
537 uint32_t nBits;
538 uint64_t *freq;
539 HufDec *hdec;
540 int ret, i;
541
542 src_size = bytestream2_get_le32(gb);
543 im = bytestream2_get_le32(gb);
544 iM = bytestream2_get_le32(gb);
545 bytestream2_skip(gb, 4);
546 nBits = bytestream2_get_le32(gb);
547 if (im < 0 || im >= HUF_ENCSIZE ||
548 iM < 0 || iM >= HUF_ENCSIZE ||
549 src_size < 0)
550 return AVERROR_INVALIDDATA;
551
552 bytestream2_skip(gb, 4);
553
554 freq = av_mallocz_array(HUF_ENCSIZE, sizeof(*freq));
555 hdec = av_mallocz_array(HUF_DECSIZE, sizeof(*hdec));
556 if (!freq || !hdec) {
557 ret = AVERROR(ENOMEM);
558 goto fail;
559 }
560
561 if ((ret = huf_unpack_enc_table(gb, im, iM, freq)) < 0)
562 goto fail;
563
564 if (nBits > 8 * bytestream2_get_bytes_left(gb)) {
565 ret = AVERROR_INVALIDDATA;
566 goto fail;
567 }
568
569 if ((ret = huf_build_dec_table(freq, im, iM, hdec)) < 0)
570 goto fail;
571 ret = huf_decode(freq, hdec, gb, nBits, iM, dst_size, dst);
572
573 fail:
574 for (i = 0; i < HUF_DECSIZE; i++)
575 if (hdec)
576 av_freep(&hdec[i].p);
577
578 av_free(freq);
579 av_free(hdec);
580
581 return ret;
582 }
583
wdec14(uint16_t l,uint16_t h,uint16_t * a,uint16_t * b)584 static inline void wdec14(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
585 {
586 int16_t ls = l;
587 int16_t hs = h;
588 int hi = hs;
589 int ai = ls + (hi & 1) + (hi >> 1);
590 int16_t as = ai;
591 int16_t bs = ai - hi;
592
593 *a = as;
594 *b = bs;
595 }
596
597 #define NBITS 16
598 #define A_OFFSET (1 << (NBITS - 1))
599 #define MOD_MASK ((1 << NBITS) - 1)
600
wdec16(uint16_t l,uint16_t h,uint16_t * a,uint16_t * b)601 static inline void wdec16(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
602 {
603 int m = l;
604 int d = h;
605 int bb = (m - (d >> 1)) & MOD_MASK;
606 int aa = (d + bb - A_OFFSET) & MOD_MASK;
607 *b = bb;
608 *a = aa;
609 }
610
wav_decode(uint16_t * in,int nx,int ox,int ny,int oy,uint16_t mx)611 static void wav_decode(uint16_t *in, int nx, int ox,
612 int ny, int oy, uint16_t mx)
613 {
614 int w14 = (mx < (1 << 14));
615 int n = (nx > ny) ? ny : nx;
616 int p = 1;
617 int p2;
618
619 while (p <= n)
620 p <<= 1;
621
622 p >>= 1;
623 p2 = p;
624 p >>= 1;
625
626 while (p >= 1) {
627 uint16_t *py = in;
628 uint16_t *ey = in + oy * (ny - p2);
629 uint16_t i00, i01, i10, i11;
630 int oy1 = oy * p;
631 int oy2 = oy * p2;
632 int ox1 = ox * p;
633 int ox2 = ox * p2;
634
635 for (; py <= ey; py += oy2) {
636 uint16_t *px = py;
637 uint16_t *ex = py + ox * (nx - p2);
638
639 for (; px <= ex; px += ox2) {
640 uint16_t *p01 = px + ox1;
641 uint16_t *p10 = px + oy1;
642 uint16_t *p11 = p10 + ox1;
643
644 if (w14) {
645 wdec14(*px, *p10, &i00, &i10);
646 wdec14(*p01, *p11, &i01, &i11);
647 wdec14(i00, i01, px, p01);
648 wdec14(i10, i11, p10, p11);
649 } else {
650 wdec16(*px, *p10, &i00, &i10);
651 wdec16(*p01, *p11, &i01, &i11);
652 wdec16(i00, i01, px, p01);
653 wdec16(i10, i11, p10, p11);
654 }
655 }
656
657 if (nx & p) {
658 uint16_t *p10 = px + oy1;
659
660 if (w14)
661 wdec14(*px, *p10, &i00, p10);
662 else
663 wdec16(*px, *p10, &i00, p10);
664
665 *px = i00;
666 }
667 }
668
669 if (ny & p) {
670 uint16_t *px = py;
671 uint16_t *ex = py + ox * (nx - p2);
672
673 for (; px <= ex; px += ox2) {
674 uint16_t *p01 = px + ox1;
675
676 if (w14)
677 wdec14(*px, *p01, &i00, p01);
678 else
679 wdec16(*px, *p01, &i00, p01);
680
681 *px = i00;
682 }
683 }
684
685 p2 = p;
686 p >>= 1;
687 }
688 }
689
piz_uncompress(EXRContext * s,const uint8_t * src,int ssize,int dsize,EXRThreadData * td)690 static int piz_uncompress(EXRContext *s, const uint8_t *src, int ssize,
691 int dsize, EXRThreadData *td)
692 {
693 GetByteContext gb;
694 uint16_t maxval, min_non_zero, max_non_zero;
695 uint16_t *ptr;
696 uint16_t *tmp = (uint16_t *)td->tmp;
697 uint16_t *out;
698 uint16_t *in;
699 int ret, i, j;
700 int pixel_half_size;/* 1 for half, 2 for float and uint32 */
701 EXRChannel *channel;
702 int tmp_offset;
703
704 if (!td->bitmap)
705 td->bitmap = av_malloc(BITMAP_SIZE);
706 if (!td->lut)
707 td->lut = av_malloc(1 << 17);
708 if (!td->bitmap || !td->lut) {
709 av_freep(&td->bitmap);
710 av_freep(&td->lut);
711 return AVERROR(ENOMEM);
712 }
713
714 bytestream2_init(&gb, src, ssize);
715 min_non_zero = bytestream2_get_le16(&gb);
716 max_non_zero = bytestream2_get_le16(&gb);
717
718 if (max_non_zero >= BITMAP_SIZE)
719 return AVERROR_INVALIDDATA;
720
721 memset(td->bitmap, 0, FFMIN(min_non_zero, BITMAP_SIZE));
722 if (min_non_zero <= max_non_zero)
723 bytestream2_get_buffer(&gb, td->bitmap + min_non_zero,
724 max_non_zero - min_non_zero + 1);
725 memset(td->bitmap + max_non_zero + 1, 0, BITMAP_SIZE - max_non_zero - 1);
726
727 maxval = reverse_lut(td->bitmap, td->lut);
728
729 ret = huf_uncompress(&gb, tmp, dsize / sizeof(uint16_t));
730 if (ret)
731 return ret;
732
733 ptr = tmp;
734 for (i = 0; i < s->nb_channels; i++) {
735 channel = &s->channels[i];
736
737 if (channel->pixel_type == EXR_HALF)
738 pixel_half_size = 1;
739 else
740 pixel_half_size = 2;
741
742 for (j = 0; j < pixel_half_size; j++)
743 wav_decode(ptr + j, td->xsize, pixel_half_size, td->ysize,
744 td->xsize * pixel_half_size, maxval);
745 ptr += td->xsize * td->ysize * pixel_half_size;
746 }
747
748 apply_lut(td->lut, tmp, dsize / sizeof(uint16_t));
749
750 out = (uint16_t *)td->uncompressed_data;
751 for (i = 0; i < td->ysize; i++) {
752 tmp_offset = 0;
753 for (j = 0; j < s->nb_channels; j++) {
754 channel = &s->channels[j];
755 if (channel->pixel_type == EXR_HALF)
756 pixel_half_size = 1;
757 else
758 pixel_half_size = 2;
759
760 in = tmp + tmp_offset * td->xsize * td->ysize + i * td->xsize * pixel_half_size;
761 tmp_offset += pixel_half_size;
762
763 #if HAVE_BIGENDIAN
764 s->bbdsp.bswap16_buf(out, in, td->xsize * pixel_half_size);
765 #else
766 memcpy(out, in, td->xsize * 2 * pixel_half_size);
767 #endif
768 out += td->xsize * pixel_half_size;
769 }
770 }
771
772 return 0;
773 }
774
pxr24_uncompress(EXRContext * s,const uint8_t * src,int compressed_size,int uncompressed_size,EXRThreadData * td)775 static int pxr24_uncompress(EXRContext *s, const uint8_t *src,
776 int compressed_size, int uncompressed_size,
777 EXRThreadData *td)
778 {
779 unsigned long dest_len, expected_len = 0;
780 const uint8_t *in = td->tmp;
781 uint8_t *out;
782 int c, i, j;
783
784 for (i = 0; i < s->nb_channels; i++) {
785 if (s->channels[i].pixel_type == EXR_FLOAT) {
786 expected_len += (td->xsize * td->ysize * 3);/* PRX 24 store float in 24 bit instead of 32 */
787 } else if (s->channels[i].pixel_type == EXR_HALF) {
788 expected_len += (td->xsize * td->ysize * 2);
789 } else {//UINT 32
790 expected_len += (td->xsize * td->ysize * 4);
791 }
792 }
793
794 dest_len = expected_len;
795
796 if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK) {
797 return AVERROR_INVALIDDATA;
798 } else if (dest_len != expected_len) {
799 return AVERROR_INVALIDDATA;
800 }
801
802 out = td->uncompressed_data;
803 for (i = 0; i < td->ysize; i++)
804 for (c = 0; c < s->nb_channels; c++) {
805 EXRChannel *channel = &s->channels[c];
806 const uint8_t *ptr[4];
807 uint32_t pixel = 0;
808
809 switch (channel->pixel_type) {
810 case EXR_FLOAT:
811 ptr[0] = in;
812 ptr[1] = ptr[0] + td->xsize;
813 ptr[2] = ptr[1] + td->xsize;
814 in = ptr[2] + td->xsize;
815
816 for (j = 0; j < td->xsize; ++j) {
817 uint32_t diff = ((unsigned)*(ptr[0]++) << 24) |
818 (*(ptr[1]++) << 16) |
819 (*(ptr[2]++) << 8);
820 pixel += diff;
821 bytestream_put_le32(&out, pixel);
822 }
823 break;
824 case EXR_HALF:
825 ptr[0] = in;
826 ptr[1] = ptr[0] + td->xsize;
827 in = ptr[1] + td->xsize;
828 for (j = 0; j < td->xsize; j++) {
829 uint32_t diff = (*(ptr[0]++) << 8) | *(ptr[1]++);
830
831 pixel += diff;
832 bytestream_put_le16(&out, pixel);
833 }
834 break;
835 case EXR_UINT:
836 ptr[0] = in;
837 ptr[1] = ptr[0] + s->xdelta;
838 ptr[2] = ptr[1] + s->xdelta;
839 ptr[3] = ptr[2] + s->xdelta;
840 in = ptr[3] + s->xdelta;
841
842 for (j = 0; j < s->xdelta; ++j) {
843 uint32_t diff = ((uint32_t)*(ptr[0]++) << 24) |
844 (*(ptr[1]++) << 16) |
845 (*(ptr[2]++) << 8 ) |
846 (*(ptr[3]++));
847 pixel += diff;
848 bytestream_put_le32(&out, pixel);
849 }
850 break;
851 default:
852 return AVERROR_INVALIDDATA;
853 }
854 }
855
856 return 0;
857 }
858
unpack_14(const uint8_t b[14],uint16_t s[16])859 static void unpack_14(const uint8_t b[14], uint16_t s[16])
860 {
861 unsigned short shift = (b[ 2] >> 2) & 15;
862 unsigned short bias = (0x20 << shift);
863 int i;
864
865 s[ 0] = (b[0] << 8) | b[1];
866
867 s[ 4] = s[ 0] + ((((b[ 2] << 4) | (b[ 3] >> 4)) & 0x3f) << shift) - bias;
868 s[ 8] = s[ 4] + ((((b[ 3] << 2) | (b[ 4] >> 6)) & 0x3f) << shift) - bias;
869 s[12] = s[ 8] + ((b[ 4] & 0x3f) << shift) - bias;
870
871 s[ 1] = s[ 0] + ((b[ 5] >> 2) << shift) - bias;
872 s[ 5] = s[ 4] + ((((b[ 5] << 4) | (b[ 6] >> 4)) & 0x3f) << shift) - bias;
873 s[ 9] = s[ 8] + ((((b[ 6] << 2) | (b[ 7] >> 6)) & 0x3f) << shift) - bias;
874 s[13] = s[12] + ((b[ 7] & 0x3f) << shift) - bias;
875
876 s[ 2] = s[ 1] + ((b[ 8] >> 2) << shift) - bias;
877 s[ 6] = s[ 5] + ((((b[ 8] << 4) | (b[ 9] >> 4)) & 0x3f) << shift) - bias;
878 s[10] = s[ 9] + ((((b[ 9] << 2) | (b[10] >> 6)) & 0x3f) << shift) - bias;
879 s[14] = s[13] + ((b[10] & 0x3f) << shift) - bias;
880
881 s[ 3] = s[ 2] + ((b[11] >> 2) << shift) - bias;
882 s[ 7] = s[ 6] + ((((b[11] << 4) | (b[12] >> 4)) & 0x3f) << shift) - bias;
883 s[11] = s[10] + ((((b[12] << 2) | (b[13] >> 6)) & 0x3f) << shift) - bias;
884 s[15] = s[14] + ((b[13] & 0x3f) << shift) - bias;
885
886 for (i = 0; i < 16; ++i) {
887 if (s[i] & 0x8000)
888 s[i] &= 0x7fff;
889 else
890 s[i] = ~s[i];
891 }
892 }
893
unpack_3(const uint8_t b[3],uint16_t s[16])894 static void unpack_3(const uint8_t b[3], uint16_t s[16])
895 {
896 int i;
897
898 s[0] = (b[0] << 8) | b[1];
899
900 if (s[0] & 0x8000)
901 s[0] &= 0x7fff;
902 else
903 s[0] = ~s[0];
904
905 for (i = 1; i < 16; i++)
906 s[i] = s[0];
907 }
908
909
b44_uncompress(EXRContext * s,const uint8_t * src,int compressed_size,int uncompressed_size,EXRThreadData * td)910 static int b44_uncompress(EXRContext *s, const uint8_t *src, int compressed_size,
911 int uncompressed_size, EXRThreadData *td) {
912 const int8_t *sr = src;
913 int stay_to_uncompress = compressed_size;
914 int nb_b44_block_w, nb_b44_block_h;
915 int index_tl_x, index_tl_y, index_out, index_tmp;
916 uint16_t tmp_buffer[16]; /* B44 use 4x4 half float pixel */
917 int c, iY, iX, y, x;
918 int target_channel_offset = 0;
919
920 /* calc B44 block count */
921 nb_b44_block_w = td->xsize / 4;
922 if ((td->xsize % 4) != 0)
923 nb_b44_block_w++;
924
925 nb_b44_block_h = td->ysize / 4;
926 if ((td->ysize % 4) != 0)
927 nb_b44_block_h++;
928
929 for (c = 0; c < s->nb_channels; c++) {
930 if (s->channels[c].pixel_type == EXR_HALF) {/* B44 only compress half float data */
931 for (iY = 0; iY < nb_b44_block_h; iY++) {
932 for (iX = 0; iX < nb_b44_block_w; iX++) {/* For each B44 block */
933 if (stay_to_uncompress < 3) {
934 av_log(s, AV_LOG_ERROR, "Not enough data for B44A block: %d", stay_to_uncompress);
935 return AVERROR_INVALIDDATA;
936 }
937
938 if (src[compressed_size - stay_to_uncompress + 2] == 0xfc) { /* B44A block */
939 unpack_3(sr, tmp_buffer);
940 sr += 3;
941 stay_to_uncompress -= 3;
942 } else {/* B44 Block */
943 if (stay_to_uncompress < 14) {
944 av_log(s, AV_LOG_ERROR, "Not enough data for B44 block: %d", stay_to_uncompress);
945 return AVERROR_INVALIDDATA;
946 }
947 unpack_14(sr, tmp_buffer);
948 sr += 14;
949 stay_to_uncompress -= 14;
950 }
951
952 /* copy data to uncompress buffer (B44 block can exceed target resolution)*/
953 index_tl_x = iX * 4;
954 index_tl_y = iY * 4;
955
956 for (y = index_tl_y; y < FFMIN(index_tl_y + 4, td->ysize); y++) {
957 for (x = index_tl_x; x < FFMIN(index_tl_x + 4, td->xsize); x++) {
958 index_out = target_channel_offset * td->xsize + y * td->channel_line_size + 2 * x;
959 index_tmp = (y-index_tl_y) * 4 + (x-index_tl_x);
960 td->uncompressed_data[index_out] = tmp_buffer[index_tmp] & 0xff;
961 td->uncompressed_data[index_out + 1] = tmp_buffer[index_tmp] >> 8;
962 }
963 }
964 }
965 }
966 target_channel_offset += 2;
967 } else {/* Float or UINT 32 channel */
968 if (stay_to_uncompress < td->ysize * td->xsize * 4) {
969 av_log(s, AV_LOG_ERROR, "Not enough data for uncompress channel: %d", stay_to_uncompress);
970 return AVERROR_INVALIDDATA;
971 }
972
973 for (y = 0; y < td->ysize; y++) {
974 index_out = target_channel_offset * td->xsize + y * td->channel_line_size;
975 memcpy(&td->uncompressed_data[index_out], sr, td->xsize * 4);
976 sr += td->xsize * 4;
977 }
978 target_channel_offset += 4;
979
980 stay_to_uncompress -= td->ysize * td->xsize * 4;
981 }
982 }
983
984 return 0;
985 }
986
decode_block(AVCodecContext * avctx,void * tdata,int jobnr,int threadnr)987 static int decode_block(AVCodecContext *avctx, void *tdata,
988 int jobnr, int threadnr)
989 {
990 EXRContext *s = avctx->priv_data;
991 AVFrame *const p = s->picture;
992 EXRThreadData *td = &s->thread_data[threadnr];
993 const uint8_t *channel_buffer[4] = { 0 };
994 const uint8_t *buf = s->buf;
995 uint64_t line_offset, uncompressed_size;
996 uint8_t *ptr;
997 uint32_t data_size;
998 uint64_t line, col = 0;
999 uint64_t tile_x, tile_y, tile_level_x, tile_level_y;
1000 const uint8_t *src;
1001 int step = s->desc->flags & AV_PIX_FMT_FLAG_FLOAT ? 4 : 2 * s->desc->nb_components;
1002 int axmax = (avctx->width - (s->xmax + 1)) * step; /* nb pixel to add at the right of the datawindow */
1003 int bxmin = s->xmin * step; /* nb pixel to add at the left of the datawindow */
1004 int i, x, buf_size = s->buf_size;
1005 int c, rgb_channel_count;
1006 float one_gamma = 1.0f / s->gamma;
1007 avpriv_trc_function trc_func = avpriv_get_trc_function_from_trc(s->apply_trc_type);
1008 int ret;
1009
1010 line_offset = AV_RL64(s->gb.buffer + jobnr * 8);
1011
1012 if (s->is_tile) {
1013 if (buf_size < 20 || line_offset > buf_size - 20)
1014 return AVERROR_INVALIDDATA;
1015
1016 src = buf + line_offset + 20;
1017
1018 tile_x = AV_RL32(src - 20);
1019 tile_y = AV_RL32(src - 16);
1020 tile_level_x = AV_RL32(src - 12);
1021 tile_level_y = AV_RL32(src - 8);
1022
1023 data_size = AV_RL32(src - 4);
1024 if (data_size <= 0 || data_size > buf_size - line_offset - 20)
1025 return AVERROR_INVALIDDATA;
1026
1027 if (tile_level_x || tile_level_y) { /* tile level, is not the full res level */
1028 avpriv_report_missing_feature(s->avctx, "Subres tile before full res tile");
1029 return AVERROR_PATCHWELCOME;
1030 }
1031
1032 if (s->xmin || s->ymin) {
1033 avpriv_report_missing_feature(s->avctx, "Tiles with xmin/ymin");
1034 return AVERROR_PATCHWELCOME;
1035 }
1036
1037 line = s->tile_attr.ySize * tile_y;
1038 col = s->tile_attr.xSize * tile_x;
1039
1040 if (line < s->ymin || line > s->ymax ||
1041 col < s->xmin || col > s->xmax)
1042 return AVERROR_INVALIDDATA;
1043
1044 td->ysize = FFMIN(s->tile_attr.ySize, s->ydelta - tile_y * s->tile_attr.ySize);
1045 td->xsize = FFMIN(s->tile_attr.xSize, s->xdelta - tile_x * s->tile_attr.xSize);
1046
1047 if (col) { /* not the first tile of the line */
1048 bxmin = 0; /* doesn't add pixel at the left of the datawindow */
1049 }
1050
1051 if ((col + td->xsize) != s->xdelta)/* not the last tile of the line */
1052 axmax = 0; /* doesn't add pixel at the right of the datawindow */
1053
1054 if (td->xsize * (uint64_t)s->current_channel_offset > INT_MAX)
1055 return AVERROR_INVALIDDATA;
1056
1057 td->channel_line_size = td->xsize * s->current_channel_offset;/* uncompress size of one line */
1058 uncompressed_size = td->channel_line_size * (uint64_t)td->ysize;/* uncompress size of the block */
1059 } else {
1060 if (buf_size < 8 || line_offset > buf_size - 8)
1061 return AVERROR_INVALIDDATA;
1062
1063 src = buf + line_offset + 8;
1064 line = AV_RL32(src - 8);
1065
1066 if (line < s->ymin || line > s->ymax)
1067 return AVERROR_INVALIDDATA;
1068
1069 data_size = AV_RL32(src - 4);
1070 if (data_size <= 0 || data_size > buf_size - line_offset - 8)
1071 return AVERROR_INVALIDDATA;
1072
1073 td->ysize = FFMIN(s->scan_lines_per_block, s->ymax - line + 1); /* s->ydelta - line ?? */
1074 td->xsize = s->xdelta;
1075
1076 if (td->xsize * (uint64_t)s->current_channel_offset > INT_MAX)
1077 return AVERROR_INVALIDDATA;
1078
1079 td->channel_line_size = td->xsize * s->current_channel_offset;/* uncompress size of one line */
1080 uncompressed_size = td->channel_line_size * (uint64_t)td->ysize;/* uncompress size of the block */
1081
1082 if ((s->compression == EXR_RAW && (data_size != uncompressed_size ||
1083 line_offset > buf_size - uncompressed_size)) ||
1084 (s->compression != EXR_RAW && (data_size > uncompressed_size ||
1085 line_offset > buf_size - data_size))) {
1086 return AVERROR_INVALIDDATA;
1087 }
1088 }
1089
1090 if (data_size < uncompressed_size || s->is_tile) { /* td->tmp is use for tile reorganization */
1091 av_fast_padded_malloc(&td->tmp, &td->tmp_size, uncompressed_size);
1092 if (!td->tmp)
1093 return AVERROR(ENOMEM);
1094 }
1095
1096 if (data_size < uncompressed_size) {
1097 av_fast_padded_malloc(&td->uncompressed_data,
1098 &td->uncompressed_size, uncompressed_size + 64);/* Force 64 padding for AVX2 reorder_pixels dst */
1099
1100 if (!td->uncompressed_data)
1101 return AVERROR(ENOMEM);
1102
1103 ret = AVERROR_INVALIDDATA;
1104 switch (s->compression) {
1105 case EXR_ZIP1:
1106 case EXR_ZIP16:
1107 ret = zip_uncompress(s, src, data_size, uncompressed_size, td);
1108 break;
1109 case EXR_PIZ:
1110 ret = piz_uncompress(s, src, data_size, uncompressed_size, td);
1111 break;
1112 case EXR_PXR24:
1113 ret = pxr24_uncompress(s, src, data_size, uncompressed_size, td);
1114 break;
1115 case EXR_RLE:
1116 ret = rle_uncompress(s, src, data_size, uncompressed_size, td);
1117 break;
1118 case EXR_B44:
1119 case EXR_B44A:
1120 ret = b44_uncompress(s, src, data_size, uncompressed_size, td);
1121 break;
1122 }
1123 if (ret < 0) {
1124 av_log(avctx, AV_LOG_ERROR, "decode_block() failed.\n");
1125 return ret;
1126 }
1127 src = td->uncompressed_data;
1128 }
1129
1130 if (!s->is_luma) {
1131 channel_buffer[0] = src + td->xsize * s->channel_offsets[0];
1132 channel_buffer[1] = src + td->xsize * s->channel_offsets[1];
1133 channel_buffer[2] = src + td->xsize * s->channel_offsets[2];
1134 rgb_channel_count = 3;
1135 } else { /* put y data in the first channel_buffer */
1136 channel_buffer[0] = src + td->xsize * s->channel_offsets[1];
1137 rgb_channel_count = 1;
1138 }
1139 if (s->channel_offsets[3] >= 0)
1140 channel_buffer[3] = src + td->xsize * s->channel_offsets[3];
1141
1142 if (s->desc->flags & AV_PIX_FMT_FLAG_FLOAT) {
1143
1144 /* todo: change this when a floating point pixel format with luma with alpha is implemented */
1145 int channel_count = s->channel_offsets[3] >= 0 ? 4 : rgb_channel_count;
1146 if (s->is_luma) {
1147 channel_buffer[1] = channel_buffer[0];
1148 channel_buffer[2] = channel_buffer[0];
1149 }
1150
1151 for (c = 0; c < channel_count; c++) {
1152 int plane = s->desc->comp[c].plane;
1153 ptr = p->data[plane] + line * p->linesize[plane] + (col * 4);
1154
1155 for (i = 0; i < td->ysize; i++, ptr += p->linesize[plane]) {
1156 const uint8_t *src;
1157 union av_intfloat32 *ptr_x;
1158
1159 src = channel_buffer[c];
1160 ptr_x = (union av_intfloat32 *)ptr;
1161
1162 // Zero out the start if xmin is not 0
1163 memset(ptr_x, 0, bxmin);
1164 ptr_x += s->xmin;
1165
1166 if (s->pixel_type == EXR_FLOAT) {
1167 // 32-bit
1168 union av_intfloat32 t;
1169 if (trc_func && c < 3) {
1170 for (x = 0; x < td->xsize; x++) {
1171 t.i = bytestream_get_le32(&src);
1172 t.f = trc_func(t.f);
1173 *ptr_x++ = t;
1174 }
1175 } else {
1176 for (x = 0; x < td->xsize; x++) {
1177 t.i = bytestream_get_le32(&src);
1178 if (t.f > 0.0f && c < 3) /* avoid negative values */
1179 t.f = powf(t.f, one_gamma);
1180 *ptr_x++ = t;
1181 }
1182 }
1183 } else if (s->pixel_type == EXR_HALF) {
1184 // 16-bit
1185 if (c < 3) {
1186 for (x = 0; x < td->xsize; x++) {
1187 *ptr_x++ = s->gamma_table[bytestream_get_le16(&src)];
1188 }
1189 } else {
1190 for (x = 0; x < td->xsize; x++) {
1191 *ptr_x++ = exr_half2float(bytestream_get_le16(&src));;
1192 }
1193 }
1194 }
1195
1196 // Zero out the end if xmax+1 is not w
1197 memset(ptr_x, 0, axmax);
1198 channel_buffer[c] += td->channel_line_size;
1199 }
1200 }
1201 } else {
1202
1203 av_assert1(s->pixel_type == EXR_UINT);
1204 ptr = p->data[0] + line * p->linesize[0] + (col * s->desc->nb_components * 2);
1205
1206 for (i = 0; i < td->ysize; i++, ptr += p->linesize[0]) {
1207
1208 const uint8_t * a;
1209 const uint8_t *rgb[3];
1210 uint16_t *ptr_x;
1211
1212 for (c = 0; c < rgb_channel_count; c++) {
1213 rgb[c] = channel_buffer[c];
1214 }
1215
1216 if (channel_buffer[3])
1217 a = channel_buffer[3];
1218
1219 ptr_x = (uint16_t *) ptr;
1220
1221 // Zero out the start if xmin is not 0
1222 memset(ptr_x, 0, bxmin);
1223 ptr_x += s->xmin * s->desc->nb_components;
1224
1225 for (x = 0; x < td->xsize; x++) {
1226 for (c = 0; c < rgb_channel_count; c++) {
1227 *ptr_x++ = bytestream_get_le32(&rgb[c]) >> 16;
1228 }
1229
1230 if (channel_buffer[3])
1231 *ptr_x++ = bytestream_get_le32(&a) >> 16;
1232 }
1233
1234 // Zero out the end if xmax+1 is not w
1235 memset(ptr_x, 0, axmax);
1236
1237 channel_buffer[0] += td->channel_line_size;
1238 channel_buffer[1] += td->channel_line_size;
1239 channel_buffer[2] += td->channel_line_size;
1240 if (channel_buffer[3])
1241 channel_buffer[3] += td->channel_line_size;
1242 }
1243 }
1244
1245 return 0;
1246 }
1247
1248 /**
1249 * Check if the variable name corresponds to its data type.
1250 *
1251 * @param s the EXRContext
1252 * @param value_name name of the variable to check
1253 * @param value_type type of the variable to check
1254 * @param minimum_length minimum length of the variable data
1255 *
1256 * @return bytes to read containing variable data
1257 * -1 if variable is not found
1258 * 0 if buffer ended prematurely
1259 */
check_header_variable(EXRContext * s,const char * value_name,const char * value_type,unsigned int minimum_length)1260 static int check_header_variable(EXRContext *s,
1261 const char *value_name,
1262 const char *value_type,
1263 unsigned int minimum_length)
1264 {
1265 int var_size = -1;
1266
1267 if (bytestream2_get_bytes_left(&s->gb) >= minimum_length &&
1268 !strcmp(s->gb.buffer, value_name)) {
1269 // found value_name, jump to value_type (null terminated strings)
1270 s->gb.buffer += strlen(value_name) + 1;
1271 if (!strcmp(s->gb.buffer, value_type)) {
1272 s->gb.buffer += strlen(value_type) + 1;
1273 var_size = bytestream2_get_le32(&s->gb);
1274 // don't go read past boundaries
1275 if (var_size > bytestream2_get_bytes_left(&s->gb))
1276 var_size = 0;
1277 } else {
1278 // value_type not found, reset the buffer
1279 s->gb.buffer -= strlen(value_name) + 1;
1280 av_log(s->avctx, AV_LOG_WARNING,
1281 "Unknown data type %s for header variable %s.\n",
1282 value_type, value_name);
1283 }
1284 }
1285
1286 return var_size;
1287 }
1288
decode_header(EXRContext * s,AVFrame * frame)1289 static int decode_header(EXRContext *s, AVFrame *frame)
1290 {
1291 AVDictionary *metadata = NULL;
1292 int magic_number, version, i, flags, sar = 0;
1293 int layer_match = 0;
1294 int ret;
1295 int dup_channels = 0;
1296
1297 s->current_channel_offset = 0;
1298 s->xmin = ~0;
1299 s->xmax = ~0;
1300 s->ymin = ~0;
1301 s->ymax = ~0;
1302 s->xdelta = ~0;
1303 s->ydelta = ~0;
1304 s->channel_offsets[0] = -1;
1305 s->channel_offsets[1] = -1;
1306 s->channel_offsets[2] = -1;
1307 s->channel_offsets[3] = -1;
1308 s->pixel_type = EXR_UNKNOWN;
1309 s->compression = EXR_UNKN;
1310 s->nb_channels = 0;
1311 s->w = 0;
1312 s->h = 0;
1313 s->tile_attr.xSize = -1;
1314 s->tile_attr.ySize = -1;
1315 s->is_tile = 0;
1316 s->is_luma = 0;
1317
1318 if (bytestream2_get_bytes_left(&s->gb) < 10) {
1319 av_log(s->avctx, AV_LOG_ERROR, "Header too short to parse.\n");
1320 return AVERROR_INVALIDDATA;
1321 }
1322
1323 magic_number = bytestream2_get_le32(&s->gb);
1324 if (magic_number != 20000630) {
1325 /* As per documentation of OpenEXR, it is supposed to be
1326 * int 20000630 little-endian */
1327 av_log(s->avctx, AV_LOG_ERROR, "Wrong magic number %d.\n", magic_number);
1328 return AVERROR_INVALIDDATA;
1329 }
1330
1331 version = bytestream2_get_byte(&s->gb);
1332 if (version != 2) {
1333 avpriv_report_missing_feature(s->avctx, "Version %d", version);
1334 return AVERROR_PATCHWELCOME;
1335 }
1336
1337 flags = bytestream2_get_le24(&s->gb);
1338
1339 if (flags & 0x02)
1340 s->is_tile = 1;
1341 if (flags & 0x08) {
1342 avpriv_report_missing_feature(s->avctx, "deep data");
1343 return AVERROR_PATCHWELCOME;
1344 }
1345 if (flags & 0x10) {
1346 avpriv_report_missing_feature(s->avctx, "multipart");
1347 return AVERROR_PATCHWELCOME;
1348 }
1349
1350 // Parse the header
1351 while (bytestream2_get_bytes_left(&s->gb) > 0 && *s->gb.buffer) {
1352 int var_size;
1353 if ((var_size = check_header_variable(s, "channels",
1354 "chlist", 38)) >= 0) {
1355 GetByteContext ch_gb;
1356 if (!var_size) {
1357 ret = AVERROR_INVALIDDATA;
1358 goto fail;
1359 }
1360
1361 bytestream2_init(&ch_gb, s->gb.buffer, var_size);
1362
1363 while (bytestream2_get_bytes_left(&ch_gb) >= 19) {
1364 EXRChannel *channel;
1365 enum ExrPixelType current_pixel_type;
1366 int channel_index = -1;
1367 int xsub, ysub;
1368
1369 if (strcmp(s->layer, "") != 0) {
1370 if (strncmp(ch_gb.buffer, s->layer, strlen(s->layer)) == 0) {
1371 layer_match = 1;
1372 av_log(s->avctx, AV_LOG_INFO,
1373 "Channel match layer : %s.\n", ch_gb.buffer);
1374 ch_gb.buffer += strlen(s->layer);
1375 if (*ch_gb.buffer == '.')
1376 ch_gb.buffer++; /* skip dot if not given */
1377 } else {
1378 layer_match = 0;
1379 av_log(s->avctx, AV_LOG_INFO,
1380 "Channel doesn't match layer : %s.\n", ch_gb.buffer);
1381 }
1382 } else {
1383 layer_match = 1;
1384 }
1385
1386 if (layer_match) { /* only search channel if the layer match is valid */
1387 if (!av_strcasecmp(ch_gb.buffer, "R") ||
1388 !av_strcasecmp(ch_gb.buffer, "X") ||
1389 !av_strcasecmp(ch_gb.buffer, "U")) {
1390 channel_index = 0;
1391 s->is_luma = 0;
1392 } else if (!av_strcasecmp(ch_gb.buffer, "G") ||
1393 !av_strcasecmp(ch_gb.buffer, "V")) {
1394 channel_index = 1;
1395 s->is_luma = 0;
1396 } else if (!av_strcasecmp(ch_gb.buffer, "Y")) {
1397 channel_index = 1;
1398 s->is_luma = 1;
1399 } else if (!av_strcasecmp(ch_gb.buffer, "B") ||
1400 !av_strcasecmp(ch_gb.buffer, "Z") ||
1401 !av_strcasecmp(ch_gb.buffer, "W")) {
1402 channel_index = 2;
1403 s->is_luma = 0;
1404 } else if (!av_strcasecmp(ch_gb.buffer, "A")) {
1405 channel_index = 3;
1406 } else {
1407 av_log(s->avctx, AV_LOG_WARNING,
1408 "Unsupported channel %.256s.\n", ch_gb.buffer);
1409 }
1410 }
1411
1412 /* skip until you get a 0 */
1413 while (bytestream2_get_bytes_left(&ch_gb) > 0 &&
1414 bytestream2_get_byte(&ch_gb))
1415 continue;
1416
1417 if (bytestream2_get_bytes_left(&ch_gb) < 4) {
1418 av_log(s->avctx, AV_LOG_ERROR, "Incomplete header.\n");
1419 ret = AVERROR_INVALIDDATA;
1420 goto fail;
1421 }
1422
1423 current_pixel_type = bytestream2_get_le32(&ch_gb);
1424 if (current_pixel_type >= EXR_UNKNOWN) {
1425 avpriv_report_missing_feature(s->avctx, "Pixel type %d",
1426 current_pixel_type);
1427 ret = AVERROR_PATCHWELCOME;
1428 goto fail;
1429 }
1430
1431 bytestream2_skip(&ch_gb, 4);
1432 xsub = bytestream2_get_le32(&ch_gb);
1433 ysub = bytestream2_get_le32(&ch_gb);
1434
1435 if (xsub != 1 || ysub != 1) {
1436 avpriv_report_missing_feature(s->avctx,
1437 "Subsampling %dx%d",
1438 xsub, ysub);
1439 ret = AVERROR_PATCHWELCOME;
1440 goto fail;
1441 }
1442
1443 if (channel_index >= 0 && s->channel_offsets[channel_index] == -1) { /* channel has not been previously assigned */
1444 if (s->pixel_type != EXR_UNKNOWN &&
1445 s->pixel_type != current_pixel_type) {
1446 av_log(s->avctx, AV_LOG_ERROR,
1447 "RGB channels not of the same depth.\n");
1448 ret = AVERROR_INVALIDDATA;
1449 goto fail;
1450 }
1451 s->pixel_type = current_pixel_type;
1452 s->channel_offsets[channel_index] = s->current_channel_offset;
1453 } else if (channel_index >= 0) {
1454 av_log(s->avctx, AV_LOG_WARNING,
1455 "Multiple channels with index %d.\n", channel_index);
1456 if (++dup_channels > 10) {
1457 ret = AVERROR_INVALIDDATA;
1458 goto fail;
1459 }
1460 }
1461
1462 s->channels = av_realloc(s->channels,
1463 ++s->nb_channels * sizeof(EXRChannel));
1464 if (!s->channels) {
1465 ret = AVERROR(ENOMEM);
1466 goto fail;
1467 }
1468 channel = &s->channels[s->nb_channels - 1];
1469 channel->pixel_type = current_pixel_type;
1470 channel->xsub = xsub;
1471 channel->ysub = ysub;
1472
1473 if (current_pixel_type == EXR_HALF) {
1474 s->current_channel_offset += 2;
1475 } else {/* Float or UINT32 */
1476 s->current_channel_offset += 4;
1477 }
1478 }
1479
1480 /* Check if all channels are set with an offset or if the channels
1481 * are causing an overflow */
1482 if (!s->is_luma) {/* if we expected to have at least 3 channels */
1483 if (FFMIN3(s->channel_offsets[0],
1484 s->channel_offsets[1],
1485 s->channel_offsets[2]) < 0) {
1486 if (s->channel_offsets[0] < 0)
1487 av_log(s->avctx, AV_LOG_ERROR, "Missing red channel.\n");
1488 if (s->channel_offsets[1] < 0)
1489 av_log(s->avctx, AV_LOG_ERROR, "Missing green channel.\n");
1490 if (s->channel_offsets[2] < 0)
1491 av_log(s->avctx, AV_LOG_ERROR, "Missing blue channel.\n");
1492 ret = AVERROR_INVALIDDATA;
1493 goto fail;
1494 }
1495 }
1496
1497 // skip one last byte and update main gb
1498 s->gb.buffer = ch_gb.buffer + 1;
1499 continue;
1500 } else if ((var_size = check_header_variable(s, "dataWindow", "box2i",
1501 31)) >= 0) {
1502 int xmin, ymin, xmax, ymax;
1503 if (!var_size) {
1504 ret = AVERROR_INVALIDDATA;
1505 goto fail;
1506 }
1507
1508 xmin = bytestream2_get_le32(&s->gb);
1509 ymin = bytestream2_get_le32(&s->gb);
1510 xmax = bytestream2_get_le32(&s->gb);
1511 ymax = bytestream2_get_le32(&s->gb);
1512
1513 if (xmin > xmax || ymin > ymax ||
1514 (unsigned)xmax - xmin >= INT_MAX ||
1515 (unsigned)ymax - ymin >= INT_MAX) {
1516 ret = AVERROR_INVALIDDATA;
1517 goto fail;
1518 }
1519 s->xmin = xmin;
1520 s->xmax = xmax;
1521 s->ymin = ymin;
1522 s->ymax = ymax;
1523 s->xdelta = (s->xmax - s->xmin) + 1;
1524 s->ydelta = (s->ymax - s->ymin) + 1;
1525
1526 continue;
1527 } else if ((var_size = check_header_variable(s, "displayWindow",
1528 "box2i", 34)) >= 0) {
1529 if (!var_size) {
1530 ret = AVERROR_INVALIDDATA;
1531 goto fail;
1532 }
1533
1534 bytestream2_skip(&s->gb, 8);
1535 s->w = bytestream2_get_le32(&s->gb) + 1;
1536 s->h = bytestream2_get_le32(&s->gb) + 1;
1537
1538 continue;
1539 } else if ((var_size = check_header_variable(s, "lineOrder",
1540 "lineOrder", 25)) >= 0) {
1541 int line_order;
1542 if (!var_size) {
1543 ret = AVERROR_INVALIDDATA;
1544 goto fail;
1545 }
1546
1547 line_order = bytestream2_get_byte(&s->gb);
1548 av_log(s->avctx, AV_LOG_DEBUG, "line order: %d.\n", line_order);
1549 if (line_order > 2) {
1550 av_log(s->avctx, AV_LOG_ERROR, "Unknown line order.\n");
1551 ret = AVERROR_INVALIDDATA;
1552 goto fail;
1553 }
1554
1555 continue;
1556 } else if ((var_size = check_header_variable(s, "pixelAspectRatio",
1557 "float", 31)) >= 0) {
1558 if (!var_size) {
1559 ret = AVERROR_INVALIDDATA;
1560 goto fail;
1561 }
1562
1563 sar = bytestream2_get_le32(&s->gb);
1564
1565 continue;
1566 } else if ((var_size = check_header_variable(s, "compression",
1567 "compression", 29)) >= 0) {
1568 if (!var_size) {
1569 ret = AVERROR_INVALIDDATA;
1570 goto fail;
1571 }
1572
1573 if (s->compression == EXR_UNKN)
1574 s->compression = bytestream2_get_byte(&s->gb);
1575 else
1576 av_log(s->avctx, AV_LOG_WARNING,
1577 "Found more than one compression attribute.\n");
1578
1579 continue;
1580 } else if ((var_size = check_header_variable(s, "tiles",
1581 "tiledesc", 22)) >= 0) {
1582 char tileLevel;
1583
1584 if (!s->is_tile)
1585 av_log(s->avctx, AV_LOG_WARNING,
1586 "Found tile attribute and scanline flags. Exr will be interpreted as scanline.\n");
1587
1588 s->tile_attr.xSize = bytestream2_get_le32(&s->gb);
1589 s->tile_attr.ySize = bytestream2_get_le32(&s->gb);
1590
1591 tileLevel = bytestream2_get_byte(&s->gb);
1592 s->tile_attr.level_mode = tileLevel & 0x0f;
1593 s->tile_attr.level_round = (tileLevel >> 4) & 0x0f;
1594
1595 if (s->tile_attr.level_mode >= EXR_TILE_LEVEL_UNKNOWN) {
1596 avpriv_report_missing_feature(s->avctx, "Tile level mode %d",
1597 s->tile_attr.level_mode);
1598 ret = AVERROR_PATCHWELCOME;
1599 goto fail;
1600 }
1601
1602 if (s->tile_attr.level_round >= EXR_TILE_ROUND_UNKNOWN) {
1603 avpriv_report_missing_feature(s->avctx, "Tile level round %d",
1604 s->tile_attr.level_round);
1605 ret = AVERROR_PATCHWELCOME;
1606 goto fail;
1607 }
1608
1609 continue;
1610 } else if ((var_size = check_header_variable(s, "writer",
1611 "string", 1)) >= 0) {
1612 uint8_t key[256] = { 0 };
1613
1614 bytestream2_get_buffer(&s->gb, key, FFMIN(sizeof(key) - 1, var_size));
1615 av_dict_set(&metadata, "writer", key, 0);
1616
1617 continue;
1618 }
1619
1620 // Check if there are enough bytes for a header
1621 if (bytestream2_get_bytes_left(&s->gb) <= 9) {
1622 av_log(s->avctx, AV_LOG_ERROR, "Incomplete header\n");
1623 ret = AVERROR_INVALIDDATA;
1624 goto fail;
1625 }
1626
1627 // Process unknown variables
1628 for (i = 0; i < 2; i++) // value_name and value_type
1629 while (bytestream2_get_byte(&s->gb) != 0);
1630
1631 // Skip variable length
1632 bytestream2_skip(&s->gb, bytestream2_get_le32(&s->gb));
1633 }
1634
1635 ff_set_sar(s->avctx, av_d2q(av_int2float(sar), 255));
1636
1637 if (s->compression == EXR_UNKN) {
1638 av_log(s->avctx, AV_LOG_ERROR, "Missing compression attribute.\n");
1639 ret = AVERROR_INVALIDDATA;
1640 goto fail;
1641 }
1642
1643 if (s->is_tile) {
1644 if (s->tile_attr.xSize < 1 || s->tile_attr.ySize < 1) {
1645 av_log(s->avctx, AV_LOG_ERROR, "Invalid tile attribute.\n");
1646 ret = AVERROR_INVALIDDATA;
1647 goto fail;
1648 }
1649 }
1650
1651 if (bytestream2_get_bytes_left(&s->gb) <= 0) {
1652 av_log(s->avctx, AV_LOG_ERROR, "Incomplete frame.\n");
1653 ret = AVERROR_INVALIDDATA;
1654 goto fail;
1655 }
1656
1657 frame->metadata = metadata;
1658
1659 // aaand we are done
1660 bytestream2_skip(&s->gb, 1);
1661 return 0;
1662 fail:
1663 av_dict_free(&metadata);
1664 return ret;
1665 }
1666
decode_frame(AVCodecContext * avctx,void * data,int * got_frame,AVPacket * avpkt)1667 static int decode_frame(AVCodecContext *avctx, void *data,
1668 int *got_frame, AVPacket *avpkt)
1669 {
1670 EXRContext *s = avctx->priv_data;
1671 ThreadFrame frame = { .f = data };
1672 AVFrame *picture = data;
1673 uint8_t *ptr;
1674
1675 int i, y, ret;
1676 int planes;
1677 int out_line_size;
1678 int nb_blocks; /* nb scanline or nb tile */
1679 uint64_t start_offset_table;
1680 uint64_t start_next_scanline;
1681 PutByteContext offset_table_writer;
1682
1683 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
1684
1685 if ((ret = decode_header(s, picture)) < 0)
1686 return ret;
1687
1688 switch (s->pixel_type) {
1689 case EXR_FLOAT:
1690 case EXR_HALF:
1691 if (s->channel_offsets[3] >= 0) {
1692 if (!s->is_luma) {
1693 avctx->pix_fmt = AV_PIX_FMT_GBRAPF32;
1694 } else {
1695 /* todo: change this when a floating point pixel format with luma with alpha is implemented */
1696 avctx->pix_fmt = AV_PIX_FMT_GBRAPF32;
1697 }
1698 } else {
1699 if (!s->is_luma) {
1700 avctx->pix_fmt = AV_PIX_FMT_GBRPF32;
1701 } else {
1702 avctx->pix_fmt = AV_PIX_FMT_GRAYF32;
1703 }
1704 }
1705 break;
1706 case EXR_UINT:
1707 if (s->channel_offsets[3] >= 0) {
1708 if (!s->is_luma) {
1709 avctx->pix_fmt = AV_PIX_FMT_RGBA64;
1710 } else {
1711 avctx->pix_fmt = AV_PIX_FMT_YA16;
1712 }
1713 } else {
1714 if (!s->is_luma) {
1715 avctx->pix_fmt = AV_PIX_FMT_RGB48;
1716 } else {
1717 avctx->pix_fmt = AV_PIX_FMT_GRAY16;
1718 }
1719 }
1720 break;
1721 default:
1722 av_log(avctx, AV_LOG_ERROR, "Missing channel list.\n");
1723 return AVERROR_INVALIDDATA;
1724 }
1725
1726 if (s->apply_trc_type != AVCOL_TRC_UNSPECIFIED)
1727 avctx->color_trc = s->apply_trc_type;
1728
1729 switch (s->compression) {
1730 case EXR_RAW:
1731 case EXR_RLE:
1732 case EXR_ZIP1:
1733 s->scan_lines_per_block = 1;
1734 break;
1735 case EXR_PXR24:
1736 case EXR_ZIP16:
1737 s->scan_lines_per_block = 16;
1738 break;
1739 case EXR_PIZ:
1740 case EXR_B44:
1741 case EXR_B44A:
1742 s->scan_lines_per_block = 32;
1743 break;
1744 default:
1745 avpriv_report_missing_feature(avctx, "Compression %d", s->compression);
1746 return AVERROR_PATCHWELCOME;
1747 }
1748
1749 /* Verify the xmin, xmax, ymin, ymax and xdelta before setting
1750 * the actual image size. */
1751 if (s->xmin > s->xmax ||
1752 s->ymin > s->ymax ||
1753 s->xdelta != s->xmax - s->xmin + 1 ||
1754 s->xmax >= s->w ||
1755 s->ymax >= s->h ||
1756 s->ydelta == 0xFFFFFFFF || s->xdelta == 0xFFFFFFFF
1757 ) {
1758 av_log(avctx, AV_LOG_ERROR, "Wrong or missing size information.\n");
1759 return AVERROR_INVALIDDATA;
1760 }
1761
1762 if ((ret = ff_set_dimensions(avctx, s->w, s->h)) < 0)
1763 return ret;
1764
1765 s->desc = av_pix_fmt_desc_get(avctx->pix_fmt);
1766 if (!s->desc)
1767 return AVERROR_INVALIDDATA;
1768
1769 if (s->desc->flags & AV_PIX_FMT_FLAG_FLOAT) {
1770 planes = s->desc->nb_components;
1771 out_line_size = avctx->width * 4;
1772 } else {
1773 planes = 1;
1774 out_line_size = avctx->width * 2 * s->desc->nb_components;
1775 }
1776
1777 if (s->is_tile) {
1778 nb_blocks = ((s->xdelta + s->tile_attr.xSize - 1) / s->tile_attr.xSize) *
1779 ((s->ydelta + s->tile_attr.ySize - 1) / s->tile_attr.ySize);
1780 } else { /* scanline */
1781 nb_blocks = (s->ydelta + s->scan_lines_per_block - 1) /
1782 s->scan_lines_per_block;
1783 }
1784
1785 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
1786 return ret;
1787
1788 if (bytestream2_get_bytes_left(&s->gb)/8 < nb_blocks)
1789 return AVERROR_INVALIDDATA;
1790
1791 // check offset table and recreate it if need
1792 if (!s->is_tile && bytestream2_peek_le64(&s->gb) == 0) {
1793 av_log(s->avctx, AV_LOG_DEBUG, "recreating invalid scanline offset table\n");
1794
1795 start_offset_table = bytestream2_tell(&s->gb);
1796 start_next_scanline = start_offset_table + nb_blocks * 8;
1797 bytestream2_init_writer(&offset_table_writer, &avpkt->data[start_offset_table], nb_blocks * 8);
1798
1799 for (y = 0; y < nb_blocks; y++) {
1800 /* write offset of prev scanline in offset table */
1801 bytestream2_put_le64(&offset_table_writer, start_next_scanline);
1802
1803 /* get len of next scanline */
1804 bytestream2_seek(&s->gb, start_next_scanline + 4, SEEK_SET);/* skip line number */
1805 start_next_scanline += (bytestream2_get_le32(&s->gb) + 8);
1806 }
1807 bytestream2_seek(&s->gb, start_offset_table, SEEK_SET);
1808 }
1809
1810 // save pointer we are going to use in decode_block
1811 s->buf = avpkt->data;
1812 s->buf_size = avpkt->size;
1813
1814 // Zero out the start if ymin is not 0
1815 for (i = 0; i < planes; i++) {
1816 ptr = picture->data[i];
1817 for (y = 0; y < FFMIN(s->ymin, s->h); y++) {
1818 memset(ptr, 0, out_line_size);
1819 ptr += picture->linesize[i];
1820 }
1821 }
1822
1823 s->picture = picture;
1824
1825 avctx->execute2(avctx, decode_block, s->thread_data, NULL, nb_blocks);
1826
1827 // Zero out the end if ymax+1 is not h
1828 for (i = 0; i < planes; i++) {
1829 ptr = picture->data[i] + ((s->ymax+1) * picture->linesize[i]);
1830 for (y = s->ymax + 1; y < avctx->height; y++) {
1831 memset(ptr, 0, out_line_size);
1832 ptr += picture->linesize[i];
1833 }
1834 }
1835
1836 picture->pict_type = AV_PICTURE_TYPE_I;
1837 *got_frame = 1;
1838
1839 return avpkt->size;
1840 }
1841
decode_init(AVCodecContext * avctx)1842 static av_cold int decode_init(AVCodecContext *avctx)
1843 {
1844 EXRContext *s = avctx->priv_data;
1845 uint32_t i;
1846 union av_intfloat32 t;
1847 float one_gamma = 1.0f / s->gamma;
1848 avpriv_trc_function trc_func = NULL;
1849
1850 s->avctx = avctx;
1851
1852 ff_exrdsp_init(&s->dsp);
1853
1854 #if HAVE_BIGENDIAN
1855 ff_bswapdsp_init(&s->bbdsp);
1856 #endif
1857
1858 trc_func = avpriv_get_trc_function_from_trc(s->apply_trc_type);
1859 if (trc_func) {
1860 for (i = 0; i < 65536; ++i) {
1861 t = exr_half2float(i);
1862 t.f = trc_func(t.f);
1863 s->gamma_table[i] = t;
1864 }
1865 } else {
1866 if (one_gamma > 0.9999f && one_gamma < 1.0001f) {
1867 for (i = 0; i < 65536; ++i) {
1868 s->gamma_table[i] = exr_half2float(i);
1869 }
1870 } else {
1871 for (i = 0; i < 65536; ++i) {
1872 t = exr_half2float(i);
1873 /* If negative value we reuse half value */
1874 if (t.f <= 0.0f) {
1875 s->gamma_table[i] = t;
1876 } else {
1877 t.f = powf(t.f, one_gamma);
1878 s->gamma_table[i] = t;
1879 }
1880 }
1881 }
1882 }
1883
1884 // allocate thread data, used for non EXR_RAW compression types
1885 s->thread_data = av_mallocz_array(avctx->thread_count, sizeof(EXRThreadData));
1886 if (!s->thread_data)
1887 return AVERROR_INVALIDDATA;
1888
1889 return 0;
1890 }
1891
decode_end(AVCodecContext * avctx)1892 static av_cold int decode_end(AVCodecContext *avctx)
1893 {
1894 EXRContext *s = avctx->priv_data;
1895 int i;
1896 for (i = 0; i < avctx->thread_count; i++) {
1897 EXRThreadData *td = &s->thread_data[i];
1898 av_freep(&td->uncompressed_data);
1899 av_freep(&td->tmp);
1900 av_freep(&td->bitmap);
1901 av_freep(&td->lut);
1902 }
1903
1904 av_freep(&s->thread_data);
1905 av_freep(&s->channels);
1906
1907 return 0;
1908 }
1909
1910 #define OFFSET(x) offsetof(EXRContext, x)
1911 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1912 static const AVOption options[] = {
1913 { "layer", "Set the decoding layer", OFFSET(layer),
1914 AV_OPT_TYPE_STRING, { .str = "" }, 0, 0, VD },
1915 { "gamma", "Set the float gamma value when decoding", OFFSET(gamma),
1916 AV_OPT_TYPE_FLOAT, { .dbl = 1.0f }, 0.001, FLT_MAX, VD },
1917
1918 // XXX: Note the abuse of the enum using AVCOL_TRC_UNSPECIFIED to subsume the existing gamma option
1919 { "apply_trc", "color transfer characteristics to apply to EXR linear input", OFFSET(apply_trc_type),
1920 AV_OPT_TYPE_INT, {.i64 = AVCOL_TRC_UNSPECIFIED }, 1, AVCOL_TRC_NB-1, VD, "apply_trc_type"},
1921 { "bt709", "BT.709", 0,
1922 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT709 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1923 { "gamma", "gamma", 0,
1924 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_UNSPECIFIED }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1925 { "gamma22", "BT.470 M", 0,
1926 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_GAMMA22 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1927 { "gamma28", "BT.470 BG", 0,
1928 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_GAMMA28 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1929 { "smpte170m", "SMPTE 170 M", 0,
1930 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTE170M }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1931 { "smpte240m", "SMPTE 240 M", 0,
1932 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTE240M }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1933 { "linear", "Linear", 0,
1934 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LINEAR }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1935 { "log", "Log", 0,
1936 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LOG }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1937 { "log_sqrt", "Log square root", 0,
1938 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LOG_SQRT }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1939 { "iec61966_2_4", "IEC 61966-2-4", 0,
1940 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_IEC61966_2_4 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1941 { "bt1361", "BT.1361", 0,
1942 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT1361_ECG }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1943 { "iec61966_2_1", "IEC 61966-2-1", 0,
1944 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_IEC61966_2_1 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1945 { "bt2020_10bit", "BT.2020 - 10 bit", 0,
1946 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT2020_10 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1947 { "bt2020_12bit", "BT.2020 - 12 bit", 0,
1948 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT2020_12 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1949 { "smpte2084", "SMPTE ST 2084", 0,
1950 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTEST2084 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1951 { "smpte428_1", "SMPTE ST 428-1", 0,
1952 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTEST428_1 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1953
1954 { NULL },
1955 };
1956
1957 static const AVClass exr_class = {
1958 .class_name = "EXR",
1959 .item_name = av_default_item_name,
1960 .option = options,
1961 .version = LIBAVUTIL_VERSION_INT,
1962 };
1963
1964 AVCodec ff_exr_decoder = {
1965 .name = "exr",
1966 .long_name = NULL_IF_CONFIG_SMALL("OpenEXR image"),
1967 .type = AVMEDIA_TYPE_VIDEO,
1968 .id = AV_CODEC_ID_EXR,
1969 .priv_data_size = sizeof(EXRContext),
1970 .init = decode_init,
1971 .close = decode_end,
1972 .decode = decode_frame,
1973 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS |
1974 AV_CODEC_CAP_SLICE_THREADS,
1975 .priv_class = &exr_class,
1976 };
1977