2 * OpenEXR (.exr) image decoder
3 * Copyright (c) 2009 Jimmy Christensen
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * @author Jimmy Christensen
27 * For more information on the OpenEXR format, visit:
30 * exr_flt2uint() and exr_halflt2uint() is credited to Reimar Döffinger.
31 * exr_half2float() is credited to Aaftab Munshi, Dan Ginsburg, Dave Shreiner.
37 #include "libavutil/imgutils.h"
38 #include "libavutil/intfloat.h"
39 #include "libavutil/opt.h"
42 #include "bytestream.h"
67 typedef struct EXRChannel {
69 enum ExrPixelType pixel_type;
72 typedef struct EXRThreadData {
73 uint8_t *uncompressed_data;
74 int uncompressed_size;
83 typedef struct EXRContext {
86 AVCodecContext *avctx;
88 enum ExrCompr compression;
89 enum ExrPixelType pixel_type;
90 int channel_offsets[4]; // 0 = red, 1 = green, 2 = blue and 3 = alpha
91 const AVPixFmtDescriptor *desc;
96 uint32_t xdelta, ydelta;
99 uint64_t scan_line_size;
100 int scan_lines_per_block;
106 EXRChannel *channels;
109 EXRThreadData *thread_data;
114 uint16_t gamma_table[65536];
117 /* -15 stored using a single precision bias of 127 */
118 #define HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP 0x38000000
120 /* max exponent value in single precision that will be converted
121 * to Inf or Nan when stored as a half-float */
122 #define HALF_FLOAT_MAX_BIASED_EXP_AS_SINGLE_FP_EXP 0x47800000
124 /* 255 is the max exponent biased value */
125 #define FLOAT_MAX_BIASED_EXP (0xFF << 23)
127 #define HALF_FLOAT_MAX_BIASED_EXP (0x1F << 10)
130 * Convert a half float as a uint16_t into a full float.
132 * @param hf half float as uint16_t
134 * @return float value
136 static union av_intfloat32 exr_half2float(uint16_t hf)
138 unsigned int sign = (unsigned int) (hf >> 15);
139 unsigned int mantissa = (unsigned int) (hf & ((1 << 10) - 1));
140 unsigned int exp = (unsigned int) (hf & HALF_FLOAT_MAX_BIASED_EXP);
141 union av_intfloat32 f;
143 if (exp == HALF_FLOAT_MAX_BIASED_EXP) {
144 // we have a half-float NaN or Inf
145 // half-float NaNs will be converted to a single precision NaN
146 // half-float Infs will be converted to a single precision Inf
147 exp = FLOAT_MAX_BIASED_EXP;
149 mantissa = (1 << 23) - 1; // set all bits to indicate a NaN
150 } else if (exp == 0x0) {
151 // convert half-float zero/denorm to single precision value
154 exp = HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP;
155 // check for leading 1 in denorm mantissa
156 while ((mantissa & (1 << 10))) {
157 // for every leading 0, decrement single precision exponent by 1
158 // and shift half-float mantissa value to the left
162 // clamp the mantissa to 10-bits
163 mantissa &= ((1 << 10) - 1);
164 // shift left to generate single-precision mantissa of 23-bits
168 // shift left to generate single-precision mantissa of 23-bits
170 // generate single precision biased exponent value
171 exp = (exp << 13) + HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP;
174 f.i = (sign << 31) | exp | mantissa;
181 * Convert from 32-bit float as uint32_t to uint16_t.
183 * @param v 32-bit float
185 * @return normalized 16-bit unsigned int
187 static inline uint16_t exr_flt2uint(uint32_t v)
189 unsigned int exp = v >> 23;
190 // "HACK": negative values result in exp< 0, so clipping them to 0
191 // is also handled by this condition, avoids explicit check for sign bit.
192 if (exp <= 127 + 7 - 24) // we would shift out all bits anyway
197 return (v + (1 << 23)) >> (127 + 7 - exp);
201 * Convert from 16-bit float as uint16_t to uint16_t.
203 * @param v 16-bit float
205 * @return normalized 16-bit unsigned int
207 static inline uint16_t exr_halflt2uint(uint16_t v)
209 unsigned exp = 14 - (v >> 10);
214 return (v & 0x8000) ? 0 : 0xffff;
217 return (v + (1 << 16)) >> (exp + 1);
220 static void predictor(uint8_t *src, int size)
222 uint8_t *t = src + 1;
223 uint8_t *stop = src + size;
226 int d = (int) t[-1] + (int) t[0] - 128;
232 static void reorder_pixels(uint8_t *src, uint8_t *dst, int size)
234 const int8_t *t1 = src;
235 const int8_t *t2 = src + (size + 1) / 2;
237 int8_t *stop = s + size;
252 static int zip_uncompress(const uint8_t *src, int compressed_size,
253 int uncompressed_size, EXRThreadData *td)
255 unsigned long dest_len = uncompressed_size;
257 if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK ||
258 dest_len != uncompressed_size)
259 return AVERROR_INVALIDDATA;
261 predictor(td->tmp, uncompressed_size);
262 reorder_pixels(td->tmp, td->uncompressed_data, uncompressed_size);
267 static int rle_uncompress(const uint8_t *src, int compressed_size,
268 int uncompressed_size, EXRThreadData *td)
270 uint8_t *d = td->tmp;
271 const int8_t *s = src;
272 int ssize = compressed_size;
273 int dsize = uncompressed_size;
274 uint8_t *dend = d + dsize;
283 if ((dsize -= count) < 0 ||
284 (ssize -= count + 1) < 0)
285 return AVERROR_INVALIDDATA;
292 if ((dsize -= count) < 0 ||
294 return AVERROR_INVALIDDATA;
304 return AVERROR_INVALIDDATA;
306 predictor(td->tmp, uncompressed_size);
307 reorder_pixels(td->tmp, td->uncompressed_data, uncompressed_size);
312 #define USHORT_RANGE (1 << 16)
313 #define BITMAP_SIZE (1 << 13)
315 static uint16_t reverse_lut(const uint8_t *bitmap, uint16_t *lut)
319 for (i = 0; i < USHORT_RANGE; i++)
320 if ((i == 0) || (bitmap[i >> 3] & (1 << (i & 7))))
325 memset(lut + k, 0, (USHORT_RANGE - k) * 2);
330 static void apply_lut(const uint16_t *lut, uint16_t *dst, int dsize)
334 for (i = 0; i < dsize; ++i)
335 dst[i] = lut[dst[i]];
338 #define HUF_ENCBITS 16 // literal (value) bit length
339 #define HUF_DECBITS 14 // decoding bit size (>= 8)
341 #define HUF_ENCSIZE ((1 << HUF_ENCBITS) + 1) // encoding table size
342 #define HUF_DECSIZE (1 << HUF_DECBITS) // decoding table size
343 #define HUF_DECMASK (HUF_DECSIZE - 1)
345 typedef struct HufDec {
351 static void huf_canonical_code_table(uint64_t *hcode)
353 uint64_t c, n[59] = { 0 };
356 for (i = 0; i < HUF_ENCSIZE; ++i)
360 for (i = 58; i > 0; --i) {
361 uint64_t nc = ((c + n[i]) >> 1);
366 for (i = 0; i < HUF_ENCSIZE; ++i) {
370 hcode[i] = l | (n[l]++ << 6);
374 #define SHORT_ZEROCODE_RUN 59
375 #define LONG_ZEROCODE_RUN 63
376 #define SHORTEST_LONG_RUN (2 + LONG_ZEROCODE_RUN - SHORT_ZEROCODE_RUN)
377 #define LONGEST_LONG_RUN (255 + SHORTEST_LONG_RUN)
379 static int huf_unpack_enc_table(GetByteContext *gb,
380 int32_t im, int32_t iM, uint64_t *hcode)
383 int ret = init_get_bits8(&gbit, gb->buffer, bytestream2_get_bytes_left(gb));
387 for (; im <= iM; im++) {
388 uint64_t l = hcode[im] = get_bits(&gbit, 6);
390 if (l == LONG_ZEROCODE_RUN) {
391 int zerun = get_bits(&gbit, 8) + SHORTEST_LONG_RUN;
393 if (im + zerun > iM + 1)
394 return AVERROR_INVALIDDATA;
400 } else if (l >= SHORT_ZEROCODE_RUN) {
401 int zerun = l - SHORT_ZEROCODE_RUN + 2;
403 if (im + zerun > iM + 1)
404 return AVERROR_INVALIDDATA;
413 bytestream2_skip(gb, (get_bits_count(&gbit) + 7) / 8);
414 huf_canonical_code_table(hcode);
419 static int huf_build_dec_table(const uint64_t *hcode, int im,
420 int iM, HufDec *hdecod)
422 for (; im <= iM; im++) {
423 uint64_t c = hcode[im] >> 6;
424 int i, l = hcode[im] & 63;
427 return AVERROR_INVALIDDATA;
429 if (l > HUF_DECBITS) {
430 HufDec *pl = hdecod + (c >> (l - HUF_DECBITS));
432 return AVERROR_INVALIDDATA;
436 pl->p = av_realloc(pl->p, pl->lit * sizeof(int));
438 return AVERROR(ENOMEM);
440 pl->p[pl->lit - 1] = im;
442 HufDec *pl = hdecod + (c << (HUF_DECBITS - l));
444 for (i = 1 << (HUF_DECBITS - l); i > 0; i--, pl++) {
445 if (pl->len || pl->p)
446 return AVERROR_INVALIDDATA;
456 #define get_char(c, lc, gb) \
458 c = (c << 8) | bytestream2_get_byte(gb); \
462 #define get_code(po, rlc, c, lc, gb, out, oe) \
466 get_char(c, lc, gb); \
472 return AVERROR_INVALIDDATA; \
478 } else if (out < oe) { \
481 return AVERROR_INVALIDDATA; \
485 static int huf_decode(const uint64_t *hcode, const HufDec *hdecod,
486 GetByteContext *gb, int nbits,
487 int rlc, int no, uint16_t *out)
490 uint16_t *outb = out;
491 uint16_t *oe = out + no;
492 const uint8_t *ie = gb->buffer + (nbits + 7) / 8; // input byte size
496 while (gb->buffer < ie) {
499 while (lc >= HUF_DECBITS) {
500 const HufDec pl = hdecod[(c >> (lc - HUF_DECBITS)) & HUF_DECMASK];
504 get_code(pl.lit, rlc, c, lc, gb, out, oe);
509 return AVERROR_INVALIDDATA;
511 for (j = 0; j < pl.lit; j++) {
512 int l = hcode[pl.p[j]] & 63;
514 while (lc < l && bytestream2_get_bytes_left(gb) > 0)
518 if ((hcode[pl.p[j]] >> 6) ==
519 ((c >> (lc - l)) & ((1LL << l) - 1))) {
521 get_code(pl.p[j], rlc, c, lc, gb, out, oe);
528 return AVERROR_INVALIDDATA;
538 const HufDec pl = hdecod[(c << (HUF_DECBITS - lc)) & HUF_DECMASK];
542 get_code(pl.lit, rlc, c, lc, gb, out, oe);
544 return AVERROR_INVALIDDATA;
548 if (out - outb != no)
549 return AVERROR_INVALIDDATA;
553 static int huf_uncompress(GetByteContext *gb,
554 uint16_t *dst, int dst_size)
556 int32_t src_size, im, iM;
562 src_size = bytestream2_get_le32(gb);
563 im = bytestream2_get_le32(gb);
564 iM = bytestream2_get_le32(gb);
565 bytestream2_skip(gb, 4);
566 nBits = bytestream2_get_le32(gb);
567 if (im < 0 || im >= HUF_ENCSIZE ||
568 iM < 0 || iM >= HUF_ENCSIZE ||
570 return AVERROR_INVALIDDATA;
572 bytestream2_skip(gb, 4);
574 freq = av_mallocz_array(HUF_ENCSIZE, sizeof(*freq));
575 hdec = av_mallocz_array(HUF_DECSIZE, sizeof(*hdec));
576 if (!freq || !hdec) {
577 ret = AVERROR(ENOMEM);
581 if ((ret = huf_unpack_enc_table(gb, im, iM, freq)) < 0)
584 if (nBits > 8 * bytestream2_get_bytes_left(gb)) {
585 ret = AVERROR_INVALIDDATA;
589 if ((ret = huf_build_dec_table(freq, im, iM, hdec)) < 0)
591 ret = huf_decode(freq, hdec, gb, nBits, iM, dst_size, dst);
594 for (i = 0; i < HUF_DECSIZE; i++)
596 av_freep(&hdec[i].p);
604 static inline void wdec14(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
609 int ai = ls + (hi & 1) + (hi >> 1);
611 int16_t bs = ai - hi;
618 #define A_OFFSET (1 << (NBITS - 1))
619 #define MOD_MASK ((1 << NBITS) - 1)
621 static inline void wdec16(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
625 int bb = (m - (d >> 1)) & MOD_MASK;
626 int aa = (d + bb - A_OFFSET) & MOD_MASK;
631 static void wav_decode(uint16_t *in, int nx, int ox,
632 int ny, int oy, uint16_t mx)
634 int w14 = (mx < (1 << 14));
635 int n = (nx > ny) ? ny : nx;
648 uint16_t *ey = in + oy * (ny - p2);
649 uint16_t i00, i01, i10, i11;
655 for (; py <= ey; py += oy2) {
657 uint16_t *ex = py + ox * (nx - p2);
659 for (; px <= ex; px += ox2) {
660 uint16_t *p01 = px + ox1;
661 uint16_t *p10 = px + oy1;
662 uint16_t *p11 = p10 + ox1;
665 wdec14(*px, *p10, &i00, &i10);
666 wdec14(*p01, *p11, &i01, &i11);
667 wdec14(i00, i01, px, p01);
668 wdec14(i10, i11, p10, p11);
670 wdec16(*px, *p10, &i00, &i10);
671 wdec16(*p01, *p11, &i01, &i11);
672 wdec16(i00, i01, px, p01);
673 wdec16(i10, i11, p10, p11);
678 uint16_t *p10 = px + oy1;
681 wdec14(*px, *p10, &i00, p10);
683 wdec16(*px, *p10, &i00, p10);
691 uint16_t *ex = py + ox * (nx - p2);
693 for (; px <= ex; px += ox2) {
694 uint16_t *p01 = px + ox1;
697 wdec14(*px, *p01, &i00, p01);
699 wdec16(*px, *p01, &i00, p01);
710 static int piz_uncompress(EXRContext *s, const uint8_t *src, int ssize,
711 int dsize, EXRThreadData *td)
714 uint16_t maxval, min_non_zero, max_non_zero;
716 uint16_t *tmp = (uint16_t *)td->tmp;
721 td->bitmap = av_malloc(BITMAP_SIZE);
723 td->lut = av_malloc(1 << 17);
724 if (!td->bitmap || !td->lut) {
725 av_freep(&td->bitmap);
727 return AVERROR(ENOMEM);
730 bytestream2_init(&gb, src, ssize);
731 min_non_zero = bytestream2_get_le16(&gb);
732 max_non_zero = bytestream2_get_le16(&gb);
734 if (max_non_zero >= BITMAP_SIZE)
735 return AVERROR_INVALIDDATA;
737 memset(td->bitmap, 0, FFMIN(min_non_zero, BITMAP_SIZE));
738 if (min_non_zero <= max_non_zero)
739 bytestream2_get_buffer(&gb, td->bitmap + min_non_zero,
740 max_non_zero - min_non_zero + 1);
741 memset(td->bitmap + max_non_zero, 0, BITMAP_SIZE - max_non_zero);
743 maxval = reverse_lut(td->bitmap, td->lut);
745 ret = huf_uncompress(&gb, tmp, dsize / sizeof(uint16_t));
750 for (i = 0; i < s->nb_channels; i++) {
751 EXRChannel *channel = &s->channels[i];
752 int size = channel->pixel_type;
754 for (j = 0; j < size; j++)
755 wav_decode(ptr + j, s->xdelta, size, s->ysize,
756 s->xdelta * size, maxval);
757 ptr += s->xdelta * s->ysize * size;
760 apply_lut(td->lut, tmp, dsize / sizeof(uint16_t));
762 out = td->uncompressed_data;
763 for (i = 0; i < s->ysize; i++)
764 for (j = 0; j < s->nb_channels; j++) {
765 uint16_t *in = tmp + j * s->xdelta * s->ysize + i * s->xdelta;
766 memcpy(out, in, s->xdelta * 2);
767 out += s->xdelta * 2;
773 static int pxr24_uncompress(EXRContext *s, const uint8_t *src,
774 int compressed_size, int uncompressed_size,
777 unsigned long dest_len = uncompressed_size;
778 const uint8_t *in = td->tmp;
782 if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK ||
783 dest_len != uncompressed_size)
784 return AVERROR_INVALIDDATA;
786 out = td->uncompressed_data;
787 for (i = 0; i < s->ysize; i++)
788 for (c = 0; c < s->nb_channels; c++) {
789 EXRChannel *channel = &s->channels[c];
790 const uint8_t *ptr[4];
793 switch (channel->pixel_type) {
796 ptr[1] = ptr[0] + s->xdelta;
797 ptr[2] = ptr[1] + s->xdelta;
798 in = ptr[2] + s->xdelta;
800 for (j = 0; j < s->xdelta; ++j) {
801 uint32_t diff = (*(ptr[0]++) << 24) |
802 (*(ptr[1]++) << 16) |
805 bytestream_put_le32(&out, pixel);
810 ptr[1] = ptr[0] + s->xdelta;
811 in = ptr[1] + s->xdelta;
812 for (j = 0; j < s->xdelta; j++) {
813 uint32_t diff = (*(ptr[0]++) << 8) | *(ptr[1]++);
816 bytestream_put_le16(&out, pixel);
820 return AVERROR_INVALIDDATA;
827 static int decode_block(AVCodecContext *avctx, void *tdata,
828 int jobnr, int threadnr)
830 EXRContext *s = avctx->priv_data;
831 AVFrame *const p = s->picture;
832 EXRThreadData *td = &s->thread_data[threadnr];
833 const uint8_t *channel_buffer[4] = { 0 };
834 const uint8_t *buf = s->buf;
835 uint64_t line_offset, uncompressed_size;
836 uint32_t xdelta = s->xdelta;
839 uint32_t data_size, line;
841 int axmax = (avctx->width - (s->xmax + 1)) * 2 * s->desc->nb_components;
842 int bxmin = s->xmin * 2 * s->desc->nb_components;
843 int i, x, buf_size = s->buf_size;
844 float one_gamma = 1.0f / s->gamma;
847 line_offset = AV_RL64(s->gb.buffer + jobnr * 8);
848 // Check if the buffer has the required bytes needed from the offset
849 if (line_offset > buf_size - 8)
850 return AVERROR_INVALIDDATA;
852 src = buf + line_offset + 8;
853 line = AV_RL32(src - 8);
854 if (line < s->ymin || line > s->ymax)
855 return AVERROR_INVALIDDATA;
857 data_size = AV_RL32(src - 4);
858 if (data_size <= 0 || data_size > buf_size)
859 return AVERROR_INVALIDDATA;
861 s->ysize = FFMIN(s->scan_lines_per_block, s->ymax - line + 1);
862 uncompressed_size = s->scan_line_size * s->ysize;
863 if ((s->compression == EXR_RAW && (data_size != uncompressed_size ||
864 line_offset > buf_size - uncompressed_size)) ||
865 (s->compression != EXR_RAW && (data_size > uncompressed_size ||
866 line_offset > buf_size - data_size))) {
867 return AVERROR_INVALIDDATA;
870 if (data_size < uncompressed_size) {
871 av_fast_padded_malloc(&td->uncompressed_data,
872 &td->uncompressed_size, uncompressed_size);
873 av_fast_padded_malloc(&td->tmp, &td->tmp_size, uncompressed_size);
874 if (!td->uncompressed_data || !td->tmp)
875 return AVERROR(ENOMEM);
877 ret = AVERROR_INVALIDDATA;
878 switch (s->compression) {
881 ret = zip_uncompress(src, data_size, uncompressed_size, td);
884 ret = piz_uncompress(s, src, data_size, uncompressed_size, td);
887 ret = pxr24_uncompress(s, src, data_size, uncompressed_size, td);
890 ret = rle_uncompress(src, data_size, uncompressed_size, td);
893 av_log(avctx, AV_LOG_ERROR, "decode_block() failed.\n");
896 src = td->uncompressed_data;
899 channel_buffer[0] = src + xdelta * s->channel_offsets[0];
900 channel_buffer[1] = src + xdelta * s->channel_offsets[1];
901 channel_buffer[2] = src + xdelta * s->channel_offsets[2];
902 if (s->channel_offsets[3] >= 0)
903 channel_buffer[3] = src + xdelta * s->channel_offsets[3];
905 ptr = p->data[0] + line * p->linesize[0];
907 i < s->scan_lines_per_block && line + i <= s->ymax;
908 i++, ptr += p->linesize[0]) {
909 const uint8_t *r, *g, *b, *a;
911 r = channel_buffer[0];
912 g = channel_buffer[1];
913 b = channel_buffer[2];
914 if (channel_buffer[3])
915 a = channel_buffer[3];
917 ptr_x = (uint16_t *) ptr;
919 // Zero out the start if xmin is not 0
920 memset(ptr_x, 0, bxmin);
921 ptr_x += s->xmin * s->desc->nb_components;
922 if (s->pixel_type == EXR_FLOAT) {
924 for (x = 0; x < xdelta; x++) {
925 union av_intfloat32 t;
926 t.i = bytestream_get_le32(&r);
927 if (t.f > 0.0f) /* avoid negative values */
928 t.f = powf(t.f, one_gamma);
929 *ptr_x++ = exr_flt2uint(t.i);
931 t.i = bytestream_get_le32(&g);
933 t.f = powf(t.f, one_gamma);
934 *ptr_x++ = exr_flt2uint(t.i);
936 t.i = bytestream_get_le32(&b);
938 t.f = powf(t.f, one_gamma);
939 *ptr_x++ = exr_flt2uint(t.i);
940 if (channel_buffer[3])
941 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a));
945 for (x = 0; x < xdelta; x++) {
946 *ptr_x++ = s->gamma_table[bytestream_get_le16(&r)];
947 *ptr_x++ = s->gamma_table[bytestream_get_le16(&g)];
948 *ptr_x++ = s->gamma_table[bytestream_get_le16(&b)];
949 if (channel_buffer[3])
950 *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&a));
954 // Zero out the end if xmax+1 is not w
955 memset(ptr_x, 0, axmax);
957 channel_buffer[0] += s->scan_line_size;
958 channel_buffer[1] += s->scan_line_size;
959 channel_buffer[2] += s->scan_line_size;
960 if (channel_buffer[3])
961 channel_buffer[3] += s->scan_line_size;
968 * Check if the variable name corresponds to its data type.
970 * @param s the EXRContext
971 * @param value_name name of the variable to check
972 * @param value_type type of the variable to check
973 * @param minimum_length minimum length of the variable data
975 * @return bytes to read containing variable data
976 * -1 if variable is not found
977 * 0 if buffer ended prematurely
979 static int check_header_variable(EXRContext *s,
980 const char *value_name,
981 const char *value_type,
982 unsigned int minimum_length)
986 if (bytestream2_get_bytes_left(&s->gb) >= minimum_length &&
987 !strcmp(s->gb.buffer, value_name)) {
988 // found value_name, jump to value_type (null terminated strings)
989 s->gb.buffer += strlen(value_name) + 1;
990 if (!strcmp(s->gb.buffer, value_type)) {
991 s->gb.buffer += strlen(value_type) + 1;
992 var_size = bytestream2_get_le32(&s->gb);
993 // don't go read past boundaries
994 if (var_size > bytestream2_get_bytes_left(&s->gb))
997 // value_type not found, reset the buffer
998 s->gb.buffer -= strlen(value_name) + 1;
999 av_log(s->avctx, AV_LOG_WARNING,
1000 "Unknown data type %s for header variable %s.\n",
1001 value_type, value_name);
1008 static int decode_header(EXRContext *s)
1010 int current_channel_offset = 0;
1011 int magic_number, version, flags, i;
1019 s->channel_offsets[0] = -1;
1020 s->channel_offsets[1] = -1;
1021 s->channel_offsets[2] = -1;
1022 s->channel_offsets[3] = -1;
1023 s->pixel_type = EXR_UNKNOWN;
1024 s->compression = EXR_UNKN;
1029 if (bytestream2_get_bytes_left(&s->gb) < 10) {
1030 av_log(s->avctx, AV_LOG_ERROR, "Header too short to parse.\n");
1031 return AVERROR_INVALIDDATA;
1034 magic_number = bytestream2_get_le32(&s->gb);
1035 if (magic_number != 20000630) {
1036 /* As per documentation of OpenEXR, it is supposed to be
1037 * int 20000630 little-endian */
1038 av_log(s->avctx, AV_LOG_ERROR, "Wrong magic number %d.\n", magic_number);
1039 return AVERROR_INVALIDDATA;
1042 version = bytestream2_get_byte(&s->gb);
1044 avpriv_report_missing_feature(s->avctx, "Version %d", version);
1045 return AVERROR_PATCHWELCOME;
1048 flags = bytestream2_get_le24(&s->gb);
1050 avpriv_report_missing_feature(s->avctx, "Tile support");
1051 return AVERROR_PATCHWELCOME;
1055 while (bytestream2_get_bytes_left(&s->gb) > 0 && *s->gb.buffer) {
1057 if ((var_size = check_header_variable(s, "channels",
1058 "chlist", 38)) >= 0) {
1059 GetByteContext ch_gb;
1061 return AVERROR_INVALIDDATA;
1063 bytestream2_init(&ch_gb, s->gb.buffer, var_size);
1065 while (bytestream2_get_bytes_left(&ch_gb) >= 19) {
1066 EXRChannel *channel;
1067 enum ExrPixelType current_pixel_type;
1068 int channel_index = -1;
1071 if (strcmp(s->layer, "") != 0) {
1072 if (strncmp(ch_gb.buffer, s->layer, strlen(s->layer)) == 0) {
1073 ch_gb.buffer += strlen(s->layer);
1074 if (*ch_gb.buffer == '.')
1075 ch_gb.buffer++; /* skip dot if not given */
1076 av_log(s->avctx, AV_LOG_INFO,
1077 "Layer %s.%s matched.\n", s->layer, ch_gb.buffer);
1081 if (!strcmp(ch_gb.buffer, "R") ||
1082 !strcmp(ch_gb.buffer, "X") ||
1083 !strcmp(ch_gb.buffer, "U"))
1085 else if (!strcmp(ch_gb.buffer, "G") ||
1086 !strcmp(ch_gb.buffer, "Y") ||
1087 !strcmp(ch_gb.buffer, "V"))
1089 else if (!strcmp(ch_gb.buffer, "B") ||
1090 !strcmp(ch_gb.buffer, "Z") ||
1091 !strcmp(ch_gb.buffer, "W"))
1093 else if (!strcmp(ch_gb.buffer, "A"))
1096 av_log(s->avctx, AV_LOG_WARNING,
1097 "Unsupported channel %.256s.\n", ch_gb.buffer);
1099 /* skip until you get a 0 */
1100 while (bytestream2_get_bytes_left(&ch_gb) > 0 &&
1101 bytestream2_get_byte(&ch_gb))
1104 if (bytestream2_get_bytes_left(&ch_gb) < 4) {
1105 av_log(s->avctx, AV_LOG_ERROR, "Incomplete header.\n");
1106 return AVERROR_INVALIDDATA;
1109 current_pixel_type = bytestream2_get_le32(&ch_gb);
1110 if (current_pixel_type >= EXR_UNKNOWN) {
1111 avpriv_report_missing_feature(s->avctx,
1113 current_pixel_type);
1114 return AVERROR_PATCHWELCOME;
1117 bytestream2_skip(&ch_gb, 4);
1118 xsub = bytestream2_get_le32(&ch_gb);
1119 ysub = bytestream2_get_le32(&ch_gb);
1120 if (xsub != 1 || ysub != 1) {
1121 avpriv_report_missing_feature(s->avctx,
1122 "Subsampling %dx%d",
1124 return AVERROR_PATCHWELCOME;
1127 if (channel_index >= 0) {
1128 if (s->pixel_type != EXR_UNKNOWN &&
1129 s->pixel_type != current_pixel_type) {
1130 av_log(s->avctx, AV_LOG_ERROR,
1131 "RGB channels not of the same depth.\n");
1132 return AVERROR_INVALIDDATA;
1134 s->pixel_type = current_pixel_type;
1135 s->channel_offsets[channel_index] = current_channel_offset;
1138 s->channels = av_realloc(s->channels,
1139 ++s->nb_channels * sizeof(EXRChannel));
1141 return AVERROR(ENOMEM);
1142 channel = &s->channels[s->nb_channels - 1];
1143 channel->pixel_type = current_pixel_type;
1144 channel->xsub = xsub;
1145 channel->ysub = ysub;
1147 current_channel_offset += 1 << current_pixel_type;
1150 /* Check if all channels are set with an offset or if the channels
1151 * are causing an overflow */
1152 if (FFMIN3(s->channel_offsets[0],
1153 s->channel_offsets[1],
1154 s->channel_offsets[2]) < 0) {
1155 if (s->channel_offsets[0] < 0)
1156 av_log(s->avctx, AV_LOG_ERROR, "Missing red channel.\n");
1157 if (s->channel_offsets[1] < 0)
1158 av_log(s->avctx, AV_LOG_ERROR, "Missing green channel.\n");
1159 if (s->channel_offsets[2] < 0)
1160 av_log(s->avctx, AV_LOG_ERROR, "Missing blue channel.\n");
1161 return AVERROR_INVALIDDATA;
1164 // skip one last byte and update main gb
1165 s->gb.buffer = ch_gb.buffer + 1;
1167 } else if ((var_size = check_header_variable(s, "dataWindow", "box2i",
1170 return AVERROR_INVALIDDATA;
1172 s->xmin = bytestream2_get_le32(&s->gb);
1173 s->ymin = bytestream2_get_le32(&s->gb);
1174 s->xmax = bytestream2_get_le32(&s->gb);
1175 s->ymax = bytestream2_get_le32(&s->gb);
1176 s->xdelta = (s->xmax - s->xmin) + 1;
1177 s->ydelta = (s->ymax - s->ymin) + 1;
1180 } else if ((var_size = check_header_variable(s, "displayWindow",
1181 "box2i", 34)) >= 0) {
1183 return AVERROR_INVALIDDATA;
1185 bytestream2_skip(&s->gb, 8);
1186 s->w = bytestream2_get_le32(&s->gb) + 1;
1187 s->h = bytestream2_get_le32(&s->gb) + 1;
1190 } else if ((var_size = check_header_variable(s, "lineOrder",
1191 "lineOrder", 25)) >= 0) {
1194 return AVERROR_INVALIDDATA;
1196 line_order = bytestream2_get_byte(&s->gb);
1197 av_log(s->avctx, AV_LOG_DEBUG, "line order: %d.\n", line_order);
1198 if (line_order > 2) {
1199 av_log(s->avctx, AV_LOG_ERROR, "Unknown line order.\n");
1200 return AVERROR_INVALIDDATA;
1204 } else if ((var_size = check_header_variable(s, "pixelAspectRatio",
1205 "float", 31)) >= 0) {
1207 return AVERROR_INVALIDDATA;
1209 ff_set_sar(s->avctx,
1210 av_d2q(av_int2float(bytestream2_get_le32(&s->gb)), 255));
1213 } else if ((var_size = check_header_variable(s, "compression",
1214 "compression", 29)) >= 0) {
1216 return AVERROR_INVALIDDATA;
1218 if (s->compression == EXR_UNKN)
1219 s->compression = bytestream2_get_byte(&s->gb);
1221 av_log(s->avctx, AV_LOG_WARNING,
1222 "Found more than one compression attribute.\n");
1227 // Check if there are enough bytes for a header
1228 if (bytestream2_get_bytes_left(&s->gb) <= 9) {
1229 av_log(s->avctx, AV_LOG_ERROR, "Incomplete header\n");
1230 return AVERROR_INVALIDDATA;
1233 // Process unknown variables
1234 for (i = 0; i < 2; i++) // value_name and value_type
1235 while (bytestream2_get_byte(&s->gb) != 0);
1237 // Skip variable length
1238 bytestream2_skip(&s->gb, bytestream2_get_le32(&s->gb));
1241 if (s->compression == EXR_UNKN) {
1242 av_log(s->avctx, AV_LOG_ERROR, "Missing compression attribute.\n");
1243 return AVERROR_INVALIDDATA;
1245 s->scan_line_size = s->xdelta * current_channel_offset;
1247 if (bytestream2_get_bytes_left(&s->gb) <= 0) {
1248 av_log(s->avctx, AV_LOG_ERROR, "Incomplete frame.\n");
1249 return AVERROR_INVALIDDATA;
1252 // aaand we are done
1253 bytestream2_skip(&s->gb, 1);
1257 static int decode_frame(AVCodecContext *avctx, void *data,
1258 int *got_frame, AVPacket *avpkt)
1260 EXRContext *s = avctx->priv_data;
1261 ThreadFrame frame = { .f = data };
1262 AVFrame *picture = data;
1267 int scan_line_blocks;
1269 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
1271 if ((ret = decode_header(s)) < 0)
1274 switch (s->pixel_type) {
1277 if (s->channel_offsets[3] >= 0)
1278 avctx->pix_fmt = AV_PIX_FMT_RGBA64;
1280 avctx->pix_fmt = AV_PIX_FMT_RGB48;
1283 avpriv_request_sample(avctx, "32-bit unsigned int");
1284 return AVERROR_PATCHWELCOME;
1286 av_log(avctx, AV_LOG_ERROR, "Missing channel list.\n");
1287 return AVERROR_INVALIDDATA;
1290 switch (s->compression) {
1294 s->scan_lines_per_block = 1;
1298 s->scan_lines_per_block = 16;
1301 s->scan_lines_per_block = 32;
1304 avpriv_report_missing_feature(avctx, "Compression %d", s->compression);
1305 return AVERROR_PATCHWELCOME;
1308 /* Verify the xmin, xmax, ymin, ymax and xdelta before setting
1309 * the actual image size. */
1310 if (s->xmin > s->xmax ||
1311 s->ymin > s->ymax ||
1312 s->xdelta != s->xmax - s->xmin + 1 ||
1315 av_log(avctx, AV_LOG_ERROR, "Wrong or missing size information.\n");
1316 return AVERROR_INVALIDDATA;
1319 if ((ret = ff_set_dimensions(avctx, s->w, s->h)) < 0)
1322 s->desc = av_pix_fmt_desc_get(avctx->pix_fmt);
1324 return AVERROR_INVALIDDATA;
1325 out_line_size = avctx->width * 2 * s->desc->nb_components;
1326 scan_line_blocks = (s->ydelta + s->scan_lines_per_block - 1) /
1327 s->scan_lines_per_block;
1329 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
1332 if (bytestream2_get_bytes_left(&s->gb) < scan_line_blocks * 8)
1333 return AVERROR_INVALIDDATA;
1335 // save pointer we are going to use in decode_block
1336 s->buf = avpkt->data;
1337 s->buf_size = avpkt->size;
1338 ptr = picture->data[0];
1340 // Zero out the start if ymin is not 0
1341 for (y = 0; y < s->ymin; y++) {
1342 memset(ptr, 0, out_line_size);
1343 ptr += picture->linesize[0];
1346 s->picture = picture;
1347 avctx->execute2(avctx, decode_block, s->thread_data, NULL, scan_line_blocks);
1349 // Zero out the end if ymax+1 is not h
1350 for (y = s->ymax + 1; y < avctx->height; y++) {
1351 memset(ptr, 0, out_line_size);
1352 ptr += picture->linesize[0];
1355 picture->pict_type = AV_PICTURE_TYPE_I;
1361 static av_cold int decode_init(AVCodecContext *avctx)
1363 EXRContext *s = avctx->priv_data;
1365 union av_intfloat32 t;
1366 float one_gamma = 1.0f / s->gamma;
1370 if (one_gamma > 0.9999f && one_gamma < 1.0001f) {
1371 for (i = 0; i < 65536; ++i)
1372 s->gamma_table[i] = exr_halflt2uint(i);
1374 for (i = 0; i < 65536; ++i) {
1375 t = exr_half2float(i);
1376 /* If negative value we reuse half value */
1378 s->gamma_table[i] = exr_halflt2uint(i);
1380 t.f = powf(t.f, one_gamma);
1381 s->gamma_table[i] = exr_flt2uint(t.i);
1386 // allocate thread data, used for non EXR_RAW compreesion types
1387 s->thread_data = av_mallocz_array(avctx->thread_count, sizeof(EXRThreadData));
1388 if (!s->thread_data)
1389 return AVERROR_INVALIDDATA;
1394 static int decode_init_thread_copy(AVCodecContext *avctx)
1395 { EXRContext *s = avctx->priv_data;
1397 // allocate thread data, used for non EXR_RAW compreesion types
1398 s->thread_data = av_mallocz_array(avctx->thread_count, sizeof(EXRThreadData));
1399 if (!s->thread_data)
1400 return AVERROR_INVALIDDATA;
1405 static av_cold int decode_end(AVCodecContext *avctx)
1407 EXRContext *s = avctx->priv_data;
1409 for (i = 0; i < avctx->thread_count; i++) {
1410 EXRThreadData *td = &s->thread_data[i];
1411 av_freep(&td->uncompressed_data);
1413 av_freep(&td->bitmap);
1417 av_freep(&s->thread_data);
1418 av_freep(&s->channels);
1423 #define OFFSET(x) offsetof(EXRContext, x)
1424 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1425 static const AVOption options[] = {
1426 { "layer", "Set the decoding layer", OFFSET(layer),
1427 AV_OPT_TYPE_STRING, { .str = "" }, 0, 0, VD },
1428 { "gamma", "Set the float gamma value when decoding", OFFSET(gamma),
1429 AV_OPT_TYPE_FLOAT, { .dbl = 1.0f }, 0.001, FLT_MAX, VD },
1433 static const AVClass exr_class = {
1434 .class_name = "EXR",
1435 .item_name = av_default_item_name,
1437 .version = LIBAVUTIL_VERSION_INT,
1440 AVCodec ff_exr_decoder = {
1442 .long_name = NULL_IF_CONFIG_SMALL("OpenEXR image"),
1443 .type = AVMEDIA_TYPE_VIDEO,
1444 .id = AV_CODEC_ID_EXR,
1445 .priv_data_size = sizeof(EXRContext),
1446 .init = decode_init,
1447 .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
1448 .close = decode_end,
1449 .decode = decode_frame,
1450 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS |
1451 AV_CODEC_CAP_SLICE_THREADS,
1452 .priv_class = &exr_class,