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/avstring.h"
45 #include "libavutil/opt.h"
46 #include "libavutil/color_utils.h"
49 #include "bytestream.h"
82 enum ExrTileLevelMode {
84 EXR_TILE_LEVEL_MIPMAP,
85 EXR_TILE_LEVEL_RIPMAP,
86 EXR_TILE_LEVEL_UNKNOWN,
89 enum ExrTileLevelRound {
92 EXR_TILE_ROUND_UNKNOWN,
95 typedef struct EXRChannel {
97 enum ExrPixelType pixel_type;
100 typedef struct EXRTileAttribute {
103 enum ExrTileLevelMode level_mode;
104 enum ExrTileLevelRound level_round;
107 typedef struct EXRThreadData {
108 uint8_t *uncompressed_data;
109 int uncompressed_size;
119 int channel_line_size;
122 typedef struct EXRContext {
125 AVCodecContext *avctx;
129 BswapDSPContext bbdsp;
132 enum ExrCompr compression;
133 enum ExrPixelType pixel_type;
134 int channel_offsets[4]; // 0 = red, 1 = green, 2 = blue and 3 = alpha
135 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 uint16_t 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;
198 mantissa = (1 << 23) - 1; // set all bits to indicate a NaN
199 } else if (exp == 0x0) {
200 // convert half-float zero/denorm to single precision value
203 exp = HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP;
204 // check for leading 1 in denorm mantissa
205 while ((mantissa & (1 << 10))) {
206 // for every leading 0, decrement single precision exponent by 1
207 // and shift half-float mantissa value to the left
211 // clamp the mantissa to 10 bits
212 mantissa &= ((1 << 10) - 1);
213 // shift left to generate single-precision mantissa of 23 bits
217 // shift left to generate single-precision mantissa of 23 bits
219 // generate single precision biased exponent value
220 exp = (exp << 13) + HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP;
223 f.i = (sign << 31) | exp | mantissa;
230 * Convert from 32-bit float as uint32_t to uint16_t.
232 * @param v 32-bit float
234 * @return normalized 16-bit unsigned int
236 static inline uint16_t exr_flt2uint(int32_t v)
238 int32_t exp = v >> 23;
239 // "HACK": negative values result in exp< 0, so clipping them to 0
240 // is also handled by this condition, avoids explicit check for sign bit.
241 if (exp <= 127 + 7 - 24) // we would shift out all bits anyway
246 return (v + (1 << 23)) >> (127 + 7 - exp);
250 * Convert from 16-bit float as uint16_t to uint16_t.
252 * @param v 16-bit float
254 * @return normalized 16-bit unsigned int
256 static inline uint16_t exr_halflt2uint(uint16_t v)
258 unsigned exp = 14 - (v >> 10);
263 return (v & 0x8000) ? 0 : 0xffff;
266 return (v + (1 << 16)) >> (exp + 1);
269 static int zip_uncompress(EXRContext *s, const uint8_t *src, int compressed_size,
270 int uncompressed_size, EXRThreadData *td)
272 unsigned long dest_len = uncompressed_size;
274 if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK ||
275 dest_len != uncompressed_size)
276 return AVERROR_INVALIDDATA;
278 av_assert1(uncompressed_size % 2 == 0);
280 s->dsp.predictor(td->tmp, uncompressed_size);
281 s->dsp.reorder_pixels(td->uncompressed_data, td->tmp, uncompressed_size);
286 static int rle_uncompress(EXRContext *ctx, const uint8_t *src, int compressed_size,
287 int uncompressed_size, EXRThreadData *td)
289 uint8_t *d = td->tmp;
290 const int8_t *s = src;
291 int ssize = compressed_size;
292 int dsize = uncompressed_size;
293 uint8_t *dend = d + dsize;
302 if ((dsize -= count) < 0 ||
303 (ssize -= count + 1) < 0)
304 return AVERROR_INVALIDDATA;
311 if ((dsize -= count) < 0 ||
313 return AVERROR_INVALIDDATA;
323 return AVERROR_INVALIDDATA;
325 av_assert1(uncompressed_size % 2 == 0);
327 ctx->dsp.predictor(td->tmp, uncompressed_size);
328 ctx->dsp.reorder_pixels(td->uncompressed_data, td->tmp, uncompressed_size);
333 #define USHORT_RANGE (1 << 16)
334 #define BITMAP_SIZE (1 << 13)
336 static uint16_t reverse_lut(const uint8_t *bitmap, uint16_t *lut)
340 for (i = 0; i < USHORT_RANGE; i++)
341 if ((i == 0) || (bitmap[i >> 3] & (1 << (i & 7))))
346 memset(lut + k, 0, (USHORT_RANGE - k) * 2);
351 static void apply_lut(const uint16_t *lut, uint16_t *dst, int dsize)
355 for (i = 0; i < dsize; ++i)
356 dst[i] = lut[dst[i]];
359 #define HUF_ENCBITS 16 // literal (value) bit length
360 #define HUF_DECBITS 14 // decoding bit size (>= 8)
362 #define HUF_ENCSIZE ((1 << HUF_ENCBITS) + 1) // encoding table size
363 #define HUF_DECSIZE (1 << HUF_DECBITS) // decoding table size
364 #define HUF_DECMASK (HUF_DECSIZE - 1)
366 typedef struct HufDec {
372 static void huf_canonical_code_table(uint64_t *hcode)
374 uint64_t c, n[59] = { 0 };
377 for (i = 0; i < HUF_ENCSIZE; ++i)
381 for (i = 58; i > 0; --i) {
382 uint64_t nc = ((c + n[i]) >> 1);
387 for (i = 0; i < HUF_ENCSIZE; ++i) {
391 hcode[i] = l | (n[l]++ << 6);
395 #define SHORT_ZEROCODE_RUN 59
396 #define LONG_ZEROCODE_RUN 63
397 #define SHORTEST_LONG_RUN (2 + LONG_ZEROCODE_RUN - SHORT_ZEROCODE_RUN)
398 #define LONGEST_LONG_RUN (255 + SHORTEST_LONG_RUN)
400 static int huf_unpack_enc_table(GetByteContext *gb,
401 int32_t im, int32_t iM, uint64_t *hcode)
404 int ret = init_get_bits8(&gbit, gb->buffer, bytestream2_get_bytes_left(gb));
408 for (; im <= iM; im++) {
409 uint64_t l = hcode[im] = get_bits(&gbit, 6);
411 if (l == LONG_ZEROCODE_RUN) {
412 int zerun = get_bits(&gbit, 8) + SHORTEST_LONG_RUN;
414 if (im + zerun > iM + 1)
415 return AVERROR_INVALIDDATA;
421 } else if (l >= SHORT_ZEROCODE_RUN) {
422 int zerun = l - SHORT_ZEROCODE_RUN + 2;
424 if (im + zerun > iM + 1)
425 return AVERROR_INVALIDDATA;
434 bytestream2_skip(gb, (get_bits_count(&gbit) + 7) / 8);
435 huf_canonical_code_table(hcode);
440 static int huf_build_dec_table(const uint64_t *hcode, int im,
441 int iM, HufDec *hdecod)
443 for (; im <= iM; im++) {
444 uint64_t c = hcode[im] >> 6;
445 int i, l = hcode[im] & 63;
448 return AVERROR_INVALIDDATA;
450 if (l > HUF_DECBITS) {
451 HufDec *pl = hdecod + (c >> (l - HUF_DECBITS));
453 return AVERROR_INVALIDDATA;
457 pl->p = av_realloc(pl->p, pl->lit * sizeof(int));
459 return AVERROR(ENOMEM);
461 pl->p[pl->lit - 1] = im;
463 HufDec *pl = hdecod + (c << (HUF_DECBITS - l));
465 for (i = 1 << (HUF_DECBITS - l); i > 0; i--, pl++) {
466 if (pl->len || pl->p)
467 return AVERROR_INVALIDDATA;
477 #define get_char(c, lc, gb) \
479 c = (c << 8) | bytestream2_get_byte(gb); \
483 #define get_code(po, rlc, c, lc, gb, out, oe, outb) \
487 get_char(c, lc, gb); \
492 if (out + cs > oe || out == outb) \
493 return AVERROR_INVALIDDATA; \
499 } else if (out < oe) { \
502 return AVERROR_INVALIDDATA; \
506 static int huf_decode(const uint64_t *hcode, const HufDec *hdecod,
507 GetByteContext *gb, int nbits,
508 int rlc, int no, uint16_t *out)
511 uint16_t *outb = out;
512 uint16_t *oe = out + no;
513 const uint8_t *ie = gb->buffer + (nbits + 7) / 8; // input byte size
518 while (gb->buffer < ie) {
521 while (lc >= HUF_DECBITS) {
522 const HufDec pl = hdecod[(c >> (lc - HUF_DECBITS)) & HUF_DECMASK];
526 get_code(pl.lit, rlc, c, lc, gb, out, oe, outb);
531 return AVERROR_INVALIDDATA;
533 for (j = 0; j < pl.lit; j++) {
534 int l = hcode[pl.p[j]] & 63;
536 while (lc < l && bytestream2_get_bytes_left(gb) > 0)
540 if ((hcode[pl.p[j]] >> 6) ==
541 ((c >> (lc - l)) & ((1LL << l) - 1))) {
543 get_code(pl.p[j], rlc, c, lc, gb, out, oe, outb);
550 return AVERROR_INVALIDDATA;
560 const HufDec pl = hdecod[(c << (HUF_DECBITS - lc)) & HUF_DECMASK];
562 if (pl.len && lc >= pl.len) {
564 get_code(pl.lit, rlc, c, lc, gb, out, oe, outb);
566 return AVERROR_INVALIDDATA;
570 if (out - outb != no)
571 return AVERROR_INVALIDDATA;
575 static int huf_uncompress(GetByteContext *gb,
576 uint16_t *dst, int dst_size)
578 int32_t src_size, im, iM;
584 src_size = bytestream2_get_le32(gb);
585 im = bytestream2_get_le32(gb);
586 iM = bytestream2_get_le32(gb);
587 bytestream2_skip(gb, 4);
588 nBits = bytestream2_get_le32(gb);
589 if (im < 0 || im >= HUF_ENCSIZE ||
590 iM < 0 || iM >= HUF_ENCSIZE ||
592 return AVERROR_INVALIDDATA;
594 bytestream2_skip(gb, 4);
596 freq = av_mallocz_array(HUF_ENCSIZE, sizeof(*freq));
597 hdec = av_mallocz_array(HUF_DECSIZE, sizeof(*hdec));
598 if (!freq || !hdec) {
599 ret = AVERROR(ENOMEM);
603 if ((ret = huf_unpack_enc_table(gb, im, iM, freq)) < 0)
606 if (nBits > 8 * bytestream2_get_bytes_left(gb)) {
607 ret = AVERROR_INVALIDDATA;
611 if ((ret = huf_build_dec_table(freq, im, iM, hdec)) < 0)
613 ret = huf_decode(freq, hdec, gb, nBits, iM, dst_size, dst);
616 for (i = 0; i < HUF_DECSIZE; i++)
618 av_freep(&hdec[i].p);
626 static inline void wdec14(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
631 int ai = ls + (hi & 1) + (hi >> 1);
633 int16_t bs = ai - hi;
640 #define A_OFFSET (1 << (NBITS - 1))
641 #define MOD_MASK ((1 << NBITS) - 1)
643 static inline void wdec16(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
647 int bb = (m - (d >> 1)) & MOD_MASK;
648 int aa = (d + bb - A_OFFSET) & MOD_MASK;
653 static void wav_decode(uint16_t *in, int nx, int ox,
654 int ny, int oy, uint16_t mx)
656 int w14 = (mx < (1 << 14));
657 int n = (nx > ny) ? ny : nx;
670 uint16_t *ey = in + oy * (ny - p2);
671 uint16_t i00, i01, i10, i11;
677 for (; py <= ey; py += oy2) {
679 uint16_t *ex = py + ox * (nx - p2);
681 for (; px <= ex; px += ox2) {
682 uint16_t *p01 = px + ox1;
683 uint16_t *p10 = px + oy1;
684 uint16_t *p11 = p10 + ox1;
687 wdec14(*px, *p10, &i00, &i10);
688 wdec14(*p01, *p11, &i01, &i11);
689 wdec14(i00, i01, px, p01);
690 wdec14(i10, i11, p10, p11);
692 wdec16(*px, *p10, &i00, &i10);
693 wdec16(*p01, *p11, &i01, &i11);
694 wdec16(i00, i01, px, p01);
695 wdec16(i10, i11, p10, p11);
700 uint16_t *p10 = px + oy1;
703 wdec14(*px, *p10, &i00, p10);
705 wdec16(*px, *p10, &i00, p10);
713 uint16_t *ex = py + ox * (nx - p2);
715 for (; px <= ex; px += ox2) {
716 uint16_t *p01 = px + ox1;
719 wdec14(*px, *p01, &i00, p01);
721 wdec16(*px, *p01, &i00, p01);
732 static int piz_uncompress(EXRContext *s, const uint8_t *src, int ssize,
733 int dsize, EXRThreadData *td)
736 uint16_t maxval, min_non_zero, max_non_zero;
738 uint16_t *tmp = (uint16_t *)td->tmp;
742 int pixel_half_size;/* 1 for half, 2 for float and uint32 */
747 td->bitmap = av_malloc(BITMAP_SIZE);
749 td->lut = av_malloc(1 << 17);
750 if (!td->bitmap || !td->lut) {
751 av_freep(&td->bitmap);
753 return AVERROR(ENOMEM);
756 bytestream2_init(&gb, src, ssize);
757 min_non_zero = bytestream2_get_le16(&gb);
758 max_non_zero = bytestream2_get_le16(&gb);
760 if (max_non_zero >= BITMAP_SIZE)
761 return AVERROR_INVALIDDATA;
763 memset(td->bitmap, 0, FFMIN(min_non_zero, BITMAP_SIZE));
764 if (min_non_zero <= max_non_zero)
765 bytestream2_get_buffer(&gb, td->bitmap + min_non_zero,
766 max_non_zero - min_non_zero + 1);
767 memset(td->bitmap + max_non_zero + 1, 0, BITMAP_SIZE - max_non_zero - 1);
769 maxval = reverse_lut(td->bitmap, td->lut);
771 ret = huf_uncompress(&gb, tmp, dsize / sizeof(uint16_t));
776 for (i = 0; i < s->nb_channels; i++) {
777 channel = &s->channels[i];
779 if (channel->pixel_type == EXR_HALF)
784 for (j = 0; j < pixel_half_size; j++)
785 wav_decode(ptr + j, td->xsize, pixel_half_size, td->ysize,
786 td->xsize * pixel_half_size, maxval);
787 ptr += td->xsize * td->ysize * pixel_half_size;
790 apply_lut(td->lut, tmp, dsize / sizeof(uint16_t));
792 out = (uint16_t *)td->uncompressed_data;
793 for (i = 0; i < td->ysize; i++) {
795 for (j = 0; j < s->nb_channels; j++) {
796 channel = &s->channels[j];
797 if (channel->pixel_type == EXR_HALF)
802 in = tmp + tmp_offset * td->xsize * td->ysize + i * td->xsize * pixel_half_size;
803 tmp_offset += pixel_half_size;
806 s->bbdsp.bswap16_buf(out, in, td->xsize * pixel_half_size);
808 memcpy(out, in, td->xsize * 2 * pixel_half_size);
810 out += td->xsize * pixel_half_size;
817 static int pxr24_uncompress(EXRContext *s, const uint8_t *src,
818 int compressed_size, int uncompressed_size,
821 unsigned long dest_len, expected_len = 0;
822 const uint8_t *in = td->tmp;
826 for (i = 0; i < s->nb_channels; i++) {
827 if (s->channels[i].pixel_type == EXR_FLOAT) {
828 expected_len += (td->xsize * td->ysize * 3);/* PRX 24 store float in 24 bit instead of 32 */
829 } else if (s->channels[i].pixel_type == EXR_HALF) {
830 expected_len += (td->xsize * td->ysize * 2);
832 expected_len += (td->xsize * td->ysize * 4);
836 dest_len = expected_len;
838 if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK) {
839 return AVERROR_INVALIDDATA;
840 } else if (dest_len != expected_len) {
841 return AVERROR_INVALIDDATA;
844 out = td->uncompressed_data;
845 for (i = 0; i < td->ysize; i++)
846 for (c = 0; c < s->nb_channels; c++) {
847 EXRChannel *channel = &s->channels[c];
848 const uint8_t *ptr[4];
851 switch (channel->pixel_type) {
854 ptr[1] = ptr[0] + td->xsize;
855 ptr[2] = ptr[1] + td->xsize;
856 in = ptr[2] + td->xsize;
858 for (j = 0; j < td->xsize; ++j) {
859 uint32_t diff = ((unsigned)*(ptr[0]++) << 24) |
860 (*(ptr[1]++) << 16) |
863 bytestream_put_le32(&out, pixel);
868 ptr[1] = ptr[0] + td->xsize;
869 in = ptr[1] + td->xsize;
870 for (j = 0; j < td->xsize; j++) {
871 uint32_t diff = (*(ptr[0]++) << 8) | *(ptr[1]++);
874 bytestream_put_le16(&out, pixel);
879 ptr[1] = ptr[0] + s->xdelta;
880 ptr[2] = ptr[1] + s->xdelta;
881 ptr[3] = ptr[2] + s->xdelta;
882 in = ptr[3] + s->xdelta;
884 for (j = 0; j < s->xdelta; ++j) {
885 uint32_t diff = ((uint32_t)*(ptr[0]++) << 24) |
886 (*(ptr[1]++) << 16) |
887 (*(ptr[2]++) << 8 ) |
890 bytestream_put_le32(&out, pixel);
894 return AVERROR_INVALIDDATA;
901 static void unpack_14(const uint8_t b[14], uint16_t s[16])
903 unsigned short shift = (b[ 2] >> 2) & 15;
904 unsigned short bias = (0x20 << shift);
907 s[ 0] = (b[0] << 8) | b[1];
909 s[ 4] = s[ 0] + ((((b[ 2] << 4) | (b[ 3] >> 4)) & 0x3f) << shift) - bias;
910 s[ 8] = s[ 4] + ((((b[ 3] << 2) | (b[ 4] >> 6)) & 0x3f) << shift) - bias;
911 s[12] = s[ 8] + ((b[ 4] & 0x3f) << shift) - bias;
913 s[ 1] = s[ 0] + ((b[ 5] >> 2) << shift) - bias;
914 s[ 5] = s[ 4] + ((((b[ 5] << 4) | (b[ 6] >> 4)) & 0x3f) << shift) - bias;
915 s[ 9] = s[ 8] + ((((b[ 6] << 2) | (b[ 7] >> 6)) & 0x3f) << shift) - bias;
916 s[13] = s[12] + ((b[ 7] & 0x3f) << shift) - bias;
918 s[ 2] = s[ 1] + ((b[ 8] >> 2) << shift) - bias;
919 s[ 6] = s[ 5] + ((((b[ 8] << 4) | (b[ 9] >> 4)) & 0x3f) << shift) - bias;
920 s[10] = s[ 9] + ((((b[ 9] << 2) | (b[10] >> 6)) & 0x3f) << shift) - bias;
921 s[14] = s[13] + ((b[10] & 0x3f) << shift) - bias;
923 s[ 3] = s[ 2] + ((b[11] >> 2) << shift) - bias;
924 s[ 7] = s[ 6] + ((((b[11] << 4) | (b[12] >> 4)) & 0x3f) << shift) - bias;
925 s[11] = s[10] + ((((b[12] << 2) | (b[13] >> 6)) & 0x3f) << shift) - bias;
926 s[15] = s[14] + ((b[13] & 0x3f) << shift) - bias;
928 for (i = 0; i < 16; ++i) {
936 static void unpack_3(const uint8_t b[3], uint16_t s[16])
940 s[0] = (b[0] << 8) | b[1];
947 for (i = 1; i < 16; i++)
952 static int b44_uncompress(EXRContext *s, const uint8_t *src, int compressed_size,
953 int uncompressed_size, EXRThreadData *td) {
954 const int8_t *sr = src;
955 int stay_to_uncompress = compressed_size;
956 int nb_b44_block_w, nb_b44_block_h;
957 int index_tl_x, index_tl_y, index_out, index_tmp;
958 uint16_t tmp_buffer[16]; /* B44 use 4x4 half float pixel */
960 int target_channel_offset = 0;
962 /* calc B44 block count */
963 nb_b44_block_w = td->xsize / 4;
964 if ((td->xsize % 4) != 0)
967 nb_b44_block_h = td->ysize / 4;
968 if ((td->ysize % 4) != 0)
971 for (c = 0; c < s->nb_channels; c++) {
972 if (s->channels[c].pixel_type == EXR_HALF) {/* B44 only compress half float data */
973 for (iY = 0; iY < nb_b44_block_h; iY++) {
974 for (iX = 0; iX < nb_b44_block_w; iX++) {/* For each B44 block */
975 if (stay_to_uncompress < 3) {
976 av_log(s, AV_LOG_ERROR, "Not enough data for B44A block: %d", stay_to_uncompress);
977 return AVERROR_INVALIDDATA;
980 if (src[compressed_size - stay_to_uncompress + 2] == 0xfc) { /* B44A block */
981 unpack_3(sr, tmp_buffer);
983 stay_to_uncompress -= 3;
984 } else {/* B44 Block */
985 if (stay_to_uncompress < 14) {
986 av_log(s, AV_LOG_ERROR, "Not enough data for B44 block: %d", stay_to_uncompress);
987 return AVERROR_INVALIDDATA;
989 unpack_14(sr, tmp_buffer);
991 stay_to_uncompress -= 14;
994 /* copy data to uncompress buffer (B44 block can exceed target resolution)*/
998 for (y = index_tl_y; y < FFMIN(index_tl_y + 4, td->ysize); y++) {
999 for (x = index_tl_x; x < FFMIN(index_tl_x + 4, td->xsize); x++) {
1000 index_out = target_channel_offset * td->xsize + y * td->channel_line_size + 2 * x;
1001 index_tmp = (y-index_tl_y) * 4 + (x-index_tl_x);
1002 td->uncompressed_data[index_out] = tmp_buffer[index_tmp] & 0xff;
1003 td->uncompressed_data[index_out + 1] = tmp_buffer[index_tmp] >> 8;
1008 target_channel_offset += 2;
1009 } else {/* Float or UINT 32 channel */
1010 if (stay_to_uncompress < td->ysize * td->xsize * 4) {
1011 av_log(s, AV_LOG_ERROR, "Not enough data for uncompress channel: %d", stay_to_uncompress);
1012 return AVERROR_INVALIDDATA;
1015 for (y = 0; y < td->ysize; y++) {
1016 index_out = target_channel_offset * td->xsize + y * td->channel_line_size;
1017 memcpy(&td->uncompressed_data[index_out], sr, td->xsize * 4);
1018 sr += td->xsize * 4;
1020 target_channel_offset += 4;
1022 stay_to_uncompress -= td->ysize * td->xsize * 4;
1029 static int decode_block(AVCodecContext *avctx, void *tdata,
1030 int jobnr, int threadnr)
1032 EXRContext *s = avctx->priv_data;
1033 AVFrame *const p = s->picture;
1034 EXRThreadData *td = &s->thread_data[threadnr];
1035 const uint8_t *channel_buffer[4] = { 0 };
1036 const uint8_t *buf = s->buf;
1037 uint64_t line_offset, uncompressed_size;
1041 uint64_t line, col = 0;
1042 uint64_t tile_x, tile_y, tile_level_x, tile_level_y;
1044 int axmax = (avctx->width - (s->xmax + 1)) * 2 * s->desc->nb_components; /* nb pixel to add at the right of the datawindow */
1045 int bxmin = s->xmin * 2 * s->desc->nb_components; /* nb pixel to add at the left of the datawindow */
1046 int i, x, buf_size = s->buf_size;
1047 int c, rgb_channel_count;
1048 float one_gamma = 1.0f / s->gamma;
1049 avpriv_trc_function trc_func = avpriv_get_trc_function_from_trc(s->apply_trc_type);
1052 line_offset = AV_RL64(s->gb.buffer + jobnr * 8);
1055 if (buf_size < 20 || line_offset > buf_size - 20)
1056 return AVERROR_INVALIDDATA;
1058 src = buf + line_offset + 20;
1060 tile_x = AV_RL32(src - 20);
1061 tile_y = AV_RL32(src - 16);
1062 tile_level_x = AV_RL32(src - 12);
1063 tile_level_y = AV_RL32(src - 8);
1065 data_size = AV_RL32(src - 4);
1066 if (data_size <= 0 || data_size > buf_size - line_offset - 20)
1067 return AVERROR_INVALIDDATA;
1069 if (tile_level_x || tile_level_y) { /* tile level, is not the full res level */
1070 avpriv_report_missing_feature(s->avctx, "Subres tile before full res tile");
1071 return AVERROR_PATCHWELCOME;
1074 if (s->xmin || s->ymin) {
1075 avpriv_report_missing_feature(s->avctx, "Tiles with xmin/ymin");
1076 return AVERROR_PATCHWELCOME;
1079 line = s->tile_attr.ySize * tile_y;
1080 col = s->tile_attr.xSize * tile_x;
1082 if (line < s->ymin || line > s->ymax ||
1083 col < s->xmin || col > s->xmax)
1084 return AVERROR_INVALIDDATA;
1086 td->ysize = FFMIN(s->tile_attr.ySize, s->ydelta - tile_y * s->tile_attr.ySize);
1087 td->xsize = FFMIN(s->tile_attr.xSize, s->xdelta - tile_x * s->tile_attr.xSize);
1089 if (col) { /* not the first tile of the line */
1090 bxmin = 0; /* doesn't add pixel at the left of the datawindow */
1093 if ((col + td->xsize) != s->xdelta)/* not the last tile of the line */
1094 axmax = 0; /* doesn't add pixel at the right of the datawindow */
1096 td->channel_line_size = td->xsize * s->current_channel_offset;/* uncompress size of one line */
1097 uncompressed_size = td->channel_line_size * (uint64_t)td->ysize;/* uncompress size of the block */
1099 if (buf_size < 8 || line_offset > buf_size - 8)
1100 return AVERROR_INVALIDDATA;
1102 src = buf + line_offset + 8;
1103 line = AV_RL32(src - 8);
1105 if (line < s->ymin || line > s->ymax)
1106 return AVERROR_INVALIDDATA;
1108 data_size = AV_RL32(src - 4);
1109 if (data_size <= 0 || data_size > buf_size - line_offset - 8)
1110 return AVERROR_INVALIDDATA;
1112 td->ysize = FFMIN(s->scan_lines_per_block, s->ymax - line + 1); /* s->ydelta - line ?? */
1113 td->xsize = s->xdelta;
1115 td->channel_line_size = td->xsize * s->current_channel_offset;/* uncompress size of one line */
1116 uncompressed_size = td->channel_line_size * (uint64_t)td->ysize;/* uncompress size of the block */
1118 if ((s->compression == EXR_RAW && (data_size != uncompressed_size ||
1119 line_offset > buf_size - uncompressed_size)) ||
1120 (s->compression != EXR_RAW && (data_size > uncompressed_size ||
1121 line_offset > buf_size - data_size))) {
1122 return AVERROR_INVALIDDATA;
1126 if (data_size < uncompressed_size || s->is_tile) { /* td->tmp is use for tile reorganization */
1127 av_fast_padded_malloc(&td->tmp, &td->tmp_size, uncompressed_size);
1129 return AVERROR(ENOMEM);
1132 if (data_size < uncompressed_size) {
1133 av_fast_padded_malloc(&td->uncompressed_data,
1134 &td->uncompressed_size, uncompressed_size + 64);/* Force 64 padding for AVX2 reorder_pixels dst */
1136 if (!td->uncompressed_data)
1137 return AVERROR(ENOMEM);
1139 ret = AVERROR_INVALIDDATA;
1140 switch (s->compression) {
1143 ret = zip_uncompress(s, src, data_size, uncompressed_size, td);
1146 ret = piz_uncompress(s, src, data_size, uncompressed_size, td);
1149 ret = pxr24_uncompress(s, src, data_size, uncompressed_size, td);
1152 ret = rle_uncompress(s, src, data_size, uncompressed_size, td);
1156 ret = b44_uncompress(s, src, data_size, uncompressed_size, td);
1160 av_log(avctx, AV_LOG_ERROR, "decode_block() failed.\n");
1163 src = td->uncompressed_data;
1167 channel_buffer[0] = src + td->xsize * s->channel_offsets[0];
1168 channel_buffer[1] = src + td->xsize * s->channel_offsets[1];
1169 channel_buffer[2] = src + td->xsize * s->channel_offsets[2];
1170 rgb_channel_count = 3;
1171 } else { /* put y data in the first channel_buffer */
1172 channel_buffer[0] = src + td->xsize * s->channel_offsets[1];
1173 rgb_channel_count = 1;
1175 if (s->channel_offsets[3] >= 0)
1176 channel_buffer[3] = src + td->xsize * s->channel_offsets[3];
1178 ptr = p->data[0] + line * p->linesize[0] + (col * s->desc->nb_components * 2);
1181 i < td->ysize; i++, ptr += p->linesize[0]) {
1184 const uint8_t *rgb[3];
1186 for (c = 0; c < rgb_channel_count; c++) {
1187 rgb[c] = channel_buffer[c];
1190 if (channel_buffer[3])
1191 a = channel_buffer[3];
1193 ptr_x = (uint16_t *) ptr;
1195 // Zero out the start if xmin is not 0
1196 memset(ptr_x, 0, bxmin);
1197 ptr_x += s->xmin * s->desc->nb_components;
1199 if (s->pixel_type == EXR_FLOAT) {
1202 for (x = 0; x < td->xsize; x++) {
1203 union av_intfloat32 t;
1205 for (c = 0; c < rgb_channel_count; c++) {
1206 t.i = bytestream_get_le32(&rgb[c]);
1207 t.f = trc_func(t.f);
1208 *ptr_x++ = exr_flt2uint(t.i);
1210 if (channel_buffer[3])
1211 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a));
1214 for (x = 0; x < td->xsize; x++) {
1215 union av_intfloat32 t;
1218 for (c = 0; c < rgb_channel_count; c++) {
1219 t.i = bytestream_get_le32(&rgb[c]);
1220 if (t.f > 0.0f) /* avoid negative values */
1221 t.f = powf(t.f, one_gamma);
1222 *ptr_x++ = exr_flt2uint(t.i);
1225 if (channel_buffer[3])
1226 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a));
1229 } else if (s->pixel_type == EXR_HALF) {
1231 for (x = 0; x < td->xsize; x++) {
1233 for (c = 0; c < rgb_channel_count; c++) {
1234 *ptr_x++ = s->gamma_table[bytestream_get_le16(&rgb[c])];
1237 if (channel_buffer[3])
1238 *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&a));
1240 } else if (s->pixel_type == EXR_UINT) {
1241 for (x = 0; x < td->xsize; x++) {
1242 for (c = 0; c < rgb_channel_count; c++) {
1243 *ptr_x++ = bytestream_get_le32(&rgb[c]) >> 16;
1246 if (channel_buffer[3])
1247 *ptr_x++ = bytestream_get_le32(&a) >> 16;
1251 // Zero out the end if xmax+1 is not w
1252 memset(ptr_x, 0, axmax);
1254 channel_buffer[0] += td->channel_line_size;
1255 channel_buffer[1] += td->channel_line_size;
1256 channel_buffer[2] += td->channel_line_size;
1257 if (channel_buffer[3])
1258 channel_buffer[3] += td->channel_line_size;
1265 * Check if the variable name corresponds to its data type.
1267 * @param s the EXRContext
1268 * @param value_name name of the variable to check
1269 * @param value_type type of the variable to check
1270 * @param minimum_length minimum length of the variable data
1272 * @return bytes to read containing variable data
1273 * -1 if variable is not found
1274 * 0 if buffer ended prematurely
1276 static int check_header_variable(EXRContext *s,
1277 const char *value_name,
1278 const char *value_type,
1279 unsigned int minimum_length)
1283 if (bytestream2_get_bytes_left(&s->gb) >= minimum_length &&
1284 !strcmp(s->gb.buffer, value_name)) {
1285 // found value_name, jump to value_type (null terminated strings)
1286 s->gb.buffer += strlen(value_name) + 1;
1287 if (!strcmp(s->gb.buffer, value_type)) {
1288 s->gb.buffer += strlen(value_type) + 1;
1289 var_size = bytestream2_get_le32(&s->gb);
1290 // don't go read past boundaries
1291 if (var_size > bytestream2_get_bytes_left(&s->gb))
1294 // value_type not found, reset the buffer
1295 s->gb.buffer -= strlen(value_name) + 1;
1296 av_log(s->avctx, AV_LOG_WARNING,
1297 "Unknown data type %s for header variable %s.\n",
1298 value_type, value_name);
1305 static int decode_header(EXRContext *s, AVFrame *frame)
1307 AVDictionary *metadata = NULL;
1308 int magic_number, version, i, flags, sar = 0;
1309 int layer_match = 0;
1311 int dup_channels = 0;
1313 s->current_channel_offset = 0;
1320 s->channel_offsets[0] = -1;
1321 s->channel_offsets[1] = -1;
1322 s->channel_offsets[2] = -1;
1323 s->channel_offsets[3] = -1;
1324 s->pixel_type = EXR_UNKNOWN;
1325 s->compression = EXR_UNKN;
1329 s->tile_attr.xSize = -1;
1330 s->tile_attr.ySize = -1;
1334 if (bytestream2_get_bytes_left(&s->gb) < 10) {
1335 av_log(s->avctx, AV_LOG_ERROR, "Header too short to parse.\n");
1336 return AVERROR_INVALIDDATA;
1339 magic_number = bytestream2_get_le32(&s->gb);
1340 if (magic_number != 20000630) {
1341 /* As per documentation of OpenEXR, it is supposed to be
1342 * int 20000630 little-endian */
1343 av_log(s->avctx, AV_LOG_ERROR, "Wrong magic number %d.\n", magic_number);
1344 return AVERROR_INVALIDDATA;
1347 version = bytestream2_get_byte(&s->gb);
1349 avpriv_report_missing_feature(s->avctx, "Version %d", version);
1350 return AVERROR_PATCHWELCOME;
1353 flags = bytestream2_get_le24(&s->gb);
1358 avpriv_report_missing_feature(s->avctx, "deep data");
1359 return AVERROR_PATCHWELCOME;
1362 avpriv_report_missing_feature(s->avctx, "multipart");
1363 return AVERROR_PATCHWELCOME;
1367 while (bytestream2_get_bytes_left(&s->gb) > 0 && *s->gb.buffer) {
1369 if ((var_size = check_header_variable(s, "channels",
1370 "chlist", 38)) >= 0) {
1371 GetByteContext ch_gb;
1373 ret = AVERROR_INVALIDDATA;
1377 bytestream2_init(&ch_gb, s->gb.buffer, var_size);
1379 while (bytestream2_get_bytes_left(&ch_gb) >= 19) {
1380 EXRChannel *channel;
1381 enum ExrPixelType current_pixel_type;
1382 int channel_index = -1;
1385 if (strcmp(s->layer, "") != 0) {
1386 if (strncmp(ch_gb.buffer, s->layer, strlen(s->layer)) == 0) {
1388 av_log(s->avctx, AV_LOG_INFO,
1389 "Channel match layer : %s.\n", ch_gb.buffer);
1390 ch_gb.buffer += strlen(s->layer);
1391 if (*ch_gb.buffer == '.')
1392 ch_gb.buffer++; /* skip dot if not given */
1395 av_log(s->avctx, AV_LOG_INFO,
1396 "Channel doesn't match layer : %s.\n", ch_gb.buffer);
1402 if (layer_match) { /* only search channel if the layer match is valid */
1403 if (!av_strcasecmp(ch_gb.buffer, "R") ||
1404 !av_strcasecmp(ch_gb.buffer, "X") ||
1405 !av_strcasecmp(ch_gb.buffer, "U")) {
1408 } else if (!av_strcasecmp(ch_gb.buffer, "G") ||
1409 !av_strcasecmp(ch_gb.buffer, "V")) {
1412 } else if (!av_strcasecmp(ch_gb.buffer, "Y")) {
1415 } else if (!av_strcasecmp(ch_gb.buffer, "B") ||
1416 !av_strcasecmp(ch_gb.buffer, "Z") ||
1417 !av_strcasecmp(ch_gb.buffer, "W")) {
1420 } else if (!av_strcasecmp(ch_gb.buffer, "A")) {
1423 av_log(s->avctx, AV_LOG_WARNING,
1424 "Unsupported channel %.256s.\n", ch_gb.buffer);
1428 /* skip until you get a 0 */
1429 while (bytestream2_get_bytes_left(&ch_gb) > 0 &&
1430 bytestream2_get_byte(&ch_gb))
1433 if (bytestream2_get_bytes_left(&ch_gb) < 4) {
1434 av_log(s->avctx, AV_LOG_ERROR, "Incomplete header.\n");
1435 ret = AVERROR_INVALIDDATA;
1439 current_pixel_type = bytestream2_get_le32(&ch_gb);
1440 if (current_pixel_type >= EXR_UNKNOWN) {
1441 avpriv_report_missing_feature(s->avctx, "Pixel type %d",
1442 current_pixel_type);
1443 ret = AVERROR_PATCHWELCOME;
1447 bytestream2_skip(&ch_gb, 4);
1448 xsub = bytestream2_get_le32(&ch_gb);
1449 ysub = bytestream2_get_le32(&ch_gb);
1451 if (xsub != 1 || ysub != 1) {
1452 avpriv_report_missing_feature(s->avctx,
1453 "Subsampling %dx%d",
1455 ret = AVERROR_PATCHWELCOME;
1459 if (channel_index >= 0 && s->channel_offsets[channel_index] == -1) { /* channel has not been previously assigned */
1460 if (s->pixel_type != EXR_UNKNOWN &&
1461 s->pixel_type != current_pixel_type) {
1462 av_log(s->avctx, AV_LOG_ERROR,
1463 "RGB channels not of the same depth.\n");
1464 ret = AVERROR_INVALIDDATA;
1467 s->pixel_type = current_pixel_type;
1468 s->channel_offsets[channel_index] = s->current_channel_offset;
1469 } else if (channel_index >= 0) {
1470 av_log(s->avctx, AV_LOG_WARNING,
1471 "Multiple channels with index %d.\n", channel_index);
1472 if (++dup_channels > 10) {
1473 ret = AVERROR_INVALIDDATA;
1478 s->channels = av_realloc(s->channels,
1479 ++s->nb_channels * sizeof(EXRChannel));
1481 ret = AVERROR(ENOMEM);
1484 channel = &s->channels[s->nb_channels - 1];
1485 channel->pixel_type = current_pixel_type;
1486 channel->xsub = xsub;
1487 channel->ysub = ysub;
1489 if (current_pixel_type == EXR_HALF) {
1490 s->current_channel_offset += 2;
1491 } else {/* Float or UINT32 */
1492 s->current_channel_offset += 4;
1496 /* Check if all channels are set with an offset or if the channels
1497 * are causing an overflow */
1498 if (!s->is_luma) {/* if we expected to have at least 3 channels */
1499 if (FFMIN3(s->channel_offsets[0],
1500 s->channel_offsets[1],
1501 s->channel_offsets[2]) < 0) {
1502 if (s->channel_offsets[0] < 0)
1503 av_log(s->avctx, AV_LOG_ERROR, "Missing red channel.\n");
1504 if (s->channel_offsets[1] < 0)
1505 av_log(s->avctx, AV_LOG_ERROR, "Missing green channel.\n");
1506 if (s->channel_offsets[2] < 0)
1507 av_log(s->avctx, AV_LOG_ERROR, "Missing blue channel.\n");
1508 ret = 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 ret = AVERROR_INVALIDDATA;
1523 s->xmin = bytestream2_get_le32(&s->gb);
1524 s->ymin = bytestream2_get_le32(&s->gb);
1525 s->xmax = bytestream2_get_le32(&s->gb);
1526 s->ymax = bytestream2_get_le32(&s->gb);
1527 s->xdelta = (s->xmax - s->xmin) + 1;
1528 s->ydelta = (s->ymax - s->ymin) + 1;
1531 } else if ((var_size = check_header_variable(s, "displayWindow",
1532 "box2i", 34)) >= 0) {
1534 ret = AVERROR_INVALIDDATA;
1538 bytestream2_skip(&s->gb, 8);
1539 s->w = bytestream2_get_le32(&s->gb) + 1;
1540 s->h = bytestream2_get_le32(&s->gb) + 1;
1543 } else if ((var_size = check_header_variable(s, "lineOrder",
1544 "lineOrder", 25)) >= 0) {
1547 ret = AVERROR_INVALIDDATA;
1551 line_order = bytestream2_get_byte(&s->gb);
1552 av_log(s->avctx, AV_LOG_DEBUG, "line order: %d.\n", line_order);
1553 if (line_order > 2) {
1554 av_log(s->avctx, AV_LOG_ERROR, "Unknown line order.\n");
1555 ret = AVERROR_INVALIDDATA;
1560 } else if ((var_size = check_header_variable(s, "pixelAspectRatio",
1561 "float", 31)) >= 0) {
1563 ret = AVERROR_INVALIDDATA;
1567 sar = bytestream2_get_le32(&s->gb);
1570 } else if ((var_size = check_header_variable(s, "compression",
1571 "compression", 29)) >= 0) {
1573 ret = AVERROR_INVALIDDATA;
1577 if (s->compression == EXR_UNKN)
1578 s->compression = bytestream2_get_byte(&s->gb);
1580 av_log(s->avctx, AV_LOG_WARNING,
1581 "Found more than one compression attribute.\n");
1584 } else if ((var_size = check_header_variable(s, "tiles",
1585 "tiledesc", 22)) >= 0) {
1589 av_log(s->avctx, AV_LOG_WARNING,
1590 "Found tile attribute and scanline flags. Exr will be interpreted as scanline.\n");
1592 s->tile_attr.xSize = bytestream2_get_le32(&s->gb);
1593 s->tile_attr.ySize = bytestream2_get_le32(&s->gb);
1595 tileLevel = bytestream2_get_byte(&s->gb);
1596 s->tile_attr.level_mode = tileLevel & 0x0f;
1597 s->tile_attr.level_round = (tileLevel >> 4) & 0x0f;
1599 if (s->tile_attr.level_mode >= EXR_TILE_LEVEL_UNKNOWN) {
1600 avpriv_report_missing_feature(s->avctx, "Tile level mode %d",
1601 s->tile_attr.level_mode);
1602 ret = AVERROR_PATCHWELCOME;
1606 if (s->tile_attr.level_round >= EXR_TILE_ROUND_UNKNOWN) {
1607 avpriv_report_missing_feature(s->avctx, "Tile level round %d",
1608 s->tile_attr.level_round);
1609 ret = AVERROR_PATCHWELCOME;
1614 } else if ((var_size = check_header_variable(s, "writer",
1615 "string", 1)) >= 0) {
1616 uint8_t key[256] = { 0 };
1618 bytestream2_get_buffer(&s->gb, key, FFMIN(sizeof(key) - 1, var_size));
1619 av_dict_set(&metadata, "writer", key, 0);
1624 // Check if there are enough bytes for a header
1625 if (bytestream2_get_bytes_left(&s->gb) <= 9) {
1626 av_log(s->avctx, AV_LOG_ERROR, "Incomplete header\n");
1627 ret = AVERROR_INVALIDDATA;
1631 // Process unknown variables
1632 for (i = 0; i < 2; i++) // value_name and value_type
1633 while (bytestream2_get_byte(&s->gb) != 0);
1635 // Skip variable length
1636 bytestream2_skip(&s->gb, bytestream2_get_le32(&s->gb));
1639 ff_set_sar(s->avctx, av_d2q(av_int2float(sar), 255));
1641 if (s->compression == EXR_UNKN) {
1642 av_log(s->avctx, AV_LOG_ERROR, "Missing compression attribute.\n");
1643 ret = AVERROR_INVALIDDATA;
1648 if (s->tile_attr.xSize < 1 || s->tile_attr.ySize < 1) {
1649 av_log(s->avctx, AV_LOG_ERROR, "Invalid tile attribute.\n");
1650 ret = AVERROR_INVALIDDATA;
1655 if (bytestream2_get_bytes_left(&s->gb) <= 0) {
1656 av_log(s->avctx, AV_LOG_ERROR, "Incomplete frame.\n");
1657 ret = AVERROR_INVALIDDATA;
1661 frame->metadata = metadata;
1663 // aaand we are done
1664 bytestream2_skip(&s->gb, 1);
1667 av_dict_free(&metadata);
1671 static int decode_frame(AVCodecContext *avctx, void *data,
1672 int *got_frame, AVPacket *avpkt)
1674 EXRContext *s = avctx->priv_data;
1675 ThreadFrame frame = { .f = data };
1676 AVFrame *picture = data;
1681 int nb_blocks; /* nb scanline or nb tile */
1682 uint64_t start_offset_table;
1683 uint64_t start_next_scanline;
1684 PutByteContext offset_table_writer;
1686 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
1688 if ((ret = decode_header(s, picture)) < 0)
1691 switch (s->pixel_type) {
1695 if (s->channel_offsets[3] >= 0) {
1697 avctx->pix_fmt = AV_PIX_FMT_RGBA64;
1699 avctx->pix_fmt = AV_PIX_FMT_YA16;
1703 avctx->pix_fmt = AV_PIX_FMT_RGB48;
1705 avctx->pix_fmt = AV_PIX_FMT_GRAY16;
1710 av_log(avctx, AV_LOG_ERROR, "Missing channel list.\n");
1711 return AVERROR_INVALIDDATA;
1714 if (s->apply_trc_type != AVCOL_TRC_UNSPECIFIED)
1715 avctx->color_trc = s->apply_trc_type;
1717 switch (s->compression) {
1721 s->scan_lines_per_block = 1;
1725 s->scan_lines_per_block = 16;
1730 s->scan_lines_per_block = 32;
1733 avpriv_report_missing_feature(avctx, "Compression %d", s->compression);
1734 return AVERROR_PATCHWELCOME;
1737 /* Verify the xmin, xmax, ymin, ymax and xdelta before setting
1738 * the actual image size. */
1739 if (s->xmin > s->xmax ||
1740 s->ymin > s->ymax ||
1741 s->xdelta != s->xmax - s->xmin + 1 ||
1744 av_log(avctx, AV_LOG_ERROR, "Wrong or missing size information.\n");
1745 return AVERROR_INVALIDDATA;
1748 if ((ret = ff_set_dimensions(avctx, s->w, s->h)) < 0)
1751 s->desc = av_pix_fmt_desc_get(avctx->pix_fmt);
1753 return AVERROR_INVALIDDATA;
1754 out_line_size = avctx->width * 2 * s->desc->nb_components;
1757 nb_blocks = ((s->xdelta + s->tile_attr.xSize - 1) / s->tile_attr.xSize) *
1758 ((s->ydelta + s->tile_attr.ySize - 1) / s->tile_attr.ySize);
1759 } else { /* scanline */
1760 nb_blocks = (s->ydelta + s->scan_lines_per_block - 1) /
1761 s->scan_lines_per_block;
1764 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
1767 if (bytestream2_get_bytes_left(&s->gb) < nb_blocks * 8)
1768 return AVERROR_INVALIDDATA;
1770 // check offset table and recreate it if need
1771 if (!s->is_tile && bytestream2_peek_le64(&s->gb) == 0) {
1772 av_log(s->avctx, AV_LOG_DEBUG, "recreating invalid scanline offset table\n");
1774 start_offset_table = bytestream2_tell(&s->gb);
1775 start_next_scanline = start_offset_table + nb_blocks * 8;
1776 bytestream2_init_writer(&offset_table_writer, &avpkt->data[start_offset_table], nb_blocks * 8);
1778 for (y = 0; y < nb_blocks; y++) {
1779 /* write offset of prev scanline in offset table */
1780 bytestream2_put_le64(&offset_table_writer, start_next_scanline);
1782 /* get len of next scanline */
1783 bytestream2_seek(&s->gb, start_next_scanline + 4, SEEK_SET);/* skip line number */
1784 start_next_scanline += (bytestream2_get_le32(&s->gb) + 8);
1786 bytestream2_seek(&s->gb, start_offset_table, SEEK_SET);
1789 // save pointer we are going to use in decode_block
1790 s->buf = avpkt->data;
1791 s->buf_size = avpkt->size;
1792 ptr = picture->data[0];
1794 // Zero out the start if ymin is not 0
1795 for (y = 0; y < s->ymin; y++) {
1796 memset(ptr, 0, out_line_size);
1797 ptr += picture->linesize[0];
1800 s->picture = picture;
1802 avctx->execute2(avctx, decode_block, s->thread_data, NULL, nb_blocks);
1804 // Zero out the end if ymax+1 is not h
1805 ptr = picture->data[0] + ((s->ymax+1) * picture->linesize[0]);
1806 for (y = s->ymax + 1; y < avctx->height; y++) {
1807 memset(ptr, 0, out_line_size);
1808 ptr += picture->linesize[0];
1811 picture->pict_type = AV_PICTURE_TYPE_I;
1817 static av_cold int decode_init(AVCodecContext *avctx)
1819 EXRContext *s = avctx->priv_data;
1821 union av_intfloat32 t;
1822 float one_gamma = 1.0f / s->gamma;
1823 avpriv_trc_function trc_func = NULL;
1827 ff_exrdsp_init(&s->dsp);
1830 ff_bswapdsp_init(&s->bbdsp);
1833 trc_func = avpriv_get_trc_function_from_trc(s->apply_trc_type);
1835 for (i = 0; i < 65536; ++i) {
1836 t = exr_half2float(i);
1837 t.f = trc_func(t.f);
1838 s->gamma_table[i] = exr_flt2uint(t.i);
1841 if (one_gamma > 0.9999f && one_gamma < 1.0001f) {
1842 for (i = 0; i < 65536; ++i)
1843 s->gamma_table[i] = exr_halflt2uint(i);
1845 for (i = 0; i < 65536; ++i) {
1846 t = exr_half2float(i);
1847 /* If negative value we reuse half value */
1849 s->gamma_table[i] = exr_halflt2uint(i);
1851 t.f = powf(t.f, one_gamma);
1852 s->gamma_table[i] = exr_flt2uint(t.i);
1858 // allocate thread data, used for non EXR_RAW compression types
1859 s->thread_data = av_mallocz_array(avctx->thread_count, sizeof(EXRThreadData));
1860 if (!s->thread_data)
1861 return AVERROR_INVALIDDATA;
1866 static av_cold int decode_end(AVCodecContext *avctx)
1868 EXRContext *s = avctx->priv_data;
1870 for (i = 0; i < avctx->thread_count; i++) {
1871 EXRThreadData *td = &s->thread_data[i];
1872 av_freep(&td->uncompressed_data);
1874 av_freep(&td->bitmap);
1878 av_freep(&s->thread_data);
1879 av_freep(&s->channels);
1884 #define OFFSET(x) offsetof(EXRContext, x)
1885 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1886 static const AVOption options[] = {
1887 { "layer", "Set the decoding layer", OFFSET(layer),
1888 AV_OPT_TYPE_STRING, { .str = "" }, 0, 0, VD },
1889 { "gamma", "Set the float gamma value when decoding", OFFSET(gamma),
1890 AV_OPT_TYPE_FLOAT, { .dbl = 1.0f }, 0.001, FLT_MAX, VD },
1892 // XXX: Note the abuse of the enum using AVCOL_TRC_UNSPECIFIED to subsume the existing gamma option
1893 { "apply_trc", "color transfer characteristics to apply to EXR linear input", OFFSET(apply_trc_type),
1894 AV_OPT_TYPE_INT, {.i64 = AVCOL_TRC_UNSPECIFIED }, 1, AVCOL_TRC_NB-1, VD, "apply_trc_type"},
1895 { "bt709", "BT.709", 0,
1896 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT709 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1897 { "gamma", "gamma", 0,
1898 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_UNSPECIFIED }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1899 { "gamma22", "BT.470 M", 0,
1900 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_GAMMA22 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1901 { "gamma28", "BT.470 BG", 0,
1902 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_GAMMA28 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1903 { "smpte170m", "SMPTE 170 M", 0,
1904 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTE170M }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1905 { "smpte240m", "SMPTE 240 M", 0,
1906 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTE240M }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1907 { "linear", "Linear", 0,
1908 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LINEAR }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1910 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LOG }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1911 { "log_sqrt", "Log square root", 0,
1912 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LOG_SQRT }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1913 { "iec61966_2_4", "IEC 61966-2-4", 0,
1914 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_IEC61966_2_4 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1915 { "bt1361", "BT.1361", 0,
1916 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT1361_ECG }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1917 { "iec61966_2_1", "IEC 61966-2-1", 0,
1918 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_IEC61966_2_1 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1919 { "bt2020_10bit", "BT.2020 - 10 bit", 0,
1920 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT2020_10 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1921 { "bt2020_12bit", "BT.2020 - 12 bit", 0,
1922 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT2020_12 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1923 { "smpte2084", "SMPTE ST 2084", 0,
1924 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTEST2084 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1925 { "smpte428_1", "SMPTE ST 428-1", 0,
1926 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTEST428_1 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1931 static const AVClass exr_class = {
1932 .class_name = "EXR",
1933 .item_name = av_default_item_name,
1935 .version = LIBAVUTIL_VERSION_INT,
1938 AVCodec ff_exr_decoder = {
1940 .long_name = NULL_IF_CONFIG_SMALL("OpenEXR image"),
1941 .type = AVMEDIA_TYPE_VIDEO,
1942 .id = AV_CODEC_ID_EXR,
1943 .priv_data_size = sizeof(EXRContext),
1944 .init = decode_init,
1945 .close = decode_end,
1946 .decode = decode_frame,
1947 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS |
1948 AV_CODEC_CAP_SLICE_THREADS,
1949 .priv_class = &exr_class,