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, UINT32 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_half2float() is credited to Aaftab Munshi, Dan Ginsburg, Dave Shreiner.
39 #include "libavutil/avassert.h"
40 #include "libavutil/common.h"
41 #include "libavutil/imgutils.h"
42 #include "libavutil/intfloat.h"
43 #include "libavutil/avstring.h"
44 #include "libavutil/opt.h"
45 #include "libavutil/color_utils.h"
48 #include "bytestream.h"
81 enum ExrTileLevelMode {
83 EXR_TILE_LEVEL_MIPMAP,
84 EXR_TILE_LEVEL_RIPMAP,
85 EXR_TILE_LEVEL_UNKNOWN,
88 enum ExrTileLevelRound {
91 EXR_TILE_ROUND_UNKNOWN,
94 typedef struct EXRChannel {
96 enum ExrPixelType pixel_type;
99 typedef struct EXRTileAttribute {
102 enum ExrTileLevelMode level_mode;
103 enum ExrTileLevelRound level_round;
106 typedef struct EXRThreadData {
107 uint8_t *uncompressed_data;
108 int uncompressed_size;
118 int channel_line_size;
121 typedef struct EXRContext {
124 AVCodecContext *avctx;
128 BswapDSPContext bbdsp;
131 enum ExrCompr compression;
132 enum ExrPixelType pixel_type;
133 int channel_offsets[4]; // 0 = red, 1 = green, 2 = blue and 3 = alpha
134 const AVPixFmtDescriptor *desc;
140 uint32_t xdelta, ydelta;
142 int scan_lines_per_block;
144 EXRTileAttribute tile_attr; /* header data attribute of tile */
145 int is_tile; /* 0 if scanline, 1 if tile */
147 int is_luma;/* 1 if there is an Y plane */
153 EXRChannel *channels;
155 int current_channel_offset;
157 EXRThreadData *thread_data;
161 enum AVColorTransferCharacteristic apply_trc_type;
163 union av_intfloat32 gamma_table[65536];
166 /* -15 stored using a single precision bias of 127 */
167 #define HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP 0x38000000
169 /* max exponent value in single precision that will be converted
170 * to Inf or Nan when stored as a half-float */
171 #define HALF_FLOAT_MAX_BIASED_EXP_AS_SINGLE_FP_EXP 0x47800000
173 /* 255 is the max exponent biased value */
174 #define FLOAT_MAX_BIASED_EXP (0xFF << 23)
176 #define HALF_FLOAT_MAX_BIASED_EXP (0x1F << 10)
179 * Convert a half float as a uint16_t into a full float.
181 * @param hf half float as uint16_t
183 * @return float value
185 static union av_intfloat32 exr_half2float(uint16_t hf)
187 unsigned int sign = (unsigned int) (hf >> 15);
188 unsigned int mantissa = (unsigned int) (hf & ((1 << 10) - 1));
189 unsigned int exp = (unsigned int) (hf & HALF_FLOAT_MAX_BIASED_EXP);
190 union av_intfloat32 f;
192 if (exp == HALF_FLOAT_MAX_BIASED_EXP) {
193 // we have a half-float NaN or Inf
194 // half-float NaNs will be converted to a single precision NaN
195 // half-float Infs will be converted to a single precision Inf
196 exp = FLOAT_MAX_BIASED_EXP;
197 mantissa <<= 13; // preserve half-float NaN bits if set
198 } else if (exp == 0x0) {
199 // convert half-float zero/denorm to single precision value
202 exp = HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP;
203 // check for leading 1 in denorm mantissa
204 while (!(mantissa & (1 << 10))) {
205 // for every leading 0, decrement single precision exponent by 1
206 // and shift half-float mantissa value to the left
210 // clamp the mantissa to 10 bits
211 mantissa &= ((1 << 10) - 1);
212 // shift left to generate single-precision mantissa of 23 bits
216 // shift left to generate single-precision mantissa of 23 bits
218 // generate single precision biased exponent value
219 exp = (exp << 13) + HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP;
222 f.i = (sign << 31) | exp | mantissa;
227 static int zip_uncompress(EXRContext *s, const uint8_t *src, int compressed_size,
228 int uncompressed_size, EXRThreadData *td)
230 unsigned long dest_len = uncompressed_size;
232 if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK ||
233 dest_len != uncompressed_size)
234 return AVERROR_INVALIDDATA;
236 av_assert1(uncompressed_size % 2 == 0);
238 s->dsp.predictor(td->tmp, uncompressed_size);
239 s->dsp.reorder_pixels(td->uncompressed_data, td->tmp, uncompressed_size);
244 static int rle_uncompress(EXRContext *ctx, const uint8_t *src, int compressed_size,
245 int uncompressed_size, EXRThreadData *td)
247 uint8_t *d = td->tmp;
248 const int8_t *s = src;
249 int ssize = compressed_size;
250 int dsize = uncompressed_size;
251 uint8_t *dend = d + dsize;
260 if ((dsize -= count) < 0 ||
261 (ssize -= count + 1) < 0)
262 return AVERROR_INVALIDDATA;
269 if ((dsize -= count) < 0 ||
271 return AVERROR_INVALIDDATA;
281 return AVERROR_INVALIDDATA;
283 av_assert1(uncompressed_size % 2 == 0);
285 ctx->dsp.predictor(td->tmp, uncompressed_size);
286 ctx->dsp.reorder_pixels(td->uncompressed_data, td->tmp, uncompressed_size);
291 #define USHORT_RANGE (1 << 16)
292 #define BITMAP_SIZE (1 << 13)
294 static uint16_t reverse_lut(const uint8_t *bitmap, uint16_t *lut)
298 for (i = 0; i < USHORT_RANGE; i++)
299 if ((i == 0) || (bitmap[i >> 3] & (1 << (i & 7))))
304 memset(lut + k, 0, (USHORT_RANGE - k) * 2);
309 static void apply_lut(const uint16_t *lut, uint16_t *dst, int dsize)
313 for (i = 0; i < dsize; ++i)
314 dst[i] = lut[dst[i]];
317 #define HUF_ENCBITS 16 // literal (value) bit length
318 #define HUF_DECBITS 14 // decoding bit size (>= 8)
320 #define HUF_ENCSIZE ((1 << HUF_ENCBITS) + 1) // encoding table size
321 #define HUF_DECSIZE (1 << HUF_DECBITS) // decoding table size
322 #define HUF_DECMASK (HUF_DECSIZE - 1)
324 typedef struct HufDec {
330 static void huf_canonical_code_table(uint64_t *hcode)
332 uint64_t c, n[59] = { 0 };
335 for (i = 0; i < HUF_ENCSIZE; ++i)
339 for (i = 58; i > 0; --i) {
340 uint64_t nc = ((c + n[i]) >> 1);
345 for (i = 0; i < HUF_ENCSIZE; ++i) {
349 hcode[i] = l | (n[l]++ << 6);
353 #define SHORT_ZEROCODE_RUN 59
354 #define LONG_ZEROCODE_RUN 63
355 #define SHORTEST_LONG_RUN (2 + LONG_ZEROCODE_RUN - SHORT_ZEROCODE_RUN)
356 #define LONGEST_LONG_RUN (255 + SHORTEST_LONG_RUN)
358 static int huf_unpack_enc_table(GetByteContext *gb,
359 int32_t im, int32_t iM, uint64_t *hcode)
362 int ret = init_get_bits8(&gbit, gb->buffer, bytestream2_get_bytes_left(gb));
366 for (; im <= iM; im++) {
367 uint64_t l = hcode[im] = get_bits(&gbit, 6);
369 if (l == LONG_ZEROCODE_RUN) {
370 int zerun = get_bits(&gbit, 8) + SHORTEST_LONG_RUN;
372 if (im + zerun > iM + 1)
373 return AVERROR_INVALIDDATA;
379 } else if (l >= SHORT_ZEROCODE_RUN) {
380 int zerun = l - SHORT_ZEROCODE_RUN + 2;
382 if (im + zerun > iM + 1)
383 return AVERROR_INVALIDDATA;
392 bytestream2_skip(gb, (get_bits_count(&gbit) + 7) / 8);
393 huf_canonical_code_table(hcode);
398 static int huf_build_dec_table(const uint64_t *hcode, int im,
399 int iM, HufDec *hdecod)
401 for (; im <= iM; im++) {
402 uint64_t c = hcode[im] >> 6;
403 int i, l = hcode[im] & 63;
406 return AVERROR_INVALIDDATA;
408 if (l > HUF_DECBITS) {
409 HufDec *pl = hdecod + (c >> (l - HUF_DECBITS));
411 return AVERROR_INVALIDDATA;
415 pl->p = av_realloc(pl->p, pl->lit * sizeof(int));
417 return AVERROR(ENOMEM);
419 pl->p[pl->lit - 1] = im;
421 HufDec *pl = hdecod + (c << (HUF_DECBITS - l));
423 for (i = 1 << (HUF_DECBITS - l); i > 0; i--, pl++) {
424 if (pl->len || pl->p)
425 return AVERROR_INVALIDDATA;
435 #define get_char(c, lc, gb) \
437 c = (c << 8) | bytestream2_get_byte(gb); \
441 #define get_code(po, rlc, c, lc, gb, out, oe, outb) \
445 get_char(c, lc, gb); \
450 if (out + cs > oe || out == outb) \
451 return AVERROR_INVALIDDATA; \
457 } else if (out < oe) { \
460 return AVERROR_INVALIDDATA; \
464 static int huf_decode(const uint64_t *hcode, const HufDec *hdecod,
465 GetByteContext *gb, int nbits,
466 int rlc, int no, uint16_t *out)
469 uint16_t *outb = out;
470 uint16_t *oe = out + no;
471 const uint8_t *ie = gb->buffer + (nbits + 7) / 8; // input byte size
476 while (gb->buffer < ie) {
479 while (lc >= HUF_DECBITS) {
480 const HufDec pl = hdecod[(c >> (lc - HUF_DECBITS)) & HUF_DECMASK];
484 get_code(pl.lit, rlc, c, lc, gb, out, oe, outb);
489 return AVERROR_INVALIDDATA;
491 for (j = 0; j < pl.lit; j++) {
492 int l = hcode[pl.p[j]] & 63;
494 while (lc < l && bytestream2_get_bytes_left(gb) > 0)
498 if ((hcode[pl.p[j]] >> 6) ==
499 ((c >> (lc - l)) & ((1LL << l) - 1))) {
501 get_code(pl.p[j], rlc, c, lc, gb, out, oe, outb);
508 return AVERROR_INVALIDDATA;
518 const HufDec pl = hdecod[(c << (HUF_DECBITS - lc)) & HUF_DECMASK];
520 if (pl.len && lc >= pl.len) {
522 get_code(pl.lit, rlc, c, lc, gb, out, oe, outb);
524 return AVERROR_INVALIDDATA;
528 if (out - outb != no)
529 return AVERROR_INVALIDDATA;
533 static int huf_uncompress(GetByteContext *gb,
534 uint16_t *dst, int dst_size)
536 int32_t src_size, im, iM;
542 src_size = bytestream2_get_le32(gb);
543 im = bytestream2_get_le32(gb);
544 iM = bytestream2_get_le32(gb);
545 bytestream2_skip(gb, 4);
546 nBits = bytestream2_get_le32(gb);
547 if (im < 0 || im >= HUF_ENCSIZE ||
548 iM < 0 || iM >= HUF_ENCSIZE ||
550 return AVERROR_INVALIDDATA;
552 bytestream2_skip(gb, 4);
554 freq = av_mallocz_array(HUF_ENCSIZE, sizeof(*freq));
555 hdec = av_mallocz_array(HUF_DECSIZE, sizeof(*hdec));
556 if (!freq || !hdec) {
557 ret = AVERROR(ENOMEM);
561 if ((ret = huf_unpack_enc_table(gb, im, iM, freq)) < 0)
564 if (nBits > 8 * bytestream2_get_bytes_left(gb)) {
565 ret = AVERROR_INVALIDDATA;
569 if ((ret = huf_build_dec_table(freq, im, iM, hdec)) < 0)
571 ret = huf_decode(freq, hdec, gb, nBits, iM, dst_size, dst);
574 for (i = 0; i < HUF_DECSIZE; i++)
576 av_freep(&hdec[i].p);
584 static inline void wdec14(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
589 int ai = ls + (hi & 1) + (hi >> 1);
591 int16_t bs = ai - hi;
598 #define A_OFFSET (1 << (NBITS - 1))
599 #define MOD_MASK ((1 << NBITS) - 1)
601 static inline void wdec16(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
605 int bb = (m - (d >> 1)) & MOD_MASK;
606 int aa = (d + bb - A_OFFSET) & MOD_MASK;
611 static void wav_decode(uint16_t *in, int nx, int ox,
612 int ny, int oy, uint16_t mx)
614 int w14 = (mx < (1 << 14));
615 int n = (nx > ny) ? ny : nx;
628 uint16_t *ey = in + oy * (ny - p2);
629 uint16_t i00, i01, i10, i11;
635 for (; py <= ey; py += oy2) {
637 uint16_t *ex = py + ox * (nx - p2);
639 for (; px <= ex; px += ox2) {
640 uint16_t *p01 = px + ox1;
641 uint16_t *p10 = px + oy1;
642 uint16_t *p11 = p10 + ox1;
645 wdec14(*px, *p10, &i00, &i10);
646 wdec14(*p01, *p11, &i01, &i11);
647 wdec14(i00, i01, px, p01);
648 wdec14(i10, i11, p10, p11);
650 wdec16(*px, *p10, &i00, &i10);
651 wdec16(*p01, *p11, &i01, &i11);
652 wdec16(i00, i01, px, p01);
653 wdec16(i10, i11, p10, p11);
658 uint16_t *p10 = px + oy1;
661 wdec14(*px, *p10, &i00, p10);
663 wdec16(*px, *p10, &i00, p10);
671 uint16_t *ex = py + ox * (nx - p2);
673 for (; px <= ex; px += ox2) {
674 uint16_t *p01 = px + ox1;
677 wdec14(*px, *p01, &i00, p01);
679 wdec16(*px, *p01, &i00, p01);
690 static int piz_uncompress(EXRContext *s, const uint8_t *src, int ssize,
691 int dsize, EXRThreadData *td)
694 uint16_t maxval, min_non_zero, max_non_zero;
696 uint16_t *tmp = (uint16_t *)td->tmp;
700 int pixel_half_size;/* 1 for half, 2 for float and uint32 */
705 td->bitmap = av_malloc(BITMAP_SIZE);
707 td->lut = av_malloc(1 << 17);
708 if (!td->bitmap || !td->lut) {
709 av_freep(&td->bitmap);
711 return AVERROR(ENOMEM);
714 bytestream2_init(&gb, src, ssize);
715 min_non_zero = bytestream2_get_le16(&gb);
716 max_non_zero = bytestream2_get_le16(&gb);
718 if (max_non_zero >= BITMAP_SIZE)
719 return AVERROR_INVALIDDATA;
721 memset(td->bitmap, 0, FFMIN(min_non_zero, BITMAP_SIZE));
722 if (min_non_zero <= max_non_zero)
723 bytestream2_get_buffer(&gb, td->bitmap + min_non_zero,
724 max_non_zero - min_non_zero + 1);
725 memset(td->bitmap + max_non_zero + 1, 0, BITMAP_SIZE - max_non_zero - 1);
727 maxval = reverse_lut(td->bitmap, td->lut);
729 ret = huf_uncompress(&gb, tmp, dsize / sizeof(uint16_t));
734 for (i = 0; i < s->nb_channels; i++) {
735 channel = &s->channels[i];
737 if (channel->pixel_type == EXR_HALF)
742 for (j = 0; j < pixel_half_size; j++)
743 wav_decode(ptr + j, td->xsize, pixel_half_size, td->ysize,
744 td->xsize * pixel_half_size, maxval);
745 ptr += td->xsize * td->ysize * pixel_half_size;
748 apply_lut(td->lut, tmp, dsize / sizeof(uint16_t));
750 out = (uint16_t *)td->uncompressed_data;
751 for (i = 0; i < td->ysize; i++) {
753 for (j = 0; j < s->nb_channels; j++) {
754 channel = &s->channels[j];
755 if (channel->pixel_type == EXR_HALF)
760 in = tmp + tmp_offset * td->xsize * td->ysize + i * td->xsize * pixel_half_size;
761 tmp_offset += pixel_half_size;
764 s->bbdsp.bswap16_buf(out, in, td->xsize * pixel_half_size);
766 memcpy(out, in, td->xsize * 2 * pixel_half_size);
768 out += td->xsize * pixel_half_size;
775 static int pxr24_uncompress(EXRContext *s, const uint8_t *src,
776 int compressed_size, int uncompressed_size,
779 unsigned long dest_len, expected_len = 0;
780 const uint8_t *in = td->tmp;
784 for (i = 0; i < s->nb_channels; i++) {
785 if (s->channels[i].pixel_type == EXR_FLOAT) {
786 expected_len += (td->xsize * td->ysize * 3);/* PRX 24 store float in 24 bit instead of 32 */
787 } else if (s->channels[i].pixel_type == EXR_HALF) {
788 expected_len += (td->xsize * td->ysize * 2);
790 expected_len += (td->xsize * td->ysize * 4);
794 dest_len = expected_len;
796 if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK) {
797 return AVERROR_INVALIDDATA;
798 } else if (dest_len != expected_len) {
799 return AVERROR_INVALIDDATA;
802 out = td->uncompressed_data;
803 for (i = 0; i < td->ysize; i++)
804 for (c = 0; c < s->nb_channels; c++) {
805 EXRChannel *channel = &s->channels[c];
806 const uint8_t *ptr[4];
809 switch (channel->pixel_type) {
812 ptr[1] = ptr[0] + td->xsize;
813 ptr[2] = ptr[1] + td->xsize;
814 in = ptr[2] + td->xsize;
816 for (j = 0; j < td->xsize; ++j) {
817 uint32_t diff = ((unsigned)*(ptr[0]++) << 24) |
818 (*(ptr[1]++) << 16) |
821 bytestream_put_le32(&out, pixel);
826 ptr[1] = ptr[0] + td->xsize;
827 in = ptr[1] + td->xsize;
828 for (j = 0; j < td->xsize; j++) {
829 uint32_t diff = (*(ptr[0]++) << 8) | *(ptr[1]++);
832 bytestream_put_le16(&out, pixel);
837 ptr[1] = ptr[0] + s->xdelta;
838 ptr[2] = ptr[1] + s->xdelta;
839 ptr[3] = ptr[2] + s->xdelta;
840 in = ptr[3] + s->xdelta;
842 for (j = 0; j < s->xdelta; ++j) {
843 uint32_t diff = ((uint32_t)*(ptr[0]++) << 24) |
844 (*(ptr[1]++) << 16) |
845 (*(ptr[2]++) << 8 ) |
848 bytestream_put_le32(&out, pixel);
852 return AVERROR_INVALIDDATA;
859 static void unpack_14(const uint8_t b[14], uint16_t s[16])
861 unsigned short shift = (b[ 2] >> 2) & 15;
862 unsigned short bias = (0x20 << shift);
865 s[ 0] = (b[0] << 8) | b[1];
867 s[ 4] = s[ 0] + ((((b[ 2] << 4) | (b[ 3] >> 4)) & 0x3f) << shift) - bias;
868 s[ 8] = s[ 4] + ((((b[ 3] << 2) | (b[ 4] >> 6)) & 0x3f) << shift) - bias;
869 s[12] = s[ 8] + ((b[ 4] & 0x3f) << shift) - bias;
871 s[ 1] = s[ 0] + ((b[ 5] >> 2) << shift) - bias;
872 s[ 5] = s[ 4] + ((((b[ 5] << 4) | (b[ 6] >> 4)) & 0x3f) << shift) - bias;
873 s[ 9] = s[ 8] + ((((b[ 6] << 2) | (b[ 7] >> 6)) & 0x3f) << shift) - bias;
874 s[13] = s[12] + ((b[ 7] & 0x3f) << shift) - bias;
876 s[ 2] = s[ 1] + ((b[ 8] >> 2) << shift) - bias;
877 s[ 6] = s[ 5] + ((((b[ 8] << 4) | (b[ 9] >> 4)) & 0x3f) << shift) - bias;
878 s[10] = s[ 9] + ((((b[ 9] << 2) | (b[10] >> 6)) & 0x3f) << shift) - bias;
879 s[14] = s[13] + ((b[10] & 0x3f) << shift) - bias;
881 s[ 3] = s[ 2] + ((b[11] >> 2) << shift) - bias;
882 s[ 7] = s[ 6] + ((((b[11] << 4) | (b[12] >> 4)) & 0x3f) << shift) - bias;
883 s[11] = s[10] + ((((b[12] << 2) | (b[13] >> 6)) & 0x3f) << shift) - bias;
884 s[15] = s[14] + ((b[13] & 0x3f) << shift) - bias;
886 for (i = 0; i < 16; ++i) {
894 static void unpack_3(const uint8_t b[3], uint16_t s[16])
898 s[0] = (b[0] << 8) | b[1];
905 for (i = 1; i < 16; i++)
910 static int b44_uncompress(EXRContext *s, const uint8_t *src, int compressed_size,
911 int uncompressed_size, EXRThreadData *td) {
912 const int8_t *sr = src;
913 int stay_to_uncompress = compressed_size;
914 int nb_b44_block_w, nb_b44_block_h;
915 int index_tl_x, index_tl_y, index_out, index_tmp;
916 uint16_t tmp_buffer[16]; /* B44 use 4x4 half float pixel */
918 int target_channel_offset = 0;
920 /* calc B44 block count */
921 nb_b44_block_w = td->xsize / 4;
922 if ((td->xsize % 4) != 0)
925 nb_b44_block_h = td->ysize / 4;
926 if ((td->ysize % 4) != 0)
929 for (c = 0; c < s->nb_channels; c++) {
930 if (s->channels[c].pixel_type == EXR_HALF) {/* B44 only compress half float data */
931 for (iY = 0; iY < nb_b44_block_h; iY++) {
932 for (iX = 0; iX < nb_b44_block_w; iX++) {/* For each B44 block */
933 if (stay_to_uncompress < 3) {
934 av_log(s, AV_LOG_ERROR, "Not enough data for B44A block: %d", stay_to_uncompress);
935 return AVERROR_INVALIDDATA;
938 if (src[compressed_size - stay_to_uncompress + 2] == 0xfc) { /* B44A block */
939 unpack_3(sr, tmp_buffer);
941 stay_to_uncompress -= 3;
942 } else {/* B44 Block */
943 if (stay_to_uncompress < 14) {
944 av_log(s, AV_LOG_ERROR, "Not enough data for B44 block: %d", stay_to_uncompress);
945 return AVERROR_INVALIDDATA;
947 unpack_14(sr, tmp_buffer);
949 stay_to_uncompress -= 14;
952 /* copy data to uncompress buffer (B44 block can exceed target resolution)*/
956 for (y = index_tl_y; y < FFMIN(index_tl_y + 4, td->ysize); y++) {
957 for (x = index_tl_x; x < FFMIN(index_tl_x + 4, td->xsize); x++) {
958 index_out = target_channel_offset * td->xsize + y * td->channel_line_size + 2 * x;
959 index_tmp = (y-index_tl_y) * 4 + (x-index_tl_x);
960 td->uncompressed_data[index_out] = tmp_buffer[index_tmp] & 0xff;
961 td->uncompressed_data[index_out + 1] = tmp_buffer[index_tmp] >> 8;
966 target_channel_offset += 2;
967 } else {/* Float or UINT 32 channel */
968 if (stay_to_uncompress < td->ysize * td->xsize * 4) {
969 av_log(s, AV_LOG_ERROR, "Not enough data for uncompress channel: %d", stay_to_uncompress);
970 return AVERROR_INVALIDDATA;
973 for (y = 0; y < td->ysize; y++) {
974 index_out = target_channel_offset * td->xsize + y * td->channel_line_size;
975 memcpy(&td->uncompressed_data[index_out], sr, td->xsize * 4);
978 target_channel_offset += 4;
980 stay_to_uncompress -= td->ysize * td->xsize * 4;
987 static int decode_block(AVCodecContext *avctx, void *tdata,
988 int jobnr, int threadnr)
990 EXRContext *s = avctx->priv_data;
991 AVFrame *const p = s->picture;
992 EXRThreadData *td = &s->thread_data[threadnr];
993 const uint8_t *channel_buffer[4] = { 0 };
994 const uint8_t *buf = s->buf;
995 uint64_t line_offset, uncompressed_size;
999 uint64_t tile_x, tile_y, tile_level_x, tile_level_y;
1001 int step = s->desc->flags & AV_PIX_FMT_FLAG_FLOAT ? 4 : 2 * s->desc->nb_components;
1002 int bxmin = 0, axmax = 0, window_xoffset = 0;
1003 int window_xmin, window_xmax, window_ymin, window_ymax;
1004 int data_xoffset, data_yoffset, data_window_offset, xsize, ysize;
1005 int i, x, buf_size = s->buf_size;
1006 int c, rgb_channel_count;
1007 float one_gamma = 1.0f / s->gamma;
1008 avpriv_trc_function trc_func = avpriv_get_trc_function_from_trc(s->apply_trc_type);
1011 line_offset = AV_RL64(s->gb.buffer + jobnr * 8);
1014 if (buf_size < 20 || line_offset > buf_size - 20)
1015 return AVERROR_INVALIDDATA;
1017 src = buf + line_offset + 20;
1019 tile_x = AV_RL32(src - 20);
1020 tile_y = AV_RL32(src - 16);
1021 tile_level_x = AV_RL32(src - 12);
1022 tile_level_y = AV_RL32(src - 8);
1024 data_size = AV_RL32(src - 4);
1025 if (data_size <= 0 || data_size > buf_size - line_offset - 20)
1026 return AVERROR_INVALIDDATA;
1028 if (tile_level_x || tile_level_y) { /* tile level, is not the full res level */
1029 avpriv_report_missing_feature(s->avctx, "Subres tile before full res tile");
1030 return AVERROR_PATCHWELCOME;
1033 line = s->ymin + s->tile_attr.ySize * tile_y;
1034 col = s->tile_attr.xSize * tile_x;
1036 if (line < s->ymin || line > s->ymax ||
1037 s->xmin + col < s->xmin || s->xmin + col > s->xmax)
1038 return AVERROR_INVALIDDATA;
1040 td->ysize = FFMIN(s->tile_attr.ySize, s->ydelta - tile_y * s->tile_attr.ySize);
1041 td->xsize = FFMIN(s->tile_attr.xSize, s->xdelta - tile_x * s->tile_attr.xSize);
1043 if (td->xsize * (uint64_t)s->current_channel_offset > INT_MAX)
1044 return AVERROR_INVALIDDATA;
1046 td->channel_line_size = td->xsize * s->current_channel_offset;/* uncompress size of one line */
1047 uncompressed_size = td->channel_line_size * (uint64_t)td->ysize;/* uncompress size of the block */
1049 if (buf_size < 8 || line_offset > buf_size - 8)
1050 return AVERROR_INVALIDDATA;
1052 src = buf + line_offset + 8;
1053 line = AV_RL32(src - 8);
1055 if (line < s->ymin || line > s->ymax)
1056 return AVERROR_INVALIDDATA;
1058 data_size = AV_RL32(src - 4);
1059 if (data_size <= 0 || data_size > buf_size - line_offset - 8)
1060 return AVERROR_INVALIDDATA;
1062 td->ysize = FFMIN(s->scan_lines_per_block, s->ymax - line + 1); /* s->ydelta - line ?? */
1063 td->xsize = s->xdelta;
1065 if (td->xsize * (uint64_t)s->current_channel_offset > INT_MAX)
1066 return AVERROR_INVALIDDATA;
1068 td->channel_line_size = td->xsize * s->current_channel_offset;/* uncompress size of one line */
1069 uncompressed_size = td->channel_line_size * (uint64_t)td->ysize;/* uncompress size of the block */
1071 if ((s->compression == EXR_RAW && (data_size != uncompressed_size ||
1072 line_offset > buf_size - uncompressed_size)) ||
1073 (s->compression != EXR_RAW && (data_size > uncompressed_size ||
1074 line_offset > buf_size - data_size))) {
1075 return AVERROR_INVALIDDATA;
1079 window_xmin = FFMIN(avctx->width, FFMAX(0, s->xmin + col));
1080 window_xmax = FFMIN(avctx->width, FFMAX(0, s->xmin + col + td->xsize));
1081 window_ymin = FFMIN(avctx->height, FFMAX(0, line ));
1082 window_ymax = FFMIN(avctx->height, FFMAX(0, line + td->ysize));
1083 xsize = window_xmax - window_xmin;
1084 ysize = window_ymax - window_ymin;
1086 /* tile or scanline not visible skip decoding */
1087 if (xsize <= 0 || ysize <= 0)
1090 /* is the first tile or is a scanline */
1093 /* pixels to add at the left of the display window */
1094 window_xoffset = FFMAX(0, s->xmin);
1095 /* bytes to add at the left of the display window */
1096 bxmin = window_xoffset * step;
1099 /* is the last tile or is a scanline */
1100 if(col + td->xsize == s->xdelta) {
1101 window_xmax = avctx->width;
1102 /* bytes to add at the right of the display window */
1103 axmax = FFMAX(0, (avctx->width - (s->xmax + 1))) * step;
1106 if (data_size < uncompressed_size || s->is_tile) { /* td->tmp is use for tile reorganization */
1107 av_fast_padded_malloc(&td->tmp, &td->tmp_size, uncompressed_size);
1109 return AVERROR(ENOMEM);
1112 if (data_size < uncompressed_size) {
1113 av_fast_padded_malloc(&td->uncompressed_data,
1114 &td->uncompressed_size, uncompressed_size + 64);/* Force 64 padding for AVX2 reorder_pixels dst */
1116 if (!td->uncompressed_data)
1117 return AVERROR(ENOMEM);
1119 ret = AVERROR_INVALIDDATA;
1120 switch (s->compression) {
1123 ret = zip_uncompress(s, src, data_size, uncompressed_size, td);
1126 ret = piz_uncompress(s, src, data_size, uncompressed_size, td);
1129 ret = pxr24_uncompress(s, src, data_size, uncompressed_size, td);
1132 ret = rle_uncompress(s, src, data_size, uncompressed_size, td);
1136 ret = b44_uncompress(s, src, data_size, uncompressed_size, td);
1140 av_log(avctx, AV_LOG_ERROR, "decode_block() failed.\n");
1143 src = td->uncompressed_data;
1146 /* offsets to crop data outside display window */
1147 data_xoffset = FFABS(FFMIN(0, s->xmin + col)) * (s->pixel_type == EXR_HALF ? 2 : 4);
1148 data_yoffset = FFABS(FFMIN(0, line));
1149 data_window_offset = (data_yoffset * td->channel_line_size) + data_xoffset;
1152 channel_buffer[0] = src + (td->xsize * s->channel_offsets[0]) + data_window_offset;
1153 channel_buffer[1] = src + (td->xsize * s->channel_offsets[1]) + data_window_offset;
1154 channel_buffer[2] = src + (td->xsize * s->channel_offsets[2]) + data_window_offset;
1155 rgb_channel_count = 3;
1156 } else { /* put y data in the first channel_buffer */
1157 channel_buffer[0] = src + (td->xsize * s->channel_offsets[1]) + data_window_offset;
1158 rgb_channel_count = 1;
1160 if (s->channel_offsets[3] >= 0)
1161 channel_buffer[3] = src + (td->xsize * s->channel_offsets[3]) + data_window_offset;
1163 if (s->desc->flags & AV_PIX_FMT_FLAG_FLOAT) {
1165 /* todo: change this when a floating point pixel format with luma with alpha is implemented */
1166 int channel_count = s->channel_offsets[3] >= 0 ? 4 : rgb_channel_count;
1168 channel_buffer[1] = channel_buffer[0];
1169 channel_buffer[2] = channel_buffer[0];
1172 for (c = 0; c < channel_count; c++) {
1173 int plane = s->desc->comp[c].plane;
1174 ptr = p->data[plane] + window_ymin * p->linesize[plane] + (window_xmin * 4);
1176 for (i = 0; i < ysize; i++, ptr += p->linesize[plane]) {
1178 union av_intfloat32 *ptr_x;
1180 src = channel_buffer[c];
1181 ptr_x = (union av_intfloat32 *)ptr;
1183 // Zero out the start if xmin is not 0
1184 memset(ptr_x, 0, bxmin);
1185 ptr_x += window_xoffset;
1187 if (s->pixel_type == EXR_FLOAT) {
1189 union av_intfloat32 t;
1190 if (trc_func && c < 3) {
1191 for (x = 0; x < xsize; x++) {
1192 t.i = bytestream_get_le32(&src);
1193 t.f = trc_func(t.f);
1197 for (x = 0; x < xsize; x++) {
1198 t.i = bytestream_get_le32(&src);
1199 if (t.f > 0.0f && c < 3) /* avoid negative values */
1200 t.f = powf(t.f, one_gamma);
1204 } else if (s->pixel_type == EXR_HALF) {
1206 if (c < 3 || !trc_func) {
1207 for (x = 0; x < xsize; x++) {
1208 *ptr_x++ = s->gamma_table[bytestream_get_le16(&src)];
1211 for (x = 0; x < xsize; x++) {
1212 *ptr_x++ = exr_half2float(bytestream_get_le16(&src));;
1217 // Zero out the end if xmax+1 is not w
1218 memset(ptr_x, 0, axmax);
1219 channel_buffer[c] += td->channel_line_size;
1224 av_assert1(s->pixel_type == EXR_UINT);
1225 ptr = p->data[0] + window_ymin * p->linesize[0] + (window_xmin * s->desc->nb_components * 2);
1227 for (i = 0; i < ysize; i++, ptr += p->linesize[0]) {
1230 const uint8_t *rgb[3];
1233 for (c = 0; c < rgb_channel_count; c++) {
1234 rgb[c] = channel_buffer[c];
1237 if (channel_buffer[3])
1238 a = channel_buffer[3];
1240 ptr_x = (uint16_t *) ptr;
1242 // Zero out the start if xmin is not 0
1243 memset(ptr_x, 0, bxmin);
1244 ptr_x += window_xoffset * s->desc->nb_components;
1246 for (x = 0; x < xsize; x++) {
1247 for (c = 0; c < rgb_channel_count; c++) {
1248 *ptr_x++ = bytestream_get_le32(&rgb[c]) >> 16;
1251 if (channel_buffer[3])
1252 *ptr_x++ = bytestream_get_le32(&a) >> 16;
1255 // Zero out the end if xmax+1 is not w
1256 memset(ptr_x, 0, axmax);
1258 channel_buffer[0] += td->channel_line_size;
1259 channel_buffer[1] += td->channel_line_size;
1260 channel_buffer[2] += td->channel_line_size;
1261 if (channel_buffer[3])
1262 channel_buffer[3] += td->channel_line_size;
1270 * Check if the variable name corresponds to its data type.
1272 * @param s the EXRContext
1273 * @param value_name name of the variable to check
1274 * @param value_type type of the variable to check
1275 * @param minimum_length minimum length of the variable data
1277 * @return bytes to read containing variable data
1278 * -1 if variable is not found
1279 * 0 if buffer ended prematurely
1281 static int check_header_variable(EXRContext *s,
1282 const char *value_name,
1283 const char *value_type,
1284 unsigned int minimum_length)
1288 if (bytestream2_get_bytes_left(&s->gb) >= minimum_length &&
1289 !strcmp(s->gb.buffer, value_name)) {
1290 // found value_name, jump to value_type (null terminated strings)
1291 s->gb.buffer += strlen(value_name) + 1;
1292 if (!strcmp(s->gb.buffer, value_type)) {
1293 s->gb.buffer += strlen(value_type) + 1;
1294 var_size = bytestream2_get_le32(&s->gb);
1295 // don't go read past boundaries
1296 if (var_size > bytestream2_get_bytes_left(&s->gb))
1299 // value_type not found, reset the buffer
1300 s->gb.buffer -= strlen(value_name) + 1;
1301 av_log(s->avctx, AV_LOG_WARNING,
1302 "Unknown data type %s for header variable %s.\n",
1303 value_type, value_name);
1310 static int decode_header(EXRContext *s, AVFrame *frame)
1312 AVDictionary *metadata = NULL;
1313 int magic_number, version, i, flags;
1314 int layer_match = 0;
1316 int dup_channels = 0;
1318 s->current_channel_offset = 0;
1325 s->channel_offsets[0] = -1;
1326 s->channel_offsets[1] = -1;
1327 s->channel_offsets[2] = -1;
1328 s->channel_offsets[3] = -1;
1329 s->pixel_type = EXR_UNKNOWN;
1330 s->compression = EXR_UNKN;
1334 s->tile_attr.xSize = -1;
1335 s->tile_attr.ySize = -1;
1339 if (bytestream2_get_bytes_left(&s->gb) < 10) {
1340 av_log(s->avctx, AV_LOG_ERROR, "Header too short to parse.\n");
1341 return AVERROR_INVALIDDATA;
1344 magic_number = bytestream2_get_le32(&s->gb);
1345 if (magic_number != 20000630) {
1346 /* As per documentation of OpenEXR, it is supposed to be
1347 * int 20000630 little-endian */
1348 av_log(s->avctx, AV_LOG_ERROR, "Wrong magic number %d.\n", magic_number);
1349 return AVERROR_INVALIDDATA;
1352 version = bytestream2_get_byte(&s->gb);
1354 avpriv_report_missing_feature(s->avctx, "Version %d", version);
1355 return AVERROR_PATCHWELCOME;
1358 flags = bytestream2_get_le24(&s->gb);
1363 avpriv_report_missing_feature(s->avctx, "deep data");
1364 return AVERROR_PATCHWELCOME;
1367 avpriv_report_missing_feature(s->avctx, "multipart");
1368 return AVERROR_PATCHWELCOME;
1372 while (bytestream2_get_bytes_left(&s->gb) > 0 && *s->gb.buffer) {
1374 if ((var_size = check_header_variable(s, "channels",
1375 "chlist", 38)) >= 0) {
1376 GetByteContext ch_gb;
1378 ret = AVERROR_INVALIDDATA;
1382 bytestream2_init(&ch_gb, s->gb.buffer, var_size);
1384 while (bytestream2_get_bytes_left(&ch_gb) >= 19) {
1385 EXRChannel *channel;
1386 enum ExrPixelType current_pixel_type;
1387 int channel_index = -1;
1390 if (strcmp(s->layer, "") != 0) {
1391 if (strncmp(ch_gb.buffer, s->layer, strlen(s->layer)) == 0) {
1393 av_log(s->avctx, AV_LOG_INFO,
1394 "Channel match layer : %s.\n", ch_gb.buffer);
1395 ch_gb.buffer += strlen(s->layer);
1396 if (*ch_gb.buffer == '.')
1397 ch_gb.buffer++; /* skip dot if not given */
1400 av_log(s->avctx, AV_LOG_INFO,
1401 "Channel doesn't match layer : %s.\n", ch_gb.buffer);
1407 if (layer_match) { /* only search channel if the layer match is valid */
1408 if (!av_strcasecmp(ch_gb.buffer, "R") ||
1409 !av_strcasecmp(ch_gb.buffer, "X") ||
1410 !av_strcasecmp(ch_gb.buffer, "U")) {
1413 } else if (!av_strcasecmp(ch_gb.buffer, "G") ||
1414 !av_strcasecmp(ch_gb.buffer, "V")) {
1417 } else if (!av_strcasecmp(ch_gb.buffer, "Y")) {
1420 } else if (!av_strcasecmp(ch_gb.buffer, "B") ||
1421 !av_strcasecmp(ch_gb.buffer, "Z") ||
1422 !av_strcasecmp(ch_gb.buffer, "W")) {
1425 } else if (!av_strcasecmp(ch_gb.buffer, "A")) {
1428 av_log(s->avctx, AV_LOG_WARNING,
1429 "Unsupported channel %.256s.\n", ch_gb.buffer);
1433 /* skip until you get a 0 */
1434 while (bytestream2_get_bytes_left(&ch_gb) > 0 &&
1435 bytestream2_get_byte(&ch_gb))
1438 if (bytestream2_get_bytes_left(&ch_gb) < 4) {
1439 av_log(s->avctx, AV_LOG_ERROR, "Incomplete header.\n");
1440 ret = AVERROR_INVALIDDATA;
1444 current_pixel_type = bytestream2_get_le32(&ch_gb);
1445 if (current_pixel_type >= EXR_UNKNOWN) {
1446 avpriv_report_missing_feature(s->avctx, "Pixel type %d",
1447 current_pixel_type);
1448 ret = AVERROR_PATCHWELCOME;
1452 bytestream2_skip(&ch_gb, 4);
1453 xsub = bytestream2_get_le32(&ch_gb);
1454 ysub = bytestream2_get_le32(&ch_gb);
1456 if (xsub != 1 || ysub != 1) {
1457 avpriv_report_missing_feature(s->avctx,
1458 "Subsampling %dx%d",
1460 ret = AVERROR_PATCHWELCOME;
1464 if (channel_index >= 0 && s->channel_offsets[channel_index] == -1) { /* channel has not been previously assigned */
1465 if (s->pixel_type != EXR_UNKNOWN &&
1466 s->pixel_type != current_pixel_type) {
1467 av_log(s->avctx, AV_LOG_ERROR,
1468 "RGB channels not of the same depth.\n");
1469 ret = AVERROR_INVALIDDATA;
1472 s->pixel_type = current_pixel_type;
1473 s->channel_offsets[channel_index] = s->current_channel_offset;
1474 } else if (channel_index >= 0) {
1475 av_log(s->avctx, AV_LOG_WARNING,
1476 "Multiple channels with index %d.\n", channel_index);
1477 if (++dup_channels > 10) {
1478 ret = AVERROR_INVALIDDATA;
1483 s->channels = av_realloc(s->channels,
1484 ++s->nb_channels * sizeof(EXRChannel));
1486 ret = AVERROR(ENOMEM);
1489 channel = &s->channels[s->nb_channels - 1];
1490 channel->pixel_type = current_pixel_type;
1491 channel->xsub = xsub;
1492 channel->ysub = ysub;
1494 if (current_pixel_type == EXR_HALF) {
1495 s->current_channel_offset += 2;
1496 } else {/* Float or UINT32 */
1497 s->current_channel_offset += 4;
1501 /* Check if all channels are set with an offset or if the channels
1502 * are causing an overflow */
1503 if (!s->is_luma) {/* if we expected to have at least 3 channels */
1504 if (FFMIN3(s->channel_offsets[0],
1505 s->channel_offsets[1],
1506 s->channel_offsets[2]) < 0) {
1507 if (s->channel_offsets[0] < 0)
1508 av_log(s->avctx, AV_LOG_ERROR, "Missing red channel.\n");
1509 if (s->channel_offsets[1] < 0)
1510 av_log(s->avctx, AV_LOG_ERROR, "Missing green channel.\n");
1511 if (s->channel_offsets[2] < 0)
1512 av_log(s->avctx, AV_LOG_ERROR, "Missing blue channel.\n");
1513 ret = AVERROR_INVALIDDATA;
1518 // skip one last byte and update main gb
1519 s->gb.buffer = ch_gb.buffer + 1;
1521 } else if ((var_size = check_header_variable(s, "dataWindow", "box2i",
1523 int xmin, ymin, xmax, ymax;
1525 ret = AVERROR_INVALIDDATA;
1529 xmin = bytestream2_get_le32(&s->gb);
1530 ymin = bytestream2_get_le32(&s->gb);
1531 xmax = bytestream2_get_le32(&s->gb);
1532 ymax = bytestream2_get_le32(&s->gb);
1534 if (xmin > xmax || ymin > ymax ||
1535 (unsigned)xmax - xmin >= INT_MAX ||
1536 (unsigned)ymax - ymin >= INT_MAX) {
1537 ret = AVERROR_INVALIDDATA;
1544 s->xdelta = (s->xmax - s->xmin) + 1;
1545 s->ydelta = (s->ymax - s->ymin) + 1;
1548 } else if ((var_size = check_header_variable(s, "displayWindow",
1549 "box2i", 34)) >= 0) {
1551 ret = AVERROR_INVALIDDATA;
1555 bytestream2_skip(&s->gb, 8);
1556 s->w = bytestream2_get_le32(&s->gb) + 1;
1557 s->h = bytestream2_get_le32(&s->gb) + 1;
1560 } else if ((var_size = check_header_variable(s, "lineOrder",
1561 "lineOrder", 25)) >= 0) {
1564 ret = AVERROR_INVALIDDATA;
1568 line_order = bytestream2_get_byte(&s->gb);
1569 av_log(s->avctx, AV_LOG_DEBUG, "line order: %d.\n", line_order);
1570 if (line_order > 2) {
1571 av_log(s->avctx, AV_LOG_ERROR, "Unknown line order.\n");
1572 ret = AVERROR_INVALIDDATA;
1577 } else if ((var_size = check_header_variable(s, "pixelAspectRatio",
1578 "float", 31)) >= 0) {
1580 ret = AVERROR_INVALIDDATA;
1584 s->sar = bytestream2_get_le32(&s->gb);
1587 } else if ((var_size = check_header_variable(s, "compression",
1588 "compression", 29)) >= 0) {
1590 ret = AVERROR_INVALIDDATA;
1594 if (s->compression == EXR_UNKN)
1595 s->compression = bytestream2_get_byte(&s->gb);
1597 av_log(s->avctx, AV_LOG_WARNING,
1598 "Found more than one compression attribute.\n");
1601 } else if ((var_size = check_header_variable(s, "tiles",
1602 "tiledesc", 22)) >= 0) {
1606 av_log(s->avctx, AV_LOG_WARNING,
1607 "Found tile attribute and scanline flags. Exr will be interpreted as scanline.\n");
1609 s->tile_attr.xSize = bytestream2_get_le32(&s->gb);
1610 s->tile_attr.ySize = bytestream2_get_le32(&s->gb);
1612 tileLevel = bytestream2_get_byte(&s->gb);
1613 s->tile_attr.level_mode = tileLevel & 0x0f;
1614 s->tile_attr.level_round = (tileLevel >> 4) & 0x0f;
1616 if (s->tile_attr.level_mode >= EXR_TILE_LEVEL_UNKNOWN) {
1617 avpriv_report_missing_feature(s->avctx, "Tile level mode %d",
1618 s->tile_attr.level_mode);
1619 ret = AVERROR_PATCHWELCOME;
1623 if (s->tile_attr.level_round >= EXR_TILE_ROUND_UNKNOWN) {
1624 avpriv_report_missing_feature(s->avctx, "Tile level round %d",
1625 s->tile_attr.level_round);
1626 ret = AVERROR_PATCHWELCOME;
1631 } else if ((var_size = check_header_variable(s, "writer",
1632 "string", 1)) >= 0) {
1633 uint8_t key[256] = { 0 };
1635 bytestream2_get_buffer(&s->gb, key, FFMIN(sizeof(key) - 1, var_size));
1636 av_dict_set(&metadata, "writer", key, 0);
1639 } else if ((var_size = check_header_variable(s, "framesPerSecond",
1640 "rational", 33)) >= 0) {
1642 ret = AVERROR_INVALIDDATA;
1646 s->avctx->framerate.num = bytestream2_get_le32(&s->gb);
1647 s->avctx->framerate.den = bytestream2_get_le32(&s->gb);
1652 // Check if there are enough bytes for a header
1653 if (bytestream2_get_bytes_left(&s->gb) <= 9) {
1654 av_log(s->avctx, AV_LOG_ERROR, "Incomplete header\n");
1655 ret = AVERROR_INVALIDDATA;
1659 // Process unknown variables
1660 for (i = 0; i < 2; i++) // value_name and value_type
1661 while (bytestream2_get_byte(&s->gb) != 0);
1663 // Skip variable length
1664 bytestream2_skip(&s->gb, bytestream2_get_le32(&s->gb));
1667 if (s->compression == EXR_UNKN) {
1668 av_log(s->avctx, AV_LOG_ERROR, "Missing compression attribute.\n");
1669 ret = AVERROR_INVALIDDATA;
1674 if (s->tile_attr.xSize < 1 || s->tile_attr.ySize < 1) {
1675 av_log(s->avctx, AV_LOG_ERROR, "Invalid tile attribute.\n");
1676 ret = AVERROR_INVALIDDATA;
1681 if (bytestream2_get_bytes_left(&s->gb) <= 0) {
1682 av_log(s->avctx, AV_LOG_ERROR, "Incomplete frame.\n");
1683 ret = AVERROR_INVALIDDATA;
1687 frame->metadata = metadata;
1689 // aaand we are done
1690 bytestream2_skip(&s->gb, 1);
1693 av_dict_free(&metadata);
1697 static int decode_frame(AVCodecContext *avctx, void *data,
1698 int *got_frame, AVPacket *avpkt)
1700 EXRContext *s = avctx->priv_data;
1701 ThreadFrame frame = { .f = data };
1702 AVFrame *picture = data;
1705 int i, y, ret, ymax;
1708 int nb_blocks; /* nb scanline or nb tile */
1709 uint64_t start_offset_table;
1710 uint64_t start_next_scanline;
1711 PutByteContext offset_table_writer;
1713 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
1715 if ((ret = decode_header(s, picture)) < 0)
1718 switch (s->pixel_type) {
1721 if (s->channel_offsets[3] >= 0) {
1723 avctx->pix_fmt = AV_PIX_FMT_GBRAPF32;
1725 /* todo: change this when a floating point pixel format with luma with alpha is implemented */
1726 avctx->pix_fmt = AV_PIX_FMT_GBRAPF32;
1730 avctx->pix_fmt = AV_PIX_FMT_GBRPF32;
1732 avctx->pix_fmt = AV_PIX_FMT_GRAYF32;
1737 if (s->channel_offsets[3] >= 0) {
1739 avctx->pix_fmt = AV_PIX_FMT_RGBA64;
1741 avctx->pix_fmt = AV_PIX_FMT_YA16;
1745 avctx->pix_fmt = AV_PIX_FMT_RGB48;
1747 avctx->pix_fmt = AV_PIX_FMT_GRAY16;
1752 av_log(avctx, AV_LOG_ERROR, "Missing channel list.\n");
1753 return AVERROR_INVALIDDATA;
1756 if (s->apply_trc_type != AVCOL_TRC_UNSPECIFIED)
1757 avctx->color_trc = s->apply_trc_type;
1759 switch (s->compression) {
1763 s->scan_lines_per_block = 1;
1767 s->scan_lines_per_block = 16;
1772 s->scan_lines_per_block = 32;
1775 avpriv_report_missing_feature(avctx, "Compression %d", s->compression);
1776 return AVERROR_PATCHWELCOME;
1779 /* Verify the xmin, xmax, ymin and ymax before setting the actual image size.
1780 * It's possible for the data window can larger or outside the display window */
1781 if (s->xmin > s->xmax || s->ymin > s->ymax ||
1782 s->ydelta == 0xFFFFFFFF || s->xdelta == 0xFFFFFFFF) {
1783 av_log(avctx, AV_LOG_ERROR, "Wrong or missing size information.\n");
1784 return AVERROR_INVALIDDATA;
1787 if ((ret = ff_set_dimensions(avctx, s->w, s->h)) < 0)
1790 ff_set_sar(s->avctx, av_d2q(av_int2float(s->sar), 255));
1792 s->desc = av_pix_fmt_desc_get(avctx->pix_fmt);
1794 return AVERROR_INVALIDDATA;
1796 if (s->desc->flags & AV_PIX_FMT_FLAG_FLOAT) {
1797 planes = s->desc->nb_components;
1798 out_line_size = avctx->width * 4;
1801 out_line_size = avctx->width * 2 * s->desc->nb_components;
1805 nb_blocks = ((s->xdelta + s->tile_attr.xSize - 1) / s->tile_attr.xSize) *
1806 ((s->ydelta + s->tile_attr.ySize - 1) / s->tile_attr.ySize);
1807 } else { /* scanline */
1808 nb_blocks = (s->ydelta + s->scan_lines_per_block - 1) /
1809 s->scan_lines_per_block;
1812 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
1815 if (bytestream2_get_bytes_left(&s->gb)/8 < nb_blocks)
1816 return AVERROR_INVALIDDATA;
1818 // check offset table and recreate it if need
1819 if (!s->is_tile && bytestream2_peek_le64(&s->gb) == 0) {
1820 av_log(s->avctx, AV_LOG_DEBUG, "recreating invalid scanline offset table\n");
1822 start_offset_table = bytestream2_tell(&s->gb);
1823 start_next_scanline = start_offset_table + nb_blocks * 8;
1824 bytestream2_init_writer(&offset_table_writer, &avpkt->data[start_offset_table], nb_blocks * 8);
1826 for (y = 0; y < nb_blocks; y++) {
1827 /* write offset of prev scanline in offset table */
1828 bytestream2_put_le64(&offset_table_writer, start_next_scanline);
1830 /* get len of next scanline */
1831 bytestream2_seek(&s->gb, start_next_scanline + 4, SEEK_SET);/* skip line number */
1832 start_next_scanline += (bytestream2_get_le32(&s->gb) + 8);
1834 bytestream2_seek(&s->gb, start_offset_table, SEEK_SET);
1837 // save pointer we are going to use in decode_block
1838 s->buf = avpkt->data;
1839 s->buf_size = avpkt->size;
1841 // Zero out the start if ymin is not 0
1842 for (i = 0; i < planes; i++) {
1843 ptr = picture->data[i];
1844 for (y = 0; y < FFMIN(s->ymin, s->h); y++) {
1845 memset(ptr, 0, out_line_size);
1846 ptr += picture->linesize[i];
1850 s->picture = picture;
1852 avctx->execute2(avctx, decode_block, s->thread_data, NULL, nb_blocks);
1854 ymax = FFMAX(0, s->ymax + 1);
1855 // Zero out the end if ymax+1 is not h
1856 if (ymax < avctx->height)
1857 for (i = 0; i < planes; i++) {
1858 ptr = picture->data[i] + (ymax * picture->linesize[i]);
1859 for (y = ymax; y < avctx->height; y++) {
1860 memset(ptr, 0, out_line_size);
1861 ptr += picture->linesize[i];
1865 picture->pict_type = AV_PICTURE_TYPE_I;
1871 static av_cold int decode_init(AVCodecContext *avctx)
1873 EXRContext *s = avctx->priv_data;
1875 union av_intfloat32 t;
1876 float one_gamma = 1.0f / s->gamma;
1877 avpriv_trc_function trc_func = NULL;
1881 ff_exrdsp_init(&s->dsp);
1884 ff_bswapdsp_init(&s->bbdsp);
1887 trc_func = avpriv_get_trc_function_from_trc(s->apply_trc_type);
1889 for (i = 0; i < 65536; ++i) {
1890 t = exr_half2float(i);
1891 t.f = trc_func(t.f);
1892 s->gamma_table[i] = t;
1895 if (one_gamma > 0.9999f && one_gamma < 1.0001f) {
1896 for (i = 0; i < 65536; ++i) {
1897 s->gamma_table[i] = exr_half2float(i);
1900 for (i = 0; i < 65536; ++i) {
1901 t = exr_half2float(i);
1902 /* If negative value we reuse half value */
1904 s->gamma_table[i] = t;
1906 t.f = powf(t.f, one_gamma);
1907 s->gamma_table[i] = t;
1913 // allocate thread data, used for non EXR_RAW compression types
1914 s->thread_data = av_mallocz_array(avctx->thread_count, sizeof(EXRThreadData));
1915 if (!s->thread_data)
1916 return AVERROR_INVALIDDATA;
1921 static av_cold int decode_end(AVCodecContext *avctx)
1923 EXRContext *s = avctx->priv_data;
1925 for (i = 0; i < avctx->thread_count; i++) {
1926 EXRThreadData *td = &s->thread_data[i];
1927 av_freep(&td->uncompressed_data);
1929 av_freep(&td->bitmap);
1933 av_freep(&s->thread_data);
1934 av_freep(&s->channels);
1939 #define OFFSET(x) offsetof(EXRContext, x)
1940 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1941 static const AVOption options[] = {
1942 { "layer", "Set the decoding layer", OFFSET(layer),
1943 AV_OPT_TYPE_STRING, { .str = "" }, 0, 0, VD },
1944 { "gamma", "Set the float gamma value when decoding", OFFSET(gamma),
1945 AV_OPT_TYPE_FLOAT, { .dbl = 1.0f }, 0.001, FLT_MAX, VD },
1947 // XXX: Note the abuse of the enum using AVCOL_TRC_UNSPECIFIED to subsume the existing gamma option
1948 { "apply_trc", "color transfer characteristics to apply to EXR linear input", OFFSET(apply_trc_type),
1949 AV_OPT_TYPE_INT, {.i64 = AVCOL_TRC_UNSPECIFIED }, 1, AVCOL_TRC_NB-1, VD, "apply_trc_type"},
1950 { "bt709", "BT.709", 0,
1951 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT709 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1952 { "gamma", "gamma", 0,
1953 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_UNSPECIFIED }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1954 { "gamma22", "BT.470 M", 0,
1955 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_GAMMA22 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1956 { "gamma28", "BT.470 BG", 0,
1957 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_GAMMA28 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1958 { "smpte170m", "SMPTE 170 M", 0,
1959 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTE170M }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1960 { "smpte240m", "SMPTE 240 M", 0,
1961 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTE240M }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1962 { "linear", "Linear", 0,
1963 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LINEAR }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1965 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LOG }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1966 { "log_sqrt", "Log square root", 0,
1967 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LOG_SQRT }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1968 { "iec61966_2_4", "IEC 61966-2-4", 0,
1969 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_IEC61966_2_4 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1970 { "bt1361", "BT.1361", 0,
1971 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT1361_ECG }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1972 { "iec61966_2_1", "IEC 61966-2-1", 0,
1973 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_IEC61966_2_1 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1974 { "bt2020_10bit", "BT.2020 - 10 bit", 0,
1975 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT2020_10 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1976 { "bt2020_12bit", "BT.2020 - 12 bit", 0,
1977 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT2020_12 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1978 { "smpte2084", "SMPTE ST 2084", 0,
1979 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTEST2084 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1980 { "smpte428_1", "SMPTE ST 428-1", 0,
1981 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTEST428_1 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1986 static const AVClass exr_class = {
1987 .class_name = "EXR",
1988 .item_name = av_default_item_name,
1990 .version = LIBAVUTIL_VERSION_INT,
1993 AVCodec ff_exr_decoder = {
1995 .long_name = NULL_IF_CONFIG_SMALL("OpenEXR image"),
1996 .type = AVMEDIA_TYPE_VIDEO,
1997 .id = AV_CODEC_ID_EXR,
1998 .priv_data_size = sizeof(EXRContext),
1999 .init = decode_init,
2000 .close = decode_end,
2001 .decode = decode_frame,
2002 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS |
2003 AV_CODEC_CAP_SLICE_THREADS,
2004 .priv_class = &exr_class,