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