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;
108 typedef struct EXRContext {
111 AVCodecContext *avctx;
113 enum ExrCompr compression;
114 enum ExrPixelType pixel_type;
115 int channel_offsets[4]; // 0 = red, 1 = green, 2 = blue and 3 = alpha
116 const AVPixFmtDescriptor *desc;
121 uint32_t xdelta, ydelta;
124 uint64_t scan_line_size;
125 int scan_lines_per_block;
127 EXRTileAttribute tile_attr; /* header data attribute of tile */
128 int is_tile; /* 0 if scanline, 1 if tile */
134 EXRChannel *channels;
136 int current_channel_offset;
138 EXRThreadData *thread_data;
142 enum AVColorTransferCharacteristic apply_trc_type;
144 uint16_t gamma_table[65536];
147 /* -15 stored using a single precision bias of 127 */
148 #define HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP 0x38000000
150 /* max exponent value in single precision that will be converted
151 * to Inf or Nan when stored as a half-float */
152 #define HALF_FLOAT_MAX_BIASED_EXP_AS_SINGLE_FP_EXP 0x47800000
154 /* 255 is the max exponent biased value */
155 #define FLOAT_MAX_BIASED_EXP (0xFF << 23)
157 #define HALF_FLOAT_MAX_BIASED_EXP (0x1F << 10)
160 * Convert a half float as a uint16_t into a full float.
162 * @param hf half float as uint16_t
164 * @return float value
166 static union av_intfloat32 exr_half2float(uint16_t hf)
168 unsigned int sign = (unsigned int) (hf >> 15);
169 unsigned int mantissa = (unsigned int) (hf & ((1 << 10) - 1));
170 unsigned int exp = (unsigned int) (hf & HALF_FLOAT_MAX_BIASED_EXP);
171 union av_intfloat32 f;
173 if (exp == HALF_FLOAT_MAX_BIASED_EXP) {
174 // we have a half-float NaN or Inf
175 // half-float NaNs will be converted to a single precision NaN
176 // half-float Infs will be converted to a single precision Inf
177 exp = FLOAT_MAX_BIASED_EXP;
179 mantissa = (1 << 23) - 1; // set all bits to indicate a NaN
180 } else if (exp == 0x0) {
181 // convert half-float zero/denorm to single precision value
184 exp = HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP;
185 // check for leading 1 in denorm mantissa
186 while ((mantissa & (1 << 10))) {
187 // for every leading 0, decrement single precision exponent by 1
188 // and shift half-float mantissa value to the left
192 // clamp the mantissa to 10-bits
193 mantissa &= ((1 << 10) - 1);
194 // shift left to generate single-precision mantissa of 23-bits
198 // shift left to generate single-precision mantissa of 23-bits
200 // generate single precision biased exponent value
201 exp = (exp << 13) + HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP;
204 f.i = (sign << 31) | exp | mantissa;
211 * Convert from 32-bit float as uint32_t to uint16_t.
213 * @param v 32-bit float
215 * @return normalized 16-bit unsigned int
217 static inline uint16_t exr_flt2uint(uint32_t v)
219 unsigned int exp = v >> 23;
220 // "HACK": negative values result in exp< 0, so clipping them to 0
221 // is also handled by this condition, avoids explicit check for sign bit.
222 if (exp <= 127 + 7 - 24) // we would shift out all bits anyway
227 return (v + (1 << 23)) >> (127 + 7 - exp);
231 * Convert from 16-bit float as uint16_t to uint16_t.
233 * @param v 16-bit float
235 * @return normalized 16-bit unsigned int
237 static inline uint16_t exr_halflt2uint(uint16_t v)
239 unsigned exp = 14 - (v >> 10);
244 return (v & 0x8000) ? 0 : 0xffff;
247 return (v + (1 << 16)) >> (exp + 1);
250 static void predictor(uint8_t *src, int size)
252 uint8_t *t = src + 1;
253 uint8_t *stop = src + size;
256 int d = (int) t[-1] + (int) t[0] - 128;
262 static void reorder_pixels(uint8_t *src, uint8_t *dst, int size)
264 const int8_t *t1 = src;
265 const int8_t *t2 = src + (size + 1) / 2;
267 int8_t *stop = s + size;
282 static int zip_uncompress(const uint8_t *src, int compressed_size,
283 int uncompressed_size, EXRThreadData *td)
285 unsigned long dest_len = uncompressed_size;
287 if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK ||
288 dest_len != uncompressed_size)
289 return AVERROR_INVALIDDATA;
291 predictor(td->tmp, uncompressed_size);
292 reorder_pixels(td->tmp, td->uncompressed_data, uncompressed_size);
297 static int rle_uncompress(const uint8_t *src, int compressed_size,
298 int uncompressed_size, EXRThreadData *td)
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;
313 if ((dsize -= count) < 0 ||
314 (ssize -= count + 1) < 0)
315 return AVERROR_INVALIDDATA;
322 if ((dsize -= count) < 0 ||
324 return AVERROR_INVALIDDATA;
334 return AVERROR_INVALIDDATA;
336 predictor(td->tmp, uncompressed_size);
337 reorder_pixels(td->tmp, td->uncompressed_data, uncompressed_size);
342 #define USHORT_RANGE (1 << 16)
343 #define BITMAP_SIZE (1 << 13)
345 static uint16_t reverse_lut(const uint8_t *bitmap, uint16_t *lut)
349 for (i = 0; i < USHORT_RANGE; i++)
350 if ((i == 0) || (bitmap[i >> 3] & (1 << (i & 7))))
355 memset(lut + k, 0, (USHORT_RANGE - k) * 2);
360 static void apply_lut(const uint16_t *lut, uint16_t *dst, int dsize)
364 for (i = 0; i < dsize; ++i)
365 dst[i] = lut[dst[i]];
368 #define HUF_ENCBITS 16 // literal (value) bit length
369 #define HUF_DECBITS 14 // decoding bit size (>= 8)
371 #define HUF_ENCSIZE ((1 << HUF_ENCBITS) + 1) // encoding table size
372 #define HUF_DECSIZE (1 << HUF_DECBITS) // decoding table size
373 #define HUF_DECMASK (HUF_DECSIZE - 1)
375 typedef struct HufDec {
381 static void huf_canonical_code_table(uint64_t *hcode)
383 uint64_t c, n[59] = { 0 };
386 for (i = 0; i < HUF_ENCSIZE; ++i)
390 for (i = 58; i > 0; --i) {
391 uint64_t nc = ((c + n[i]) >> 1);
396 for (i = 0; i < HUF_ENCSIZE; ++i) {
400 hcode[i] = l | (n[l]++ << 6);
404 #define SHORT_ZEROCODE_RUN 59
405 #define LONG_ZEROCODE_RUN 63
406 #define SHORTEST_LONG_RUN (2 + LONG_ZEROCODE_RUN - SHORT_ZEROCODE_RUN)
407 #define LONGEST_LONG_RUN (255 + SHORTEST_LONG_RUN)
409 static int huf_unpack_enc_table(GetByteContext *gb,
410 int32_t im, int32_t iM, uint64_t *hcode)
413 int ret = init_get_bits8(&gbit, gb->buffer, bytestream2_get_bytes_left(gb));
417 for (; im <= iM; im++) {
418 uint64_t l = hcode[im] = get_bits(&gbit, 6);
420 if (l == LONG_ZEROCODE_RUN) {
421 int zerun = get_bits(&gbit, 8) + SHORTEST_LONG_RUN;
423 if (im + zerun > iM + 1)
424 return AVERROR_INVALIDDATA;
430 } else if (l >= SHORT_ZEROCODE_RUN) {
431 int zerun = l - SHORT_ZEROCODE_RUN + 2;
433 if (im + zerun > iM + 1)
434 return AVERROR_INVALIDDATA;
443 bytestream2_skip(gb, (get_bits_count(&gbit) + 7) / 8);
444 huf_canonical_code_table(hcode);
449 static int huf_build_dec_table(const uint64_t *hcode, int im,
450 int iM, HufDec *hdecod)
452 for (; im <= iM; im++) {
453 uint64_t c = hcode[im] >> 6;
454 int i, l = hcode[im] & 63;
457 return AVERROR_INVALIDDATA;
459 if (l > HUF_DECBITS) {
460 HufDec *pl = hdecod + (c >> (l - HUF_DECBITS));
462 return AVERROR_INVALIDDATA;
466 pl->p = av_realloc(pl->p, pl->lit * sizeof(int));
468 return AVERROR(ENOMEM);
470 pl->p[pl->lit - 1] = im;
472 HufDec *pl = hdecod + (c << (HUF_DECBITS - l));
474 for (i = 1 << (HUF_DECBITS - l); i > 0; i--, pl++) {
475 if (pl->len || pl->p)
476 return AVERROR_INVALIDDATA;
486 #define get_char(c, lc, gb) \
488 c = (c << 8) | bytestream2_get_byte(gb); \
492 #define get_code(po, rlc, c, lc, gb, out, oe, outb) \
496 get_char(c, lc, gb); \
501 if (out + cs > oe || out == outb) \
502 return AVERROR_INVALIDDATA; \
508 } else if (out < oe) { \
511 return AVERROR_INVALIDDATA; \
515 static int huf_decode(const uint64_t *hcode, const HufDec *hdecod,
516 GetByteContext *gb, int nbits,
517 int rlc, int no, uint16_t *out)
520 uint16_t *outb = out;
521 uint16_t *oe = out + no;
522 const uint8_t *ie = gb->buffer + (nbits + 7) / 8; // input byte size
526 while (gb->buffer < ie) {
529 while (lc >= HUF_DECBITS) {
530 const HufDec pl = hdecod[(c >> (lc - HUF_DECBITS)) & HUF_DECMASK];
534 get_code(pl.lit, rlc, c, lc, gb, out, oe, outb);
539 return AVERROR_INVALIDDATA;
541 for (j = 0; j < pl.lit; j++) {
542 int l = hcode[pl.p[j]] & 63;
544 while (lc < l && bytestream2_get_bytes_left(gb) > 0)
548 if ((hcode[pl.p[j]] >> 6) ==
549 ((c >> (lc - l)) & ((1LL << l) - 1))) {
551 get_code(pl.p[j], rlc, c, lc, gb, out, oe, outb);
558 return AVERROR_INVALIDDATA;
568 const HufDec pl = hdecod[(c << (HUF_DECBITS - lc)) & HUF_DECMASK];
572 get_code(pl.lit, rlc, c, lc, gb, out, oe, outb);
574 return AVERROR_INVALIDDATA;
578 if (out - outb != no)
579 return AVERROR_INVALIDDATA;
583 static int huf_uncompress(GetByteContext *gb,
584 uint16_t *dst, int dst_size)
586 int32_t src_size, im, iM;
592 src_size = bytestream2_get_le32(gb);
593 im = bytestream2_get_le32(gb);
594 iM = bytestream2_get_le32(gb);
595 bytestream2_skip(gb, 4);
596 nBits = bytestream2_get_le32(gb);
597 if (im < 0 || im >= HUF_ENCSIZE ||
598 iM < 0 || iM >= HUF_ENCSIZE ||
600 return AVERROR_INVALIDDATA;
602 bytestream2_skip(gb, 4);
604 freq = av_mallocz_array(HUF_ENCSIZE, sizeof(*freq));
605 hdec = av_mallocz_array(HUF_DECSIZE, sizeof(*hdec));
606 if (!freq || !hdec) {
607 ret = AVERROR(ENOMEM);
611 if ((ret = huf_unpack_enc_table(gb, im, iM, freq)) < 0)
614 if (nBits > 8 * bytestream2_get_bytes_left(gb)) {
615 ret = AVERROR_INVALIDDATA;
619 if ((ret = huf_build_dec_table(freq, im, iM, hdec)) < 0)
621 ret = huf_decode(freq, hdec, gb, nBits, iM, dst_size, dst);
624 for (i = 0; i < HUF_DECSIZE; i++)
626 av_freep(&hdec[i].p);
634 static inline void wdec14(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
639 int ai = ls + (hi & 1) + (hi >> 1);
641 int16_t bs = ai - hi;
648 #define A_OFFSET (1 << (NBITS - 1))
649 #define MOD_MASK ((1 << NBITS) - 1)
651 static inline void wdec16(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
655 int bb = (m - (d >> 1)) & MOD_MASK;
656 int aa = (d + bb - A_OFFSET) & MOD_MASK;
661 static void wav_decode(uint16_t *in, int nx, int ox,
662 int ny, int oy, uint16_t mx)
664 int w14 = (mx < (1 << 14));
665 int n = (nx > ny) ? ny : nx;
678 uint16_t *ey = in + oy * (ny - p2);
679 uint16_t i00, i01, i10, i11;
685 for (; py <= ey; py += oy2) {
687 uint16_t *ex = py + ox * (nx - p2);
689 for (; px <= ex; px += ox2) {
690 uint16_t *p01 = px + ox1;
691 uint16_t *p10 = px + oy1;
692 uint16_t *p11 = p10 + ox1;
695 wdec14(*px, *p10, &i00, &i10);
696 wdec14(*p01, *p11, &i01, &i11);
697 wdec14(i00, i01, px, p01);
698 wdec14(i10, i11, p10, p11);
700 wdec16(*px, *p10, &i00, &i10);
701 wdec16(*p01, *p11, &i01, &i11);
702 wdec16(i00, i01, px, p01);
703 wdec16(i10, i11, p10, p11);
708 uint16_t *p10 = px + oy1;
711 wdec14(*px, *p10, &i00, p10);
713 wdec16(*px, *p10, &i00, p10);
721 uint16_t *ex = py + ox * (nx - p2);
723 for (; px <= ex; px += ox2) {
724 uint16_t *p01 = px + ox1;
727 wdec14(*px, *p01, &i00, p01);
729 wdec16(*px, *p01, &i00, p01);
740 static int piz_uncompress(EXRContext *s, const uint8_t *src, int ssize,
741 int dsize, EXRThreadData *td)
744 uint16_t maxval, min_non_zero, max_non_zero;
746 uint16_t *tmp = (uint16_t *)td->tmp;
751 td->bitmap = av_malloc(BITMAP_SIZE);
753 td->lut = av_malloc(1 << 17);
754 if (!td->bitmap || !td->lut) {
755 av_freep(&td->bitmap);
757 return AVERROR(ENOMEM);
760 bytestream2_init(&gb, src, ssize);
761 min_non_zero = bytestream2_get_le16(&gb);
762 max_non_zero = bytestream2_get_le16(&gb);
764 if (max_non_zero >= BITMAP_SIZE)
765 return AVERROR_INVALIDDATA;
767 memset(td->bitmap, 0, FFMIN(min_non_zero, BITMAP_SIZE));
768 if (min_non_zero <= max_non_zero)
769 bytestream2_get_buffer(&gb, td->bitmap + min_non_zero,
770 max_non_zero - min_non_zero + 1);
771 memset(td->bitmap + max_non_zero, 0, BITMAP_SIZE - max_non_zero);
773 maxval = reverse_lut(td->bitmap, td->lut);
775 ret = huf_uncompress(&gb, tmp, dsize / sizeof(uint16_t));
780 for (i = 0; i < s->nb_channels; i++) {
781 EXRChannel *channel = &s->channels[i];
782 int size = channel->pixel_type;
784 for (j = 0; j < size; j++)
785 wav_decode(ptr + j, s->xsize, size, s->ysize,
786 s->xsize * size, maxval);
787 ptr += s->xsize * s->ysize * size;
790 apply_lut(td->lut, tmp, dsize / sizeof(uint16_t));
792 out = td->uncompressed_data;
793 for (i = 0; i < s->ysize; i++)
794 for (j = 0; j < s->nb_channels; j++) {
795 uint16_t *in = tmp + j * s->xsize * s->ysize + i * s->xsize;
796 memcpy(out, in, s->xsize * 2);
803 static int pxr24_uncompress(EXRContext *s, const uint8_t *src,
804 int compressed_size, int uncompressed_size,
807 unsigned long dest_len, expected_len;
808 const uint8_t *in = td->tmp;
812 if (s->pixel_type == EXR_FLOAT)
813 expected_len = (uncompressed_size / 4) * 3; /* PRX 24 store float in 24 bit instead of 32 */
815 expected_len = uncompressed_size;
817 dest_len = expected_len;
819 if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK) {
820 return AVERROR_INVALIDDATA;
821 } else if (dest_len != expected_len) {
822 return AVERROR_INVALIDDATA;
825 out = td->uncompressed_data;
826 for (i = 0; i < s->ysize; i++)
827 for (c = 0; c < s->nb_channels; c++) {
828 EXRChannel *channel = &s->channels[c];
829 const uint8_t *ptr[4];
832 switch (channel->pixel_type) {
835 ptr[1] = ptr[0] + s->xsize;
836 ptr[2] = ptr[1] + s->xsize;
837 in = ptr[2] + s->xsize;
839 for (j = 0; j < s->xsize; ++j) {
840 uint32_t diff = (*(ptr[0]++) << 24) |
841 (*(ptr[1]++) << 16) |
844 bytestream_put_le32(&out, pixel);
849 ptr[1] = ptr[0] + s->xsize;
850 in = ptr[1] + s->xsize;
851 for (j = 0; j < s->xsize; j++) {
852 uint32_t diff = (*(ptr[0]++) << 8) | *(ptr[1]++);
855 bytestream_put_le16(&out, pixel);
859 return AVERROR_INVALIDDATA;
866 static void unpack_14(const uint8_t b[14], uint16_t s[16])
868 unsigned short shift = (b[ 2] >> 2);
869 unsigned short bias = (0x20 << shift);
872 s[ 0] = (b[0] << 8) | b[1];
874 s[ 4] = s[ 0] + ((((b[ 2] << 4) | (b[ 3] >> 4)) & 0x3f) << shift) - bias;
875 s[ 8] = s[ 4] + ((((b[ 3] << 2) | (b[ 4] >> 6)) & 0x3f) << shift) - bias;
876 s[12] = s[ 8] + ((b[ 4] & 0x3f) << shift) - bias;
878 s[ 1] = s[ 0] + ((b[ 5] >> 2) << shift) - bias;
879 s[ 5] = s[ 4] + ((((b[ 5] << 4) | (b[ 6] >> 4)) & 0x3f) << shift) - bias;
880 s[ 9] = s[ 8] + ((((b[ 6] << 2) | (b[ 7] >> 6)) & 0x3f) << shift) - bias;
881 s[13] = s[12] + ((b[ 7] & 0x3f) << shift) - bias;
883 s[ 2] = s[ 1] + ((b[ 8] >> 2) << shift) - bias;
884 s[ 6] = s[ 5] + ((((b[ 8] << 4) | (b[ 9] >> 4)) & 0x3f) << shift) - bias;
885 s[10] = s[ 9] + ((((b[ 9] << 2) | (b[10] >> 6)) & 0x3f) << shift) - bias;
886 s[14] = s[13] + ((b[10] & 0x3f) << shift) - bias;
888 s[ 3] = s[ 2] + ((b[11] >> 2) << shift) - bias;
889 s[ 7] = s[ 6] + ((((b[11] << 4) | (b[12] >> 4)) & 0x3f) << shift) - bias;
890 s[11] = s[10] + ((((b[12] << 2) | (b[13] >> 6)) & 0x3f) << shift) - bias;
891 s[15] = s[14] + ((b[13] & 0x3f) << shift) - bias;
893 for (i = 0; i < 16; ++i) {
901 static void unpack_3(const uint8_t b[3], uint16_t s[16])
905 s[0] = (b[0] << 8) | b[1];
912 for (i = 1; i < 16; i++)
917 static int b44_uncompress(EXRContext *s, const uint8_t *src, int compressed_size,
918 int uncompressed_size, EXRThreadData *td) {
919 const int8_t *sr = src;
920 int stayToUncompress = compressed_size;
921 int nbB44BlockW, nbB44BlockH;
922 int indexHgX, indexHgY, indexOut, indexTmp;
923 uint16_t tmpBuffer[16]; /* B44 use 4x4 half float pixel */
926 /* calc B44 block count */
927 nbB44BlockW = s->xsize / 4;
928 if ((s->xsize % 4) != 0)
931 nbB44BlockH = s->ysize / 4;
932 if ((s->ysize % 4) != 0)
935 for (c = 0; c < s->nb_channels; c++) {
936 for (iY = 0; iY < nbB44BlockH; iY++) {
937 for (iX = 0; iX < nbB44BlockW; iX++) {/* For each B44 block */
938 if (stayToUncompress < 3) {
939 av_log(s, AV_LOG_ERROR, "Not enough data for B44A block: %d", stayToUncompress);
940 return AVERROR_INVALIDDATA;
943 if (src[compressed_size - stayToUncompress + 2] == 0xfc) { /* B44A block */
944 unpack_3(sr, tmpBuffer);
946 stayToUncompress -= 3;
947 } else {/* B44 Block */
948 if (stayToUncompress < 14) {
949 av_log(s, AV_LOG_ERROR, "Not enough data for B44 block: %d", stayToUncompress);
950 return AVERROR_INVALIDDATA;
952 unpack_14(sr, tmpBuffer);
954 stayToUncompress -= 14;
957 /* copy data to uncompress buffer (B44 block can exceed target resolution)*/
961 for (y = indexHgY; y < FFMIN(indexHgY + 4, s->ysize); y++) {
962 for (x = indexHgX; x < FFMIN(indexHgX + 4, s->xsize); x++) {
963 indexOut = (c * s->xsize + y * s->xsize * s->nb_channels + x) * 2;
964 indexTmp = (y-indexHgY) * 4 + (x-indexHgX);
965 td->uncompressed_data[indexOut] = tmpBuffer[indexTmp] & 0xff;
966 td->uncompressed_data[indexOut + 1] = tmpBuffer[indexTmp] >> 8;
976 static int decode_block(AVCodecContext *avctx, void *tdata,
977 int jobnr, int threadnr)
979 EXRContext *s = avctx->priv_data;
980 AVFrame *const p = s->picture;
981 EXRThreadData *td = &s->thread_data[threadnr];
982 const uint8_t *channel_buffer[4] = { 0 };
983 const uint8_t *buf = s->buf;
984 uint64_t line_offset, uncompressed_size;
985 uint32_t xdelta = s->xdelta;
988 uint32_t data_size, line, col = 0;
989 uint32_t tileX, tileY, tileLevelX, tileLevelY;
990 int channelLineSize, indexSrc, tX, tY, tCh;
992 int axmax = (avctx->width - (s->xmax + 1)) * 2 * s->desc->nb_components; /* nb pixel to add at the right of the datawindow */
993 int bxmin = s->xmin * 2 * s->desc->nb_components; /* nb pixel to add at the left of the datawindow */
994 int i, x, buf_size = s->buf_size;
995 float one_gamma = 1.0f / s->gamma;
996 avpriv_trc_function trc_func = avpriv_get_trc_function_from_trc(s->apply_trc_type);
999 line_offset = AV_RL64(s->gb.buffer + jobnr * 8);
1002 if (line_offset > buf_size - 20)
1003 return AVERROR_INVALIDDATA;
1005 src = buf + line_offset + 20;
1007 tileX = AV_RL32(src - 20);
1008 tileY = AV_RL32(src - 16);
1009 tileLevelX = AV_RL32(src - 12);
1010 tileLevelY = AV_RL32(src - 8);
1012 data_size = AV_RL32(src - 4);
1013 if (data_size <= 0 || data_size > buf_size)
1014 return AVERROR_INVALIDDATA;
1016 if (tileLevelX || tileLevelY) { /* tile level, is not the full res level */
1017 avpriv_report_missing_feature(s->avctx, "Subres tile before full res tile");
1018 return AVERROR_PATCHWELCOME;
1021 line = s->tile_attr.ySize * tileY;
1022 col = s->tile_attr.xSize * tileX;
1024 s->ysize = FFMIN(s->tile_attr.ySize, s->ydelta - tileY * s->tile_attr.ySize);
1025 s->xsize = FFMIN(s->tile_attr.xSize, s->xdelta - tileX * s->tile_attr.xSize);
1026 uncompressed_size = s->current_channel_offset * s->ysize * s->xsize;
1028 if (col) { /* not the first tile of the line */
1029 bxmin = 0; axmax = 0; /* doesn't add pixel at the left of the datawindow */
1032 if ((col + s->xsize) != s->xdelta)/* not the last tile of the line */
1033 axmax = 0; /* doesn't add pixel at the right of the datawindow */
1035 if (line_offset > buf_size - 8)
1036 return AVERROR_INVALIDDATA;
1038 src = buf + line_offset + 8;
1039 line = AV_RL32(src - 8);
1040 if (line < s->ymin || line > s->ymax)
1041 return AVERROR_INVALIDDATA;
1043 data_size = AV_RL32(src - 4);
1044 if (data_size <= 0 || data_size > buf_size)
1045 return AVERROR_INVALIDDATA;
1047 s->ysize = FFMIN(s->scan_lines_per_block, s->ymax - line + 1); /* s->ydelta - line ?? */
1048 s->xsize = s->xdelta;
1049 uncompressed_size = s->scan_line_size * s->ysize;
1050 if ((s->compression == EXR_RAW && (data_size != uncompressed_size ||
1051 line_offset > buf_size - uncompressed_size)) ||
1052 (s->compression != EXR_RAW && (data_size > uncompressed_size ||
1053 line_offset > buf_size - data_size))) {
1054 return AVERROR_INVALIDDATA;
1058 if (data_size < uncompressed_size || s->is_tile) { /* td->tmp is use for tile reorganization */
1059 av_fast_padded_malloc(&td->tmp, &td->tmp_size, uncompressed_size);
1061 return AVERROR(ENOMEM);
1064 if (data_size < uncompressed_size) {
1065 av_fast_padded_malloc(&td->uncompressed_data,
1066 &td->uncompressed_size, uncompressed_size);
1068 if (!td->uncompressed_data)
1069 return AVERROR(ENOMEM);
1071 ret = AVERROR_INVALIDDATA;
1072 switch (s->compression) {
1075 ret = zip_uncompress(src, data_size, uncompressed_size, td);
1078 ret = piz_uncompress(s, src, data_size, uncompressed_size, td);
1081 ret = pxr24_uncompress(s, src, data_size, uncompressed_size, td);
1084 ret = rle_uncompress(src, data_size, uncompressed_size, td);
1088 ret = b44_uncompress(s, src, data_size, uncompressed_size, td);
1092 av_log(avctx, AV_LOG_ERROR, "decode_block() failed.\n");
1095 src = td->uncompressed_data;
1100 channelLineSize = s->xsize * 2;
1101 if (s->pixel_type == EXR_FLOAT)
1102 channelLineSize *= 2;
1104 /* reorganise tile data to have each channel one after the other instead of line by line */
1105 for (tY = 0; tY < s->ysize; tY ++) {
1106 for (tCh = 0; tCh < s->nb_channels; tCh++) {
1107 for (tX = 0; tX < channelLineSize; tX++) {
1108 td->tmp[tCh * channelLineSize * s->ysize + tY * channelLineSize + tX] = src[indexSrc];
1114 channel_buffer[0] = td->tmp + s->xsize * s->channel_offsets[0] * s->ysize;
1115 channel_buffer[1] = td->tmp + s->xsize * s->channel_offsets[1] * s->ysize;
1116 channel_buffer[2] = td->tmp + s->xsize * s->channel_offsets[2] * s->ysize;
1117 if (s->channel_offsets[3] >= 0)
1118 channel_buffer[3] = td->tmp + s->xsize * s->channel_offsets[3];
1120 channel_buffer[0] = src + xdelta * s->channel_offsets[0];
1121 channel_buffer[1] = src + xdelta * s->channel_offsets[1];
1122 channel_buffer[2] = src + xdelta * s->channel_offsets[2];
1123 if (s->channel_offsets[3] >= 0)
1124 channel_buffer[3] = src + xdelta * s->channel_offsets[3];
1127 ptr = p->data[0] + line * p->linesize[0] + (col * s->desc->nb_components * 2);
1130 i < s->ysize; i++, ptr += p->linesize[0]) {
1132 const uint8_t *r, *g, *b, *a;
1134 r = channel_buffer[0];
1135 g = channel_buffer[1];
1136 b = channel_buffer[2];
1137 if (channel_buffer[3])
1138 a = channel_buffer[3];
1140 ptr_x = (uint16_t *) ptr;
1142 // Zero out the start if xmin is not 0
1143 memset(ptr_x, 0, bxmin);
1144 ptr_x += s->xmin * s->desc->nb_components;
1146 if (s->pixel_type == EXR_FLOAT) {
1149 for (x = 0; x < s->xsize; x++) {
1150 union av_intfloat32 t;
1151 t.i = bytestream_get_le32(&r);
1152 t.f = trc_func(t.f);
1153 *ptr_x++ = exr_flt2uint(t.i);
1155 t.i = bytestream_get_le32(&g);
1156 t.f = trc_func(t.f);
1157 *ptr_x++ = exr_flt2uint(t.i);
1159 t.i = bytestream_get_le32(&b);
1160 t.f = trc_func(t.f);
1161 *ptr_x++ = exr_flt2uint(t.i);
1162 if (channel_buffer[3])
1163 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a));
1166 for (x = 0; x < s->xsize; x++) {
1167 union av_intfloat32 t;
1168 t.i = bytestream_get_le32(&r);
1169 if (t.f > 0.0f) /* avoid negative values */
1170 t.f = powf(t.f, one_gamma);
1171 *ptr_x++ = exr_flt2uint(t.i);
1173 t.i = bytestream_get_le32(&g);
1175 t.f = powf(t.f, one_gamma);
1176 *ptr_x++ = exr_flt2uint(t.i);
1178 t.i = bytestream_get_le32(&b);
1180 t.f = powf(t.f, one_gamma);
1181 *ptr_x++ = exr_flt2uint(t.i);
1182 if (channel_buffer[3])
1183 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a));
1188 for (x = 0; x < s->xsize; x++) {
1189 *ptr_x++ = s->gamma_table[bytestream_get_le16(&r)];
1190 *ptr_x++ = s->gamma_table[bytestream_get_le16(&g)];
1191 *ptr_x++ = s->gamma_table[bytestream_get_le16(&b)];
1192 if (channel_buffer[3])
1193 *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&a));
1197 // Zero out the end if xmax+1 is not w
1198 memset(ptr_x, 0, axmax);
1201 channel_buffer[0] += channelLineSize;
1202 channel_buffer[1] += channelLineSize;
1203 channel_buffer[2] += channelLineSize;
1204 if (channel_buffer[3])
1205 channel_buffer[3] += channelLineSize;
1207 channel_buffer[0] += s->scan_line_size;
1208 channel_buffer[1] += s->scan_line_size;
1209 channel_buffer[2] += s->scan_line_size;
1210 if (channel_buffer[3])
1211 channel_buffer[3] += s->scan_line_size;
1219 * Check if the variable name corresponds to its data type.
1221 * @param s the EXRContext
1222 * @param value_name name of the variable to check
1223 * @param value_type type of the variable to check
1224 * @param minimum_length minimum length of the variable data
1226 * @return bytes to read containing variable data
1227 * -1 if variable is not found
1228 * 0 if buffer ended prematurely
1230 static int check_header_variable(EXRContext *s,
1231 const char *value_name,
1232 const char *value_type,
1233 unsigned int minimum_length)
1237 if (bytestream2_get_bytes_left(&s->gb) >= minimum_length &&
1238 !strcmp(s->gb.buffer, value_name)) {
1239 // found value_name, jump to value_type (null terminated strings)
1240 s->gb.buffer += strlen(value_name) + 1;
1241 if (!strcmp(s->gb.buffer, value_type)) {
1242 s->gb.buffer += strlen(value_type) + 1;
1243 var_size = bytestream2_get_le32(&s->gb);
1244 // don't go read past boundaries
1245 if (var_size > bytestream2_get_bytes_left(&s->gb))
1248 // value_type not found, reset the buffer
1249 s->gb.buffer -= strlen(value_name) + 1;
1250 av_log(s->avctx, AV_LOG_WARNING,
1251 "Unknown data type %s for header variable %s.\n",
1252 value_type, value_name);
1259 static int decode_header(EXRContext *s)
1261 int magic_number, version, i, flags, sar = 0;
1263 s->current_channel_offset = 0;
1270 s->channel_offsets[0] = -1;
1271 s->channel_offsets[1] = -1;
1272 s->channel_offsets[2] = -1;
1273 s->channel_offsets[3] = -1;
1274 s->pixel_type = EXR_UNKNOWN;
1275 s->compression = EXR_UNKN;
1279 s->tile_attr.xSize = -1;
1280 s->tile_attr.ySize = -1;
1283 if (bytestream2_get_bytes_left(&s->gb) < 10) {
1284 av_log(s->avctx, AV_LOG_ERROR, "Header too short to parse.\n");
1285 return AVERROR_INVALIDDATA;
1288 magic_number = bytestream2_get_le32(&s->gb);
1289 if (magic_number != 20000630) {
1290 /* As per documentation of OpenEXR, it is supposed to be
1291 * int 20000630 little-endian */
1292 av_log(s->avctx, AV_LOG_ERROR, "Wrong magic number %d.\n", magic_number);
1293 return AVERROR_INVALIDDATA;
1296 version = bytestream2_get_byte(&s->gb);
1298 avpriv_report_missing_feature(s->avctx, "Version %d", version);
1299 return AVERROR_PATCHWELCOME;
1302 flags = bytestream2_get_le24(&s->gb);
1306 else if (flags & 0x02)
1309 avpriv_report_missing_feature(s->avctx, "flags %d", flags);
1310 return AVERROR_PATCHWELCOME;
1314 while (bytestream2_get_bytes_left(&s->gb) > 0 && *s->gb.buffer) {
1316 if ((var_size = check_header_variable(s, "channels",
1317 "chlist", 38)) >= 0) {
1318 GetByteContext ch_gb;
1320 return AVERROR_INVALIDDATA;
1322 bytestream2_init(&ch_gb, s->gb.buffer, var_size);
1324 while (bytestream2_get_bytes_left(&ch_gb) >= 19) {
1325 EXRChannel *channel;
1326 enum ExrPixelType current_pixel_type;
1327 int channel_index = -1;
1330 if (strcmp(s->layer, "") != 0) {
1331 if (strncmp(ch_gb.buffer, s->layer, strlen(s->layer)) == 0) {
1332 ch_gb.buffer += strlen(s->layer);
1333 if (*ch_gb.buffer == '.')
1334 ch_gb.buffer++; /* skip dot if not given */
1335 av_log(s->avctx, AV_LOG_INFO,
1336 "Layer %s.%s matched.\n", s->layer, ch_gb.buffer);
1340 if (!strcmp(ch_gb.buffer, "R") ||
1341 !strcmp(ch_gb.buffer, "X") ||
1342 !strcmp(ch_gb.buffer, "U"))
1344 else if (!strcmp(ch_gb.buffer, "G") ||
1345 !strcmp(ch_gb.buffer, "Y") ||
1346 !strcmp(ch_gb.buffer, "V"))
1348 else if (!strcmp(ch_gb.buffer, "B") ||
1349 !strcmp(ch_gb.buffer, "Z") ||
1350 !strcmp(ch_gb.buffer, "W"))
1352 else if (!strcmp(ch_gb.buffer, "A"))
1355 av_log(s->avctx, AV_LOG_WARNING,
1356 "Unsupported channel %.256s.\n", ch_gb.buffer);
1358 /* skip until you get a 0 */
1359 while (bytestream2_get_bytes_left(&ch_gb) > 0 &&
1360 bytestream2_get_byte(&ch_gb))
1363 if (bytestream2_get_bytes_left(&ch_gb) < 4) {
1364 av_log(s->avctx, AV_LOG_ERROR, "Incomplete header.\n");
1365 return AVERROR_INVALIDDATA;
1368 current_pixel_type = bytestream2_get_le32(&ch_gb);
1369 if (current_pixel_type >= EXR_UNKNOWN) {
1370 avpriv_report_missing_feature(s->avctx, "Pixel type %d",
1371 current_pixel_type);
1372 return AVERROR_PATCHWELCOME;
1375 bytestream2_skip(&ch_gb, 4);
1376 xsub = bytestream2_get_le32(&ch_gb);
1377 ysub = bytestream2_get_le32(&ch_gb);
1378 if (xsub != 1 || ysub != 1) {
1379 avpriv_report_missing_feature(s->avctx,
1380 "Subsampling %dx%d",
1382 return AVERROR_PATCHWELCOME;
1385 if (s->channel_offsets[channel_index] == -1){/* channel have not been previously assign */
1386 if (channel_index >= 0) {
1387 if (s->pixel_type != EXR_UNKNOWN &&
1388 s->pixel_type != current_pixel_type) {
1389 av_log(s->avctx, AV_LOG_ERROR,
1390 "RGB channels not of the same depth.\n");
1391 return AVERROR_INVALIDDATA;
1393 s->pixel_type = current_pixel_type;
1394 s->channel_offsets[channel_index] = s->current_channel_offset;
1398 s->channels = av_realloc(s->channels,
1399 ++s->nb_channels * sizeof(EXRChannel));
1401 return AVERROR(ENOMEM);
1402 channel = &s->channels[s->nb_channels - 1];
1403 channel->pixel_type = current_pixel_type;
1404 channel->xsub = xsub;
1405 channel->ysub = ysub;
1407 s->current_channel_offset += 1 << current_pixel_type;
1410 /* Check if all channels are set with an offset or if the channels
1411 * are causing an overflow */
1412 if (FFMIN3(s->channel_offsets[0],
1413 s->channel_offsets[1],
1414 s->channel_offsets[2]) < 0) {
1415 if (s->channel_offsets[0] < 0)
1416 av_log(s->avctx, AV_LOG_ERROR, "Missing red channel.\n");
1417 if (s->channel_offsets[1] < 0)
1418 av_log(s->avctx, AV_LOG_ERROR, "Missing green channel.\n");
1419 if (s->channel_offsets[2] < 0)
1420 av_log(s->avctx, AV_LOG_ERROR, "Missing blue channel.\n");
1421 return AVERROR_INVALIDDATA;
1424 // skip one last byte and update main gb
1425 s->gb.buffer = ch_gb.buffer + 1;
1427 } else if ((var_size = check_header_variable(s, "dataWindow", "box2i",
1430 return AVERROR_INVALIDDATA;
1432 s->xmin = bytestream2_get_le32(&s->gb);
1433 s->ymin = bytestream2_get_le32(&s->gb);
1434 s->xmax = bytestream2_get_le32(&s->gb);
1435 s->ymax = bytestream2_get_le32(&s->gb);
1436 s->xdelta = (s->xmax - s->xmin) + 1;
1437 s->ydelta = (s->ymax - s->ymin) + 1;
1440 } else if ((var_size = check_header_variable(s, "displayWindow",
1441 "box2i", 34)) >= 0) {
1443 return AVERROR_INVALIDDATA;
1445 bytestream2_skip(&s->gb, 8);
1446 s->w = bytestream2_get_le32(&s->gb) + 1;
1447 s->h = bytestream2_get_le32(&s->gb) + 1;
1450 } else if ((var_size = check_header_variable(s, "lineOrder",
1451 "lineOrder", 25)) >= 0) {
1454 return AVERROR_INVALIDDATA;
1456 line_order = bytestream2_get_byte(&s->gb);
1457 av_log(s->avctx, AV_LOG_DEBUG, "line order: %d.\n", line_order);
1458 if (line_order > 2) {
1459 av_log(s->avctx, AV_LOG_ERROR, "Unknown line order.\n");
1460 return AVERROR_INVALIDDATA;
1464 } else if ((var_size = check_header_variable(s, "pixelAspectRatio",
1465 "float", 31)) >= 0) {
1467 return AVERROR_INVALIDDATA;
1469 sar = bytestream2_get_le32(&s->gb);
1472 } else if ((var_size = check_header_variable(s, "compression",
1473 "compression", 29)) >= 0) {
1475 return AVERROR_INVALIDDATA;
1477 if (s->compression == EXR_UNKN)
1478 s->compression = bytestream2_get_byte(&s->gb);
1480 av_log(s->avctx, AV_LOG_WARNING,
1481 "Found more than one compression attribute.\n");
1484 } else if ((var_size = check_header_variable(s, "tiles",
1485 "tiledesc", 22)) >= 0) {
1487 av_log(s->avctx, AV_LOG_WARNING,
1488 "Found tile attribute and scanline flags. Exr will be interpreted as scanline.\n");
1490 s->tile_attr.xSize = bytestream2_get_le32(&s->gb);
1491 s->tile_attr.ySize = bytestream2_get_le32(&s->gb);
1493 char tileLevel = bytestream2_get_byte(&s->gb);
1494 s->tile_attr.level_mode = tileLevel & 0x0f;
1495 s->tile_attr.level_round = (tileLevel >> 4) & 0x0f;
1497 if (s->tile_attr.level_mode >= EXR_TILE_LEVEL_UNKNOWN){
1498 avpriv_report_missing_feature(s->avctx, "Tile level mode %d",
1499 s->tile_attr.level_mode);
1500 return AVERROR_PATCHWELCOME;
1503 if (s->tile_attr.level_round >= EXR_TILE_ROUND_UNKNOWN) {
1504 avpriv_report_missing_feature(s->avctx, "Tile level round %d",
1505 s->tile_attr.level_round);
1506 return AVERROR_PATCHWELCOME;
1512 // Check if there are enough bytes for a header
1513 if (bytestream2_get_bytes_left(&s->gb) <= 9) {
1514 av_log(s->avctx, AV_LOG_ERROR, "Incomplete header\n");
1515 return AVERROR_INVALIDDATA;
1518 // Process unknown variables
1519 for (i = 0; i < 2; i++) // value_name and value_type
1520 while (bytestream2_get_byte(&s->gb) != 0);
1522 // Skip variable length
1523 bytestream2_skip(&s->gb, bytestream2_get_le32(&s->gb));
1526 ff_set_sar(s->avctx, av_d2q(av_int2float(sar), 255));
1528 if (s->compression == EXR_UNKN) {
1529 av_log(s->avctx, AV_LOG_ERROR, "Missing compression attribute.\n");
1530 return AVERROR_INVALIDDATA;
1534 if (s->tile_attr.xSize < 1 || s->tile_attr.ySize < 1) {
1535 av_log(s->avctx, AV_LOG_ERROR, "Invalid tile attribute.\n");
1536 return AVERROR_INVALIDDATA;
1540 s->scan_line_size = s->xdelta * s->current_channel_offset;
1542 if (bytestream2_get_bytes_left(&s->gb) <= 0) {
1543 av_log(s->avctx, AV_LOG_ERROR, "Incomplete frame.\n");
1544 return AVERROR_INVALIDDATA;
1547 // aaand we are done
1548 bytestream2_skip(&s->gb, 1);
1552 static int decode_frame(AVCodecContext *avctx, void *data,
1553 int *got_frame, AVPacket *avpkt)
1555 EXRContext *s = avctx->priv_data;
1556 ThreadFrame frame = { .f = data };
1557 AVFrame *picture = data;
1562 int nb_blocks;/* nb scanline or nb tile */
1564 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
1566 if ((ret = decode_header(s)) < 0)
1569 switch (s->pixel_type) {
1572 if (s->channel_offsets[3] >= 0)
1573 avctx->pix_fmt = AV_PIX_FMT_RGBA64;
1575 avctx->pix_fmt = AV_PIX_FMT_RGB48;
1578 avpriv_request_sample(avctx, "32-bit unsigned int");
1579 return AVERROR_PATCHWELCOME;
1581 av_log(avctx, AV_LOG_ERROR, "Missing channel list.\n");
1582 return AVERROR_INVALIDDATA;
1585 if (s->apply_trc_type != AVCOL_TRC_UNSPECIFIED)
1586 avctx->color_trc = s->apply_trc_type;
1588 switch (s->compression) {
1592 s->scan_lines_per_block = 1;
1596 s->scan_lines_per_block = 16;
1601 s->scan_lines_per_block = 32;
1604 avpriv_report_missing_feature(avctx, "Compression %d", s->compression);
1605 return AVERROR_PATCHWELCOME;
1608 /* Verify the xmin, xmax, ymin, ymax and xdelta before setting
1609 * the actual image size. */
1610 if (s->xmin > s->xmax ||
1611 s->ymin > s->ymax ||
1612 s->xdelta != s->xmax - s->xmin + 1 ||
1615 av_log(avctx, AV_LOG_ERROR, "Wrong or missing size information.\n");
1616 return AVERROR_INVALIDDATA;
1619 if ((ret = ff_set_dimensions(avctx, s->w, s->h)) < 0)
1622 s->desc = av_pix_fmt_desc_get(avctx->pix_fmt);
1624 return AVERROR_INVALIDDATA;
1625 out_line_size = avctx->width * 2 * s->desc->nb_components;
1628 nb_blocks = ((s->xdelta + s->tile_attr.xSize - 1) / s->tile_attr.xSize) *
1629 ((s->ydelta + s->tile_attr.ySize - 1) / s->tile_attr.ySize);
1630 } else { /* scanline */
1631 nb_blocks = (s->ydelta + s->scan_lines_per_block - 1) /
1632 s->scan_lines_per_block;
1635 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
1638 if (bytestream2_get_bytes_left(&s->gb) < nb_blocks * 8)
1639 return AVERROR_INVALIDDATA;
1641 // save pointer we are going to use in decode_block
1642 s->buf = avpkt->data;
1643 s->buf_size = avpkt->size;
1644 ptr = picture->data[0];
1646 // Zero out the start if ymin is not 0
1647 for (y = 0; y < s->ymin; y++) {
1648 memset(ptr, 0, out_line_size);
1649 ptr += picture->linesize[0];
1652 s->picture = picture;
1654 avctx->execute2(avctx, decode_block, s->thread_data, NULL, nb_blocks);
1656 // Zero out the end if ymax+1 is not h
1657 for (y = s->ymax + 1; y < avctx->height; y++) {
1658 memset(ptr, 0, out_line_size);
1659 ptr += picture->linesize[0];
1662 picture->pict_type = AV_PICTURE_TYPE_I;
1668 static av_cold int decode_init(AVCodecContext *avctx)
1670 EXRContext *s = avctx->priv_data;
1672 union av_intfloat32 t;
1673 float one_gamma = 1.0f / s->gamma;
1674 avpriv_trc_function trc_func = NULL;
1678 trc_func = avpriv_get_trc_function_from_trc(s->apply_trc_type);
1680 for (i = 0; i < 65536; ++i) {
1681 t = exr_half2float(i);
1682 t.f = trc_func(t.f);
1683 s->gamma_table[i] = exr_flt2uint(t.i);
1686 if (one_gamma > 0.9999f && one_gamma < 1.0001f) {
1687 for (i = 0; i < 65536; ++i)
1688 s->gamma_table[i] = exr_halflt2uint(i);
1690 for (i = 0; i < 65536; ++i) {
1691 t = exr_half2float(i);
1692 /* If negative value we reuse half value */
1694 s->gamma_table[i] = exr_halflt2uint(i);
1696 t.f = powf(t.f, one_gamma);
1697 s->gamma_table[i] = exr_flt2uint(t.i);
1703 // allocate thread data, used for non EXR_RAW compreesion types
1704 s->thread_data = av_mallocz_array(avctx->thread_count, sizeof(EXRThreadData));
1705 if (!s->thread_data)
1706 return AVERROR_INVALIDDATA;
1712 static int decode_init_thread_copy(AVCodecContext *avctx)
1713 { EXRContext *s = avctx->priv_data;
1715 // allocate thread data, used for non EXR_RAW compreesion types
1716 s->thread_data = av_mallocz_array(avctx->thread_count, sizeof(EXRThreadData));
1717 if (!s->thread_data)
1718 return AVERROR_INVALIDDATA;
1724 static av_cold int decode_end(AVCodecContext *avctx)
1726 EXRContext *s = avctx->priv_data;
1728 for (i = 0; i < avctx->thread_count; i++) {
1729 EXRThreadData *td = &s->thread_data[i];
1730 av_freep(&td->uncompressed_data);
1732 av_freep(&td->bitmap);
1736 av_freep(&s->thread_data);
1737 av_freep(&s->channels);
1742 #define OFFSET(x) offsetof(EXRContext, x)
1743 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1744 static const AVOption options[] = {
1745 { "layer", "Set the decoding layer", OFFSET(layer),
1746 AV_OPT_TYPE_STRING, { .str = "" }, 0, 0, VD },
1747 { "gamma", "Set the float gamma value when decoding", OFFSET(gamma),
1748 AV_OPT_TYPE_FLOAT, { .dbl = 1.0f }, 0.001, FLT_MAX, VD },
1750 // XXX: Note the abuse of the enum using AVCOL_TRC_UNSPECIFIED to subsume the existing gamma option
1751 { "apply_trc", "color transfer characteristics to apply to EXR linear input", OFFSET(apply_trc_type),
1752 AV_OPT_TYPE_INT, {.i64 = AVCOL_TRC_UNSPECIFIED }, 1, AVCOL_TRC_NB-1, VD, "apply_trc_type"},
1753 { "bt709", "BT.709", 0,
1754 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT709 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1755 { "gamma", "gamma", 0,
1756 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_UNSPECIFIED }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1757 { "gamma22", "BT.470 M", 0,
1758 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_GAMMA22 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1759 { "gamma28", "BT.470 BG", 0,
1760 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_GAMMA28 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1761 { "smpte170m", "SMPTE 170 M", 0,
1762 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTE170M }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1763 { "smpte240m", "SMPTE 240 M", 0,
1764 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTE240M }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1765 { "linear", "Linear", 0,
1766 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LINEAR }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1768 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LOG }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1769 { "log_sqrt", "Log square root", 0,
1770 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LOG_SQRT }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1771 { "iec61966_2_4", "IEC 61966-2-4", 0,
1772 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_IEC61966_2_4 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1773 { "bt1361", "BT.1361", 0,
1774 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT1361_ECG }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1775 { "iec61966_2_1", "IEC 61966-2-1", 0,
1776 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_IEC61966_2_1 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1777 { "bt2020_10bit", "BT.2020 - 10 bit", 0,
1778 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT2020_10 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1779 { "bt2020_12bit", "BT.2020 - 12 bit", 0,
1780 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT2020_12 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1781 { "smpte2084", "SMPTE ST 2084", 0,
1782 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTEST2084 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1783 { "smpte428_1", "SMPTE ST 428-1", 0,
1784 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTEST428_1 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1789 static const AVClass exr_class = {
1790 .class_name = "EXR",
1791 .item_name = av_default_item_name,
1793 .version = LIBAVUTIL_VERSION_INT,
1796 AVCodec ff_exr_decoder = {
1798 .long_name = NULL_IF_CONFIG_SMALL("OpenEXR image"),
1799 .type = AVMEDIA_TYPE_VIDEO,
1800 .id = AV_CODEC_ID_EXR,
1801 .priv_data_size = sizeof(EXRContext),
1802 .init = decode_init,
1803 .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
1804 .close = decode_end,
1805 .decode = decode_frame,
1806 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS |
1807 AV_CODEC_CAP_SLICE_THREADS,
1808 .priv_class = &exr_class,