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"
40 #include "libavutil/color_utils.h"
43 #include "bytestream.h"
68 typedef struct EXRChannel {
70 enum ExrPixelType pixel_type;
73 typedef struct EXRThreadData {
74 uint8_t *uncompressed_data;
75 int uncompressed_size;
84 typedef struct EXRContext {
87 AVCodecContext *avctx;
89 enum ExrCompr compression;
90 enum ExrPixelType pixel_type;
91 int channel_offsets[4]; // 0 = red, 1 = green, 2 = blue and 3 = alpha
92 const AVPixFmtDescriptor *desc;
97 uint32_t xdelta, ydelta;
100 uint64_t scan_line_size;
101 int scan_lines_per_block;
107 EXRChannel *channels;
110 EXRThreadData *thread_data;
114 enum AVColorTransferCharacteristic apply_trc_type;
116 uint16_t gamma_table[65536];
119 /* -15 stored using a single precision bias of 127 */
120 #define HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP 0x38000000
122 /* max exponent value in single precision that will be converted
123 * to Inf or Nan when stored as a half-float */
124 #define HALF_FLOAT_MAX_BIASED_EXP_AS_SINGLE_FP_EXP 0x47800000
126 /* 255 is the max exponent biased value */
127 #define FLOAT_MAX_BIASED_EXP (0xFF << 23)
129 #define HALF_FLOAT_MAX_BIASED_EXP (0x1F << 10)
132 * Convert a half float as a uint16_t into a full float.
134 * @param hf half float as uint16_t
136 * @return float value
138 static union av_intfloat32 exr_half2float(uint16_t hf)
140 unsigned int sign = (unsigned int) (hf >> 15);
141 unsigned int mantissa = (unsigned int) (hf & ((1 << 10) - 1));
142 unsigned int exp = (unsigned int) (hf & HALF_FLOAT_MAX_BIASED_EXP);
143 union av_intfloat32 f;
145 if (exp == HALF_FLOAT_MAX_BIASED_EXP) {
146 // we have a half-float NaN or Inf
147 // half-float NaNs will be converted to a single precision NaN
148 // half-float Infs will be converted to a single precision Inf
149 exp = FLOAT_MAX_BIASED_EXP;
151 mantissa = (1 << 23) - 1; // set all bits to indicate a NaN
152 } else if (exp == 0x0) {
153 // convert half-float zero/denorm to single precision value
156 exp = HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP;
157 // check for leading 1 in denorm mantissa
158 while ((mantissa & (1 << 10))) {
159 // for every leading 0, decrement single precision exponent by 1
160 // and shift half-float mantissa value to the left
164 // clamp the mantissa to 10-bits
165 mantissa &= ((1 << 10) - 1);
166 // shift left to generate single-precision mantissa of 23-bits
170 // shift left to generate single-precision mantissa of 23-bits
172 // generate single precision biased exponent value
173 exp = (exp << 13) + HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP;
176 f.i = (sign << 31) | exp | mantissa;
183 * Convert from 32-bit float as uint32_t to uint16_t.
185 * @param v 32-bit float
187 * @return normalized 16-bit unsigned int
189 static inline uint16_t exr_flt2uint(uint32_t v)
191 unsigned int exp = v >> 23;
192 // "HACK": negative values result in exp< 0, so clipping them to 0
193 // is also handled by this condition, avoids explicit check for sign bit.
194 if (exp <= 127 + 7 - 24) // we would shift out all bits anyway
199 return (v + (1 << 23)) >> (127 + 7 - exp);
203 * Convert from 16-bit float as uint16_t to uint16_t.
205 * @param v 16-bit float
207 * @return normalized 16-bit unsigned int
209 static inline uint16_t exr_halflt2uint(uint16_t v)
211 unsigned exp = 14 - (v >> 10);
216 return (v & 0x8000) ? 0 : 0xffff;
219 return (v + (1 << 16)) >> (exp + 1);
222 static void predictor(uint8_t *src, int size)
224 uint8_t *t = src + 1;
225 uint8_t *stop = src + size;
228 int d = (int) t[-1] + (int) t[0] - 128;
234 static void reorder_pixels(uint8_t *src, uint8_t *dst, int size)
236 const int8_t *t1 = src;
237 const int8_t *t2 = src + (size + 1) / 2;
239 int8_t *stop = s + size;
254 static int zip_uncompress(const uint8_t *src, int compressed_size,
255 int uncompressed_size, EXRThreadData *td)
257 unsigned long dest_len = uncompressed_size;
259 if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK ||
260 dest_len != uncompressed_size)
261 return AVERROR_INVALIDDATA;
263 predictor(td->tmp, uncompressed_size);
264 reorder_pixels(td->tmp, td->uncompressed_data, uncompressed_size);
269 static int rle_uncompress(const uint8_t *src, int compressed_size,
270 int uncompressed_size, EXRThreadData *td)
272 uint8_t *d = td->tmp;
273 const int8_t *s = src;
274 int ssize = compressed_size;
275 int dsize = uncompressed_size;
276 uint8_t *dend = d + dsize;
285 if ((dsize -= count) < 0 ||
286 (ssize -= count + 1) < 0)
287 return AVERROR_INVALIDDATA;
294 if ((dsize -= count) < 0 ||
296 return AVERROR_INVALIDDATA;
306 return AVERROR_INVALIDDATA;
308 predictor(td->tmp, uncompressed_size);
309 reorder_pixels(td->tmp, td->uncompressed_data, uncompressed_size);
314 #define USHORT_RANGE (1 << 16)
315 #define BITMAP_SIZE (1 << 13)
317 static uint16_t reverse_lut(const uint8_t *bitmap, uint16_t *lut)
321 for (i = 0; i < USHORT_RANGE; i++)
322 if ((i == 0) || (bitmap[i >> 3] & (1 << (i & 7))))
327 memset(lut + k, 0, (USHORT_RANGE - k) * 2);
332 static void apply_lut(const uint16_t *lut, uint16_t *dst, int dsize)
336 for (i = 0; i < dsize; ++i)
337 dst[i] = lut[dst[i]];
340 #define HUF_ENCBITS 16 // literal (value) bit length
341 #define HUF_DECBITS 14 // decoding bit size (>= 8)
343 #define HUF_ENCSIZE ((1 << HUF_ENCBITS) + 1) // encoding table size
344 #define HUF_DECSIZE (1 << HUF_DECBITS) // decoding table size
345 #define HUF_DECMASK (HUF_DECSIZE - 1)
347 typedef struct HufDec {
353 static void huf_canonical_code_table(uint64_t *hcode)
355 uint64_t c, n[59] = { 0 };
358 for (i = 0; i < HUF_ENCSIZE; ++i)
362 for (i = 58; i > 0; --i) {
363 uint64_t nc = ((c + n[i]) >> 1);
368 for (i = 0; i < HUF_ENCSIZE; ++i) {
372 hcode[i] = l | (n[l]++ << 6);
376 #define SHORT_ZEROCODE_RUN 59
377 #define LONG_ZEROCODE_RUN 63
378 #define SHORTEST_LONG_RUN (2 + LONG_ZEROCODE_RUN - SHORT_ZEROCODE_RUN)
379 #define LONGEST_LONG_RUN (255 + SHORTEST_LONG_RUN)
381 static int huf_unpack_enc_table(GetByteContext *gb,
382 int32_t im, int32_t iM, uint64_t *hcode)
385 int ret = init_get_bits8(&gbit, gb->buffer, bytestream2_get_bytes_left(gb));
389 for (; im <= iM; im++) {
390 uint64_t l = hcode[im] = get_bits(&gbit, 6);
392 if (l == LONG_ZEROCODE_RUN) {
393 int zerun = get_bits(&gbit, 8) + SHORTEST_LONG_RUN;
395 if (im + zerun > iM + 1)
396 return AVERROR_INVALIDDATA;
402 } else if (l >= SHORT_ZEROCODE_RUN) {
403 int zerun = l - SHORT_ZEROCODE_RUN + 2;
405 if (im + zerun > iM + 1)
406 return AVERROR_INVALIDDATA;
415 bytestream2_skip(gb, (get_bits_count(&gbit) + 7) / 8);
416 huf_canonical_code_table(hcode);
421 static int huf_build_dec_table(const uint64_t *hcode, int im,
422 int iM, HufDec *hdecod)
424 for (; im <= iM; im++) {
425 uint64_t c = hcode[im] >> 6;
426 int i, l = hcode[im] & 63;
429 return AVERROR_INVALIDDATA;
431 if (l > HUF_DECBITS) {
432 HufDec *pl = hdecod + (c >> (l - HUF_DECBITS));
434 return AVERROR_INVALIDDATA;
438 pl->p = av_realloc(pl->p, pl->lit * sizeof(int));
440 return AVERROR(ENOMEM);
442 pl->p[pl->lit - 1] = im;
444 HufDec *pl = hdecod + (c << (HUF_DECBITS - l));
446 for (i = 1 << (HUF_DECBITS - l); i > 0; i--, pl++) {
447 if (pl->len || pl->p)
448 return AVERROR_INVALIDDATA;
458 #define get_char(c, lc, gb) \
460 c = (c << 8) | bytestream2_get_byte(gb); \
464 #define get_code(po, rlc, c, lc, gb, out, oe) \
468 get_char(c, lc, gb); \
474 return AVERROR_INVALIDDATA; \
480 } else if (out < oe) { \
483 return AVERROR_INVALIDDATA; \
487 static int huf_decode(const uint64_t *hcode, const HufDec *hdecod,
488 GetByteContext *gb, int nbits,
489 int rlc, int no, uint16_t *out)
492 uint16_t *outb = out;
493 uint16_t *oe = out + no;
494 const uint8_t *ie = gb->buffer + (nbits + 7) / 8; // input byte size
498 while (gb->buffer < ie) {
501 while (lc >= HUF_DECBITS) {
502 const HufDec pl = hdecod[(c >> (lc - HUF_DECBITS)) & HUF_DECMASK];
506 get_code(pl.lit, rlc, c, lc, gb, out, oe);
511 return AVERROR_INVALIDDATA;
513 for (j = 0; j < pl.lit; j++) {
514 int l = hcode[pl.p[j]] & 63;
516 while (lc < l && bytestream2_get_bytes_left(gb) > 0)
520 if ((hcode[pl.p[j]] >> 6) ==
521 ((c >> (lc - l)) & ((1LL << l) - 1))) {
523 get_code(pl.p[j], rlc, c, lc, gb, out, oe);
530 return AVERROR_INVALIDDATA;
540 const HufDec pl = hdecod[(c << (HUF_DECBITS - lc)) & HUF_DECMASK];
544 get_code(pl.lit, rlc, c, lc, gb, out, oe);
546 return AVERROR_INVALIDDATA;
550 if (out - outb != no)
551 return AVERROR_INVALIDDATA;
555 static int huf_uncompress(GetByteContext *gb,
556 uint16_t *dst, int dst_size)
558 int32_t src_size, im, iM;
564 src_size = bytestream2_get_le32(gb);
565 im = bytestream2_get_le32(gb);
566 iM = bytestream2_get_le32(gb);
567 bytestream2_skip(gb, 4);
568 nBits = bytestream2_get_le32(gb);
569 if (im < 0 || im >= HUF_ENCSIZE ||
570 iM < 0 || iM >= HUF_ENCSIZE ||
572 return AVERROR_INVALIDDATA;
574 bytestream2_skip(gb, 4);
576 freq = av_mallocz_array(HUF_ENCSIZE, sizeof(*freq));
577 hdec = av_mallocz_array(HUF_DECSIZE, sizeof(*hdec));
578 if (!freq || !hdec) {
579 ret = AVERROR(ENOMEM);
583 if ((ret = huf_unpack_enc_table(gb, im, iM, freq)) < 0)
586 if (nBits > 8 * bytestream2_get_bytes_left(gb)) {
587 ret = AVERROR_INVALIDDATA;
591 if ((ret = huf_build_dec_table(freq, im, iM, hdec)) < 0)
593 ret = huf_decode(freq, hdec, gb, nBits, iM, dst_size, dst);
596 for (i = 0; i < HUF_DECSIZE; i++)
598 av_freep(&hdec[i].p);
606 static inline void wdec14(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
611 int ai = ls + (hi & 1) + (hi >> 1);
613 int16_t bs = ai - hi;
620 #define A_OFFSET (1 << (NBITS - 1))
621 #define MOD_MASK ((1 << NBITS) - 1)
623 static inline void wdec16(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
627 int bb = (m - (d >> 1)) & MOD_MASK;
628 int aa = (d + bb - A_OFFSET) & MOD_MASK;
633 static void wav_decode(uint16_t *in, int nx, int ox,
634 int ny, int oy, uint16_t mx)
636 int w14 = (mx < (1 << 14));
637 int n = (nx > ny) ? ny : nx;
650 uint16_t *ey = in + oy * (ny - p2);
651 uint16_t i00, i01, i10, i11;
657 for (; py <= ey; py += oy2) {
659 uint16_t *ex = py + ox * (nx - p2);
661 for (; px <= ex; px += ox2) {
662 uint16_t *p01 = px + ox1;
663 uint16_t *p10 = px + oy1;
664 uint16_t *p11 = p10 + ox1;
667 wdec14(*px, *p10, &i00, &i10);
668 wdec14(*p01, *p11, &i01, &i11);
669 wdec14(i00, i01, px, p01);
670 wdec14(i10, i11, p10, p11);
672 wdec16(*px, *p10, &i00, &i10);
673 wdec16(*p01, *p11, &i01, &i11);
674 wdec16(i00, i01, px, p01);
675 wdec16(i10, i11, p10, p11);
680 uint16_t *p10 = px + oy1;
683 wdec14(*px, *p10, &i00, p10);
685 wdec16(*px, *p10, &i00, p10);
693 uint16_t *ex = py + ox * (nx - p2);
695 for (; px <= ex; px += ox2) {
696 uint16_t *p01 = px + ox1;
699 wdec14(*px, *p01, &i00, p01);
701 wdec16(*px, *p01, &i00, p01);
712 static int piz_uncompress(EXRContext *s, const uint8_t *src, int ssize,
713 int dsize, EXRThreadData *td)
716 uint16_t maxval, min_non_zero, max_non_zero;
718 uint16_t *tmp = (uint16_t *)td->tmp;
723 td->bitmap = av_malloc(BITMAP_SIZE);
725 td->lut = av_malloc(1 << 17);
726 if (!td->bitmap || !td->lut) {
727 av_freep(&td->bitmap);
729 return AVERROR(ENOMEM);
732 bytestream2_init(&gb, src, ssize);
733 min_non_zero = bytestream2_get_le16(&gb);
734 max_non_zero = bytestream2_get_le16(&gb);
736 if (max_non_zero >= BITMAP_SIZE)
737 return AVERROR_INVALIDDATA;
739 memset(td->bitmap, 0, FFMIN(min_non_zero, BITMAP_SIZE));
740 if (min_non_zero <= max_non_zero)
741 bytestream2_get_buffer(&gb, td->bitmap + min_non_zero,
742 max_non_zero - min_non_zero + 1);
743 memset(td->bitmap + max_non_zero, 0, BITMAP_SIZE - max_non_zero);
745 maxval = reverse_lut(td->bitmap, td->lut);
747 ret = huf_uncompress(&gb, tmp, dsize / sizeof(uint16_t));
752 for (i = 0; i < s->nb_channels; i++) {
753 EXRChannel *channel = &s->channels[i];
754 int size = channel->pixel_type;
756 for (j = 0; j < size; j++)
757 wav_decode(ptr + j, s->xdelta, size, s->ysize,
758 s->xdelta * size, maxval);
759 ptr += s->xdelta * s->ysize * size;
762 apply_lut(td->lut, tmp, dsize / sizeof(uint16_t));
764 out = td->uncompressed_data;
765 for (i = 0; i < s->ysize; i++)
766 for (j = 0; j < s->nb_channels; j++) {
767 uint16_t *in = tmp + j * s->xdelta * s->ysize + i * s->xdelta;
768 memcpy(out, in, s->xdelta * 2);
769 out += s->xdelta * 2;
775 static int pxr24_uncompress(EXRContext *s, const uint8_t *src,
776 int compressed_size, int uncompressed_size,
779 unsigned long dest_len = uncompressed_size;
780 const uint8_t *in = td->tmp;
784 if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK ||
785 dest_len != uncompressed_size)
786 return AVERROR_INVALIDDATA;
788 out = td->uncompressed_data;
789 for (i = 0; i < s->ysize; i++)
790 for (c = 0; c < s->nb_channels; c++) {
791 EXRChannel *channel = &s->channels[c];
792 const uint8_t *ptr[4];
795 switch (channel->pixel_type) {
798 ptr[1] = ptr[0] + s->xdelta;
799 ptr[2] = ptr[1] + s->xdelta;
800 in = ptr[2] + s->xdelta;
802 for (j = 0; j < s->xdelta; ++j) {
803 uint32_t diff = (*(ptr[0]++) << 24) |
804 (*(ptr[1]++) << 16) |
807 bytestream_put_le32(&out, pixel);
812 ptr[1] = ptr[0] + s->xdelta;
813 in = ptr[1] + s->xdelta;
814 for (j = 0; j < s->xdelta; j++) {
815 uint32_t diff = (*(ptr[0]++) << 8) | *(ptr[1]++);
818 bytestream_put_le16(&out, pixel);
822 return AVERROR_INVALIDDATA;
829 static int decode_block(AVCodecContext *avctx, void *tdata,
830 int jobnr, int threadnr)
832 EXRContext *s = avctx->priv_data;
833 AVFrame *const p = s->picture;
834 EXRThreadData *td = &s->thread_data[threadnr];
835 const uint8_t *channel_buffer[4] = { 0 };
836 const uint8_t *buf = s->buf;
837 uint64_t line_offset, uncompressed_size;
838 uint32_t xdelta = s->xdelta;
841 uint32_t data_size, line;
843 int axmax = (avctx->width - (s->xmax + 1)) * 2 * s->desc->nb_components;
844 int bxmin = s->xmin * 2 * s->desc->nb_components;
845 int i, x, buf_size = s->buf_size;
846 float one_gamma = 1.0f / s->gamma;
847 avpriv_trc_function trc_func = avpriv_get_trc_function_from_trc(s->apply_trc_type);
850 line_offset = AV_RL64(s->gb.buffer + jobnr * 8);
851 // Check if the buffer has the required bytes needed from the offset
852 if (line_offset > buf_size - 8)
853 return AVERROR_INVALIDDATA;
855 src = buf + line_offset + 8;
856 line = AV_RL32(src - 8);
857 if (line < s->ymin || line > s->ymax)
858 return AVERROR_INVALIDDATA;
860 data_size = AV_RL32(src - 4);
861 if (data_size <= 0 || data_size > buf_size)
862 return AVERROR_INVALIDDATA;
864 s->ysize = FFMIN(s->scan_lines_per_block, s->ymax - line + 1);
865 uncompressed_size = s->scan_line_size * s->ysize;
866 if ((s->compression == EXR_RAW && (data_size != uncompressed_size ||
867 line_offset > buf_size - uncompressed_size)) ||
868 (s->compression != EXR_RAW && (data_size > uncompressed_size ||
869 line_offset > buf_size - data_size))) {
870 return AVERROR_INVALIDDATA;
873 if (data_size < uncompressed_size) {
874 av_fast_padded_malloc(&td->uncompressed_data,
875 &td->uncompressed_size, uncompressed_size);
876 av_fast_padded_malloc(&td->tmp, &td->tmp_size, uncompressed_size);
877 if (!td->uncompressed_data || !td->tmp)
878 return AVERROR(ENOMEM);
880 ret = AVERROR_INVALIDDATA;
881 switch (s->compression) {
884 ret = zip_uncompress(src, data_size, uncompressed_size, td);
887 ret = piz_uncompress(s, src, data_size, uncompressed_size, td);
890 ret = pxr24_uncompress(s, src, data_size, uncompressed_size, td);
893 ret = rle_uncompress(src, data_size, uncompressed_size, td);
896 av_log(avctx, AV_LOG_ERROR, "decode_block() failed.\n");
899 src = td->uncompressed_data;
902 channel_buffer[0] = src + xdelta * s->channel_offsets[0];
903 channel_buffer[1] = src + xdelta * s->channel_offsets[1];
904 channel_buffer[2] = src + xdelta * s->channel_offsets[2];
905 if (s->channel_offsets[3] >= 0)
906 channel_buffer[3] = src + xdelta * s->channel_offsets[3];
908 ptr = p->data[0] + line * p->linesize[0];
910 i < s->scan_lines_per_block && line + i <= s->ymax;
911 i++, ptr += p->linesize[0]) {
912 const uint8_t *r, *g, *b, *a;
914 r = channel_buffer[0];
915 g = channel_buffer[1];
916 b = channel_buffer[2];
917 if (channel_buffer[3])
918 a = channel_buffer[3];
920 ptr_x = (uint16_t *) ptr;
922 // Zero out the start if xmin is not 0
923 memset(ptr_x, 0, bxmin);
924 ptr_x += s->xmin * s->desc->nb_components;
925 if (s->pixel_type == EXR_FLOAT) {
928 for (x = 0; x < xdelta; x++) {
929 union av_intfloat32 t;
930 t.i = bytestream_get_le32(&r);
932 *ptr_x++ = exr_flt2uint(t.i);
934 t.i = bytestream_get_le32(&g);
936 *ptr_x++ = exr_flt2uint(t.i);
938 t.i = bytestream_get_le32(&b);
940 *ptr_x++ = exr_flt2uint(t.i);
941 if (channel_buffer[3])
942 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a));
945 for (x = 0; x < xdelta; x++) {
946 union av_intfloat32 t;
947 t.i = bytestream_get_le32(&r);
948 if (t.f > 0.0f) /* avoid negative values */
949 t.f = powf(t.f, one_gamma);
950 *ptr_x++ = exr_flt2uint(t.i);
952 t.i = bytestream_get_le32(&g);
954 t.f = powf(t.f, one_gamma);
955 *ptr_x++ = exr_flt2uint(t.i);
957 t.i = bytestream_get_le32(&b);
959 t.f = powf(t.f, one_gamma);
960 *ptr_x++ = exr_flt2uint(t.i);
961 if (channel_buffer[3])
962 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a));
967 for (x = 0; x < xdelta; x++) {
968 *ptr_x++ = s->gamma_table[bytestream_get_le16(&r)];
969 *ptr_x++ = s->gamma_table[bytestream_get_le16(&g)];
970 *ptr_x++ = s->gamma_table[bytestream_get_le16(&b)];
971 if (channel_buffer[3])
972 *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&a));
976 // Zero out the end if xmax+1 is not w
977 memset(ptr_x, 0, axmax);
979 channel_buffer[0] += s->scan_line_size;
980 channel_buffer[1] += s->scan_line_size;
981 channel_buffer[2] += s->scan_line_size;
982 if (channel_buffer[3])
983 channel_buffer[3] += s->scan_line_size;
990 * Check if the variable name corresponds to its data type.
992 * @param s the EXRContext
993 * @param value_name name of the variable to check
994 * @param value_type type of the variable to check
995 * @param minimum_length minimum length of the variable data
997 * @return bytes to read containing variable data
998 * -1 if variable is not found
999 * 0 if buffer ended prematurely
1001 static int check_header_variable(EXRContext *s,
1002 const char *value_name,
1003 const char *value_type,
1004 unsigned int minimum_length)
1008 if (bytestream2_get_bytes_left(&s->gb) >= minimum_length &&
1009 !strcmp(s->gb.buffer, value_name)) {
1010 // found value_name, jump to value_type (null terminated strings)
1011 s->gb.buffer += strlen(value_name) + 1;
1012 if (!strcmp(s->gb.buffer, value_type)) {
1013 s->gb.buffer += strlen(value_type) + 1;
1014 var_size = bytestream2_get_le32(&s->gb);
1015 // don't go read past boundaries
1016 if (var_size > bytestream2_get_bytes_left(&s->gb))
1019 // value_type not found, reset the buffer
1020 s->gb.buffer -= strlen(value_name) + 1;
1021 av_log(s->avctx, AV_LOG_WARNING,
1022 "Unknown data type %s for header variable %s.\n",
1023 value_type, value_name);
1030 static int decode_header(EXRContext *s)
1032 int current_channel_offset = 0;
1033 int magic_number, version, flags, i;
1041 s->channel_offsets[0] = -1;
1042 s->channel_offsets[1] = -1;
1043 s->channel_offsets[2] = -1;
1044 s->channel_offsets[3] = -1;
1045 s->pixel_type = EXR_UNKNOWN;
1046 s->compression = EXR_UNKN;
1051 if (bytestream2_get_bytes_left(&s->gb) < 10) {
1052 av_log(s->avctx, AV_LOG_ERROR, "Header too short to parse.\n");
1053 return AVERROR_INVALIDDATA;
1056 magic_number = bytestream2_get_le32(&s->gb);
1057 if (magic_number != 20000630) {
1058 /* As per documentation of OpenEXR, it is supposed to be
1059 * int 20000630 little-endian */
1060 av_log(s->avctx, AV_LOG_ERROR, "Wrong magic number %d.\n", magic_number);
1061 return AVERROR_INVALIDDATA;
1064 version = bytestream2_get_byte(&s->gb);
1066 avpriv_report_missing_feature(s->avctx, "Version %d", version);
1067 return AVERROR_PATCHWELCOME;
1070 flags = bytestream2_get_le24(&s->gb);
1072 avpriv_report_missing_feature(s->avctx, "Tile support");
1073 return AVERROR_PATCHWELCOME;
1077 while (bytestream2_get_bytes_left(&s->gb) > 0 && *s->gb.buffer) {
1079 if ((var_size = check_header_variable(s, "channels",
1080 "chlist", 38)) >= 0) {
1081 GetByteContext ch_gb;
1083 return AVERROR_INVALIDDATA;
1085 bytestream2_init(&ch_gb, s->gb.buffer, var_size);
1087 while (bytestream2_get_bytes_left(&ch_gb) >= 19) {
1088 EXRChannel *channel;
1089 enum ExrPixelType current_pixel_type;
1090 int channel_index = -1;
1093 if (strcmp(s->layer, "") != 0) {
1094 if (strncmp(ch_gb.buffer, s->layer, strlen(s->layer)) == 0) {
1095 ch_gb.buffer += strlen(s->layer);
1096 if (*ch_gb.buffer == '.')
1097 ch_gb.buffer++; /* skip dot if not given */
1098 av_log(s->avctx, AV_LOG_INFO,
1099 "Layer %s.%s matched.\n", s->layer, ch_gb.buffer);
1103 if (!strcmp(ch_gb.buffer, "R") ||
1104 !strcmp(ch_gb.buffer, "X") ||
1105 !strcmp(ch_gb.buffer, "U"))
1107 else if (!strcmp(ch_gb.buffer, "G") ||
1108 !strcmp(ch_gb.buffer, "Y") ||
1109 !strcmp(ch_gb.buffer, "V"))
1111 else if (!strcmp(ch_gb.buffer, "B") ||
1112 !strcmp(ch_gb.buffer, "Z") ||
1113 !strcmp(ch_gb.buffer, "W"))
1115 else if (!strcmp(ch_gb.buffer, "A"))
1118 av_log(s->avctx, AV_LOG_WARNING,
1119 "Unsupported channel %.256s.\n", ch_gb.buffer);
1121 /* skip until you get a 0 */
1122 while (bytestream2_get_bytes_left(&ch_gb) > 0 &&
1123 bytestream2_get_byte(&ch_gb))
1126 if (bytestream2_get_bytes_left(&ch_gb) < 4) {
1127 av_log(s->avctx, AV_LOG_ERROR, "Incomplete header.\n");
1128 return AVERROR_INVALIDDATA;
1131 current_pixel_type = bytestream2_get_le32(&ch_gb);
1132 if (current_pixel_type >= EXR_UNKNOWN) {
1133 avpriv_report_missing_feature(s->avctx,
1135 current_pixel_type);
1136 return AVERROR_PATCHWELCOME;
1139 bytestream2_skip(&ch_gb, 4);
1140 xsub = bytestream2_get_le32(&ch_gb);
1141 ysub = bytestream2_get_le32(&ch_gb);
1142 if (xsub != 1 || ysub != 1) {
1143 avpriv_report_missing_feature(s->avctx,
1144 "Subsampling %dx%d",
1146 return AVERROR_PATCHWELCOME;
1149 if (channel_index >= 0) {
1150 if (s->pixel_type != EXR_UNKNOWN &&
1151 s->pixel_type != current_pixel_type) {
1152 av_log(s->avctx, AV_LOG_ERROR,
1153 "RGB channels not of the same depth.\n");
1154 return AVERROR_INVALIDDATA;
1156 s->pixel_type = current_pixel_type;
1157 s->channel_offsets[channel_index] = current_channel_offset;
1160 s->channels = av_realloc(s->channels,
1161 ++s->nb_channels * sizeof(EXRChannel));
1163 return AVERROR(ENOMEM);
1164 channel = &s->channels[s->nb_channels - 1];
1165 channel->pixel_type = current_pixel_type;
1166 channel->xsub = xsub;
1167 channel->ysub = ysub;
1169 current_channel_offset += 1 << current_pixel_type;
1172 /* Check if all channels are set with an offset or if the channels
1173 * are causing an overflow */
1174 if (FFMIN3(s->channel_offsets[0],
1175 s->channel_offsets[1],
1176 s->channel_offsets[2]) < 0) {
1177 if (s->channel_offsets[0] < 0)
1178 av_log(s->avctx, AV_LOG_ERROR, "Missing red channel.\n");
1179 if (s->channel_offsets[1] < 0)
1180 av_log(s->avctx, AV_LOG_ERROR, "Missing green channel.\n");
1181 if (s->channel_offsets[2] < 0)
1182 av_log(s->avctx, AV_LOG_ERROR, "Missing blue channel.\n");
1183 return AVERROR_INVALIDDATA;
1186 // skip one last byte and update main gb
1187 s->gb.buffer = ch_gb.buffer + 1;
1189 } else if ((var_size = check_header_variable(s, "dataWindow", "box2i",
1192 return AVERROR_INVALIDDATA;
1194 s->xmin = bytestream2_get_le32(&s->gb);
1195 s->ymin = bytestream2_get_le32(&s->gb);
1196 s->xmax = bytestream2_get_le32(&s->gb);
1197 s->ymax = bytestream2_get_le32(&s->gb);
1198 s->xdelta = (s->xmax - s->xmin) + 1;
1199 s->ydelta = (s->ymax - s->ymin) + 1;
1202 } else if ((var_size = check_header_variable(s, "displayWindow",
1203 "box2i", 34)) >= 0) {
1205 return AVERROR_INVALIDDATA;
1207 bytestream2_skip(&s->gb, 8);
1208 s->w = bytestream2_get_le32(&s->gb) + 1;
1209 s->h = bytestream2_get_le32(&s->gb) + 1;
1212 } else if ((var_size = check_header_variable(s, "lineOrder",
1213 "lineOrder", 25)) >= 0) {
1216 return AVERROR_INVALIDDATA;
1218 line_order = bytestream2_get_byte(&s->gb);
1219 av_log(s->avctx, AV_LOG_DEBUG, "line order: %d.\n", line_order);
1220 if (line_order > 2) {
1221 av_log(s->avctx, AV_LOG_ERROR, "Unknown line order.\n");
1222 return AVERROR_INVALIDDATA;
1226 } else if ((var_size = check_header_variable(s, "pixelAspectRatio",
1227 "float", 31)) >= 0) {
1229 return AVERROR_INVALIDDATA;
1231 ff_set_sar(s->avctx,
1232 av_d2q(av_int2float(bytestream2_get_le32(&s->gb)), 255));
1235 } else if ((var_size = check_header_variable(s, "compression",
1236 "compression", 29)) >= 0) {
1238 return AVERROR_INVALIDDATA;
1240 if (s->compression == EXR_UNKN)
1241 s->compression = bytestream2_get_byte(&s->gb);
1243 av_log(s->avctx, AV_LOG_WARNING,
1244 "Found more than one compression attribute.\n");
1249 // Check if there are enough bytes for a header
1250 if (bytestream2_get_bytes_left(&s->gb) <= 9) {
1251 av_log(s->avctx, AV_LOG_ERROR, "Incomplete header\n");
1252 return AVERROR_INVALIDDATA;
1255 // Process unknown variables
1256 for (i = 0; i < 2; i++) // value_name and value_type
1257 while (bytestream2_get_byte(&s->gb) != 0);
1259 // Skip variable length
1260 bytestream2_skip(&s->gb, bytestream2_get_le32(&s->gb));
1263 if (s->compression == EXR_UNKN) {
1264 av_log(s->avctx, AV_LOG_ERROR, "Missing compression attribute.\n");
1265 return AVERROR_INVALIDDATA;
1267 s->scan_line_size = s->xdelta * current_channel_offset;
1269 if (bytestream2_get_bytes_left(&s->gb) <= 0) {
1270 av_log(s->avctx, AV_LOG_ERROR, "Incomplete frame.\n");
1271 return AVERROR_INVALIDDATA;
1274 // aaand we are done
1275 bytestream2_skip(&s->gb, 1);
1279 static int decode_frame(AVCodecContext *avctx, void *data,
1280 int *got_frame, AVPacket *avpkt)
1282 EXRContext *s = avctx->priv_data;
1283 ThreadFrame frame = { .f = data };
1284 AVFrame *picture = data;
1289 int scan_line_blocks;
1291 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
1293 if ((ret = decode_header(s)) < 0)
1296 switch (s->pixel_type) {
1299 if (s->channel_offsets[3] >= 0)
1300 avctx->pix_fmt = AV_PIX_FMT_RGBA64;
1302 avctx->pix_fmt = AV_PIX_FMT_RGB48;
1305 avpriv_request_sample(avctx, "32-bit unsigned int");
1306 return AVERROR_PATCHWELCOME;
1308 av_log(avctx, AV_LOG_ERROR, "Missing channel list.\n");
1309 return AVERROR_INVALIDDATA;
1312 if (s->apply_trc_type != AVCOL_TRC_UNSPECIFIED)
1313 avctx->color_trc = s->apply_trc_type;
1315 switch (s->compression) {
1319 s->scan_lines_per_block = 1;
1323 s->scan_lines_per_block = 16;
1326 s->scan_lines_per_block = 32;
1329 avpriv_report_missing_feature(avctx, "Compression %d", s->compression);
1330 return AVERROR_PATCHWELCOME;
1333 /* Verify the xmin, xmax, ymin, ymax and xdelta before setting
1334 * the actual image size. */
1335 if (s->xmin > s->xmax ||
1336 s->ymin > s->ymax ||
1337 s->xdelta != s->xmax - s->xmin + 1 ||
1340 av_log(avctx, AV_LOG_ERROR, "Wrong or missing size information.\n");
1341 return AVERROR_INVALIDDATA;
1344 if ((ret = ff_set_dimensions(avctx, s->w, s->h)) < 0)
1347 s->desc = av_pix_fmt_desc_get(avctx->pix_fmt);
1349 return AVERROR_INVALIDDATA;
1350 out_line_size = avctx->width * 2 * s->desc->nb_components;
1351 scan_line_blocks = (s->ydelta + s->scan_lines_per_block - 1) /
1352 s->scan_lines_per_block;
1354 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
1357 if (bytestream2_get_bytes_left(&s->gb) < scan_line_blocks * 8)
1358 return AVERROR_INVALIDDATA;
1360 // save pointer we are going to use in decode_block
1361 s->buf = avpkt->data;
1362 s->buf_size = avpkt->size;
1363 ptr = picture->data[0];
1365 // Zero out the start if ymin is not 0
1366 for (y = 0; y < s->ymin; y++) {
1367 memset(ptr, 0, out_line_size);
1368 ptr += picture->linesize[0];
1371 s->picture = picture;
1372 avctx->execute2(avctx, decode_block, s->thread_data, NULL, scan_line_blocks);
1374 // Zero out the end if ymax+1 is not h
1375 for (y = s->ymax + 1; y < avctx->height; y++) {
1376 memset(ptr, 0, out_line_size);
1377 ptr += picture->linesize[0];
1380 picture->pict_type = AV_PICTURE_TYPE_I;
1386 static av_cold int decode_init(AVCodecContext *avctx)
1388 EXRContext *s = avctx->priv_data;
1390 union av_intfloat32 t;
1391 float one_gamma = 1.0f / s->gamma;
1392 avpriv_trc_function trc_func = NULL;
1396 trc_func = avpriv_get_trc_function_from_trc(s->apply_trc_type);
1398 for (i = 0; i < 65536; ++i) {
1399 t = exr_half2float(i);
1400 t.f = trc_func(t.f);
1401 s->gamma_table[i] = exr_flt2uint(t.i);
1404 if (one_gamma > 0.9999f && one_gamma < 1.0001f) {
1405 for (i = 0; i < 65536; ++i)
1406 s->gamma_table[i] = exr_halflt2uint(i);
1408 for (i = 0; i < 65536; ++i) {
1409 t = exr_half2float(i);
1410 /* If negative value we reuse half value */
1412 s->gamma_table[i] = exr_halflt2uint(i);
1414 t.f = powf(t.f, one_gamma);
1415 s->gamma_table[i] = exr_flt2uint(t.i);
1421 // allocate thread data, used for non EXR_RAW compreesion types
1422 s->thread_data = av_mallocz_array(avctx->thread_count, sizeof(EXRThreadData));
1423 if (!s->thread_data)
1424 return AVERROR_INVALIDDATA;
1430 static int decode_init_thread_copy(AVCodecContext *avctx)
1431 { EXRContext *s = avctx->priv_data;
1433 // allocate thread data, used for non EXR_RAW compreesion types
1434 s->thread_data = av_mallocz_array(avctx->thread_count, sizeof(EXRThreadData));
1435 if (!s->thread_data)
1436 return AVERROR_INVALIDDATA;
1442 static av_cold int decode_end(AVCodecContext *avctx)
1444 EXRContext *s = avctx->priv_data;
1446 for (i = 0; i < avctx->thread_count; i++) {
1447 EXRThreadData *td = &s->thread_data[i];
1448 av_freep(&td->uncompressed_data);
1450 av_freep(&td->bitmap);
1454 av_freep(&s->thread_data);
1455 av_freep(&s->channels);
1460 #define OFFSET(x) offsetof(EXRContext, x)
1461 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1462 static const AVOption options[] = {
1463 { "layer", "Set the decoding layer", OFFSET(layer),
1464 AV_OPT_TYPE_STRING, { .str = "" }, 0, 0, VD },
1465 { "gamma", "Set the float gamma value when decoding", OFFSET(gamma),
1466 AV_OPT_TYPE_FLOAT, { .dbl = 1.0f }, 0.001, FLT_MAX, VD },
1468 // XXX: Note the abuse of the enum using AVCOL_TRC_UNSPECIFIED to subsume the existing gamma option
1469 { "apply_trc", "color transfer characteristics to apply to EXR linear input", OFFSET(apply_trc_type),
1470 AV_OPT_TYPE_INT, {.i64 = AVCOL_TRC_UNSPECIFIED }, 1, AVCOL_TRC_NB-1, VD, "apply_trc_type"},
1471 { "bt709", "BT.709", 0,
1472 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT709 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1473 { "gamma", "gamma", 0,
1474 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_UNSPECIFIED }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1475 { "gamma22", "BT.470 M", 0,
1476 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_GAMMA22 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1477 { "gamma28", "BT.470 BG", 0,
1478 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_GAMMA28 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1479 { "smpte170m", "SMPTE 170 M", 0,
1480 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTE170M }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1481 { "smpte240m", "SMPTE 240 M", 0,
1482 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTE240M }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1483 { "linear", "Linear", 0,
1484 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LINEAR }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1486 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LOG }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1487 { "log_sqrt", "Log square root", 0,
1488 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LOG_SQRT }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1489 { "iec61966_2_4", "IEC 61966-2-4", 0,
1490 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_IEC61966_2_4 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1491 { "bt1361", "BT.1361", 0,
1492 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT1361_ECG }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1493 { "iec61966_2_1", "IEC 61966-2-1", 0,
1494 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_IEC61966_2_1 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1495 { "bt2020_10bit", "BT.2020 - 10 bit", 0,
1496 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT2020_10 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1497 { "bt2020_12bit", "BT.2020 - 12 bit", 0,
1498 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT2020_12 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1499 { "smpte2084", "SMPTE ST 2084", 0,
1500 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTEST2084 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1501 { "smpte428_1", "SMPTE ST 428-1", 0,
1502 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTEST428_1 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1507 static const AVClass exr_class = {
1508 .class_name = "EXR",
1509 .item_name = av_default_item_name,
1511 .version = LIBAVUTIL_VERSION_INT,
1514 AVCodec ff_exr_decoder = {
1516 .long_name = NULL_IF_CONFIG_SMALL("OpenEXR image"),
1517 .type = AVMEDIA_TYPE_VIDEO,
1518 .id = AV_CODEC_ID_EXR,
1519 .priv_data_size = sizeof(EXRContext),
1520 .init = decode_init,
1521 .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
1522 .close = decode_end,
1523 .decode = decode_frame,
1524 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS |
1525 AV_CODEC_CAP_SLICE_THREADS,
1526 .priv_class = &exr_class,