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
6 * B44/B44A, Tile added by Jokyo Images support by CNC - French National Center for Cinema
8 * This file is part of FFmpeg.
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.
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.
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
28 * @author Jimmy Christensen
30 * For more information on the OpenEXR format, visit:
33 * exr_flt2uint() and exr_halflt2uint() is credited to Reimar Döffinger.
34 * exr_half2float() is credited to Aaftab Munshi, Dan Ginsburg, Dave Shreiner.
40 #include "libavutil/common.h"
41 #include "libavutil/imgutils.h"
42 #include "libavutil/intfloat.h"
43 #include "libavutil/opt.h"
44 #include "libavutil/color_utils.h"
47 #include "bytestream.h"
72 enum ExrTileLevelMode {
74 EXR_TILE_LEVEL_MIPMAP,
75 EXR_TILE_LEVEL_RIPMAP,
76 EXR_TILE_LEVEL_UNKNOWN,
79 enum ExrTileLevelRound {
82 EXR_TILE_ROUND_UNKNOWN,
85 typedef struct EXRChannel {
87 enum ExrPixelType pixel_type;
90 typedef struct EXRTileAttribute {
93 enum ExrTileLevelMode level_mode;
94 enum ExrTileLevelRound level_round;
97 typedef struct EXRThreadData {
98 uint8_t *uncompressed_data;
99 int uncompressed_size;
109 int channel_line_size;
112 typedef struct EXRContext {
115 AVCodecContext *avctx;
117 enum ExrCompr compression;
118 enum ExrPixelType pixel_type;
119 int channel_offsets[4]; // 0 = red, 1 = green, 2 = blue and 3 = alpha
120 const AVPixFmtDescriptor *desc;
125 uint32_t xdelta, ydelta;
127 int scan_lines_per_block;
129 EXRTileAttribute tile_attr; /* header data attribute of tile */
130 int is_tile; /* 0 if scanline, 1 if tile */
132 int is_luma;/* 1 if there is an Y plane */
138 EXRChannel *channels;
140 int current_channel_offset;
142 EXRThreadData *thread_data;
146 enum AVColorTransferCharacteristic apply_trc_type;
148 uint16_t gamma_table[65536];
151 /* -15 stored using a single precision bias of 127 */
152 #define HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP 0x38000000
154 /* max exponent value in single precision that will be converted
155 * to Inf or Nan when stored as a half-float */
156 #define HALF_FLOAT_MAX_BIASED_EXP_AS_SINGLE_FP_EXP 0x47800000
158 /* 255 is the max exponent biased value */
159 #define FLOAT_MAX_BIASED_EXP (0xFF << 23)
161 #define HALF_FLOAT_MAX_BIASED_EXP (0x1F << 10)
164 * Convert a half float as a uint16_t into a full float.
166 * @param hf half float as uint16_t
168 * @return float value
170 static union av_intfloat32 exr_half2float(uint16_t hf)
172 unsigned int sign = (unsigned int) (hf >> 15);
173 unsigned int mantissa = (unsigned int) (hf & ((1 << 10) - 1));
174 unsigned int exp = (unsigned int) (hf & HALF_FLOAT_MAX_BIASED_EXP);
175 union av_intfloat32 f;
177 if (exp == HALF_FLOAT_MAX_BIASED_EXP) {
178 // we have a half-float NaN or Inf
179 // half-float NaNs will be converted to a single precision NaN
180 // half-float Infs will be converted to a single precision Inf
181 exp = FLOAT_MAX_BIASED_EXP;
183 mantissa = (1 << 23) - 1; // set all bits to indicate a NaN
184 } else if (exp == 0x0) {
185 // convert half-float zero/denorm to single precision value
188 exp = HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP;
189 // check for leading 1 in denorm mantissa
190 while ((mantissa & (1 << 10))) {
191 // for every leading 0, decrement single precision exponent by 1
192 // and shift half-float mantissa value to the left
196 // clamp the mantissa to 10 bits
197 mantissa &= ((1 << 10) - 1);
198 // shift left to generate single-precision mantissa of 23 bits
202 // shift left to generate single-precision mantissa of 23 bits
204 // generate single precision biased exponent value
205 exp = (exp << 13) + HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP;
208 f.i = (sign << 31) | exp | mantissa;
215 * Convert from 32-bit float as uint32_t to uint16_t.
217 * @param v 32-bit float
219 * @return normalized 16-bit unsigned int
221 static inline uint16_t exr_flt2uint(uint32_t v)
223 unsigned int exp = v >> 23;
224 // "HACK": negative values result in exp< 0, so clipping them to 0
225 // is also handled by this condition, avoids explicit check for sign bit.
226 if (exp <= 127 + 7 - 24) // we would shift out all bits anyway
231 return (v + (1 << 23)) >> (127 + 7 - exp);
235 * Convert from 16-bit float as uint16_t to uint16_t.
237 * @param v 16-bit float
239 * @return normalized 16-bit unsigned int
241 static inline uint16_t exr_halflt2uint(uint16_t v)
243 unsigned exp = 14 - (v >> 10);
248 return (v & 0x8000) ? 0 : 0xffff;
251 return (v + (1 << 16)) >> (exp + 1);
254 static void predictor(uint8_t *src, int size)
256 uint8_t *t = src + 1;
257 uint8_t *stop = src + size;
260 int d = (int) t[-1] + (int) t[0] - 128;
266 static void reorder_pixels(uint8_t *src, uint8_t *dst, int size)
268 const int8_t *t1 = src;
269 const int8_t *t2 = src + (size + 1) / 2;
271 int8_t *stop = s + size;
286 static int zip_uncompress(const uint8_t *src, int compressed_size,
287 int uncompressed_size, EXRThreadData *td)
289 unsigned long dest_len = uncompressed_size;
291 if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK ||
292 dest_len != uncompressed_size)
293 return AVERROR_INVALIDDATA;
295 predictor(td->tmp, uncompressed_size);
296 reorder_pixels(td->tmp, td->uncompressed_data, uncompressed_size);
301 static int rle_uncompress(const uint8_t *src, int compressed_size,
302 int uncompressed_size, EXRThreadData *td)
304 uint8_t *d = td->tmp;
305 const int8_t *s = src;
306 int ssize = compressed_size;
307 int dsize = uncompressed_size;
308 uint8_t *dend = d + dsize;
317 if ((dsize -= count) < 0 ||
318 (ssize -= count + 1) < 0)
319 return AVERROR_INVALIDDATA;
326 if ((dsize -= count) < 0 ||
328 return AVERROR_INVALIDDATA;
338 return AVERROR_INVALIDDATA;
340 predictor(td->tmp, uncompressed_size);
341 reorder_pixels(td->tmp, td->uncompressed_data, uncompressed_size);
346 #define USHORT_RANGE (1 << 16)
347 #define BITMAP_SIZE (1 << 13)
349 static uint16_t reverse_lut(const uint8_t *bitmap, uint16_t *lut)
353 for (i = 0; i < USHORT_RANGE; i++)
354 if ((i == 0) || (bitmap[i >> 3] & (1 << (i & 7))))
359 memset(lut + k, 0, (USHORT_RANGE - k) * 2);
364 static void apply_lut(const uint16_t *lut, uint16_t *dst, int dsize)
368 for (i = 0; i < dsize; ++i)
369 dst[i] = lut[dst[i]];
372 #define HUF_ENCBITS 16 // literal (value) bit length
373 #define HUF_DECBITS 14 // decoding bit size (>= 8)
375 #define HUF_ENCSIZE ((1 << HUF_ENCBITS) + 1) // encoding table size
376 #define HUF_DECSIZE (1 << HUF_DECBITS) // decoding table size
377 #define HUF_DECMASK (HUF_DECSIZE - 1)
379 typedef struct HufDec {
385 static void huf_canonical_code_table(uint64_t *hcode)
387 uint64_t c, n[59] = { 0 };
390 for (i = 0; i < HUF_ENCSIZE; ++i)
394 for (i = 58; i > 0; --i) {
395 uint64_t nc = ((c + n[i]) >> 1);
400 for (i = 0; i < HUF_ENCSIZE; ++i) {
404 hcode[i] = l | (n[l]++ << 6);
408 #define SHORT_ZEROCODE_RUN 59
409 #define LONG_ZEROCODE_RUN 63
410 #define SHORTEST_LONG_RUN (2 + LONG_ZEROCODE_RUN - SHORT_ZEROCODE_RUN)
411 #define LONGEST_LONG_RUN (255 + SHORTEST_LONG_RUN)
413 static int huf_unpack_enc_table(GetByteContext *gb,
414 int32_t im, int32_t iM, uint64_t *hcode)
417 int ret = init_get_bits8(&gbit, gb->buffer, bytestream2_get_bytes_left(gb));
421 for (; im <= iM; im++) {
422 uint64_t l = hcode[im] = get_bits(&gbit, 6);
424 if (l == LONG_ZEROCODE_RUN) {
425 int zerun = get_bits(&gbit, 8) + SHORTEST_LONG_RUN;
427 if (im + zerun > iM + 1)
428 return AVERROR_INVALIDDATA;
434 } else if (l >= SHORT_ZEROCODE_RUN) {
435 int zerun = l - SHORT_ZEROCODE_RUN + 2;
437 if (im + zerun > iM + 1)
438 return AVERROR_INVALIDDATA;
447 bytestream2_skip(gb, (get_bits_count(&gbit) + 7) / 8);
448 huf_canonical_code_table(hcode);
453 static int huf_build_dec_table(const uint64_t *hcode, int im,
454 int iM, HufDec *hdecod)
456 for (; im <= iM; im++) {
457 uint64_t c = hcode[im] >> 6;
458 int i, l = hcode[im] & 63;
461 return AVERROR_INVALIDDATA;
463 if (l > HUF_DECBITS) {
464 HufDec *pl = hdecod + (c >> (l - HUF_DECBITS));
466 return AVERROR_INVALIDDATA;
470 pl->p = av_realloc(pl->p, pl->lit * sizeof(int));
472 return AVERROR(ENOMEM);
474 pl->p[pl->lit - 1] = im;
476 HufDec *pl = hdecod + (c << (HUF_DECBITS - l));
478 for (i = 1 << (HUF_DECBITS - l); i > 0; i--, pl++) {
479 if (pl->len || pl->p)
480 return AVERROR_INVALIDDATA;
490 #define get_char(c, lc, gb) \
492 c = (c << 8) | bytestream2_get_byte(gb); \
496 #define get_code(po, rlc, c, lc, gb, out, oe, outb) \
500 get_char(c, lc, gb); \
505 if (out + cs > oe || out == outb) \
506 return AVERROR_INVALIDDATA; \
512 } else if (out < oe) { \
515 return AVERROR_INVALIDDATA; \
519 static int huf_decode(const uint64_t *hcode, const HufDec *hdecod,
520 GetByteContext *gb, int nbits,
521 int rlc, int no, uint16_t *out)
524 uint16_t *outb = out;
525 uint16_t *oe = out + no;
526 const uint8_t *ie = gb->buffer + (nbits + 7) / 8; // input byte size
531 while (gb->buffer < ie) {
534 while (lc >= HUF_DECBITS) {
535 const HufDec pl = hdecod[(c >> (lc - HUF_DECBITS)) & HUF_DECMASK];
539 get_code(pl.lit, rlc, c, lc, gb, out, oe, outb);
544 return AVERROR_INVALIDDATA;
546 for (j = 0; j < pl.lit; j++) {
547 int l = hcode[pl.p[j]] & 63;
549 while (lc < l && bytestream2_get_bytes_left(gb) > 0)
553 if ((hcode[pl.p[j]] >> 6) ==
554 ((c >> (lc - l)) & ((1LL << l) - 1))) {
556 get_code(pl.p[j], rlc, c, lc, gb, out, oe, outb);
563 return AVERROR_INVALIDDATA;
573 const HufDec pl = hdecod[(c << (HUF_DECBITS - lc)) & HUF_DECMASK];
577 get_code(pl.lit, rlc, c, lc, gb, out, oe, outb);
579 return AVERROR_INVALIDDATA;
583 if (out - outb != no)
584 return AVERROR_INVALIDDATA;
588 static int huf_uncompress(GetByteContext *gb,
589 uint16_t *dst, int dst_size)
591 int32_t src_size, im, iM;
597 src_size = bytestream2_get_le32(gb);
598 im = bytestream2_get_le32(gb);
599 iM = bytestream2_get_le32(gb);
600 bytestream2_skip(gb, 4);
601 nBits = bytestream2_get_le32(gb);
602 if (im < 0 || im >= HUF_ENCSIZE ||
603 iM < 0 || iM >= HUF_ENCSIZE ||
605 return AVERROR_INVALIDDATA;
607 bytestream2_skip(gb, 4);
609 freq = av_mallocz_array(HUF_ENCSIZE, sizeof(*freq));
610 hdec = av_mallocz_array(HUF_DECSIZE, sizeof(*hdec));
611 if (!freq || !hdec) {
612 ret = AVERROR(ENOMEM);
616 if ((ret = huf_unpack_enc_table(gb, im, iM, freq)) < 0)
619 if (nBits > 8 * bytestream2_get_bytes_left(gb)) {
620 ret = AVERROR_INVALIDDATA;
624 if ((ret = huf_build_dec_table(freq, im, iM, hdec)) < 0)
626 ret = huf_decode(freq, hdec, gb, nBits, iM, dst_size, dst);
629 for (i = 0; i < HUF_DECSIZE; i++)
631 av_freep(&hdec[i].p);
639 static inline void wdec14(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
644 int ai = ls + (hi & 1) + (hi >> 1);
646 int16_t bs = ai - hi;
653 #define A_OFFSET (1 << (NBITS - 1))
654 #define MOD_MASK ((1 << NBITS) - 1)
656 static inline void wdec16(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
660 int bb = (m - (d >> 1)) & MOD_MASK;
661 int aa = (d + bb - A_OFFSET) & MOD_MASK;
666 static void wav_decode(uint16_t *in, int nx, int ox,
667 int ny, int oy, uint16_t mx)
669 int w14 = (mx < (1 << 14));
670 int n = (nx > ny) ? ny : nx;
683 uint16_t *ey = in + oy * (ny - p2);
684 uint16_t i00, i01, i10, i11;
690 for (; py <= ey; py += oy2) {
692 uint16_t *ex = py + ox * (nx - p2);
694 for (; px <= ex; px += ox2) {
695 uint16_t *p01 = px + ox1;
696 uint16_t *p10 = px + oy1;
697 uint16_t *p11 = p10 + ox1;
700 wdec14(*px, *p10, &i00, &i10);
701 wdec14(*p01, *p11, &i01, &i11);
702 wdec14(i00, i01, px, p01);
703 wdec14(i10, i11, p10, p11);
705 wdec16(*px, *p10, &i00, &i10);
706 wdec16(*p01, *p11, &i01, &i11);
707 wdec16(i00, i01, px, p01);
708 wdec16(i10, i11, p10, p11);
713 uint16_t *p10 = px + oy1;
716 wdec14(*px, *p10, &i00, p10);
718 wdec16(*px, *p10, &i00, p10);
726 uint16_t *ex = py + ox * (nx - p2);
728 for (; px <= ex; px += ox2) {
729 uint16_t *p01 = px + ox1;
732 wdec14(*px, *p01, &i00, p01);
734 wdec16(*px, *p01, &i00, p01);
745 static int piz_uncompress(EXRContext *s, const uint8_t *src, int ssize,
746 int dsize, EXRThreadData *td)
749 uint16_t maxval, min_non_zero, max_non_zero;
751 uint16_t *tmp = (uint16_t *)td->tmp;
754 int pixel_half_size;/* 1 for half, 2 for float and uint32 */
759 td->bitmap = av_malloc(BITMAP_SIZE);
761 td->lut = av_malloc(1 << 17);
762 if (!td->bitmap || !td->lut) {
763 av_freep(&td->bitmap);
765 return AVERROR(ENOMEM);
768 bytestream2_init(&gb, src, ssize);
769 min_non_zero = bytestream2_get_le16(&gb);
770 max_non_zero = bytestream2_get_le16(&gb);
772 if (max_non_zero >= BITMAP_SIZE)
773 return AVERROR_INVALIDDATA;
775 memset(td->bitmap, 0, FFMIN(min_non_zero, BITMAP_SIZE));
776 if (min_non_zero <= max_non_zero)
777 bytestream2_get_buffer(&gb, td->bitmap + min_non_zero,
778 max_non_zero - min_non_zero + 1);
779 memset(td->bitmap + max_non_zero + 1, 0, BITMAP_SIZE - max_non_zero - 1);
781 maxval = reverse_lut(td->bitmap, td->lut);
783 ret = huf_uncompress(&gb, tmp, dsize / sizeof(uint16_t));
788 for (i = 0; i < s->nb_channels; i++) {
789 channel = &s->channels[i];
791 if (channel->pixel_type == EXR_HALF)
796 for (j = 0; j < pixel_half_size; j++)
797 wav_decode(ptr + j, td->xsize, pixel_half_size, td->ysize,
798 td->xsize * pixel_half_size, maxval);
799 ptr += td->xsize * td->ysize * pixel_half_size;
802 apply_lut(td->lut, tmp, dsize / sizeof(uint16_t));
804 out = td->uncompressed_data;
805 for (i = 0; i < td->ysize; i++) {
807 for (j = 0; j < s->nb_channels; j++) {
809 EXRChannel *channel = &s->channels[j];
810 if (channel->pixel_type == EXR_HALF)
815 in = tmp + tmp_offset * td->xsize * td->ysize + i * td->xsize * pixel_half_size;
816 tmp_offset += pixel_half_size;
817 memcpy(out, in, td->xsize * 2 * pixel_half_size);
818 out += td->xsize * 2 * pixel_half_size;
825 static int pxr24_uncompress(EXRContext *s, const uint8_t *src,
826 int compressed_size, int uncompressed_size,
829 unsigned long dest_len, expected_len = 0;
830 const uint8_t *in = td->tmp;
834 for (i = 0; i < s->nb_channels; i++) {
835 if (s->channels[i].pixel_type == EXR_FLOAT) {
836 expected_len += (td->xsize * td->ysize * 3);/* PRX 24 store float in 24 bit instead of 32 */
837 } else if (s->channels[i].pixel_type == EXR_HALF) {
838 expected_len += (td->xsize * td->ysize * 2);
840 expected_len += (td->xsize * td->ysize * 4);
844 dest_len = expected_len;
846 if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK) {
847 return AVERROR_INVALIDDATA;
848 } else if (dest_len != expected_len) {
849 return AVERROR_INVALIDDATA;
852 out = td->uncompressed_data;
853 for (i = 0; i < td->ysize; i++)
854 for (c = 0; c < s->nb_channels; c++) {
855 EXRChannel *channel = &s->channels[c];
856 const uint8_t *ptr[4];
859 switch (channel->pixel_type) {
862 ptr[1] = ptr[0] + td->xsize;
863 ptr[2] = ptr[1] + td->xsize;
864 in = ptr[2] + td->xsize;
866 for (j = 0; j < td->xsize; ++j) {
867 uint32_t diff = (*(ptr[0]++) << 24) |
868 (*(ptr[1]++) << 16) |
871 bytestream_put_le32(&out, pixel);
876 ptr[1] = ptr[0] + td->xsize;
877 in = ptr[1] + td->xsize;
878 for (j = 0; j < td->xsize; j++) {
879 uint32_t diff = (*(ptr[0]++) << 8) | *(ptr[1]++);
882 bytestream_put_le16(&out, pixel);
886 return AVERROR_INVALIDDATA;
893 static void unpack_14(const uint8_t b[14], uint16_t s[16])
895 unsigned short shift = (b[ 2] >> 2);
896 unsigned short bias = (0x20 << shift);
899 s[ 0] = (b[0] << 8) | b[1];
901 s[ 4] = s[ 0] + ((((b[ 2] << 4) | (b[ 3] >> 4)) & 0x3f) << shift) - bias;
902 s[ 8] = s[ 4] + ((((b[ 3] << 2) | (b[ 4] >> 6)) & 0x3f) << shift) - bias;
903 s[12] = s[ 8] + ((b[ 4] & 0x3f) << shift) - bias;
905 s[ 1] = s[ 0] + ((b[ 5] >> 2) << shift) - bias;
906 s[ 5] = s[ 4] + ((((b[ 5] << 4) | (b[ 6] >> 4)) & 0x3f) << shift) - bias;
907 s[ 9] = s[ 8] + ((((b[ 6] << 2) | (b[ 7] >> 6)) & 0x3f) << shift) - bias;
908 s[13] = s[12] + ((b[ 7] & 0x3f) << shift) - bias;
910 s[ 2] = s[ 1] + ((b[ 8] >> 2) << shift) - bias;
911 s[ 6] = s[ 5] + ((((b[ 8] << 4) | (b[ 9] >> 4)) & 0x3f) << shift) - bias;
912 s[10] = s[ 9] + ((((b[ 9] << 2) | (b[10] >> 6)) & 0x3f) << shift) - bias;
913 s[14] = s[13] + ((b[10] & 0x3f) << shift) - bias;
915 s[ 3] = s[ 2] + ((b[11] >> 2) << shift) - bias;
916 s[ 7] = s[ 6] + ((((b[11] << 4) | (b[12] >> 4)) & 0x3f) << shift) - bias;
917 s[11] = s[10] + ((((b[12] << 2) | (b[13] >> 6)) & 0x3f) << shift) - bias;
918 s[15] = s[14] + ((b[13] & 0x3f) << shift) - bias;
920 for (i = 0; i < 16; ++i) {
928 static void unpack_3(const uint8_t b[3], uint16_t s[16])
932 s[0] = (b[0] << 8) | b[1];
939 for (i = 1; i < 16; i++)
944 static int b44_uncompress(EXRContext *s, const uint8_t *src, int compressed_size,
945 int uncompressed_size, EXRThreadData *td) {
946 const int8_t *sr = src;
947 int stayToUncompress = compressed_size;
948 int nbB44BlockW, nbB44BlockH;
949 int indexHgX, indexHgY, indexOut, indexTmp;
950 uint16_t tmpBuffer[16]; /* B44 use 4x4 half float pixel */
952 int target_channel_offset = 0;
954 /* calc B44 block count */
955 nbB44BlockW = td->xsize / 4;
956 if ((td->xsize % 4) != 0)
959 nbB44BlockH = td->ysize / 4;
960 if ((td->ysize % 4) != 0)
963 for (c = 0; c < s->nb_channels; c++) {
964 if (s->channels[c].pixel_type == EXR_HALF) {/* B44 only compress half float data */
965 for (iY = 0; iY < nbB44BlockH; iY++) {
966 for (iX = 0; iX < nbB44BlockW; iX++) {/* For each B44 block */
967 if (stayToUncompress < 3) {
968 av_log(s, AV_LOG_ERROR, "Not enough data for B44A block: %d", stayToUncompress);
969 return AVERROR_INVALIDDATA;
972 if (src[compressed_size - stayToUncompress + 2] == 0xfc) { /* B44A block */
973 unpack_3(sr, tmpBuffer);
975 stayToUncompress -= 3;
976 } else {/* B44 Block */
977 if (stayToUncompress < 14) {
978 av_log(s, AV_LOG_ERROR, "Not enough data for B44 block: %d", stayToUncompress);
979 return AVERROR_INVALIDDATA;
981 unpack_14(sr, tmpBuffer);
983 stayToUncompress -= 14;
986 /* copy data to uncompress buffer (B44 block can exceed target resolution)*/
990 for (y = indexHgY; y < FFMIN(indexHgY + 4, td->ysize); y++) {
991 for (x = indexHgX; x < FFMIN(indexHgX + 4, td->xsize); x++) {
992 indexOut = target_channel_offset * td->xsize + y * td->channel_line_size + 2 * x;
993 indexTmp = (y-indexHgY) * 4 + (x-indexHgX);
994 td->uncompressed_data[indexOut] = tmpBuffer[indexTmp] & 0xff;
995 td->uncompressed_data[indexOut + 1] = tmpBuffer[indexTmp] >> 8;
1000 target_channel_offset += 2;
1001 } else {/* Float or UINT 32 channel */
1002 if (stayToUncompress < td->ysize * td->xsize * 4) {
1003 av_log(s, AV_LOG_ERROR, "Not enough data for uncompress channel: %d", stayToUncompress);
1004 return AVERROR_INVALIDDATA;
1007 for (y = 0; y < td->ysize; y++) {
1008 indexOut = target_channel_offset * td->xsize + y * td->channel_line_size;
1009 memcpy(&td->uncompressed_data[indexOut], sr, td->xsize * 4);
1010 sr += td->xsize * 4;
1012 target_channel_offset += 4;
1014 stayToUncompress -= td->ysize * td->xsize * 4;
1021 static int decode_block(AVCodecContext *avctx, void *tdata,
1022 int jobnr, int threadnr)
1024 EXRContext *s = avctx->priv_data;
1025 AVFrame *const p = s->picture;
1026 EXRThreadData *td = &s->thread_data[threadnr];
1027 const uint8_t *channel_buffer[4] = { 0 };
1028 const uint8_t *buf = s->buf;
1029 uint64_t line_offset, uncompressed_size;
1032 uint32_t data_size, line, col = 0;
1033 uint32_t tileX, tileY, tileLevelX, tileLevelY;
1035 int axmax = (avctx->width - (s->xmax + 1)) * 2 * s->desc->nb_components; /* nb pixel to add at the right of the datawindow */
1036 int bxmin = s->xmin * 2 * s->desc->nb_components; /* nb pixel to add at the left of the datawindow */
1037 int i, x, buf_size = s->buf_size;
1038 int c, rgb_channel_count;
1039 float one_gamma = 1.0f / s->gamma;
1040 avpriv_trc_function trc_func = avpriv_get_trc_function_from_trc(s->apply_trc_type);
1043 line_offset = AV_RL64(s->gb.buffer + jobnr * 8);
1046 if (line_offset > buf_size - 20)
1047 return AVERROR_INVALIDDATA;
1049 src = buf + line_offset + 20;
1051 tileX = AV_RL32(src - 20);
1052 tileY = AV_RL32(src - 16);
1053 tileLevelX = AV_RL32(src - 12);
1054 tileLevelY = AV_RL32(src - 8);
1056 data_size = AV_RL32(src - 4);
1057 if (data_size <= 0 || data_size > buf_size)
1058 return AVERROR_INVALIDDATA;
1060 if (tileLevelX || tileLevelY) { /* tile level, is not the full res level */
1061 avpriv_report_missing_feature(s->avctx, "Subres tile before full res tile");
1062 return AVERROR_PATCHWELCOME;
1065 line = s->tile_attr.ySize * tileY;
1066 col = s->tile_attr.xSize * tileX;
1068 td->ysize = FFMIN(s->tile_attr.ySize, s->ydelta - tileY * s->tile_attr.ySize);
1069 td->xsize = FFMIN(s->tile_attr.xSize, s->xdelta - tileX * s->tile_attr.xSize);
1071 if (col) { /* not the first tile of the line */
1072 bxmin = 0; /* doesn't add pixel at the left of the datawindow */
1075 if ((col + td->xsize) != s->xdelta)/* not the last tile of the line */
1076 axmax = 0; /* doesn't add pixel at the right of the datawindow */
1078 td->channel_line_size = td->xsize * s->current_channel_offset;/* uncompress size of one line */
1079 uncompressed_size = td->channel_line_size * (uint64_t)td->ysize;/* uncompress size of the block */
1081 if (line_offset > buf_size - 8)
1082 return AVERROR_INVALIDDATA;
1084 src = buf + line_offset + 8;
1085 line = AV_RL32(src - 8);
1087 if (line < s->ymin || line > s->ymax)
1088 return AVERROR_INVALIDDATA;
1090 data_size = AV_RL32(src - 4);
1091 if (data_size <= 0 || data_size > buf_size)
1092 return AVERROR_INVALIDDATA;
1094 td->ysize = FFMIN(s->scan_lines_per_block, s->ymax - line + 1); /* s->ydelta - line ?? */
1095 td->xsize = s->xdelta;
1097 td->channel_line_size = td->xsize * s->current_channel_offset;/* uncompress size of one line */
1098 uncompressed_size = td->channel_line_size * (uint64_t)td->ysize;/* uncompress size of the block */
1100 if ((s->compression == EXR_RAW && (data_size != uncompressed_size ||
1101 line_offset > buf_size - uncompressed_size)) ||
1102 (s->compression != EXR_RAW && (data_size > uncompressed_size ||
1103 line_offset > buf_size - data_size))) {
1104 return AVERROR_INVALIDDATA;
1108 if (data_size < uncompressed_size || s->is_tile) { /* td->tmp is use for tile reorganization */
1109 av_fast_padded_malloc(&td->tmp, &td->tmp_size, uncompressed_size);
1111 return AVERROR(ENOMEM);
1114 if (data_size < uncompressed_size) {
1115 av_fast_padded_malloc(&td->uncompressed_data,
1116 &td->uncompressed_size, uncompressed_size);
1118 if (!td->uncompressed_data)
1119 return AVERROR(ENOMEM);
1121 ret = AVERROR_INVALIDDATA;
1122 switch (s->compression) {
1125 ret = zip_uncompress(src, data_size, uncompressed_size, td);
1128 ret = piz_uncompress(s, src, data_size, uncompressed_size, td);
1131 ret = pxr24_uncompress(s, src, data_size, uncompressed_size, td);
1134 ret = rle_uncompress(src, data_size, uncompressed_size, td);
1138 ret = b44_uncompress(s, src, data_size, uncompressed_size, td);
1142 av_log(avctx, AV_LOG_ERROR, "decode_block() failed.\n");
1145 src = td->uncompressed_data;
1149 channel_buffer[0] = src + td->xsize * s->channel_offsets[0];
1150 channel_buffer[1] = src + td->xsize * s->channel_offsets[1];
1151 channel_buffer[2] = src + td->xsize * s->channel_offsets[2];
1152 rgb_channel_count = 3;
1153 } else { /* put y data in the first channel_buffer */
1154 channel_buffer[0] = src + td->xsize * s->channel_offsets[1];
1155 rgb_channel_count = 1;
1157 if (s->channel_offsets[3] >= 0)
1158 channel_buffer[3] = src + td->xsize * s->channel_offsets[3];
1160 ptr = p->data[0] + line * p->linesize[0] + (col * s->desc->nb_components * 2);
1163 i < td->ysize; i++, ptr += p->linesize[0]) {
1166 const uint8_t *rgb[3];
1168 for (c = 0; c < rgb_channel_count; c++){
1169 rgb[c] = channel_buffer[c];
1172 if (channel_buffer[3])
1173 a = channel_buffer[3];
1175 ptr_x = (uint16_t *) ptr;
1177 // Zero out the start if xmin is not 0
1178 memset(ptr_x, 0, bxmin);
1179 ptr_x += s->xmin * s->desc->nb_components;
1181 if (s->pixel_type == EXR_FLOAT) {
1184 for (x = 0; x < td->xsize; x++) {
1185 union av_intfloat32 t;
1187 for (c = 0; c < rgb_channel_count; c++) {
1188 t.i = bytestream_get_le32(&rgb[c]);
1189 t.f = trc_func(t.f);
1190 *ptr_x++ = exr_flt2uint(t.i);
1192 if (channel_buffer[3])
1193 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a));
1196 for (x = 0; x < td->xsize; x++) {
1197 union av_intfloat32 t;
1200 for (c = 0; c < rgb_channel_count; c++) {
1201 t.i = bytestream_get_le32(&rgb[c]);
1202 if (t.f > 0.0f) /* avoid negative values */
1203 t.f = powf(t.f, one_gamma);
1204 *ptr_x++ = exr_flt2uint(t.i);
1207 if (channel_buffer[3])
1208 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a));
1213 for (x = 0; x < td->xsize; x++) {
1215 for (c = 0; c < rgb_channel_count; c++) {
1216 *ptr_x++ = s->gamma_table[bytestream_get_le16(&rgb[c])];
1219 if (channel_buffer[3])
1220 *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&a));
1224 // Zero out the end if xmax+1 is not w
1225 memset(ptr_x, 0, axmax);
1227 channel_buffer[0] += td->channel_line_size;
1228 channel_buffer[1] += td->channel_line_size;
1229 channel_buffer[2] += td->channel_line_size;
1230 if (channel_buffer[3])
1231 channel_buffer[3] += td->channel_line_size;
1238 * Check if the variable name corresponds to its data type.
1240 * @param s the EXRContext
1241 * @param value_name name of the variable to check
1242 * @param value_type type of the variable to check
1243 * @param minimum_length minimum length of the variable data
1245 * @return bytes to read containing variable data
1246 * -1 if variable is not found
1247 * 0 if buffer ended prematurely
1249 static int check_header_variable(EXRContext *s,
1250 const char *value_name,
1251 const char *value_type,
1252 unsigned int minimum_length)
1256 if (bytestream2_get_bytes_left(&s->gb) >= minimum_length &&
1257 !strcmp(s->gb.buffer, value_name)) {
1258 // found value_name, jump to value_type (null terminated strings)
1259 s->gb.buffer += strlen(value_name) + 1;
1260 if (!strcmp(s->gb.buffer, value_type)) {
1261 s->gb.buffer += strlen(value_type) + 1;
1262 var_size = bytestream2_get_le32(&s->gb);
1263 // don't go read past boundaries
1264 if (var_size > bytestream2_get_bytes_left(&s->gb))
1267 // value_type not found, reset the buffer
1268 s->gb.buffer -= strlen(value_name) + 1;
1269 av_log(s->avctx, AV_LOG_WARNING,
1270 "Unknown data type %s for header variable %s.\n",
1271 value_type, value_name);
1278 static int decode_header(EXRContext *s)
1280 int magic_number, version, i, flags, sar = 0;
1281 int layer_match = 0;
1283 s->current_channel_offset = 0;
1290 s->channel_offsets[0] = -1;
1291 s->channel_offsets[1] = -1;
1292 s->channel_offsets[2] = -1;
1293 s->channel_offsets[3] = -1;
1294 s->pixel_type = EXR_UNKNOWN;
1295 s->compression = EXR_UNKN;
1299 s->tile_attr.xSize = -1;
1300 s->tile_attr.ySize = -1;
1304 if (bytestream2_get_bytes_left(&s->gb) < 10) {
1305 av_log(s->avctx, AV_LOG_ERROR, "Header too short to parse.\n");
1306 return AVERROR_INVALIDDATA;
1309 magic_number = bytestream2_get_le32(&s->gb);
1310 if (magic_number != 20000630) {
1311 /* As per documentation of OpenEXR, it is supposed to be
1312 * int 20000630 little-endian */
1313 av_log(s->avctx, AV_LOG_ERROR, "Wrong magic number %d.\n", magic_number);
1314 return AVERROR_INVALIDDATA;
1317 version = bytestream2_get_byte(&s->gb);
1319 avpriv_report_missing_feature(s->avctx, "Version %d", version);
1320 return AVERROR_PATCHWELCOME;
1323 flags = bytestream2_get_le24(&s->gb);
1327 else if (flags & 0x02)
1330 avpriv_report_missing_feature(s->avctx, "flags %d", flags);
1331 return AVERROR_PATCHWELCOME;
1335 while (bytestream2_get_bytes_left(&s->gb) > 0 && *s->gb.buffer) {
1337 if ((var_size = check_header_variable(s, "channels",
1338 "chlist", 38)) >= 0) {
1339 GetByteContext ch_gb;
1341 return AVERROR_INVALIDDATA;
1343 bytestream2_init(&ch_gb, s->gb.buffer, var_size);
1345 while (bytestream2_get_bytes_left(&ch_gb) >= 19) {
1346 EXRChannel *channel;
1347 enum ExrPixelType current_pixel_type;
1348 int channel_index = -1;
1351 if (strcmp(s->layer, "") != 0) {
1352 if (strncmp(ch_gb.buffer, s->layer, strlen(s->layer)) == 0) {
1354 av_log(s->avctx, AV_LOG_INFO,
1355 "Channel match layer : %s.\n", ch_gb.buffer);
1356 ch_gb.buffer += strlen(s->layer);
1357 if (*ch_gb.buffer == '.')
1358 ch_gb.buffer++; /* skip dot if not given */
1360 av_log(s->avctx, AV_LOG_INFO,
1361 "Channel doesn't match layer : %s.\n", ch_gb.buffer);
1367 if (layer_match) { /* only search channel if the layer match is valid */
1368 if (!strcmp(ch_gb.buffer, "R") ||
1369 !strcmp(ch_gb.buffer, "X") ||
1370 !strcmp(ch_gb.buffer, "U")) {
1373 } else if (!strcmp(ch_gb.buffer, "G") ||
1374 !strcmp(ch_gb.buffer, "V")) {
1377 } else if (!strcmp(ch_gb.buffer, "Y")) {
1380 } else if (!strcmp(ch_gb.buffer, "B") ||
1381 !strcmp(ch_gb.buffer, "Z") ||
1382 !strcmp(ch_gb.buffer, "W")){
1385 } else if (!strcmp(ch_gb.buffer, "A")) {
1388 av_log(s->avctx, AV_LOG_WARNING,
1389 "Unsupported channel %.256s.\n", ch_gb.buffer);
1393 /* skip until you get a 0 */
1394 while (bytestream2_get_bytes_left(&ch_gb) > 0 &&
1395 bytestream2_get_byte(&ch_gb))
1398 if (bytestream2_get_bytes_left(&ch_gb) < 4) {
1399 av_log(s->avctx, AV_LOG_ERROR, "Incomplete header.\n");
1400 return AVERROR_INVALIDDATA;
1403 current_pixel_type = bytestream2_get_le32(&ch_gb);
1404 if (current_pixel_type >= EXR_UNKNOWN) {
1405 avpriv_report_missing_feature(s->avctx, "Pixel type %d",
1406 current_pixel_type);
1407 return AVERROR_PATCHWELCOME;
1410 bytestream2_skip(&ch_gb, 4);
1411 xsub = bytestream2_get_le32(&ch_gb);
1412 ysub = bytestream2_get_le32(&ch_gb);
1414 if (xsub != 1 || ysub != 1) {
1415 avpriv_report_missing_feature(s->avctx,
1416 "Subsampling %dx%d",
1418 return AVERROR_PATCHWELCOME;
1421 if (s->channel_offsets[channel_index] == -1){/* channel have not been previously assign */
1422 if (channel_index >= 0) {
1423 if (s->pixel_type != EXR_UNKNOWN &&
1424 s->pixel_type != current_pixel_type) {
1425 av_log(s->avctx, AV_LOG_ERROR,
1426 "RGB channels not of the same depth.\n");
1427 return AVERROR_INVALIDDATA;
1429 s->pixel_type = current_pixel_type;
1430 s->channel_offsets[channel_index] = s->current_channel_offset;
1434 s->channels = av_realloc(s->channels,
1435 ++s->nb_channels * sizeof(EXRChannel));
1437 return AVERROR(ENOMEM);
1438 channel = &s->channels[s->nb_channels - 1];
1439 channel->pixel_type = current_pixel_type;
1440 channel->xsub = xsub;
1441 channel->ysub = ysub;
1443 s->current_channel_offset += 1 << current_pixel_type;
1446 /* Check if all channels are set with an offset or if the channels
1447 * are causing an overflow */
1448 if (!s->is_luma){/* if we expected to have at least 3 channels */
1449 if (FFMIN3(s->channel_offsets[0],
1450 s->channel_offsets[1],
1451 s->channel_offsets[2]) < 0) {
1452 if (s->channel_offsets[0] < 0)
1453 av_log(s->avctx, AV_LOG_ERROR, "Missing red channel.\n");
1454 if (s->channel_offsets[1] < 0)
1455 av_log(s->avctx, AV_LOG_ERROR, "Missing green channel.\n");
1456 if (s->channel_offsets[2] < 0)
1457 av_log(s->avctx, AV_LOG_ERROR, "Missing blue channel.\n");
1458 return AVERROR_INVALIDDATA;
1462 // skip one last byte and update main gb
1463 s->gb.buffer = ch_gb.buffer + 1;
1465 } else if ((var_size = check_header_variable(s, "dataWindow", "box2i",
1468 return AVERROR_INVALIDDATA;
1470 s->xmin = bytestream2_get_le32(&s->gb);
1471 s->ymin = bytestream2_get_le32(&s->gb);
1472 s->xmax = bytestream2_get_le32(&s->gb);
1473 s->ymax = bytestream2_get_le32(&s->gb);
1474 s->xdelta = (s->xmax - s->xmin) + 1;
1475 s->ydelta = (s->ymax - s->ymin) + 1;
1478 } else if ((var_size = check_header_variable(s, "displayWindow",
1479 "box2i", 34)) >= 0) {
1481 return AVERROR_INVALIDDATA;
1483 bytestream2_skip(&s->gb, 8);
1484 s->w = bytestream2_get_le32(&s->gb) + 1;
1485 s->h = bytestream2_get_le32(&s->gb) + 1;
1488 } else if ((var_size = check_header_variable(s, "lineOrder",
1489 "lineOrder", 25)) >= 0) {
1492 return AVERROR_INVALIDDATA;
1494 line_order = bytestream2_get_byte(&s->gb);
1495 av_log(s->avctx, AV_LOG_DEBUG, "line order: %d.\n", line_order);
1496 if (line_order > 2) {
1497 av_log(s->avctx, AV_LOG_ERROR, "Unknown line order.\n");
1498 return AVERROR_INVALIDDATA;
1502 } else if ((var_size = check_header_variable(s, "pixelAspectRatio",
1503 "float", 31)) >= 0) {
1505 return AVERROR_INVALIDDATA;
1507 sar = bytestream2_get_le32(&s->gb);
1510 } else if ((var_size = check_header_variable(s, "compression",
1511 "compression", 29)) >= 0) {
1513 return AVERROR_INVALIDDATA;
1515 if (s->compression == EXR_UNKN)
1516 s->compression = bytestream2_get_byte(&s->gb);
1518 av_log(s->avctx, AV_LOG_WARNING,
1519 "Found more than one compression attribute.\n");
1522 } else if ((var_size = check_header_variable(s, "tiles",
1523 "tiledesc", 22)) >= 0) {
1527 av_log(s->avctx, AV_LOG_WARNING,
1528 "Found tile attribute and scanline flags. Exr will be interpreted as scanline.\n");
1530 s->tile_attr.xSize = bytestream2_get_le32(&s->gb);
1531 s->tile_attr.ySize = bytestream2_get_le32(&s->gb);
1533 tileLevel = bytestream2_get_byte(&s->gb);
1534 s->tile_attr.level_mode = tileLevel & 0x0f;
1535 s->tile_attr.level_round = (tileLevel >> 4) & 0x0f;
1537 if (s->tile_attr.level_mode >= EXR_TILE_LEVEL_UNKNOWN){
1538 avpriv_report_missing_feature(s->avctx, "Tile level mode %d",
1539 s->tile_attr.level_mode);
1540 return AVERROR_PATCHWELCOME;
1543 if (s->tile_attr.level_round >= EXR_TILE_ROUND_UNKNOWN) {
1544 avpriv_report_missing_feature(s->avctx, "Tile level round %d",
1545 s->tile_attr.level_round);
1546 return AVERROR_PATCHWELCOME;
1552 // Check if there are enough bytes for a header
1553 if (bytestream2_get_bytes_left(&s->gb) <= 9) {
1554 av_log(s->avctx, AV_LOG_ERROR, "Incomplete header\n");
1555 return AVERROR_INVALIDDATA;
1558 // Process unknown variables
1559 for (i = 0; i < 2; i++) // value_name and value_type
1560 while (bytestream2_get_byte(&s->gb) != 0);
1562 // Skip variable length
1563 bytestream2_skip(&s->gb, bytestream2_get_le32(&s->gb));
1566 ff_set_sar(s->avctx, av_d2q(av_int2float(sar), 255));
1568 if (s->compression == EXR_UNKN) {
1569 av_log(s->avctx, AV_LOG_ERROR, "Missing compression attribute.\n");
1570 return AVERROR_INVALIDDATA;
1574 if (s->tile_attr.xSize < 1 || s->tile_attr.ySize < 1) {
1575 av_log(s->avctx, AV_LOG_ERROR, "Invalid tile attribute.\n");
1576 return AVERROR_INVALIDDATA;
1580 if (bytestream2_get_bytes_left(&s->gb) <= 0) {
1581 av_log(s->avctx, AV_LOG_ERROR, "Incomplete frame.\n");
1582 return AVERROR_INVALIDDATA;
1585 // aaand we are done
1586 bytestream2_skip(&s->gb, 1);
1590 static int decode_frame(AVCodecContext *avctx, void *data,
1591 int *got_frame, AVPacket *avpkt)
1593 EXRContext *s = avctx->priv_data;
1594 ThreadFrame frame = { .f = data };
1595 AVFrame *picture = data;
1600 int nb_blocks;/* nb scanline or nb tile */
1602 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
1604 if ((ret = decode_header(s)) < 0)
1607 switch (s->pixel_type) {
1610 if (s->channel_offsets[3] >= 0) {
1612 avctx->pix_fmt = AV_PIX_FMT_RGBA64;
1614 avctx->pix_fmt = AV_PIX_FMT_YA16;
1618 avctx->pix_fmt = AV_PIX_FMT_RGB48;
1620 avctx->pix_fmt = AV_PIX_FMT_GRAY16;
1625 avpriv_request_sample(avctx, "32-bit unsigned int");
1626 return AVERROR_PATCHWELCOME;
1628 av_log(avctx, AV_LOG_ERROR, "Missing channel list.\n");
1629 return AVERROR_INVALIDDATA;
1632 if (s->apply_trc_type != AVCOL_TRC_UNSPECIFIED)
1633 avctx->color_trc = s->apply_trc_type;
1635 switch (s->compression) {
1639 s->scan_lines_per_block = 1;
1643 s->scan_lines_per_block = 16;
1648 s->scan_lines_per_block = 32;
1651 avpriv_report_missing_feature(avctx, "Compression %d", s->compression);
1652 return AVERROR_PATCHWELCOME;
1655 /* Verify the xmin, xmax, ymin, ymax and xdelta before setting
1656 * the actual image size. */
1657 if (s->xmin > s->xmax ||
1658 s->ymin > s->ymax ||
1659 s->xdelta != s->xmax - s->xmin + 1 ||
1662 av_log(avctx, AV_LOG_ERROR, "Wrong or missing size information.\n");
1663 return AVERROR_INVALIDDATA;
1666 if ((ret = ff_set_dimensions(avctx, s->w, s->h)) < 0)
1669 s->desc = av_pix_fmt_desc_get(avctx->pix_fmt);
1671 return AVERROR_INVALIDDATA;
1672 out_line_size = avctx->width * 2 * s->desc->nb_components;
1675 nb_blocks = ((s->xdelta + s->tile_attr.xSize - 1) / s->tile_attr.xSize) *
1676 ((s->ydelta + s->tile_attr.ySize - 1) / s->tile_attr.ySize);
1677 } else { /* scanline */
1678 nb_blocks = (s->ydelta + s->scan_lines_per_block - 1) /
1679 s->scan_lines_per_block;
1682 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
1685 if (bytestream2_get_bytes_left(&s->gb) < nb_blocks * 8)
1686 return AVERROR_INVALIDDATA;
1688 // save pointer we are going to use in decode_block
1689 s->buf = avpkt->data;
1690 s->buf_size = avpkt->size;
1691 ptr = picture->data[0];
1693 // Zero out the start if ymin is not 0
1694 for (y = 0; y < s->ymin; y++) {
1695 memset(ptr, 0, out_line_size);
1696 ptr += picture->linesize[0];
1699 s->picture = picture;
1701 avctx->execute2(avctx, decode_block, s->thread_data, NULL, nb_blocks);
1703 // Zero out the end if ymax+1 is not h
1704 for (y = s->ymax + 1; y < avctx->height; y++) {
1705 memset(ptr, 0, out_line_size);
1706 ptr += picture->linesize[0];
1709 picture->pict_type = AV_PICTURE_TYPE_I;
1715 static av_cold int decode_init(AVCodecContext *avctx)
1717 EXRContext *s = avctx->priv_data;
1719 union av_intfloat32 t;
1720 float one_gamma = 1.0f / s->gamma;
1721 avpriv_trc_function trc_func = NULL;
1725 trc_func = avpriv_get_trc_function_from_trc(s->apply_trc_type);
1727 for (i = 0; i < 65536; ++i) {
1728 t = exr_half2float(i);
1729 t.f = trc_func(t.f);
1730 s->gamma_table[i] = exr_flt2uint(t.i);
1733 if (one_gamma > 0.9999f && one_gamma < 1.0001f) {
1734 for (i = 0; i < 65536; ++i)
1735 s->gamma_table[i] = exr_halflt2uint(i);
1737 for (i = 0; i < 65536; ++i) {
1738 t = exr_half2float(i);
1739 /* If negative value we reuse half value */
1741 s->gamma_table[i] = exr_halflt2uint(i);
1743 t.f = powf(t.f, one_gamma);
1744 s->gamma_table[i] = exr_flt2uint(t.i);
1750 // allocate thread data, used for non EXR_RAW compression types
1751 s->thread_data = av_mallocz_array(avctx->thread_count, sizeof(EXRThreadData));
1752 if (!s->thread_data)
1753 return AVERROR_INVALIDDATA;
1759 static int decode_init_thread_copy(AVCodecContext *avctx)
1760 { EXRContext *s = avctx->priv_data;
1762 // allocate thread data, used for non EXR_RAW compression types
1763 s->thread_data = av_mallocz_array(avctx->thread_count, sizeof(EXRThreadData));
1764 if (!s->thread_data)
1765 return AVERROR_INVALIDDATA;
1771 static av_cold int decode_end(AVCodecContext *avctx)
1773 EXRContext *s = avctx->priv_data;
1775 for (i = 0; i < avctx->thread_count; i++) {
1776 EXRThreadData *td = &s->thread_data[i];
1777 av_freep(&td->uncompressed_data);
1779 av_freep(&td->bitmap);
1783 av_freep(&s->thread_data);
1784 av_freep(&s->channels);
1789 #define OFFSET(x) offsetof(EXRContext, x)
1790 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1791 static const AVOption options[] = {
1792 { "layer", "Set the decoding layer", OFFSET(layer),
1793 AV_OPT_TYPE_STRING, { .str = "" }, 0, 0, VD },
1794 { "gamma", "Set the float gamma value when decoding", OFFSET(gamma),
1795 AV_OPT_TYPE_FLOAT, { .dbl = 1.0f }, 0.001, FLT_MAX, VD },
1797 // XXX: Note the abuse of the enum using AVCOL_TRC_UNSPECIFIED to subsume the existing gamma option
1798 { "apply_trc", "color transfer characteristics to apply to EXR linear input", OFFSET(apply_trc_type),
1799 AV_OPT_TYPE_INT, {.i64 = AVCOL_TRC_UNSPECIFIED }, 1, AVCOL_TRC_NB-1, VD, "apply_trc_type"},
1800 { "bt709", "BT.709", 0,
1801 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT709 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1802 { "gamma", "gamma", 0,
1803 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_UNSPECIFIED }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1804 { "gamma22", "BT.470 M", 0,
1805 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_GAMMA22 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1806 { "gamma28", "BT.470 BG", 0,
1807 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_GAMMA28 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1808 { "smpte170m", "SMPTE 170 M", 0,
1809 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTE170M }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1810 { "smpte240m", "SMPTE 240 M", 0,
1811 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTE240M }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1812 { "linear", "Linear", 0,
1813 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LINEAR }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1815 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LOG }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1816 { "log_sqrt", "Log square root", 0,
1817 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LOG_SQRT }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1818 { "iec61966_2_4", "IEC 61966-2-4", 0,
1819 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_IEC61966_2_4 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1820 { "bt1361", "BT.1361", 0,
1821 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT1361_ECG }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1822 { "iec61966_2_1", "IEC 61966-2-1", 0,
1823 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_IEC61966_2_1 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1824 { "bt2020_10bit", "BT.2020 - 10 bit", 0,
1825 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT2020_10 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1826 { "bt2020_12bit", "BT.2020 - 12 bit", 0,
1827 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT2020_12 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1828 { "smpte2084", "SMPTE ST 2084", 0,
1829 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTEST2084 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1830 { "smpte428_1", "SMPTE ST 428-1", 0,
1831 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTEST428_1 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1836 static const AVClass exr_class = {
1837 .class_name = "EXR",
1838 .item_name = av_default_item_name,
1840 .version = LIBAVUTIL_VERSION_INT,
1843 AVCodec ff_exr_decoder = {
1845 .long_name = NULL_IF_CONFIG_SMALL("OpenEXR image"),
1846 .type = AVMEDIA_TYPE_VIDEO,
1847 .id = AV_CODEC_ID_EXR,
1848 .priv_data_size = sizeof(EXRContext),
1849 .init = decode_init,
1850 .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
1851 .close = decode_end,
1852 .decode = decode_frame,
1853 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS |
1854 AV_CODEC_CAP_SLICE_THREADS,
1855 .priv_class = &exr_class,