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"
74 enum ExrTileLevelMode {
76 EXR_TILE_LEVEL_MIPMAP,
77 EXR_TILE_LEVEL_RIPMAP,
78 EXR_TILE_LEVEL_UNKNOWN,
81 enum ExrTileLevelRound {
84 EXR_TILE_ROUND_UNKNOWN,
87 typedef struct EXRChannel {
89 enum ExrPixelType pixel_type;
92 typedef struct EXRTileAttribute {
95 enum ExrTileLevelMode level_mode;
96 enum ExrTileLevelRound level_round;
99 typedef struct EXRThreadData {
100 uint8_t *uncompressed_data;
101 int uncompressed_size;
111 int channel_line_size;
114 typedef struct EXRContext {
117 AVCodecContext *avctx;
119 enum ExrCompr compression;
120 enum ExrPixelType pixel_type;
121 int channel_offsets[4]; // 0 = red, 1 = green, 2 = blue and 3 = alpha
122 const AVPixFmtDescriptor *desc;
127 uint32_t xdelta, ydelta;
129 int scan_lines_per_block;
131 EXRTileAttribute tile_attr; /* header data attribute of tile */
132 int is_tile; /* 0 if scanline, 1 if tile */
134 int is_luma;/* 1 if there is an Y plane */
140 EXRChannel *channels;
142 int current_channel_offset;
144 EXRThreadData *thread_data;
148 enum AVColorTransferCharacteristic apply_trc_type;
150 uint16_t gamma_table[65536];
153 /* -15 stored using a single precision bias of 127 */
154 #define HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP 0x38000000
156 /* max exponent value in single precision that will be converted
157 * to Inf or Nan when stored as a half-float */
158 #define HALF_FLOAT_MAX_BIASED_EXP_AS_SINGLE_FP_EXP 0x47800000
160 /* 255 is the max exponent biased value */
161 #define FLOAT_MAX_BIASED_EXP (0xFF << 23)
163 #define HALF_FLOAT_MAX_BIASED_EXP (0x1F << 10)
166 * Convert a half float as a uint16_t into a full float.
168 * @param hf half float as uint16_t
170 * @return float value
172 static union av_intfloat32 exr_half2float(uint16_t hf)
174 unsigned int sign = (unsigned int) (hf >> 15);
175 unsigned int mantissa = (unsigned int) (hf & ((1 << 10) - 1));
176 unsigned int exp = (unsigned int) (hf & HALF_FLOAT_MAX_BIASED_EXP);
177 union av_intfloat32 f;
179 if (exp == HALF_FLOAT_MAX_BIASED_EXP) {
180 // we have a half-float NaN or Inf
181 // half-float NaNs will be converted to a single precision NaN
182 // half-float Infs will be converted to a single precision Inf
183 exp = FLOAT_MAX_BIASED_EXP;
185 mantissa = (1 << 23) - 1; // set all bits to indicate a NaN
186 } else if (exp == 0x0) {
187 // convert half-float zero/denorm to single precision value
190 exp = HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP;
191 // check for leading 1 in denorm mantissa
192 while ((mantissa & (1 << 10))) {
193 // for every leading 0, decrement single precision exponent by 1
194 // and shift half-float mantissa value to the left
198 // clamp the mantissa to 10 bits
199 mantissa &= ((1 << 10) - 1);
200 // shift left to generate single-precision mantissa of 23 bits
204 // shift left to generate single-precision mantissa of 23 bits
206 // generate single precision biased exponent value
207 exp = (exp << 13) + HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP;
210 f.i = (sign << 31) | exp | mantissa;
217 * Convert from 32-bit float as uint32_t to uint16_t.
219 * @param v 32-bit float
221 * @return normalized 16-bit unsigned int
223 static inline uint16_t exr_flt2uint(uint32_t v)
225 unsigned int exp = v >> 23;
226 // "HACK": negative values result in exp< 0, so clipping them to 0
227 // is also handled by this condition, avoids explicit check for sign bit.
228 if (exp <= 127 + 7 - 24) // we would shift out all bits anyway
233 return (v + (1 << 23)) >> (127 + 7 - exp);
237 * Convert from 16-bit float as uint16_t to uint16_t.
239 * @param v 16-bit float
241 * @return normalized 16-bit unsigned int
243 static inline uint16_t exr_halflt2uint(uint16_t v)
245 unsigned exp = 14 - (v >> 10);
250 return (v & 0x8000) ? 0 : 0xffff;
253 return (v + (1 << 16)) >> (exp + 1);
256 static void predictor(uint8_t *src, int size)
258 uint8_t *t = src + 1;
259 uint8_t *stop = src + size;
262 int d = (int) t[-1] + (int) t[0] - 128;
268 static void reorder_pixels(uint8_t *src, uint8_t *dst, int size)
270 const int8_t *t1 = src;
271 const int8_t *t2 = src + (size + 1) / 2;
273 int8_t *stop = s + size;
288 static int zip_uncompress(const uint8_t *src, int compressed_size,
289 int uncompressed_size, EXRThreadData *td)
291 unsigned long dest_len = uncompressed_size;
293 if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK ||
294 dest_len != uncompressed_size)
295 return AVERROR_INVALIDDATA;
297 predictor(td->tmp, uncompressed_size);
298 reorder_pixels(td->tmp, td->uncompressed_data, uncompressed_size);
303 static int rle_uncompress(const uint8_t *src, int compressed_size,
304 int uncompressed_size, EXRThreadData *td)
306 uint8_t *d = td->tmp;
307 const int8_t *s = src;
308 int ssize = compressed_size;
309 int dsize = uncompressed_size;
310 uint8_t *dend = d + dsize;
319 if ((dsize -= count) < 0 ||
320 (ssize -= count + 1) < 0)
321 return AVERROR_INVALIDDATA;
328 if ((dsize -= count) < 0 ||
330 return AVERROR_INVALIDDATA;
340 return AVERROR_INVALIDDATA;
342 predictor(td->tmp, uncompressed_size);
343 reorder_pixels(td->tmp, td->uncompressed_data, uncompressed_size);
348 #define USHORT_RANGE (1 << 16)
349 #define BITMAP_SIZE (1 << 13)
351 static uint16_t reverse_lut(const uint8_t *bitmap, uint16_t *lut)
355 for (i = 0; i < USHORT_RANGE; i++)
356 if ((i == 0) || (bitmap[i >> 3] & (1 << (i & 7))))
361 memset(lut + k, 0, (USHORT_RANGE - k) * 2);
366 static void apply_lut(const uint16_t *lut, uint16_t *dst, int dsize)
370 for (i = 0; i < dsize; ++i)
371 dst[i] = lut[dst[i]];
374 #define HUF_ENCBITS 16 // literal (value) bit length
375 #define HUF_DECBITS 14 // decoding bit size (>= 8)
377 #define HUF_ENCSIZE ((1 << HUF_ENCBITS) + 1) // encoding table size
378 #define HUF_DECSIZE (1 << HUF_DECBITS) // decoding table size
379 #define HUF_DECMASK (HUF_DECSIZE - 1)
381 typedef struct HufDec {
387 static void huf_canonical_code_table(uint64_t *hcode)
389 uint64_t c, n[59] = { 0 };
392 for (i = 0; i < HUF_ENCSIZE; ++i)
396 for (i = 58; i > 0; --i) {
397 uint64_t nc = ((c + n[i]) >> 1);
402 for (i = 0; i < HUF_ENCSIZE; ++i) {
406 hcode[i] = l | (n[l]++ << 6);
410 #define SHORT_ZEROCODE_RUN 59
411 #define LONG_ZEROCODE_RUN 63
412 #define SHORTEST_LONG_RUN (2 + LONG_ZEROCODE_RUN - SHORT_ZEROCODE_RUN)
413 #define LONGEST_LONG_RUN (255 + SHORTEST_LONG_RUN)
415 static int huf_unpack_enc_table(GetByteContext *gb,
416 int32_t im, int32_t iM, uint64_t *hcode)
419 int ret = init_get_bits8(&gbit, gb->buffer, bytestream2_get_bytes_left(gb));
423 for (; im <= iM; im++) {
424 uint64_t l = hcode[im] = get_bits(&gbit, 6);
426 if (l == LONG_ZEROCODE_RUN) {
427 int zerun = get_bits(&gbit, 8) + SHORTEST_LONG_RUN;
429 if (im + zerun > iM + 1)
430 return AVERROR_INVALIDDATA;
436 } else if (l >= SHORT_ZEROCODE_RUN) {
437 int zerun = l - SHORT_ZEROCODE_RUN + 2;
439 if (im + zerun > iM + 1)
440 return AVERROR_INVALIDDATA;
449 bytestream2_skip(gb, (get_bits_count(&gbit) + 7) / 8);
450 huf_canonical_code_table(hcode);
455 static int huf_build_dec_table(const uint64_t *hcode, int im,
456 int iM, HufDec *hdecod)
458 for (; im <= iM; im++) {
459 uint64_t c = hcode[im] >> 6;
460 int i, l = hcode[im] & 63;
463 return AVERROR_INVALIDDATA;
465 if (l > HUF_DECBITS) {
466 HufDec *pl = hdecod + (c >> (l - HUF_DECBITS));
468 return AVERROR_INVALIDDATA;
472 pl->p = av_realloc(pl->p, pl->lit * sizeof(int));
474 return AVERROR(ENOMEM);
476 pl->p[pl->lit - 1] = im;
478 HufDec *pl = hdecod + (c << (HUF_DECBITS - l));
480 for (i = 1 << (HUF_DECBITS - l); i > 0; i--, pl++) {
481 if (pl->len || pl->p)
482 return AVERROR_INVALIDDATA;
492 #define get_char(c, lc, gb) \
494 c = (c << 8) | bytestream2_get_byte(gb); \
498 #define get_code(po, rlc, c, lc, gb, out, oe, outb) \
502 get_char(c, lc, gb); \
507 if (out + cs > oe || out == outb) \
508 return AVERROR_INVALIDDATA; \
514 } else if (out < oe) { \
517 return AVERROR_INVALIDDATA; \
521 static int huf_decode(const uint64_t *hcode, const HufDec *hdecod,
522 GetByteContext *gb, int nbits,
523 int rlc, int no, uint16_t *out)
526 uint16_t *outb = out;
527 uint16_t *oe = out + no;
528 const uint8_t *ie = gb->buffer + (nbits + 7) / 8; // input byte size
533 while (gb->buffer < ie) {
536 while (lc >= HUF_DECBITS) {
537 const HufDec pl = hdecod[(c >> (lc - HUF_DECBITS)) & HUF_DECMASK];
541 get_code(pl.lit, rlc, c, lc, gb, out, oe, outb);
546 return AVERROR_INVALIDDATA;
548 for (j = 0; j < pl.lit; j++) {
549 int l = hcode[pl.p[j]] & 63;
551 while (lc < l && bytestream2_get_bytes_left(gb) > 0)
555 if ((hcode[pl.p[j]] >> 6) ==
556 ((c >> (lc - l)) & ((1LL << l) - 1))) {
558 get_code(pl.p[j], rlc, c, lc, gb, out, oe, outb);
565 return AVERROR_INVALIDDATA;
575 const HufDec pl = hdecod[(c << (HUF_DECBITS - lc)) & HUF_DECMASK];
579 get_code(pl.lit, rlc, c, lc, gb, out, oe, outb);
581 return AVERROR_INVALIDDATA;
585 if (out - outb != no)
586 return AVERROR_INVALIDDATA;
590 static int huf_uncompress(GetByteContext *gb,
591 uint16_t *dst, int dst_size)
593 int32_t src_size, im, iM;
599 src_size = bytestream2_get_le32(gb);
600 im = bytestream2_get_le32(gb);
601 iM = bytestream2_get_le32(gb);
602 bytestream2_skip(gb, 4);
603 nBits = bytestream2_get_le32(gb);
604 if (im < 0 || im >= HUF_ENCSIZE ||
605 iM < 0 || iM >= HUF_ENCSIZE ||
607 return AVERROR_INVALIDDATA;
609 bytestream2_skip(gb, 4);
611 freq = av_mallocz_array(HUF_ENCSIZE, sizeof(*freq));
612 hdec = av_mallocz_array(HUF_DECSIZE, sizeof(*hdec));
613 if (!freq || !hdec) {
614 ret = AVERROR(ENOMEM);
618 if ((ret = huf_unpack_enc_table(gb, im, iM, freq)) < 0)
621 if (nBits > 8 * bytestream2_get_bytes_left(gb)) {
622 ret = AVERROR_INVALIDDATA;
626 if ((ret = huf_build_dec_table(freq, im, iM, hdec)) < 0)
628 ret = huf_decode(freq, hdec, gb, nBits, iM, dst_size, dst);
631 for (i = 0; i < HUF_DECSIZE; i++)
633 av_freep(&hdec[i].p);
641 static inline void wdec14(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
646 int ai = ls + (hi & 1) + (hi >> 1);
648 int16_t bs = ai - hi;
655 #define A_OFFSET (1 << (NBITS - 1))
656 #define MOD_MASK ((1 << NBITS) - 1)
658 static inline void wdec16(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
662 int bb = (m - (d >> 1)) & MOD_MASK;
663 int aa = (d + bb - A_OFFSET) & MOD_MASK;
668 static void wav_decode(uint16_t *in, int nx, int ox,
669 int ny, int oy, uint16_t mx)
671 int w14 = (mx < (1 << 14));
672 int n = (nx > ny) ? ny : nx;
685 uint16_t *ey = in + oy * (ny - p2);
686 uint16_t i00, i01, i10, i11;
692 for (; py <= ey; py += oy2) {
694 uint16_t *ex = py + ox * (nx - p2);
696 for (; px <= ex; px += ox2) {
697 uint16_t *p01 = px + ox1;
698 uint16_t *p10 = px + oy1;
699 uint16_t *p11 = p10 + ox1;
702 wdec14(*px, *p10, &i00, &i10);
703 wdec14(*p01, *p11, &i01, &i11);
704 wdec14(i00, i01, px, p01);
705 wdec14(i10, i11, p10, p11);
707 wdec16(*px, *p10, &i00, &i10);
708 wdec16(*p01, *p11, &i01, &i11);
709 wdec16(i00, i01, px, p01);
710 wdec16(i10, i11, p10, p11);
715 uint16_t *p10 = px + oy1;
718 wdec14(*px, *p10, &i00, p10);
720 wdec16(*px, *p10, &i00, p10);
728 uint16_t *ex = py + ox * (nx - p2);
730 for (; px <= ex; px += ox2) {
731 uint16_t *p01 = px + ox1;
734 wdec14(*px, *p01, &i00, p01);
736 wdec16(*px, *p01, &i00, p01);
747 static int piz_uncompress(EXRContext *s, const uint8_t *src, int ssize,
748 int dsize, EXRThreadData *td)
751 uint16_t maxval, min_non_zero, max_non_zero;
753 uint16_t *tmp = (uint16_t *)td->tmp;
756 int pixel_half_size;/* 1 for half, 2 for float and uint32 */
761 td->bitmap = av_malloc(BITMAP_SIZE);
763 td->lut = av_malloc(1 << 17);
764 if (!td->bitmap || !td->lut) {
765 av_freep(&td->bitmap);
767 return AVERROR(ENOMEM);
770 bytestream2_init(&gb, src, ssize);
771 min_non_zero = bytestream2_get_le16(&gb);
772 max_non_zero = bytestream2_get_le16(&gb);
774 if (max_non_zero >= BITMAP_SIZE)
775 return AVERROR_INVALIDDATA;
777 memset(td->bitmap, 0, FFMIN(min_non_zero, BITMAP_SIZE));
778 if (min_non_zero <= max_non_zero)
779 bytestream2_get_buffer(&gb, td->bitmap + min_non_zero,
780 max_non_zero - min_non_zero + 1);
781 memset(td->bitmap + max_non_zero + 1, 0, BITMAP_SIZE - max_non_zero - 1);
783 maxval = reverse_lut(td->bitmap, td->lut);
785 ret = huf_uncompress(&gb, tmp, dsize / sizeof(uint16_t));
790 for (i = 0; i < s->nb_channels; i++) {
791 channel = &s->channels[i];
793 if (channel->pixel_type == EXR_HALF)
798 for (j = 0; j < pixel_half_size; j++)
799 wav_decode(ptr + j, td->xsize, pixel_half_size, td->ysize,
800 td->xsize * pixel_half_size, maxval);
801 ptr += td->xsize * td->ysize * pixel_half_size;
804 apply_lut(td->lut, tmp, dsize / sizeof(uint16_t));
806 out = td->uncompressed_data;
807 for (i = 0; i < td->ysize; i++) {
809 for (j = 0; j < s->nb_channels; j++) {
811 EXRChannel *channel = &s->channels[j];
812 if (channel->pixel_type == EXR_HALF)
817 in = tmp + tmp_offset * td->xsize * td->ysize + i * td->xsize * pixel_half_size;
818 tmp_offset += pixel_half_size;
819 memcpy(out, in, td->xsize * 2 * pixel_half_size);
820 out += td->xsize * 2 * pixel_half_size;
827 static int pxr24_uncompress(EXRContext *s, const uint8_t *src,
828 int compressed_size, int uncompressed_size,
831 unsigned long dest_len, expected_len = 0;
832 const uint8_t *in = td->tmp;
836 for (i = 0; i < s->nb_channels; i++) {
837 if (s->channels[i].pixel_type == EXR_FLOAT) {
838 expected_len += (td->xsize * td->ysize * 3);/* PRX 24 store float in 24 bit instead of 32 */
839 } else if (s->channels[i].pixel_type == EXR_HALF) {
840 expected_len += (td->xsize * td->ysize * 2);
842 expected_len += (td->xsize * td->ysize * 4);
846 dest_len = expected_len;
848 if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK) {
849 return AVERROR_INVALIDDATA;
850 } else if (dest_len != expected_len) {
851 return AVERROR_INVALIDDATA;
854 out = td->uncompressed_data;
855 for (i = 0; i < td->ysize; i++)
856 for (c = 0; c < s->nb_channels; c++) {
857 EXRChannel *channel = &s->channels[c];
858 const uint8_t *ptr[4];
861 switch (channel->pixel_type) {
864 ptr[1] = ptr[0] + td->xsize;
865 ptr[2] = ptr[1] + td->xsize;
866 in = ptr[2] + td->xsize;
868 for (j = 0; j < td->xsize; ++j) {
869 uint32_t diff = (*(ptr[0]++) << 24) |
870 (*(ptr[1]++) << 16) |
873 bytestream_put_le32(&out, pixel);
878 ptr[1] = ptr[0] + td->xsize;
879 in = ptr[1] + td->xsize;
880 for (j = 0; j < td->xsize; j++) {
881 uint32_t diff = (*(ptr[0]++) << 8) | *(ptr[1]++);
884 bytestream_put_le16(&out, pixel);
889 ptr[1] = ptr[0] + s->xdelta;
890 ptr[2] = ptr[1] + s->xdelta;
891 ptr[3] = ptr[2] + s->xdelta;
892 in = ptr[3] + s->xdelta;
894 for (j = 0; j < s->xdelta; ++j) {
895 uint32_t diff = (*(ptr[0]++) << 24) |
896 (*(ptr[1]++) << 16) |
897 (*(ptr[2]++) << 8 ) |
900 bytestream_put_le32(&out, pixel);
904 return AVERROR_INVALIDDATA;
911 static void unpack_14(const uint8_t b[14], uint16_t s[16])
913 unsigned short shift = (b[ 2] >> 2);
914 unsigned short bias = (0x20 << shift);
917 s[ 0] = (b[0] << 8) | b[1];
919 s[ 4] = s[ 0] + ((((b[ 2] << 4) | (b[ 3] >> 4)) & 0x3f) << shift) - bias;
920 s[ 8] = s[ 4] + ((((b[ 3] << 2) | (b[ 4] >> 6)) & 0x3f) << shift) - bias;
921 s[12] = s[ 8] + ((b[ 4] & 0x3f) << shift) - bias;
923 s[ 1] = s[ 0] + ((b[ 5] >> 2) << shift) - bias;
924 s[ 5] = s[ 4] + ((((b[ 5] << 4) | (b[ 6] >> 4)) & 0x3f) << shift) - bias;
925 s[ 9] = s[ 8] + ((((b[ 6] << 2) | (b[ 7] >> 6)) & 0x3f) << shift) - bias;
926 s[13] = s[12] + ((b[ 7] & 0x3f) << shift) - bias;
928 s[ 2] = s[ 1] + ((b[ 8] >> 2) << shift) - bias;
929 s[ 6] = s[ 5] + ((((b[ 8] << 4) | (b[ 9] >> 4)) & 0x3f) << shift) - bias;
930 s[10] = s[ 9] + ((((b[ 9] << 2) | (b[10] >> 6)) & 0x3f) << shift) - bias;
931 s[14] = s[13] + ((b[10] & 0x3f) << shift) - bias;
933 s[ 3] = s[ 2] + ((b[11] >> 2) << shift) - bias;
934 s[ 7] = s[ 6] + ((((b[11] << 4) | (b[12] >> 4)) & 0x3f) << shift) - bias;
935 s[11] = s[10] + ((((b[12] << 2) | (b[13] >> 6)) & 0x3f) << shift) - bias;
936 s[15] = s[14] + ((b[13] & 0x3f) << shift) - bias;
938 for (i = 0; i < 16; ++i) {
946 static void unpack_3(const uint8_t b[3], uint16_t s[16])
950 s[0] = (b[0] << 8) | b[1];
957 for (i = 1; i < 16; i++)
962 static int b44_uncompress(EXRContext *s, const uint8_t *src, int compressed_size,
963 int uncompressed_size, EXRThreadData *td) {
964 const int8_t *sr = src;
965 int stay_to_uncompress = compressed_size;
966 int nb_b44_block_w, nb_b44_block_h;
967 int index_tl_x, index_tl_y, index_out, index_tmp;
968 uint16_t tmp_buffer[16]; /* B44 use 4x4 half float pixel */
970 int target_channel_offset = 0;
972 /* calc B44 block count */
973 nb_b44_block_w = td->xsize / 4;
974 if ((td->xsize % 4) != 0)
977 nb_b44_block_h = td->ysize / 4;
978 if ((td->ysize % 4) != 0)
981 for (c = 0; c < s->nb_channels; c++) {
982 if (s->channels[c].pixel_type == EXR_HALF) {/* B44 only compress half float data */
983 for (iY = 0; iY < nb_b44_block_h; iY++) {
984 for (iX = 0; iX < nb_b44_block_w; iX++) {/* For each B44 block */
985 if (stay_to_uncompress < 3) {
986 av_log(s, AV_LOG_ERROR, "Not enough data for B44A block: %d", stay_to_uncompress);
987 return AVERROR_INVALIDDATA;
990 if (src[compressed_size - stay_to_uncompress + 2] == 0xfc) { /* B44A block */
991 unpack_3(sr, tmp_buffer);
993 stay_to_uncompress -= 3;
994 } else {/* B44 Block */
995 if (stay_to_uncompress < 14) {
996 av_log(s, AV_LOG_ERROR, "Not enough data for B44 block: %d", stay_to_uncompress);
997 return AVERROR_INVALIDDATA;
999 unpack_14(sr, tmp_buffer);
1001 stay_to_uncompress -= 14;
1004 /* copy data to uncompress buffer (B44 block can exceed target resolution)*/
1005 index_tl_x = iX * 4;
1006 index_tl_y = iY * 4;
1008 for (y = index_tl_y; y < FFMIN(index_tl_y + 4, td->ysize); y++) {
1009 for (x = index_tl_x; x < FFMIN(index_tl_x + 4, td->xsize); x++) {
1010 index_out = target_channel_offset * td->xsize + y * td->channel_line_size + 2 * x;
1011 index_tmp = (y-index_tl_y) * 4 + (x-index_tl_x);
1012 td->uncompressed_data[index_out] = tmp_buffer[index_tmp] & 0xff;
1013 td->uncompressed_data[index_out + 1] = tmp_buffer[index_tmp] >> 8;
1018 target_channel_offset += 2;
1019 } else {/* Float or UINT 32 channel */
1020 if (stay_to_uncompress < td->ysize * td->xsize * 4) {
1021 av_log(s, AV_LOG_ERROR, "Not enough data for uncompress channel: %d", stay_to_uncompress);
1022 return AVERROR_INVALIDDATA;
1025 for (y = 0; y < td->ysize; y++) {
1026 index_out = target_channel_offset * td->xsize + y * td->channel_line_size;
1027 memcpy(&td->uncompressed_data[index_out], sr, td->xsize * 4);
1028 sr += td->xsize * 4;
1030 target_channel_offset += 4;
1032 stay_to_uncompress -= td->ysize * td->xsize * 4;
1039 static int decode_block(AVCodecContext *avctx, void *tdata,
1040 int jobnr, int threadnr)
1042 EXRContext *s = avctx->priv_data;
1043 AVFrame *const p = s->picture;
1044 EXRThreadData *td = &s->thread_data[threadnr];
1045 const uint8_t *channel_buffer[4] = { 0 };
1046 const uint8_t *buf = s->buf;
1047 uint64_t line_offset, uncompressed_size;
1051 uint64_t line, col = 0;
1052 uint64_t tileX, tileY, tileLevelX, tileLevelY;
1054 int axmax = (avctx->width - (s->xmax + 1)) * 2 * s->desc->nb_components; /* nb pixel to add at the right of the datawindow */
1055 int bxmin = s->xmin * 2 * s->desc->nb_components; /* nb pixel to add at the left of the datawindow */
1056 int i, x, buf_size = s->buf_size;
1057 int c, rgb_channel_count;
1058 float one_gamma = 1.0f / s->gamma;
1059 avpriv_trc_function trc_func = avpriv_get_trc_function_from_trc(s->apply_trc_type);
1062 line_offset = AV_RL64(s->gb.buffer + jobnr * 8);
1065 if (line_offset > buf_size - 20)
1066 return AVERROR_INVALIDDATA;
1068 src = buf + line_offset + 20;
1070 tileX = AV_RL32(src - 20);
1071 tileY = AV_RL32(src - 16);
1072 tileLevelX = AV_RL32(src - 12);
1073 tileLevelY = AV_RL32(src - 8);
1075 data_size = AV_RL32(src - 4);
1076 if (data_size <= 0 || data_size > buf_size)
1077 return AVERROR_INVALIDDATA;
1079 if (tileLevelX || tileLevelY) { /* tile level, is not the full res level */
1080 avpriv_report_missing_feature(s->avctx, "Subres tile before full res tile");
1081 return AVERROR_PATCHWELCOME;
1084 if (s->xmin || s->ymin) {
1085 avpriv_report_missing_feature(s->avctx, "Tiles with xmin/ymin");
1086 return AVERROR_PATCHWELCOME;
1089 line = s->tile_attr.ySize * tileY;
1090 col = s->tile_attr.xSize * tileX;
1092 if (line < s->ymin || line > s->ymax ||
1093 col < s->xmin || col > s->xmax)
1094 return AVERROR_INVALIDDATA;
1096 td->ysize = FFMIN(s->tile_attr.ySize, s->ydelta - tileY * s->tile_attr.ySize);
1097 td->xsize = FFMIN(s->tile_attr.xSize, s->xdelta - tileX * s->tile_attr.xSize);
1099 if (col) { /* not the first tile of the line */
1100 bxmin = 0; /* doesn't add pixel at the left of the datawindow */
1103 if ((col + td->xsize) != s->xdelta)/* not the last tile of the line */
1104 axmax = 0; /* doesn't add pixel at the right of the datawindow */
1106 td->channel_line_size = td->xsize * s->current_channel_offset;/* uncompress size of one line */
1107 uncompressed_size = td->channel_line_size * (uint64_t)td->ysize;/* uncompress size of the block */
1109 if (line_offset > buf_size - 8)
1110 return AVERROR_INVALIDDATA;
1112 src = buf + line_offset + 8;
1113 line = AV_RL32(src - 8);
1115 if (line < s->ymin || line > s->ymax)
1116 return AVERROR_INVALIDDATA;
1118 data_size = AV_RL32(src - 4);
1119 if (data_size <= 0 || data_size > buf_size)
1120 return AVERROR_INVALIDDATA;
1122 td->ysize = FFMIN(s->scan_lines_per_block, s->ymax - line + 1); /* s->ydelta - line ?? */
1123 td->xsize = s->xdelta;
1125 td->channel_line_size = td->xsize * s->current_channel_offset;/* uncompress size of one line */
1126 uncompressed_size = td->channel_line_size * (uint64_t)td->ysize;/* uncompress size of the block */
1128 if ((s->compression == EXR_RAW && (data_size != uncompressed_size ||
1129 line_offset > buf_size - uncompressed_size)) ||
1130 (s->compression != EXR_RAW && (data_size > uncompressed_size ||
1131 line_offset > buf_size - data_size))) {
1132 return AVERROR_INVALIDDATA;
1136 if (data_size < uncompressed_size || s->is_tile) { /* td->tmp is use for tile reorganization */
1137 av_fast_padded_malloc(&td->tmp, &td->tmp_size, uncompressed_size);
1139 return AVERROR(ENOMEM);
1142 if (data_size < uncompressed_size) {
1143 av_fast_padded_malloc(&td->uncompressed_data,
1144 &td->uncompressed_size, uncompressed_size);
1146 if (!td->uncompressed_data)
1147 return AVERROR(ENOMEM);
1149 ret = AVERROR_INVALIDDATA;
1150 switch (s->compression) {
1153 ret = zip_uncompress(src, data_size, uncompressed_size, td);
1156 ret = piz_uncompress(s, src, data_size, uncompressed_size, td);
1159 ret = pxr24_uncompress(s, src, data_size, uncompressed_size, td);
1162 ret = rle_uncompress(src, data_size, uncompressed_size, td);
1166 ret = b44_uncompress(s, src, data_size, uncompressed_size, td);
1170 av_log(avctx, AV_LOG_ERROR, "decode_block() failed.\n");
1173 src = td->uncompressed_data;
1177 channel_buffer[0] = src + td->xsize * s->channel_offsets[0];
1178 channel_buffer[1] = src + td->xsize * s->channel_offsets[1];
1179 channel_buffer[2] = src + td->xsize * s->channel_offsets[2];
1180 rgb_channel_count = 3;
1181 } else { /* put y data in the first channel_buffer */
1182 channel_buffer[0] = src + td->xsize * s->channel_offsets[1];
1183 rgb_channel_count = 1;
1185 if (s->channel_offsets[3] >= 0)
1186 channel_buffer[3] = src + td->xsize * s->channel_offsets[3];
1188 ptr = p->data[0] + line * p->linesize[0] + (col * s->desc->nb_components * 2);
1191 i < td->ysize; i++, ptr += p->linesize[0]) {
1194 const uint8_t *rgb[3];
1196 for (c = 0; c < rgb_channel_count; c++){
1197 rgb[c] = channel_buffer[c];
1200 if (channel_buffer[3])
1201 a = channel_buffer[3];
1203 ptr_x = (uint16_t *) ptr;
1205 // Zero out the start if xmin is not 0
1206 memset(ptr_x, 0, bxmin);
1207 ptr_x += s->xmin * s->desc->nb_components;
1209 if (s->pixel_type == EXR_FLOAT) {
1212 for (x = 0; x < td->xsize; x++) {
1213 union av_intfloat32 t;
1215 for (c = 0; c < rgb_channel_count; c++) {
1216 t.i = bytestream_get_le32(&rgb[c]);
1217 t.f = trc_func(t.f);
1218 *ptr_x++ = exr_flt2uint(t.i);
1220 if (channel_buffer[3])
1221 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a));
1224 for (x = 0; x < td->xsize; x++) {
1225 union av_intfloat32 t;
1228 for (c = 0; c < rgb_channel_count; c++) {
1229 t.i = bytestream_get_le32(&rgb[c]);
1230 if (t.f > 0.0f) /* avoid negative values */
1231 t.f = powf(t.f, one_gamma);
1232 *ptr_x++ = exr_flt2uint(t.i);
1235 if (channel_buffer[3])
1236 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a));
1241 for (x = 0; x < td->xsize; x++) {
1243 for (c = 0; c < rgb_channel_count; c++) {
1244 *ptr_x++ = s->gamma_table[bytestream_get_le16(&rgb[c])];
1247 if (channel_buffer[3])
1248 *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&a));
1252 // Zero out the end if xmax+1 is not w
1253 memset(ptr_x, 0, axmax);
1255 channel_buffer[0] += td->channel_line_size;
1256 channel_buffer[1] += td->channel_line_size;
1257 channel_buffer[2] += td->channel_line_size;
1258 if (channel_buffer[3])
1259 channel_buffer[3] += td->channel_line_size;
1266 * Check if the variable name corresponds to its data type.
1268 * @param s the EXRContext
1269 * @param value_name name of the variable to check
1270 * @param value_type type of the variable to check
1271 * @param minimum_length minimum length of the variable data
1273 * @return bytes to read containing variable data
1274 * -1 if variable is not found
1275 * 0 if buffer ended prematurely
1277 static int check_header_variable(EXRContext *s,
1278 const char *value_name,
1279 const char *value_type,
1280 unsigned int minimum_length)
1284 if (bytestream2_get_bytes_left(&s->gb) >= minimum_length &&
1285 !strcmp(s->gb.buffer, value_name)) {
1286 // found value_name, jump to value_type (null terminated strings)
1287 s->gb.buffer += strlen(value_name) + 1;
1288 if (!strcmp(s->gb.buffer, value_type)) {
1289 s->gb.buffer += strlen(value_type) + 1;
1290 var_size = bytestream2_get_le32(&s->gb);
1291 // don't go read past boundaries
1292 if (var_size > bytestream2_get_bytes_left(&s->gb))
1295 // value_type not found, reset the buffer
1296 s->gb.buffer -= strlen(value_name) + 1;
1297 av_log(s->avctx, AV_LOG_WARNING,
1298 "Unknown data type %s for header variable %s.\n",
1299 value_type, value_name);
1306 static int decode_header(EXRContext *s, AVFrame *frame)
1308 AVDictionary *metadata = NULL;
1309 int magic_number, version, i, flags, sar = 0;
1310 int layer_match = 0;
1312 s->current_channel_offset = 0;
1319 s->channel_offsets[0] = -1;
1320 s->channel_offsets[1] = -1;
1321 s->channel_offsets[2] = -1;
1322 s->channel_offsets[3] = -1;
1323 s->pixel_type = EXR_UNKNOWN;
1324 s->compression = EXR_UNKN;
1328 s->tile_attr.xSize = -1;
1329 s->tile_attr.ySize = -1;
1333 if (bytestream2_get_bytes_left(&s->gb) < 10) {
1334 av_log(s->avctx, AV_LOG_ERROR, "Header too short to parse.\n");
1335 return AVERROR_INVALIDDATA;
1338 magic_number = bytestream2_get_le32(&s->gb);
1339 if (magic_number != 20000630) {
1340 /* As per documentation of OpenEXR, it is supposed to be
1341 * int 20000630 little-endian */
1342 av_log(s->avctx, AV_LOG_ERROR, "Wrong magic number %d.\n", magic_number);
1343 return AVERROR_INVALIDDATA;
1346 version = bytestream2_get_byte(&s->gb);
1348 avpriv_report_missing_feature(s->avctx, "Version %d", version);
1349 return AVERROR_PATCHWELCOME;
1352 flags = bytestream2_get_le24(&s->gb);
1356 else if (flags & 0x02)
1359 avpriv_report_missing_feature(s->avctx, "flags %d", flags);
1360 return AVERROR_PATCHWELCOME;
1364 while (bytestream2_get_bytes_left(&s->gb) > 0 && *s->gb.buffer) {
1366 if ((var_size = check_header_variable(s, "channels",
1367 "chlist", 38)) >= 0) {
1368 GetByteContext ch_gb;
1370 return AVERROR_INVALIDDATA;
1372 bytestream2_init(&ch_gb, s->gb.buffer, var_size);
1374 while (bytestream2_get_bytes_left(&ch_gb) >= 19) {
1375 EXRChannel *channel;
1376 enum ExrPixelType current_pixel_type;
1377 int channel_index = -1;
1380 if (strcmp(s->layer, "") != 0) {
1381 if (strncmp(ch_gb.buffer, s->layer, strlen(s->layer)) == 0) {
1383 av_log(s->avctx, AV_LOG_INFO,
1384 "Channel match layer : %s.\n", ch_gb.buffer);
1385 ch_gb.buffer += strlen(s->layer);
1386 if (*ch_gb.buffer == '.')
1387 ch_gb.buffer++; /* skip dot if not given */
1389 av_log(s->avctx, AV_LOG_INFO,
1390 "Channel doesn't match layer : %s.\n", ch_gb.buffer);
1396 if (layer_match) { /* only search channel if the layer match is valid */
1397 if (!strcmp(ch_gb.buffer, "R") ||
1398 !strcmp(ch_gb.buffer, "X") ||
1399 !strcmp(ch_gb.buffer, "U")) {
1402 } else if (!strcmp(ch_gb.buffer, "G") ||
1403 !strcmp(ch_gb.buffer, "V")) {
1406 } else if (!strcmp(ch_gb.buffer, "Y")) {
1409 } else if (!strcmp(ch_gb.buffer, "B") ||
1410 !strcmp(ch_gb.buffer, "Z") ||
1411 !strcmp(ch_gb.buffer, "W")){
1414 } else if (!strcmp(ch_gb.buffer, "A")) {
1417 av_log(s->avctx, AV_LOG_WARNING,
1418 "Unsupported channel %.256s.\n", ch_gb.buffer);
1422 /* skip until you get a 0 */
1423 while (bytestream2_get_bytes_left(&ch_gb) > 0 &&
1424 bytestream2_get_byte(&ch_gb))
1427 if (bytestream2_get_bytes_left(&ch_gb) < 4) {
1428 av_log(s->avctx, AV_LOG_ERROR, "Incomplete header.\n");
1429 return AVERROR_INVALIDDATA;
1432 current_pixel_type = bytestream2_get_le32(&ch_gb);
1433 if (current_pixel_type >= EXR_UNKNOWN) {
1434 avpriv_report_missing_feature(s->avctx, "Pixel type %d",
1435 current_pixel_type);
1436 return AVERROR_PATCHWELCOME;
1439 bytestream2_skip(&ch_gb, 4);
1440 xsub = bytestream2_get_le32(&ch_gb);
1441 ysub = bytestream2_get_le32(&ch_gb);
1443 if (xsub != 1 || ysub != 1) {
1444 avpriv_report_missing_feature(s->avctx,
1445 "Subsampling %dx%d",
1447 return AVERROR_PATCHWELCOME;
1450 if (channel_index >= 0 && s->channel_offsets[channel_index] == -1) { /* channel has not been previously assigned */
1451 if (s->pixel_type != EXR_UNKNOWN &&
1452 s->pixel_type != current_pixel_type) {
1453 av_log(s->avctx, AV_LOG_ERROR,
1454 "RGB channels not of the same depth.\n");
1455 return AVERROR_INVALIDDATA;
1457 s->pixel_type = current_pixel_type;
1458 s->channel_offsets[channel_index] = s->current_channel_offset;
1461 s->channels = av_realloc(s->channels,
1462 ++s->nb_channels * sizeof(EXRChannel));
1464 return AVERROR(ENOMEM);
1465 channel = &s->channels[s->nb_channels - 1];
1466 channel->pixel_type = current_pixel_type;
1467 channel->xsub = xsub;
1468 channel->ysub = ysub;
1470 if (current_pixel_type == EXR_HALF) {
1471 s->current_channel_offset += 2;
1472 } else {/* Float or UINT32 */
1473 s->current_channel_offset += 4;
1477 /* Check if all channels are set with an offset or if the channels
1478 * are causing an overflow */
1479 if (!s->is_luma){/* if we expected to have at least 3 channels */
1480 if (FFMIN3(s->channel_offsets[0],
1481 s->channel_offsets[1],
1482 s->channel_offsets[2]) < 0) {
1483 if (s->channel_offsets[0] < 0)
1484 av_log(s->avctx, AV_LOG_ERROR, "Missing red channel.\n");
1485 if (s->channel_offsets[1] < 0)
1486 av_log(s->avctx, AV_LOG_ERROR, "Missing green channel.\n");
1487 if (s->channel_offsets[2] < 0)
1488 av_log(s->avctx, AV_LOG_ERROR, "Missing blue channel.\n");
1489 return AVERROR_INVALIDDATA;
1493 // skip one last byte and update main gb
1494 s->gb.buffer = ch_gb.buffer + 1;
1496 } else if ((var_size = check_header_variable(s, "dataWindow", "box2i",
1499 return AVERROR_INVALIDDATA;
1501 s->xmin = bytestream2_get_le32(&s->gb);
1502 s->ymin = bytestream2_get_le32(&s->gb);
1503 s->xmax = bytestream2_get_le32(&s->gb);
1504 s->ymax = bytestream2_get_le32(&s->gb);
1505 s->xdelta = (s->xmax - s->xmin) + 1;
1506 s->ydelta = (s->ymax - s->ymin) + 1;
1509 } else if ((var_size = check_header_variable(s, "displayWindow",
1510 "box2i", 34)) >= 0) {
1512 return AVERROR_INVALIDDATA;
1514 bytestream2_skip(&s->gb, 8);
1515 s->w = bytestream2_get_le32(&s->gb) + 1;
1516 s->h = bytestream2_get_le32(&s->gb) + 1;
1519 } else if ((var_size = check_header_variable(s, "lineOrder",
1520 "lineOrder", 25)) >= 0) {
1523 return AVERROR_INVALIDDATA;
1525 line_order = bytestream2_get_byte(&s->gb);
1526 av_log(s->avctx, AV_LOG_DEBUG, "line order: %d.\n", line_order);
1527 if (line_order > 2) {
1528 av_log(s->avctx, AV_LOG_ERROR, "Unknown line order.\n");
1529 return AVERROR_INVALIDDATA;
1533 } else if ((var_size = check_header_variable(s, "pixelAspectRatio",
1534 "float", 31)) >= 0) {
1536 return AVERROR_INVALIDDATA;
1538 sar = bytestream2_get_le32(&s->gb);
1541 } else if ((var_size = check_header_variable(s, "compression",
1542 "compression", 29)) >= 0) {
1544 return AVERROR_INVALIDDATA;
1546 if (s->compression == EXR_UNKN)
1547 s->compression = bytestream2_get_byte(&s->gb);
1549 av_log(s->avctx, AV_LOG_WARNING,
1550 "Found more than one compression attribute.\n");
1553 } else if ((var_size = check_header_variable(s, "tiles",
1554 "tiledesc", 22)) >= 0) {
1558 av_log(s->avctx, AV_LOG_WARNING,
1559 "Found tile attribute and scanline flags. Exr will be interpreted as scanline.\n");
1561 s->tile_attr.xSize = bytestream2_get_le32(&s->gb);
1562 s->tile_attr.ySize = bytestream2_get_le32(&s->gb);
1564 tileLevel = bytestream2_get_byte(&s->gb);
1565 s->tile_attr.level_mode = tileLevel & 0x0f;
1566 s->tile_attr.level_round = (tileLevel >> 4) & 0x0f;
1568 if (s->tile_attr.level_mode >= EXR_TILE_LEVEL_UNKNOWN){
1569 avpriv_report_missing_feature(s->avctx, "Tile level mode %d",
1570 s->tile_attr.level_mode);
1571 return AVERROR_PATCHWELCOME;
1574 if (s->tile_attr.level_round >= EXR_TILE_ROUND_UNKNOWN) {
1575 avpriv_report_missing_feature(s->avctx, "Tile level round %d",
1576 s->tile_attr.level_round);
1577 return AVERROR_PATCHWELCOME;
1581 } else if ((var_size = check_header_variable(s, "writer",
1582 "string", 1)) >= 0) {
1583 uint8_t key[256] = { 0 };
1585 bytestream2_get_buffer(&s->gb, key, FFMIN(sizeof(key) - 1, var_size));
1586 av_dict_set(&metadata, "writer", key, 0);
1591 // Check if there are enough bytes for a header
1592 if (bytestream2_get_bytes_left(&s->gb) <= 9) {
1593 av_log(s->avctx, AV_LOG_ERROR, "Incomplete header\n");
1594 return AVERROR_INVALIDDATA;
1597 // Process unknown variables
1598 for (i = 0; i < 2; i++) // value_name and value_type
1599 while (bytestream2_get_byte(&s->gb) != 0);
1601 // Skip variable length
1602 bytestream2_skip(&s->gb, bytestream2_get_le32(&s->gb));
1605 ff_set_sar(s->avctx, av_d2q(av_int2float(sar), 255));
1607 if (s->compression == EXR_UNKN) {
1608 av_log(s->avctx, AV_LOG_ERROR, "Missing compression attribute.\n");
1609 return AVERROR_INVALIDDATA;
1613 if (s->tile_attr.xSize < 1 || s->tile_attr.ySize < 1) {
1614 av_log(s->avctx, AV_LOG_ERROR, "Invalid tile attribute.\n");
1615 return AVERROR_INVALIDDATA;
1619 if (bytestream2_get_bytes_left(&s->gb) <= 0) {
1620 av_log(s->avctx, AV_LOG_ERROR, "Incomplete frame.\n");
1621 return AVERROR_INVALIDDATA;
1624 av_frame_set_metadata(frame, metadata);
1626 // aaand we are done
1627 bytestream2_skip(&s->gb, 1);
1631 static int decode_frame(AVCodecContext *avctx, void *data,
1632 int *got_frame, AVPacket *avpkt)
1634 EXRContext *s = avctx->priv_data;
1635 ThreadFrame frame = { .f = data };
1636 AVFrame *picture = data;
1641 int nb_blocks;/* nb scanline or nb tile */
1643 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
1645 if ((ret = decode_header(s, picture)) < 0)
1648 switch (s->pixel_type) {
1651 if (s->channel_offsets[3] >= 0) {
1653 avctx->pix_fmt = AV_PIX_FMT_RGBA64;
1655 avctx->pix_fmt = AV_PIX_FMT_YA16;
1659 avctx->pix_fmt = AV_PIX_FMT_RGB48;
1661 avctx->pix_fmt = AV_PIX_FMT_GRAY16;
1666 avpriv_request_sample(avctx, "32-bit unsigned int");
1667 return AVERROR_PATCHWELCOME;
1669 av_log(avctx, AV_LOG_ERROR, "Missing channel list.\n");
1670 return AVERROR_INVALIDDATA;
1673 if (s->apply_trc_type != AVCOL_TRC_UNSPECIFIED)
1674 avctx->color_trc = s->apply_trc_type;
1676 switch (s->compression) {
1680 s->scan_lines_per_block = 1;
1684 s->scan_lines_per_block = 16;
1689 s->scan_lines_per_block = 32;
1692 avpriv_report_missing_feature(avctx, "Compression %d", s->compression);
1693 return AVERROR_PATCHWELCOME;
1696 /* Verify the xmin, xmax, ymin, ymax and xdelta before setting
1697 * the actual image size. */
1698 if (s->xmin > s->xmax ||
1699 s->ymin > s->ymax ||
1700 s->xdelta != s->xmax - s->xmin + 1 ||
1703 av_log(avctx, AV_LOG_ERROR, "Wrong or missing size information.\n");
1704 return AVERROR_INVALIDDATA;
1707 if ((ret = ff_set_dimensions(avctx, s->w, s->h)) < 0)
1710 s->desc = av_pix_fmt_desc_get(avctx->pix_fmt);
1712 return AVERROR_INVALIDDATA;
1713 out_line_size = avctx->width * 2 * s->desc->nb_components;
1716 nb_blocks = ((s->xdelta + s->tile_attr.xSize - 1) / s->tile_attr.xSize) *
1717 ((s->ydelta + s->tile_attr.ySize - 1) / s->tile_attr.ySize);
1718 } else { /* scanline */
1719 nb_blocks = (s->ydelta + s->scan_lines_per_block - 1) /
1720 s->scan_lines_per_block;
1723 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
1726 if (bytestream2_get_bytes_left(&s->gb) < nb_blocks * 8)
1727 return AVERROR_INVALIDDATA;
1729 // save pointer we are going to use in decode_block
1730 s->buf = avpkt->data;
1731 s->buf_size = avpkt->size;
1732 ptr = picture->data[0];
1734 // Zero out the start if ymin is not 0
1735 for (y = 0; y < s->ymin; y++) {
1736 memset(ptr, 0, out_line_size);
1737 ptr += picture->linesize[0];
1740 s->picture = picture;
1742 avctx->execute2(avctx, decode_block, s->thread_data, NULL, nb_blocks);
1744 // Zero out the end if ymax+1 is not h
1745 ptr = picture->data[0] + ((s->ymax+1) * picture->linesize[0]);
1746 for (y = s->ymax + 1; y < avctx->height; y++) {
1747 memset(ptr, 0, out_line_size);
1748 ptr += picture->linesize[0];
1751 picture->pict_type = AV_PICTURE_TYPE_I;
1757 static av_cold int decode_init(AVCodecContext *avctx)
1759 EXRContext *s = avctx->priv_data;
1761 union av_intfloat32 t;
1762 float one_gamma = 1.0f / s->gamma;
1763 avpriv_trc_function trc_func = NULL;
1767 trc_func = avpriv_get_trc_function_from_trc(s->apply_trc_type);
1769 for (i = 0; i < 65536; ++i) {
1770 t = exr_half2float(i);
1771 t.f = trc_func(t.f);
1772 s->gamma_table[i] = exr_flt2uint(t.i);
1775 if (one_gamma > 0.9999f && one_gamma < 1.0001f) {
1776 for (i = 0; i < 65536; ++i)
1777 s->gamma_table[i] = exr_halflt2uint(i);
1779 for (i = 0; i < 65536; ++i) {
1780 t = exr_half2float(i);
1781 /* If negative value we reuse half value */
1783 s->gamma_table[i] = exr_halflt2uint(i);
1785 t.f = powf(t.f, one_gamma);
1786 s->gamma_table[i] = exr_flt2uint(t.i);
1792 // allocate thread data, used for non EXR_RAW compression types
1793 s->thread_data = av_mallocz_array(avctx->thread_count, sizeof(EXRThreadData));
1794 if (!s->thread_data)
1795 return AVERROR_INVALIDDATA;
1801 static int decode_init_thread_copy(AVCodecContext *avctx)
1802 { EXRContext *s = avctx->priv_data;
1804 // allocate thread data, used for non EXR_RAW compression types
1805 s->thread_data = av_mallocz_array(avctx->thread_count, sizeof(EXRThreadData));
1806 if (!s->thread_data)
1807 return AVERROR_INVALIDDATA;
1813 static av_cold int decode_end(AVCodecContext *avctx)
1815 EXRContext *s = avctx->priv_data;
1817 for (i = 0; i < avctx->thread_count; i++) {
1818 EXRThreadData *td = &s->thread_data[i];
1819 av_freep(&td->uncompressed_data);
1821 av_freep(&td->bitmap);
1825 av_freep(&s->thread_data);
1826 av_freep(&s->channels);
1831 #define OFFSET(x) offsetof(EXRContext, x)
1832 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1833 static const AVOption options[] = {
1834 { "layer", "Set the decoding layer", OFFSET(layer),
1835 AV_OPT_TYPE_STRING, { .str = "" }, 0, 0, VD },
1836 { "gamma", "Set the float gamma value when decoding", OFFSET(gamma),
1837 AV_OPT_TYPE_FLOAT, { .dbl = 1.0f }, 0.001, FLT_MAX, VD },
1839 // XXX: Note the abuse of the enum using AVCOL_TRC_UNSPECIFIED to subsume the existing gamma option
1840 { "apply_trc", "color transfer characteristics to apply to EXR linear input", OFFSET(apply_trc_type),
1841 AV_OPT_TYPE_INT, {.i64 = AVCOL_TRC_UNSPECIFIED }, 1, AVCOL_TRC_NB-1, VD, "apply_trc_type"},
1842 { "bt709", "BT.709", 0,
1843 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT709 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1844 { "gamma", "gamma", 0,
1845 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_UNSPECIFIED }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1846 { "gamma22", "BT.470 M", 0,
1847 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_GAMMA22 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1848 { "gamma28", "BT.470 BG", 0,
1849 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_GAMMA28 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1850 { "smpte170m", "SMPTE 170 M", 0,
1851 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTE170M }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1852 { "smpte240m", "SMPTE 240 M", 0,
1853 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTE240M }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1854 { "linear", "Linear", 0,
1855 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LINEAR }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1857 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LOG }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1858 { "log_sqrt", "Log square root", 0,
1859 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LOG_SQRT }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1860 { "iec61966_2_4", "IEC 61966-2-4", 0,
1861 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_IEC61966_2_4 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1862 { "bt1361", "BT.1361", 0,
1863 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT1361_ECG }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1864 { "iec61966_2_1", "IEC 61966-2-1", 0,
1865 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_IEC61966_2_1 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1866 { "bt2020_10bit", "BT.2020 - 10 bit", 0,
1867 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT2020_10 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1868 { "bt2020_12bit", "BT.2020 - 12 bit", 0,
1869 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT2020_12 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1870 { "smpte2084", "SMPTE ST 2084", 0,
1871 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTEST2084 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1872 { "smpte428_1", "SMPTE ST 428-1", 0,
1873 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTEST428_1 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1878 static const AVClass exr_class = {
1879 .class_name = "EXR",
1880 .item_name = av_default_item_name,
1882 .version = LIBAVUTIL_VERSION_INT,
1885 AVCodec ff_exr_decoder = {
1887 .long_name = NULL_IF_CONFIG_SMALL("OpenEXR image"),
1888 .type = AVMEDIA_TYPE_VIDEO,
1889 .id = AV_CODEC_ID_EXR,
1890 .priv_data_size = sizeof(EXRContext),
1891 .init = decode_init,
1892 .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
1893 .close = decode_end,
1894 .decode = decode_frame,
1895 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS |
1896 AV_CODEC_CAP_SLICE_THREADS,
1897 .priv_class = &exr_class,