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_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/avassert.h"
41 #include "libavutil/common.h"
42 #include "libavutil/imgutils.h"
43 #include "libavutil/intfloat.h"
44 #include "libavutil/opt.h"
45 #include "libavutil/color_utils.h"
48 #include "bytestream.h"
80 enum ExrTileLevelMode {
82 EXR_TILE_LEVEL_MIPMAP,
83 EXR_TILE_LEVEL_RIPMAP,
84 EXR_TILE_LEVEL_UNKNOWN,
87 enum ExrTileLevelRound {
90 EXR_TILE_ROUND_UNKNOWN,
93 typedef struct EXRChannel {
95 enum ExrPixelType pixel_type;
98 typedef struct EXRTileAttribute {
101 enum ExrTileLevelMode level_mode;
102 enum ExrTileLevelRound level_round;
105 typedef struct EXRThreadData {
106 uint8_t *uncompressed_data;
107 int uncompressed_size;
117 int channel_line_size;
120 typedef struct EXRContext {
123 AVCodecContext *avctx;
126 BswapDSPContext bbdsp;
129 enum ExrCompr compression;
130 enum ExrPixelType pixel_type;
131 int channel_offsets[4]; // 0 = red, 1 = green, 2 = blue and 3 = alpha
132 const AVPixFmtDescriptor *desc;
137 uint32_t xdelta, ydelta;
139 int scan_lines_per_block;
141 EXRTileAttribute tile_attr; /* header data attribute of tile */
142 int is_tile; /* 0 if scanline, 1 if tile */
144 int is_luma;/* 1 if there is an Y plane */
150 EXRChannel *channels;
152 int current_channel_offset;
154 EXRThreadData *thread_data;
158 enum AVColorTransferCharacteristic apply_trc_type;
160 uint16_t gamma_table[65536];
163 /* -15 stored using a single precision bias of 127 */
164 #define HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP 0x38000000
166 /* max exponent value in single precision that will be converted
167 * to Inf or Nan when stored as a half-float */
168 #define HALF_FLOAT_MAX_BIASED_EXP_AS_SINGLE_FP_EXP 0x47800000
170 /* 255 is the max exponent biased value */
171 #define FLOAT_MAX_BIASED_EXP (0xFF << 23)
173 #define HALF_FLOAT_MAX_BIASED_EXP (0x1F << 10)
176 * Convert a half float as a uint16_t into a full float.
178 * @param hf half float as uint16_t
180 * @return float value
182 static union av_intfloat32 exr_half2float(uint16_t hf)
184 unsigned int sign = (unsigned int) (hf >> 15);
185 unsigned int mantissa = (unsigned int) (hf & ((1 << 10) - 1));
186 unsigned int exp = (unsigned int) (hf & HALF_FLOAT_MAX_BIASED_EXP);
187 union av_intfloat32 f;
189 if (exp == HALF_FLOAT_MAX_BIASED_EXP) {
190 // we have a half-float NaN or Inf
191 // half-float NaNs will be converted to a single precision NaN
192 // half-float Infs will be converted to a single precision Inf
193 exp = FLOAT_MAX_BIASED_EXP;
195 mantissa = (1 << 23) - 1; // set all bits to indicate a NaN
196 } else if (exp == 0x0) {
197 // convert half-float zero/denorm to single precision value
200 exp = HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP;
201 // check for leading 1 in denorm mantissa
202 while ((mantissa & (1 << 10))) {
203 // for every leading 0, decrement single precision exponent by 1
204 // and shift half-float mantissa value to the left
208 // clamp the mantissa to 10 bits
209 mantissa &= ((1 << 10) - 1);
210 // shift left to generate single-precision mantissa of 23 bits
214 // shift left to generate single-precision mantissa of 23 bits
216 // generate single precision biased exponent value
217 exp = (exp << 13) + HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP;
220 f.i = (sign << 31) | exp | mantissa;
227 * Convert from 32-bit float as uint32_t to uint16_t.
229 * @param v 32-bit float
231 * @return normalized 16-bit unsigned int
233 static inline uint16_t exr_flt2uint(int32_t v)
235 int32_t exp = v >> 23;
236 // "HACK": negative values result in exp< 0, so clipping them to 0
237 // is also handled by this condition, avoids explicit check for sign bit.
238 if (exp <= 127 + 7 - 24) // we would shift out all bits anyway
243 return (v + (1 << 23)) >> (127 + 7 - exp);
247 * Convert from 16-bit float as uint16_t to uint16_t.
249 * @param v 16-bit float
251 * @return normalized 16-bit unsigned int
253 static inline uint16_t exr_halflt2uint(uint16_t v)
255 unsigned exp = 14 - (v >> 10);
260 return (v & 0x8000) ? 0 : 0xffff;
263 return (v + (1 << 16)) >> (exp + 1);
266 static void predictor(uint8_t *src, int size)
268 uint8_t *t = src + 1;
269 uint8_t *stop = src + size;
272 int d = (int) t[-1] + (int) t[0] - 128;
278 static void reorder_pixels(uint8_t *src, uint8_t *dst, int size)
280 const uint8_t *t1 = src;
281 int half_size = size / 2;
282 const uint8_t *t2 = src + half_size;
286 av_assert1(size % 2 == 0);
288 for (i = 0; i < half_size; i++) {
294 static int zip_uncompress(const uint8_t *src, int compressed_size,
295 int uncompressed_size, EXRThreadData *td)
297 unsigned long dest_len = uncompressed_size;
299 if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK ||
300 dest_len != uncompressed_size)
301 return AVERROR_INVALIDDATA;
303 predictor(td->tmp, uncompressed_size);
304 reorder_pixels(td->tmp, td->uncompressed_data, uncompressed_size);
309 static int rle_uncompress(const uint8_t *src, int compressed_size,
310 int uncompressed_size, EXRThreadData *td)
312 uint8_t *d = td->tmp;
313 const int8_t *s = src;
314 int ssize = compressed_size;
315 int dsize = uncompressed_size;
316 uint8_t *dend = d + dsize;
325 if ((dsize -= count) < 0 ||
326 (ssize -= count + 1) < 0)
327 return AVERROR_INVALIDDATA;
334 if ((dsize -= count) < 0 ||
336 return AVERROR_INVALIDDATA;
346 return AVERROR_INVALIDDATA;
348 predictor(td->tmp, uncompressed_size);
349 reorder_pixels(td->tmp, td->uncompressed_data, uncompressed_size);
354 #define USHORT_RANGE (1 << 16)
355 #define BITMAP_SIZE (1 << 13)
357 static uint16_t reverse_lut(const uint8_t *bitmap, uint16_t *lut)
361 for (i = 0; i < USHORT_RANGE; i++)
362 if ((i == 0) || (bitmap[i >> 3] & (1 << (i & 7))))
367 memset(lut + k, 0, (USHORT_RANGE - k) * 2);
372 static void apply_lut(const uint16_t *lut, uint16_t *dst, int dsize)
376 for (i = 0; i < dsize; ++i)
377 dst[i] = lut[dst[i]];
380 #define HUF_ENCBITS 16 // literal (value) bit length
381 #define HUF_DECBITS 14 // decoding bit size (>= 8)
383 #define HUF_ENCSIZE ((1 << HUF_ENCBITS) + 1) // encoding table size
384 #define HUF_DECSIZE (1 << HUF_DECBITS) // decoding table size
385 #define HUF_DECMASK (HUF_DECSIZE - 1)
387 typedef struct HufDec {
393 static void huf_canonical_code_table(uint64_t *hcode)
395 uint64_t c, n[59] = { 0 };
398 for (i = 0; i < HUF_ENCSIZE; ++i)
402 for (i = 58; i > 0; --i) {
403 uint64_t nc = ((c + n[i]) >> 1);
408 for (i = 0; i < HUF_ENCSIZE; ++i) {
412 hcode[i] = l | (n[l]++ << 6);
416 #define SHORT_ZEROCODE_RUN 59
417 #define LONG_ZEROCODE_RUN 63
418 #define SHORTEST_LONG_RUN (2 + LONG_ZEROCODE_RUN - SHORT_ZEROCODE_RUN)
419 #define LONGEST_LONG_RUN (255 + SHORTEST_LONG_RUN)
421 static int huf_unpack_enc_table(GetByteContext *gb,
422 int32_t im, int32_t iM, uint64_t *hcode)
425 int ret = init_get_bits8(&gbit, gb->buffer, bytestream2_get_bytes_left(gb));
429 for (; im <= iM; im++) {
430 uint64_t l = hcode[im] = get_bits(&gbit, 6);
432 if (l == LONG_ZEROCODE_RUN) {
433 int zerun = get_bits(&gbit, 8) + SHORTEST_LONG_RUN;
435 if (im + zerun > iM + 1)
436 return AVERROR_INVALIDDATA;
442 } else if (l >= SHORT_ZEROCODE_RUN) {
443 int zerun = l - SHORT_ZEROCODE_RUN + 2;
445 if (im + zerun > iM + 1)
446 return AVERROR_INVALIDDATA;
455 bytestream2_skip(gb, (get_bits_count(&gbit) + 7) / 8);
456 huf_canonical_code_table(hcode);
461 static int huf_build_dec_table(const uint64_t *hcode, int im,
462 int iM, HufDec *hdecod)
464 for (; im <= iM; im++) {
465 uint64_t c = hcode[im] >> 6;
466 int i, l = hcode[im] & 63;
469 return AVERROR_INVALIDDATA;
471 if (l > HUF_DECBITS) {
472 HufDec *pl = hdecod + (c >> (l - HUF_DECBITS));
474 return AVERROR_INVALIDDATA;
478 pl->p = av_realloc(pl->p, pl->lit * sizeof(int));
480 return AVERROR(ENOMEM);
482 pl->p[pl->lit - 1] = im;
484 HufDec *pl = hdecod + (c << (HUF_DECBITS - l));
486 for (i = 1 << (HUF_DECBITS - l); i > 0; i--, pl++) {
487 if (pl->len || pl->p)
488 return AVERROR_INVALIDDATA;
498 #define get_char(c, lc, gb) \
500 c = (c << 8) | bytestream2_get_byte(gb); \
504 #define get_code(po, rlc, c, lc, gb, out, oe, outb) \
508 get_char(c, lc, gb); \
513 if (out + cs > oe || out == outb) \
514 return AVERROR_INVALIDDATA; \
520 } else if (out < oe) { \
523 return AVERROR_INVALIDDATA; \
527 static int huf_decode(const uint64_t *hcode, const HufDec *hdecod,
528 GetByteContext *gb, int nbits,
529 int rlc, int no, uint16_t *out)
532 uint16_t *outb = out;
533 uint16_t *oe = out + no;
534 const uint8_t *ie = gb->buffer + (nbits + 7) / 8; // input byte size
539 while (gb->buffer < ie) {
542 while (lc >= HUF_DECBITS) {
543 const HufDec pl = hdecod[(c >> (lc - HUF_DECBITS)) & HUF_DECMASK];
547 get_code(pl.lit, rlc, c, lc, gb, out, oe, outb);
552 return AVERROR_INVALIDDATA;
554 for (j = 0; j < pl.lit; j++) {
555 int l = hcode[pl.p[j]] & 63;
557 while (lc < l && bytestream2_get_bytes_left(gb) > 0)
561 if ((hcode[pl.p[j]] >> 6) ==
562 ((c >> (lc - l)) & ((1LL << l) - 1))) {
564 get_code(pl.p[j], rlc, c, lc, gb, out, oe, outb);
571 return AVERROR_INVALIDDATA;
581 const HufDec pl = hdecod[(c << (HUF_DECBITS - lc)) & HUF_DECMASK];
585 get_code(pl.lit, rlc, c, lc, gb, out, oe, outb);
587 return AVERROR_INVALIDDATA;
591 if (out - outb != no)
592 return AVERROR_INVALIDDATA;
596 static int huf_uncompress(GetByteContext *gb,
597 uint16_t *dst, int dst_size)
599 int32_t src_size, im, iM;
605 src_size = bytestream2_get_le32(gb);
606 im = bytestream2_get_le32(gb);
607 iM = bytestream2_get_le32(gb);
608 bytestream2_skip(gb, 4);
609 nBits = bytestream2_get_le32(gb);
610 if (im < 0 || im >= HUF_ENCSIZE ||
611 iM < 0 || iM >= HUF_ENCSIZE ||
613 return AVERROR_INVALIDDATA;
615 bytestream2_skip(gb, 4);
617 freq = av_mallocz_array(HUF_ENCSIZE, sizeof(*freq));
618 hdec = av_mallocz_array(HUF_DECSIZE, sizeof(*hdec));
619 if (!freq || !hdec) {
620 ret = AVERROR(ENOMEM);
624 if ((ret = huf_unpack_enc_table(gb, im, iM, freq)) < 0)
627 if (nBits > 8 * bytestream2_get_bytes_left(gb)) {
628 ret = AVERROR_INVALIDDATA;
632 if ((ret = huf_build_dec_table(freq, im, iM, hdec)) < 0)
634 ret = huf_decode(freq, hdec, gb, nBits, iM, dst_size, dst);
637 for (i = 0; i < HUF_DECSIZE; i++)
639 av_freep(&hdec[i].p);
647 static inline void wdec14(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
652 int ai = ls + (hi & 1) + (hi >> 1);
654 int16_t bs = ai - hi;
661 #define A_OFFSET (1 << (NBITS - 1))
662 #define MOD_MASK ((1 << NBITS) - 1)
664 static inline void wdec16(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
668 int bb = (m - (d >> 1)) & MOD_MASK;
669 int aa = (d + bb - A_OFFSET) & MOD_MASK;
674 static void wav_decode(uint16_t *in, int nx, int ox,
675 int ny, int oy, uint16_t mx)
677 int w14 = (mx < (1 << 14));
678 int n = (nx > ny) ? ny : nx;
691 uint16_t *ey = in + oy * (ny - p2);
692 uint16_t i00, i01, i10, i11;
698 for (; py <= ey; py += oy2) {
700 uint16_t *ex = py + ox * (nx - p2);
702 for (; px <= ex; px += ox2) {
703 uint16_t *p01 = px + ox1;
704 uint16_t *p10 = px + oy1;
705 uint16_t *p11 = p10 + ox1;
708 wdec14(*px, *p10, &i00, &i10);
709 wdec14(*p01, *p11, &i01, &i11);
710 wdec14(i00, i01, px, p01);
711 wdec14(i10, i11, p10, p11);
713 wdec16(*px, *p10, &i00, &i10);
714 wdec16(*p01, *p11, &i01, &i11);
715 wdec16(i00, i01, px, p01);
716 wdec16(i10, i11, p10, p11);
721 uint16_t *p10 = px + oy1;
724 wdec14(*px, *p10, &i00, p10);
726 wdec16(*px, *p10, &i00, p10);
734 uint16_t *ex = py + ox * (nx - p2);
736 for (; px <= ex; px += ox2) {
737 uint16_t *p01 = px + ox1;
740 wdec14(*px, *p01, &i00, p01);
742 wdec16(*px, *p01, &i00, p01);
753 static int piz_uncompress(EXRContext *s, const uint8_t *src, int ssize,
754 int dsize, EXRThreadData *td)
757 uint16_t maxval, min_non_zero, max_non_zero;
759 uint16_t *tmp = (uint16_t *)td->tmp;
763 int pixel_half_size;/* 1 for half, 2 for float and uint32 */
768 td->bitmap = av_malloc(BITMAP_SIZE);
770 td->lut = av_malloc(1 << 17);
771 if (!td->bitmap || !td->lut) {
772 av_freep(&td->bitmap);
774 return AVERROR(ENOMEM);
777 bytestream2_init(&gb, src, ssize);
778 min_non_zero = bytestream2_get_le16(&gb);
779 max_non_zero = bytestream2_get_le16(&gb);
781 if (max_non_zero >= BITMAP_SIZE)
782 return AVERROR_INVALIDDATA;
784 memset(td->bitmap, 0, FFMIN(min_non_zero, BITMAP_SIZE));
785 if (min_non_zero <= max_non_zero)
786 bytestream2_get_buffer(&gb, td->bitmap + min_non_zero,
787 max_non_zero - min_non_zero + 1);
788 memset(td->bitmap + max_non_zero + 1, 0, BITMAP_SIZE - max_non_zero - 1);
790 maxval = reverse_lut(td->bitmap, td->lut);
792 ret = huf_uncompress(&gb, tmp, dsize / sizeof(uint16_t));
797 for (i = 0; i < s->nb_channels; i++) {
798 channel = &s->channels[i];
800 if (channel->pixel_type == EXR_HALF)
805 for (j = 0; j < pixel_half_size; j++)
806 wav_decode(ptr + j, td->xsize, pixel_half_size, td->ysize,
807 td->xsize * pixel_half_size, maxval);
808 ptr += td->xsize * td->ysize * pixel_half_size;
811 apply_lut(td->lut, tmp, dsize / sizeof(uint16_t));
813 out = (uint16_t *)td->uncompressed_data;
814 for (i = 0; i < td->ysize; i++) {
816 for (j = 0; j < s->nb_channels; j++) {
817 channel = &s->channels[j];
818 if (channel->pixel_type == EXR_HALF)
823 in = tmp + tmp_offset * td->xsize * td->ysize + i * td->xsize * pixel_half_size;
824 tmp_offset += pixel_half_size;
827 s->bbdsp.bswap16_buf(out, in, td->xsize * pixel_half_size);
829 memcpy(out, in, td->xsize * 2 * pixel_half_size);
831 out += td->xsize * pixel_half_size;
838 static int pxr24_uncompress(EXRContext *s, const uint8_t *src,
839 int compressed_size, int uncompressed_size,
842 unsigned long dest_len, expected_len = 0;
843 const uint8_t *in = td->tmp;
847 for (i = 0; i < s->nb_channels; i++) {
848 if (s->channels[i].pixel_type == EXR_FLOAT) {
849 expected_len += (td->xsize * td->ysize * 3);/* PRX 24 store float in 24 bit instead of 32 */
850 } else if (s->channels[i].pixel_type == EXR_HALF) {
851 expected_len += (td->xsize * td->ysize * 2);
853 expected_len += (td->xsize * td->ysize * 4);
857 dest_len = expected_len;
859 if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK) {
860 return AVERROR_INVALIDDATA;
861 } else if (dest_len != expected_len) {
862 return AVERROR_INVALIDDATA;
865 out = td->uncompressed_data;
866 for (i = 0; i < td->ysize; i++)
867 for (c = 0; c < s->nb_channels; c++) {
868 EXRChannel *channel = &s->channels[c];
869 const uint8_t *ptr[4];
872 switch (channel->pixel_type) {
875 ptr[1] = ptr[0] + td->xsize;
876 ptr[2] = ptr[1] + td->xsize;
877 in = ptr[2] + td->xsize;
879 for (j = 0; j < td->xsize; ++j) {
880 uint32_t diff = (*(ptr[0]++) << 24) |
881 (*(ptr[1]++) << 16) |
884 bytestream_put_le32(&out, pixel);
889 ptr[1] = ptr[0] + td->xsize;
890 in = ptr[1] + td->xsize;
891 for (j = 0; j < td->xsize; j++) {
892 uint32_t diff = (*(ptr[0]++) << 8) | *(ptr[1]++);
895 bytestream_put_le16(&out, pixel);
900 ptr[1] = ptr[0] + s->xdelta;
901 ptr[2] = ptr[1] + s->xdelta;
902 ptr[3] = ptr[2] + s->xdelta;
903 in = ptr[3] + s->xdelta;
905 for (j = 0; j < s->xdelta; ++j) {
906 uint32_t diff = (*(ptr[0]++) << 24) |
907 (*(ptr[1]++) << 16) |
908 (*(ptr[2]++) << 8 ) |
911 bytestream_put_le32(&out, pixel);
915 return AVERROR_INVALIDDATA;
922 static void unpack_14(const uint8_t b[14], uint16_t s[16])
924 unsigned short shift = (b[ 2] >> 2);
925 unsigned short bias = (0x20 << shift);
928 s[ 0] = (b[0] << 8) | b[1];
930 s[ 4] = s[ 0] + ((((b[ 2] << 4) | (b[ 3] >> 4)) & 0x3f) << shift) - bias;
931 s[ 8] = s[ 4] + ((((b[ 3] << 2) | (b[ 4] >> 6)) & 0x3f) << shift) - bias;
932 s[12] = s[ 8] + ((b[ 4] & 0x3f) << shift) - bias;
934 s[ 1] = s[ 0] + ((b[ 5] >> 2) << shift) - bias;
935 s[ 5] = s[ 4] + ((((b[ 5] << 4) | (b[ 6] >> 4)) & 0x3f) << shift) - bias;
936 s[ 9] = s[ 8] + ((((b[ 6] << 2) | (b[ 7] >> 6)) & 0x3f) << shift) - bias;
937 s[13] = s[12] + ((b[ 7] & 0x3f) << shift) - bias;
939 s[ 2] = s[ 1] + ((b[ 8] >> 2) << shift) - bias;
940 s[ 6] = s[ 5] + ((((b[ 8] << 4) | (b[ 9] >> 4)) & 0x3f) << shift) - bias;
941 s[10] = s[ 9] + ((((b[ 9] << 2) | (b[10] >> 6)) & 0x3f) << shift) - bias;
942 s[14] = s[13] + ((b[10] & 0x3f) << shift) - bias;
944 s[ 3] = s[ 2] + ((b[11] >> 2) << shift) - bias;
945 s[ 7] = s[ 6] + ((((b[11] << 4) | (b[12] >> 4)) & 0x3f) << shift) - bias;
946 s[11] = s[10] + ((((b[12] << 2) | (b[13] >> 6)) & 0x3f) << shift) - bias;
947 s[15] = s[14] + ((b[13] & 0x3f) << shift) - bias;
949 for (i = 0; i < 16; ++i) {
957 static void unpack_3(const uint8_t b[3], uint16_t s[16])
961 s[0] = (b[0] << 8) | b[1];
968 for (i = 1; i < 16; i++)
973 static int b44_uncompress(EXRContext *s, const uint8_t *src, int compressed_size,
974 int uncompressed_size, EXRThreadData *td) {
975 const int8_t *sr = src;
976 int stay_to_uncompress = compressed_size;
977 int nb_b44_block_w, nb_b44_block_h;
978 int index_tl_x, index_tl_y, index_out, index_tmp;
979 uint16_t tmp_buffer[16]; /* B44 use 4x4 half float pixel */
981 int target_channel_offset = 0;
983 /* calc B44 block count */
984 nb_b44_block_w = td->xsize / 4;
985 if ((td->xsize % 4) != 0)
988 nb_b44_block_h = td->ysize / 4;
989 if ((td->ysize % 4) != 0)
992 for (c = 0; c < s->nb_channels; c++) {
993 if (s->channels[c].pixel_type == EXR_HALF) {/* B44 only compress half float data */
994 for (iY = 0; iY < nb_b44_block_h; iY++) {
995 for (iX = 0; iX < nb_b44_block_w; iX++) {/* For each B44 block */
996 if (stay_to_uncompress < 3) {
997 av_log(s, AV_LOG_ERROR, "Not enough data for B44A block: %d", stay_to_uncompress);
998 return AVERROR_INVALIDDATA;
1001 if (src[compressed_size - stay_to_uncompress + 2] == 0xfc) { /* B44A block */
1002 unpack_3(sr, tmp_buffer);
1004 stay_to_uncompress -= 3;
1005 } else {/* B44 Block */
1006 if (stay_to_uncompress < 14) {
1007 av_log(s, AV_LOG_ERROR, "Not enough data for B44 block: %d", stay_to_uncompress);
1008 return AVERROR_INVALIDDATA;
1010 unpack_14(sr, tmp_buffer);
1012 stay_to_uncompress -= 14;
1015 /* copy data to uncompress buffer (B44 block can exceed target resolution)*/
1016 index_tl_x = iX * 4;
1017 index_tl_y = iY * 4;
1019 for (y = index_tl_y; y < FFMIN(index_tl_y + 4, td->ysize); y++) {
1020 for (x = index_tl_x; x < FFMIN(index_tl_x + 4, td->xsize); x++) {
1021 index_out = target_channel_offset * td->xsize + y * td->channel_line_size + 2 * x;
1022 index_tmp = (y-index_tl_y) * 4 + (x-index_tl_x);
1023 td->uncompressed_data[index_out] = tmp_buffer[index_tmp] & 0xff;
1024 td->uncompressed_data[index_out + 1] = tmp_buffer[index_tmp] >> 8;
1029 target_channel_offset += 2;
1030 } else {/* Float or UINT 32 channel */
1031 if (stay_to_uncompress < td->ysize * td->xsize * 4) {
1032 av_log(s, AV_LOG_ERROR, "Not enough data for uncompress channel: %d", stay_to_uncompress);
1033 return AVERROR_INVALIDDATA;
1036 for (y = 0; y < td->ysize; y++) {
1037 index_out = target_channel_offset * td->xsize + y * td->channel_line_size;
1038 memcpy(&td->uncompressed_data[index_out], sr, td->xsize * 4);
1039 sr += td->xsize * 4;
1041 target_channel_offset += 4;
1043 stay_to_uncompress -= td->ysize * td->xsize * 4;
1050 static int decode_block(AVCodecContext *avctx, void *tdata,
1051 int jobnr, int threadnr)
1053 EXRContext *s = avctx->priv_data;
1054 AVFrame *const p = s->picture;
1055 EXRThreadData *td = &s->thread_data[threadnr];
1056 const uint8_t *channel_buffer[4] = { 0 };
1057 const uint8_t *buf = s->buf;
1058 uint64_t line_offset, uncompressed_size;
1062 uint64_t line, col = 0;
1063 uint64_t tile_x, tile_y, tile_level_x, tile_level_y;
1065 int axmax = (avctx->width - (s->xmax + 1)) * 2 * s->desc->nb_components; /* nb pixel to add at the right of the datawindow */
1066 int bxmin = s->xmin * 2 * s->desc->nb_components; /* nb pixel to add at the left of the datawindow */
1067 int i, x, buf_size = s->buf_size;
1068 int c, rgb_channel_count;
1069 float one_gamma = 1.0f / s->gamma;
1070 avpriv_trc_function trc_func = avpriv_get_trc_function_from_trc(s->apply_trc_type);
1073 line_offset = AV_RL64(s->gb.buffer + jobnr * 8);
1076 if (line_offset > buf_size - 20)
1077 return AVERROR_INVALIDDATA;
1079 src = buf + line_offset + 20;
1081 tile_x = AV_RL32(src - 20);
1082 tile_y = AV_RL32(src - 16);
1083 tile_level_x = AV_RL32(src - 12);
1084 tile_level_y = AV_RL32(src - 8);
1086 data_size = AV_RL32(src - 4);
1087 if (data_size <= 0 || data_size > buf_size)
1088 return AVERROR_INVALIDDATA;
1090 if (tile_level_x || tile_level_y) { /* tile level, is not the full res level */
1091 avpriv_report_missing_feature(s->avctx, "Subres tile before full res tile");
1092 return AVERROR_PATCHWELCOME;
1095 if (s->xmin || s->ymin) {
1096 avpriv_report_missing_feature(s->avctx, "Tiles with xmin/ymin");
1097 return AVERROR_PATCHWELCOME;
1100 line = s->tile_attr.ySize * tile_y;
1101 col = s->tile_attr.xSize * tile_x;
1103 if (line < s->ymin || line > s->ymax ||
1104 col < s->xmin || col > s->xmax)
1105 return AVERROR_INVALIDDATA;
1107 td->ysize = FFMIN(s->tile_attr.ySize, s->ydelta - tile_y * s->tile_attr.ySize);
1108 td->xsize = FFMIN(s->tile_attr.xSize, s->xdelta - tile_x * s->tile_attr.xSize);
1110 if (col) { /* not the first tile of the line */
1111 bxmin = 0; /* doesn't add pixel at the left of the datawindow */
1114 if ((col + td->xsize) != s->xdelta)/* not the last tile of the line */
1115 axmax = 0; /* doesn't add pixel at the right of the datawindow */
1117 td->channel_line_size = td->xsize * s->current_channel_offset;/* uncompress size of one line */
1118 uncompressed_size = td->channel_line_size * (uint64_t)td->ysize;/* uncompress size of the block */
1120 if (line_offset > buf_size - 8)
1121 return AVERROR_INVALIDDATA;
1123 src = buf + line_offset + 8;
1124 line = AV_RL32(src - 8);
1126 if (line < s->ymin || line > s->ymax)
1127 return AVERROR_INVALIDDATA;
1129 data_size = AV_RL32(src - 4);
1130 if (data_size <= 0 || data_size > buf_size)
1131 return AVERROR_INVALIDDATA;
1133 td->ysize = FFMIN(s->scan_lines_per_block, s->ymax - line + 1); /* s->ydelta - line ?? */
1134 td->xsize = s->xdelta;
1136 td->channel_line_size = td->xsize * s->current_channel_offset;/* uncompress size of one line */
1137 uncompressed_size = td->channel_line_size * (uint64_t)td->ysize;/* uncompress size of the block */
1139 if ((s->compression == EXR_RAW && (data_size != uncompressed_size ||
1140 line_offset > buf_size - uncompressed_size)) ||
1141 (s->compression != EXR_RAW && (data_size > uncompressed_size ||
1142 line_offset > buf_size - data_size))) {
1143 return AVERROR_INVALIDDATA;
1147 if (data_size < uncompressed_size || s->is_tile) { /* td->tmp is use for tile reorganization */
1148 av_fast_padded_malloc(&td->tmp, &td->tmp_size, uncompressed_size);
1150 return AVERROR(ENOMEM);
1153 if (data_size < uncompressed_size) {
1154 av_fast_padded_malloc(&td->uncompressed_data,
1155 &td->uncompressed_size, uncompressed_size);
1157 if (!td->uncompressed_data)
1158 return AVERROR(ENOMEM);
1160 ret = AVERROR_INVALIDDATA;
1161 switch (s->compression) {
1164 ret = zip_uncompress(src, data_size, uncompressed_size, td);
1167 ret = piz_uncompress(s, src, data_size, uncompressed_size, td);
1170 ret = pxr24_uncompress(s, src, data_size, uncompressed_size, td);
1173 ret = rle_uncompress(src, data_size, uncompressed_size, td);
1177 ret = b44_uncompress(s, src, data_size, uncompressed_size, td);
1181 av_log(avctx, AV_LOG_ERROR, "decode_block() failed.\n");
1184 src = td->uncompressed_data;
1188 channel_buffer[0] = src + td->xsize * s->channel_offsets[0];
1189 channel_buffer[1] = src + td->xsize * s->channel_offsets[1];
1190 channel_buffer[2] = src + td->xsize * s->channel_offsets[2];
1191 rgb_channel_count = 3;
1192 } else { /* put y data in the first channel_buffer */
1193 channel_buffer[0] = src + td->xsize * s->channel_offsets[1];
1194 rgb_channel_count = 1;
1196 if (s->channel_offsets[3] >= 0)
1197 channel_buffer[3] = src + td->xsize * s->channel_offsets[3];
1199 ptr = p->data[0] + line * p->linesize[0] + (col * s->desc->nb_components * 2);
1202 i < td->ysize; i++, ptr += p->linesize[0]) {
1205 const uint8_t *rgb[3];
1207 for (c = 0; c < rgb_channel_count; c++){
1208 rgb[c] = channel_buffer[c];
1211 if (channel_buffer[3])
1212 a = channel_buffer[3];
1214 ptr_x = (uint16_t *) ptr;
1216 // Zero out the start if xmin is not 0
1217 memset(ptr_x, 0, bxmin);
1218 ptr_x += s->xmin * s->desc->nb_components;
1220 if (s->pixel_type == EXR_FLOAT) {
1223 for (x = 0; x < td->xsize; x++) {
1224 union av_intfloat32 t;
1226 for (c = 0; c < rgb_channel_count; c++) {
1227 t.i = bytestream_get_le32(&rgb[c]);
1228 t.f = trc_func(t.f);
1229 *ptr_x++ = exr_flt2uint(t.i);
1231 if (channel_buffer[3])
1232 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a));
1235 for (x = 0; x < td->xsize; x++) {
1236 union av_intfloat32 t;
1239 for (c = 0; c < rgb_channel_count; c++) {
1240 t.i = bytestream_get_le32(&rgb[c]);
1241 if (t.f > 0.0f) /* avoid negative values */
1242 t.f = powf(t.f, one_gamma);
1243 *ptr_x++ = exr_flt2uint(t.i);
1246 if (channel_buffer[3])
1247 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a));
1250 } else if (s->pixel_type == EXR_HALF) {
1252 for (x = 0; x < td->xsize; x++) {
1254 for (c = 0; c < rgb_channel_count; c++) {
1255 *ptr_x++ = s->gamma_table[bytestream_get_le16(&rgb[c])];
1258 if (channel_buffer[3])
1259 *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&a));
1261 } else if (s->pixel_type == EXR_UINT) {
1262 for (x = 0; x < td->xsize; x++) {
1263 for (c = 0; c < rgb_channel_count; c++) {
1264 *ptr_x++ = bytestream_get_le32(&rgb[c]) >> 16;
1267 if (channel_buffer[3])
1268 *ptr_x++ = bytestream_get_le32(&a) >> 16;
1272 // Zero out the end if xmax+1 is not w
1273 memset(ptr_x, 0, axmax);
1275 channel_buffer[0] += td->channel_line_size;
1276 channel_buffer[1] += td->channel_line_size;
1277 channel_buffer[2] += td->channel_line_size;
1278 if (channel_buffer[3])
1279 channel_buffer[3] += td->channel_line_size;
1286 * Check if the variable name corresponds to its data type.
1288 * @param s the EXRContext
1289 * @param value_name name of the variable to check
1290 * @param value_type type of the variable to check
1291 * @param minimum_length minimum length of the variable data
1293 * @return bytes to read containing variable data
1294 * -1 if variable is not found
1295 * 0 if buffer ended prematurely
1297 static int check_header_variable(EXRContext *s,
1298 const char *value_name,
1299 const char *value_type,
1300 unsigned int minimum_length)
1304 if (bytestream2_get_bytes_left(&s->gb) >= minimum_length &&
1305 !strcmp(s->gb.buffer, value_name)) {
1306 // found value_name, jump to value_type (null terminated strings)
1307 s->gb.buffer += strlen(value_name) + 1;
1308 if (!strcmp(s->gb.buffer, value_type)) {
1309 s->gb.buffer += strlen(value_type) + 1;
1310 var_size = bytestream2_get_le32(&s->gb);
1311 // don't go read past boundaries
1312 if (var_size > bytestream2_get_bytes_left(&s->gb))
1315 // value_type not found, reset the buffer
1316 s->gb.buffer -= strlen(value_name) + 1;
1317 av_log(s->avctx, AV_LOG_WARNING,
1318 "Unknown data type %s for header variable %s.\n",
1319 value_type, value_name);
1326 static int decode_header(EXRContext *s, AVFrame *frame)
1328 AVDictionary *metadata = NULL;
1329 int magic_number, version, i, flags, sar = 0;
1330 int layer_match = 0;
1332 s->current_channel_offset = 0;
1339 s->channel_offsets[0] = -1;
1340 s->channel_offsets[1] = -1;
1341 s->channel_offsets[2] = -1;
1342 s->channel_offsets[3] = -1;
1343 s->pixel_type = EXR_UNKNOWN;
1344 s->compression = EXR_UNKN;
1348 s->tile_attr.xSize = -1;
1349 s->tile_attr.ySize = -1;
1353 if (bytestream2_get_bytes_left(&s->gb) < 10) {
1354 av_log(s->avctx, AV_LOG_ERROR, "Header too short to parse.\n");
1355 return AVERROR_INVALIDDATA;
1358 magic_number = bytestream2_get_le32(&s->gb);
1359 if (magic_number != 20000630) {
1360 /* As per documentation of OpenEXR, it is supposed to be
1361 * int 20000630 little-endian */
1362 av_log(s->avctx, AV_LOG_ERROR, "Wrong magic number %d.\n", magic_number);
1363 return AVERROR_INVALIDDATA;
1366 version = bytestream2_get_byte(&s->gb);
1368 avpriv_report_missing_feature(s->avctx, "Version %d", version);
1369 return AVERROR_PATCHWELCOME;
1372 flags = bytestream2_get_le24(&s->gb);
1376 else if (flags & 0x02)
1379 avpriv_report_missing_feature(s->avctx, "flags %d", flags);
1380 return AVERROR_PATCHWELCOME;
1384 while (bytestream2_get_bytes_left(&s->gb) > 0 && *s->gb.buffer) {
1386 if ((var_size = check_header_variable(s, "channels",
1387 "chlist", 38)) >= 0) {
1388 GetByteContext ch_gb;
1390 return AVERROR_INVALIDDATA;
1392 bytestream2_init(&ch_gb, s->gb.buffer, var_size);
1394 while (bytestream2_get_bytes_left(&ch_gb) >= 19) {
1395 EXRChannel *channel;
1396 enum ExrPixelType current_pixel_type;
1397 int channel_index = -1;
1400 if (strcmp(s->layer, "") != 0) {
1401 if (strncmp(ch_gb.buffer, s->layer, strlen(s->layer)) == 0) {
1403 av_log(s->avctx, AV_LOG_INFO,
1404 "Channel match layer : %s.\n", ch_gb.buffer);
1405 ch_gb.buffer += strlen(s->layer);
1406 if (*ch_gb.buffer == '.')
1407 ch_gb.buffer++; /* skip dot if not given */
1409 av_log(s->avctx, AV_LOG_INFO,
1410 "Channel doesn't match layer : %s.\n", ch_gb.buffer);
1416 if (layer_match) { /* only search channel if the layer match is valid */
1417 if (!strcmp(ch_gb.buffer, "R") ||
1418 !strcmp(ch_gb.buffer, "X") ||
1419 !strcmp(ch_gb.buffer, "U")) {
1422 } else if (!strcmp(ch_gb.buffer, "G") ||
1423 !strcmp(ch_gb.buffer, "V")) {
1426 } else if (!strcmp(ch_gb.buffer, "Y")) {
1429 } else if (!strcmp(ch_gb.buffer, "B") ||
1430 !strcmp(ch_gb.buffer, "Z") ||
1431 !strcmp(ch_gb.buffer, "W")){
1434 } else if (!strcmp(ch_gb.buffer, "A")) {
1437 av_log(s->avctx, AV_LOG_WARNING,
1438 "Unsupported channel %.256s.\n", ch_gb.buffer);
1442 /* skip until you get a 0 */
1443 while (bytestream2_get_bytes_left(&ch_gb) > 0 &&
1444 bytestream2_get_byte(&ch_gb))
1447 if (bytestream2_get_bytes_left(&ch_gb) < 4) {
1448 av_log(s->avctx, AV_LOG_ERROR, "Incomplete header.\n");
1449 return AVERROR_INVALIDDATA;
1452 current_pixel_type = bytestream2_get_le32(&ch_gb);
1453 if (current_pixel_type >= EXR_UNKNOWN) {
1454 avpriv_report_missing_feature(s->avctx, "Pixel type %d",
1455 current_pixel_type);
1456 return AVERROR_PATCHWELCOME;
1459 bytestream2_skip(&ch_gb, 4);
1460 xsub = bytestream2_get_le32(&ch_gb);
1461 ysub = bytestream2_get_le32(&ch_gb);
1463 if (xsub != 1 || ysub != 1) {
1464 avpriv_report_missing_feature(s->avctx,
1465 "Subsampling %dx%d",
1467 return AVERROR_PATCHWELCOME;
1470 if (channel_index >= 0 && s->channel_offsets[channel_index] == -1) { /* channel has not been previously assigned */
1471 if (s->pixel_type != EXR_UNKNOWN &&
1472 s->pixel_type != current_pixel_type) {
1473 av_log(s->avctx, AV_LOG_ERROR,
1474 "RGB channels not of the same depth.\n");
1475 return AVERROR_INVALIDDATA;
1477 s->pixel_type = current_pixel_type;
1478 s->channel_offsets[channel_index] = s->current_channel_offset;
1481 s->channels = av_realloc(s->channels,
1482 ++s->nb_channels * sizeof(EXRChannel));
1484 return AVERROR(ENOMEM);
1485 channel = &s->channels[s->nb_channels - 1];
1486 channel->pixel_type = current_pixel_type;
1487 channel->xsub = xsub;
1488 channel->ysub = ysub;
1490 if (current_pixel_type == EXR_HALF) {
1491 s->current_channel_offset += 2;
1492 } else {/* Float or UINT32 */
1493 s->current_channel_offset += 4;
1497 /* Check if all channels are set with an offset or if the channels
1498 * are causing an overflow */
1499 if (!s->is_luma){/* if we expected to have at least 3 channels */
1500 if (FFMIN3(s->channel_offsets[0],
1501 s->channel_offsets[1],
1502 s->channel_offsets[2]) < 0) {
1503 if (s->channel_offsets[0] < 0)
1504 av_log(s->avctx, AV_LOG_ERROR, "Missing red channel.\n");
1505 if (s->channel_offsets[1] < 0)
1506 av_log(s->avctx, AV_LOG_ERROR, "Missing green channel.\n");
1507 if (s->channel_offsets[2] < 0)
1508 av_log(s->avctx, AV_LOG_ERROR, "Missing blue channel.\n");
1509 return AVERROR_INVALIDDATA;
1513 // skip one last byte and update main gb
1514 s->gb.buffer = ch_gb.buffer + 1;
1516 } else if ((var_size = check_header_variable(s, "dataWindow", "box2i",
1519 return AVERROR_INVALIDDATA;
1521 s->xmin = bytestream2_get_le32(&s->gb);
1522 s->ymin = bytestream2_get_le32(&s->gb);
1523 s->xmax = bytestream2_get_le32(&s->gb);
1524 s->ymax = bytestream2_get_le32(&s->gb);
1525 s->xdelta = (s->xmax - s->xmin) + 1;
1526 s->ydelta = (s->ymax - s->ymin) + 1;
1529 } else if ((var_size = check_header_variable(s, "displayWindow",
1530 "box2i", 34)) >= 0) {
1532 return AVERROR_INVALIDDATA;
1534 bytestream2_skip(&s->gb, 8);
1535 s->w = bytestream2_get_le32(&s->gb) + 1;
1536 s->h = bytestream2_get_le32(&s->gb) + 1;
1539 } else if ((var_size = check_header_variable(s, "lineOrder",
1540 "lineOrder", 25)) >= 0) {
1543 return AVERROR_INVALIDDATA;
1545 line_order = bytestream2_get_byte(&s->gb);
1546 av_log(s->avctx, AV_LOG_DEBUG, "line order: %d.\n", line_order);
1547 if (line_order > 2) {
1548 av_log(s->avctx, AV_LOG_ERROR, "Unknown line order.\n");
1549 return AVERROR_INVALIDDATA;
1553 } else if ((var_size = check_header_variable(s, "pixelAspectRatio",
1554 "float", 31)) >= 0) {
1556 return AVERROR_INVALIDDATA;
1558 sar = bytestream2_get_le32(&s->gb);
1561 } else if ((var_size = check_header_variable(s, "compression",
1562 "compression", 29)) >= 0) {
1564 return AVERROR_INVALIDDATA;
1566 if (s->compression == EXR_UNKN)
1567 s->compression = bytestream2_get_byte(&s->gb);
1569 av_log(s->avctx, AV_LOG_WARNING,
1570 "Found more than one compression attribute.\n");
1573 } else if ((var_size = check_header_variable(s, "tiles",
1574 "tiledesc", 22)) >= 0) {
1578 av_log(s->avctx, AV_LOG_WARNING,
1579 "Found tile attribute and scanline flags. Exr will be interpreted as scanline.\n");
1581 s->tile_attr.xSize = bytestream2_get_le32(&s->gb);
1582 s->tile_attr.ySize = bytestream2_get_le32(&s->gb);
1584 tileLevel = bytestream2_get_byte(&s->gb);
1585 s->tile_attr.level_mode = tileLevel & 0x0f;
1586 s->tile_attr.level_round = (tileLevel >> 4) & 0x0f;
1588 if (s->tile_attr.level_mode >= EXR_TILE_LEVEL_UNKNOWN){
1589 avpriv_report_missing_feature(s->avctx, "Tile level mode %d",
1590 s->tile_attr.level_mode);
1591 return AVERROR_PATCHWELCOME;
1594 if (s->tile_attr.level_round >= EXR_TILE_ROUND_UNKNOWN) {
1595 avpriv_report_missing_feature(s->avctx, "Tile level round %d",
1596 s->tile_attr.level_round);
1597 return AVERROR_PATCHWELCOME;
1601 } else if ((var_size = check_header_variable(s, "writer",
1602 "string", 1)) >= 0) {
1603 uint8_t key[256] = { 0 };
1605 bytestream2_get_buffer(&s->gb, key, FFMIN(sizeof(key) - 1, var_size));
1606 av_dict_set(&metadata, "writer", key, 0);
1611 // Check if there are enough bytes for a header
1612 if (bytestream2_get_bytes_left(&s->gb) <= 9) {
1613 av_log(s->avctx, AV_LOG_ERROR, "Incomplete header\n");
1614 return AVERROR_INVALIDDATA;
1617 // Process unknown variables
1618 for (i = 0; i < 2; i++) // value_name and value_type
1619 while (bytestream2_get_byte(&s->gb) != 0);
1621 // Skip variable length
1622 bytestream2_skip(&s->gb, bytestream2_get_le32(&s->gb));
1625 ff_set_sar(s->avctx, av_d2q(av_int2float(sar), 255));
1627 if (s->compression == EXR_UNKN) {
1628 av_log(s->avctx, AV_LOG_ERROR, "Missing compression attribute.\n");
1629 return AVERROR_INVALIDDATA;
1633 if (s->tile_attr.xSize < 1 || s->tile_attr.ySize < 1) {
1634 av_log(s->avctx, AV_LOG_ERROR, "Invalid tile attribute.\n");
1635 return AVERROR_INVALIDDATA;
1639 if (bytestream2_get_bytes_left(&s->gb) <= 0) {
1640 av_log(s->avctx, AV_LOG_ERROR, "Incomplete frame.\n");
1641 return AVERROR_INVALIDDATA;
1644 frame->metadata = metadata;
1646 // aaand we are done
1647 bytestream2_skip(&s->gb, 1);
1651 static int decode_frame(AVCodecContext *avctx, void *data,
1652 int *got_frame, AVPacket *avpkt)
1654 EXRContext *s = avctx->priv_data;
1655 ThreadFrame frame = { .f = data };
1656 AVFrame *picture = data;
1661 int nb_blocks; /* nb scanline or nb tile */
1662 uint64_t start_offset_table;
1663 uint64_t start_next_scanline;
1664 PutByteContext offset_table_writer;
1666 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
1668 if ((ret = decode_header(s, picture)) < 0)
1671 switch (s->pixel_type) {
1675 if (s->channel_offsets[3] >= 0) {
1677 avctx->pix_fmt = AV_PIX_FMT_RGBA64;
1679 avctx->pix_fmt = AV_PIX_FMT_YA16;
1683 avctx->pix_fmt = AV_PIX_FMT_RGB48;
1685 avctx->pix_fmt = AV_PIX_FMT_GRAY16;
1690 av_log(avctx, AV_LOG_ERROR, "Missing channel list.\n");
1691 return AVERROR_INVALIDDATA;
1694 if (s->apply_trc_type != AVCOL_TRC_UNSPECIFIED)
1695 avctx->color_trc = s->apply_trc_type;
1697 switch (s->compression) {
1701 s->scan_lines_per_block = 1;
1705 s->scan_lines_per_block = 16;
1710 s->scan_lines_per_block = 32;
1713 avpriv_report_missing_feature(avctx, "Compression %d", s->compression);
1714 return AVERROR_PATCHWELCOME;
1717 /* Verify the xmin, xmax, ymin, ymax and xdelta before setting
1718 * the actual image size. */
1719 if (s->xmin > s->xmax ||
1720 s->ymin > s->ymax ||
1721 s->xdelta != s->xmax - s->xmin + 1 ||
1724 av_log(avctx, AV_LOG_ERROR, "Wrong or missing size information.\n");
1725 return AVERROR_INVALIDDATA;
1728 if ((ret = ff_set_dimensions(avctx, s->w, s->h)) < 0)
1731 s->desc = av_pix_fmt_desc_get(avctx->pix_fmt);
1733 return AVERROR_INVALIDDATA;
1734 out_line_size = avctx->width * 2 * s->desc->nb_components;
1737 nb_blocks = ((s->xdelta + s->tile_attr.xSize - 1) / s->tile_attr.xSize) *
1738 ((s->ydelta + s->tile_attr.ySize - 1) / s->tile_attr.ySize);
1739 } else { /* scanline */
1740 nb_blocks = (s->ydelta + s->scan_lines_per_block - 1) /
1741 s->scan_lines_per_block;
1744 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
1747 if (bytestream2_get_bytes_left(&s->gb) < nb_blocks * 8)
1748 return AVERROR_INVALIDDATA;
1750 // check offset table and recreate it if need
1751 if (!s->is_tile && bytestream2_peek_le64(&s->gb) == 0) {
1752 av_log(s->avctx, AV_LOG_DEBUG, "recreating invalid scanline offset table\n");
1754 start_offset_table = bytestream2_tell(&s->gb);
1755 start_next_scanline = start_offset_table + nb_blocks * 8;
1756 bytestream2_init_writer(&offset_table_writer, &avpkt->data[start_offset_table], nb_blocks * 8);
1758 for (y = 0; y < nb_blocks; y++) {
1759 /* write offset of prev scanline in offset table */
1760 bytestream2_put_le64(&offset_table_writer, start_next_scanline);
1762 /* get len of next scanline */
1763 bytestream2_seek(&s->gb, start_next_scanline + 4, SEEK_SET);/* skip line number */
1764 start_next_scanline += (bytestream2_get_le32(&s->gb) + 8);
1766 bytestream2_seek(&s->gb, start_offset_table, SEEK_SET);
1769 // save pointer we are going to use in decode_block
1770 s->buf = avpkt->data;
1771 s->buf_size = avpkt->size;
1772 ptr = picture->data[0];
1774 // Zero out the start if ymin is not 0
1775 for (y = 0; y < s->ymin; y++) {
1776 memset(ptr, 0, out_line_size);
1777 ptr += picture->linesize[0];
1780 s->picture = picture;
1782 avctx->execute2(avctx, decode_block, s->thread_data, NULL, nb_blocks);
1784 // Zero out the end if ymax+1 is not h
1785 ptr = picture->data[0] + ((s->ymax+1) * picture->linesize[0]);
1786 for (y = s->ymax + 1; y < avctx->height; y++) {
1787 memset(ptr, 0, out_line_size);
1788 ptr += picture->linesize[0];
1791 picture->pict_type = AV_PICTURE_TYPE_I;
1797 static av_cold int decode_init(AVCodecContext *avctx)
1799 EXRContext *s = avctx->priv_data;
1801 union av_intfloat32 t;
1802 float one_gamma = 1.0f / s->gamma;
1803 avpriv_trc_function trc_func = NULL;
1808 ff_bswapdsp_init(&s->bbdsp);
1811 trc_func = avpriv_get_trc_function_from_trc(s->apply_trc_type);
1813 for (i = 0; i < 65536; ++i) {
1814 t = exr_half2float(i);
1815 t.f = trc_func(t.f);
1816 s->gamma_table[i] = exr_flt2uint(t.i);
1819 if (one_gamma > 0.9999f && one_gamma < 1.0001f) {
1820 for (i = 0; i < 65536; ++i)
1821 s->gamma_table[i] = exr_halflt2uint(i);
1823 for (i = 0; i < 65536; ++i) {
1824 t = exr_half2float(i);
1825 /* If negative value we reuse half value */
1827 s->gamma_table[i] = exr_halflt2uint(i);
1829 t.f = powf(t.f, one_gamma);
1830 s->gamma_table[i] = exr_flt2uint(t.i);
1836 // allocate thread data, used for non EXR_RAW compression types
1837 s->thread_data = av_mallocz_array(avctx->thread_count, sizeof(EXRThreadData));
1838 if (!s->thread_data)
1839 return AVERROR_INVALIDDATA;
1845 static int decode_init_thread_copy(AVCodecContext *avctx)
1846 { EXRContext *s = avctx->priv_data;
1848 // allocate thread data, used for non EXR_RAW compression types
1849 s->thread_data = av_mallocz_array(avctx->thread_count, sizeof(EXRThreadData));
1850 if (!s->thread_data)
1851 return AVERROR_INVALIDDATA;
1857 static av_cold int decode_end(AVCodecContext *avctx)
1859 EXRContext *s = avctx->priv_data;
1861 for (i = 0; i < avctx->thread_count; i++) {
1862 EXRThreadData *td = &s->thread_data[i];
1863 av_freep(&td->uncompressed_data);
1865 av_freep(&td->bitmap);
1869 av_freep(&s->thread_data);
1870 av_freep(&s->channels);
1875 #define OFFSET(x) offsetof(EXRContext, x)
1876 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1877 static const AVOption options[] = {
1878 { "layer", "Set the decoding layer", OFFSET(layer),
1879 AV_OPT_TYPE_STRING, { .str = "" }, 0, 0, VD },
1880 { "gamma", "Set the float gamma value when decoding", OFFSET(gamma),
1881 AV_OPT_TYPE_FLOAT, { .dbl = 1.0f }, 0.001, FLT_MAX, VD },
1883 // XXX: Note the abuse of the enum using AVCOL_TRC_UNSPECIFIED to subsume the existing gamma option
1884 { "apply_trc", "color transfer characteristics to apply to EXR linear input", OFFSET(apply_trc_type),
1885 AV_OPT_TYPE_INT, {.i64 = AVCOL_TRC_UNSPECIFIED }, 1, AVCOL_TRC_NB-1, VD, "apply_trc_type"},
1886 { "bt709", "BT.709", 0,
1887 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT709 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1888 { "gamma", "gamma", 0,
1889 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_UNSPECIFIED }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1890 { "gamma22", "BT.470 M", 0,
1891 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_GAMMA22 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1892 { "gamma28", "BT.470 BG", 0,
1893 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_GAMMA28 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1894 { "smpte170m", "SMPTE 170 M", 0,
1895 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTE170M }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1896 { "smpte240m", "SMPTE 240 M", 0,
1897 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTE240M }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1898 { "linear", "Linear", 0,
1899 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LINEAR }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1901 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LOG }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1902 { "log_sqrt", "Log square root", 0,
1903 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LOG_SQRT }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1904 { "iec61966_2_4", "IEC 61966-2-4", 0,
1905 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_IEC61966_2_4 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1906 { "bt1361", "BT.1361", 0,
1907 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT1361_ECG }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1908 { "iec61966_2_1", "IEC 61966-2-1", 0,
1909 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_IEC61966_2_1 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1910 { "bt2020_10bit", "BT.2020 - 10 bit", 0,
1911 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT2020_10 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1912 { "bt2020_12bit", "BT.2020 - 12 bit", 0,
1913 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT2020_12 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1914 { "smpte2084", "SMPTE ST 2084", 0,
1915 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTEST2084 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1916 { "smpte428_1", "SMPTE ST 428-1", 0,
1917 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTEST428_1 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1922 static const AVClass exr_class = {
1923 .class_name = "EXR",
1924 .item_name = av_default_item_name,
1926 .version = LIBAVUTIL_VERSION_INT,
1929 AVCodec ff_exr_decoder = {
1931 .long_name = NULL_IF_CONFIG_SMALL("OpenEXR image"),
1932 .type = AVMEDIA_TYPE_VIDEO,
1933 .id = AV_CODEC_ID_EXR,
1934 .priv_data_size = sizeof(EXRContext),
1935 .init = decode_init,
1936 .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
1937 .close = decode_end,
1938 .decode = decode_frame,
1939 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS |
1940 AV_CODEC_CAP_SLICE_THREADS,
1941 .priv_class = &exr_class,