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
37 #include "bytestream.h"
41 #include "libavutil/imgutils.h"
42 #include "libavutil/avassert.h"
43 #include "libavutil/opt.h"
62 typedef struct EXRChannel {
64 enum ExrPixelType pixel_type;
67 typedef struct EXRThreadData {
68 uint8_t *uncompressed_data;
69 int uncompressed_size;
78 typedef struct EXRContext {
82 enum ExrPixelType pixel_type;
83 int channel_offsets[4]; // 0 = red, 1 = green, 2 = blue and 3 = alpha
84 const AVPixFmtDescriptor *desc;
88 uint32_t xdelta, ydelta;
92 uint64_t scan_line_size;
93 int scan_lines_per_block;
95 const uint8_t *buf, *table;
101 EXRThreadData *thread_data;
102 int thread_data_size;
107 #define OFFSET(x) offsetof(EXRContext, x)
108 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
109 static const AVOption options[] = {
110 { "layer", "Set the decoding layer", OFFSET(layer), AV_OPT_TYPE_STRING, { .str = "" }, 0, 0, VD},
114 static const AVClass exr_class = {
116 .item_name = av_default_item_name,
118 .version = LIBAVUTIL_VERSION_INT,
122 * Converts from 32-bit float as uint32_t to uint16_t
124 * @param v 32-bit float
125 * @return normalized 16-bit unsigned int
127 static inline uint16_t exr_flt2uint(uint32_t v)
129 unsigned int exp = v >> 23;
130 // "HACK": negative values result in exp< 0, so clipping them to 0
131 // is also handled by this condition, avoids explicit check for sign bit.
132 if (exp<= 127 + 7 - 24) // we would shift out all bits anyway
137 return (v + (1 << 23)) >> (127 + 7 - exp);
141 * Converts from 16-bit float as uint16_t to uint16_t
143 * @param v 16-bit float
144 * @return normalized 16-bit unsigned int
146 static inline uint16_t exr_halflt2uint(uint16_t v)
148 unsigned exp = 14 - (v >> 10);
150 if (exp == 14) return (v >> 9) & 1;
151 else return (v & 0x8000) ? 0 : 0xffff;
154 return (v + (1 << 16)) >> (exp + 1);
158 * Gets the size of the header variable
160 * @param **buf the current pointer location in the header where
161 * the variable data starts
162 * @param *buf_end pointer location of the end of the buffer
163 * @return size of variable data
165 static unsigned int get_header_variable_length(const uint8_t **buf,
166 const uint8_t *buf_end)
168 unsigned int variable_buffer_data_size = bytestream_get_le32(buf);
169 if (variable_buffer_data_size >= buf_end - *buf)
171 return variable_buffer_data_size;
175 * Checks if the variable name corresponds with it's data type
177 * @param *avctx the AVCodecContext
178 * @param **buf the current pointer location in the header where
179 * the variable name starts
180 * @param *buf_end pointer location of the end of the buffer
181 * @param *value_name name of the varible to check
182 * @param *value_type type of the varible to check
183 * @param minimum_length minimum length of the variable data
184 * @param variable_buffer_data_size variable length read from the header
186 * @return negative if variable is invalid
188 static int check_header_variable(AVCodecContext *avctx,
190 const uint8_t *buf_end,
191 const char *value_name,
192 const char *value_type,
193 unsigned int minimum_length,
194 unsigned int *variable_buffer_data_size)
196 if (buf_end - *buf >= minimum_length && !strcmp(*buf, value_name)) {
197 *buf += strlen(value_name)+1;
198 if (!strcmp(*buf, value_type)) {
199 *buf += strlen(value_type)+1;
200 *variable_buffer_data_size = get_header_variable_length(buf, buf_end);
201 if (!*variable_buffer_data_size)
202 av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
205 *buf -= strlen(value_name)+1;
206 av_log(avctx, AV_LOG_WARNING, "Unknown data type for header variable %s\n", value_name);
211 static void predictor(uint8_t *src, int size)
213 uint8_t *t = src + 1;
214 uint8_t *stop = src + size;
217 int d = (int)t[-1] + (int)t[0] - 128;
223 static void reorder_pixels(uint8_t *src, uint8_t *dst, int size)
225 const int8_t *t1 = src;
226 const int8_t *t2 = src + (size + 1) / 2;
228 int8_t *stop = s + size;
243 static int zip_uncompress(const uint8_t *src, int compressed_size,
244 int uncompressed_size, EXRThreadData *td)
246 unsigned long dest_len = uncompressed_size;
248 if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK ||
249 dest_len != uncompressed_size)
250 return AVERROR(EINVAL);
252 predictor(td->tmp, uncompressed_size);
253 reorder_pixels(td->tmp, td->uncompressed_data, uncompressed_size);
258 static int rle_uncompress(const uint8_t *src, int compressed_size,
259 int uncompressed_size, EXRThreadData *td)
261 int8_t *d = (int8_t *)td->tmp;
262 const int8_t *s = (const int8_t *)src;
263 int ssize = compressed_size;
264 int dsize = uncompressed_size;
265 int8_t *dend = d + dsize;
274 if ((dsize -= count ) < 0 ||
275 (ssize -= count + 1) < 0)
283 if ((dsize -= count) < 0 ||
295 return AVERROR_INVALIDDATA;
297 predictor(td->tmp, uncompressed_size);
298 reorder_pixels(td->tmp, td->uncompressed_data, uncompressed_size);
303 #define USHORT_RANGE (1 << 16)
304 #define BITMAP_SIZE (1 << 13)
306 static uint16_t reverse_lut(const uint8_t *bitmap, uint16_t *lut)
310 for (i = 0; i < USHORT_RANGE; i++) {
311 if ((i == 0) || (bitmap[i >> 3] & (1 << (i & 7))))
317 memset(lut + k, 0, (USHORT_RANGE - k) * 2);
322 static void apply_lut(const uint16_t *lut, uint16_t *dst, int dsize)
326 for (i = 0; i < dsize; ++i)
327 dst[i] = lut[dst[i]];
330 #define HUF_ENCBITS 16 // literal (value) bit length
331 #define HUF_DECBITS 14 // decoding bit size (>= 8)
333 #define HUF_ENCSIZE ((1 << HUF_ENCBITS) + 1) // encoding table size
334 #define HUF_DECSIZE (1 << HUF_DECBITS) // decoding table size
335 #define HUF_DECMASK (HUF_DECSIZE - 1)
337 typedef struct HufDec {
343 static void huf_canonical_code_table(uint64_t *hcode)
345 uint64_t c, n[59] = { 0 };
348 for (i = 0; i < HUF_ENCSIZE; ++i)
352 for (i = 58; i > 0; --i) {
353 uint64_t nc = ((c + n[i]) >> 1);
358 for (i = 0; i < HUF_ENCSIZE; ++i) {
362 hcode[i] = l | (n[l]++ << 6);
366 #define SHORT_ZEROCODE_RUN 59
367 #define LONG_ZEROCODE_RUN 63
368 #define SHORTEST_LONG_RUN (2 + LONG_ZEROCODE_RUN - SHORT_ZEROCODE_RUN)
369 #define LONGEST_LONG_RUN (255 + SHORTEST_LONG_RUN)
371 static int huf_unpack_enc_table(GetByteContext *gb,
372 int32_t im, int32_t iM, uint64_t *hcode)
376 init_get_bits8(&gbit, gb->buffer, bytestream2_get_bytes_left(gb));
378 for (; im <= iM; im++) {
379 uint64_t l = hcode[im] = get_bits(&gbit, 6);
381 if (l == LONG_ZEROCODE_RUN) {
382 int zerun = get_bits(&gbit, 8) + SHORTEST_LONG_RUN;
384 if (im + zerun > iM + 1)
385 return AVERROR_INVALIDDATA;
391 } else if (l >= (uint64_t) SHORT_ZEROCODE_RUN) {
392 int zerun = l - SHORT_ZEROCODE_RUN + 2;
394 if (im + zerun > iM + 1)
395 return AVERROR_INVALIDDATA;
404 bytestream2_skip(gb, (get_bits_count(&gbit) + 7) / 8);
405 huf_canonical_code_table(hcode);
410 static int huf_build_dec_table(const uint64_t *hcode, int im,
411 int iM, HufDec *hdecod)
413 for (; im <= iM; im++) {
414 uint64_t c = hcode[im] >> 6;
415 int i, l = hcode[im] & 63;
418 return AVERROR_INVALIDDATA;
420 if (l > HUF_DECBITS) {
421 HufDec *pl = hdecod + (c >> (l - HUF_DECBITS));
423 return AVERROR_INVALIDDATA;
427 pl->p = av_realloc_f(pl->p, pl->lit, sizeof(int));
429 return AVERROR(ENOMEM);
431 pl->p[pl->lit - 1] = im;
433 HufDec *pl = hdecod + (c << (HUF_DECBITS - l));
435 for (i = 1 << (HUF_DECBITS - l); i > 0; i--, pl++) {
436 if (pl->len || pl->p)
437 return AVERROR_INVALIDDATA;
447 #define get_char(c, lc, gb) { \
448 c = (c << 8) | bytestream2_get_byte(gb); \
452 #define get_code(po, rlc, c, lc, gb, out, oe) { \
455 get_char(c, lc, gb); \
461 return AVERROR_INVALIDDATA; \
467 } else if (out < oe) { \
470 return AVERROR_INVALIDDATA; \
474 static int huf_decode(const uint64_t *hcode, const HufDec *hdecod,
475 GetByteContext *gb, int nbits,
476 int rlc, int no, uint16_t *out)
479 uint16_t *outb = out;
480 uint16_t *oe = out + no;
481 const uint8_t *ie = gb->buffer + (nbits + 7) / 8; // input byte size
485 while (gb->buffer < ie) {
488 while (lc >= HUF_DECBITS) {
489 const HufDec pl = hdecod[(c >> (lc-HUF_DECBITS)) & HUF_DECMASK];
493 get_code(pl.lit, rlc, c, lc, gb, out, oe);
498 return AVERROR_INVALIDDATA;
500 for (j = 0; j < pl.lit; j++) {
501 int l = hcode[pl.p[j]] & 63;
503 while (lc < l && bytestream2_get_bytes_left(gb) > 0)
507 if ((hcode[pl.p[j]] >> 6) ==
508 ((c >> (lc - l)) & ((1LL << l) - 1))) {
510 get_code(pl.p[j], rlc, c, lc, gb, out, oe);
517 return AVERROR_INVALIDDATA;
527 const HufDec pl = hdecod[(c << (HUF_DECBITS - lc)) & HUF_DECMASK];
531 get_code(pl.lit, rlc, c, lc, gb, out, oe);
533 return AVERROR_INVALIDDATA;
537 if (out - outb != no)
538 return AVERROR_INVALIDDATA;
542 static int huf_uncompress(GetByteContext *gb,
543 uint16_t *dst, int dst_size)
545 int32_t src_size, im, iM;
551 src_size = bytestream2_get_le32(gb);
552 im = bytestream2_get_le32(gb);
553 iM = bytestream2_get_le32(gb);
554 bytestream2_skip(gb, 4);
555 nBits = bytestream2_get_le32(gb);
556 if (im < 0 || im >= HUF_ENCSIZE ||
557 iM < 0 || iM >= HUF_ENCSIZE ||
559 return AVERROR_INVALIDDATA;
561 bytestream2_skip(gb, 4);
563 freq = av_calloc(HUF_ENCSIZE, sizeof(*freq));
564 hdec = av_calloc(HUF_DECSIZE, sizeof(*hdec));
565 if (!freq || !hdec) {
566 ret = AVERROR(ENOMEM);
570 if ((ret = huf_unpack_enc_table(gb, im, iM, freq)) < 0)
573 if (nBits > 8 * bytestream2_get_bytes_left(gb)) {
574 ret = AVERROR_INVALIDDATA;
578 if ((ret = huf_build_dec_table(freq, im, iM, hdec)) < 0)
580 ret = huf_decode(freq, hdec, gb, nBits, iM, dst_size, dst);
583 for (i = 0; i < HUF_DECSIZE; i++) {
585 av_freep(&hdec[i].p);
594 static inline void wdec14(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
599 int ai = ls + (hi & 1) + (hi >> 1);
601 int16_t bs = ai - hi;
608 #define A_OFFSET (1 << (NBITS - 1))
609 #define MOD_MASK ((1 << NBITS) - 1)
611 static inline void wdec16(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
615 int bb = (m - (d >> 1)) & MOD_MASK;
616 int aa = (d + bb - A_OFFSET) & MOD_MASK;
621 static void wav_decode(uint16_t *in, int nx, int ox,
622 int ny, int oy, uint16_t mx)
624 int w14 = (mx < (1 << 14));
625 int n = (nx > ny) ? ny: nx;
638 uint16_t *ey = in + oy * (ny - p2);
639 uint16_t i00, i01, i10, i11;
645 for (; py <= ey; py += oy2) {
647 uint16_t *ex = py + ox * (nx - p2);
649 for (; px <= ex; px += ox2) {
650 uint16_t *p01 = px + ox1;
651 uint16_t *p10 = px + oy1;
652 uint16_t *p11 = p10 + ox1;
655 wdec14(*px, *p10, &i00, &i10);
656 wdec14(*p01, *p11, &i01, &i11);
657 wdec14(i00, i01, px, p01);
658 wdec14(i10, i11, p10, p11);
660 wdec16(*px, *p10, &i00, &i10);
661 wdec16(*p01, *p11, &i01, &i11);
662 wdec16(i00, i01, px, p01);
663 wdec16(i10, i11, p10, p11);
668 uint16_t *p10 = px + oy1;
671 wdec14(*px, *p10, &i00, p10);
673 wdec16(*px, *p10, &i00, p10);
681 uint16_t *ex = py + ox * (nx - p2);
683 for (; px <= ex; px += ox2) {
684 uint16_t *p01 = px + ox1;
687 wdec14(*px, *p01, &i00, p01);
689 wdec16(*px, *p01, &i00, p01);
700 static int piz_uncompress(EXRContext *s, const uint8_t *src, int ssize, int dsize, EXRThreadData *td)
703 uint16_t maxval, min_non_zero, max_non_zero;
704 uint16_t *ptr, *tmp = (uint16_t *)td->tmp;
709 td->bitmap = av_malloc(BITMAP_SIZE);
711 td->lut = av_malloc(1 << 17);
712 if (!td->bitmap || !td->lut)
713 return AVERROR(ENOMEM);
715 bytestream2_init(&gb, src, ssize);
716 min_non_zero = bytestream2_get_le16(&gb);
717 max_non_zero = bytestream2_get_le16(&gb);
719 if (max_non_zero >= BITMAP_SIZE)
720 return AVERROR_INVALIDDATA;
722 memset(td->bitmap, 0, FFMIN(min_non_zero, BITMAP_SIZE));
723 if (min_non_zero <= max_non_zero)
724 bytestream2_get_buffer(&gb, td->bitmap + min_non_zero,
725 max_non_zero - min_non_zero + 1);
726 memset(td->bitmap + max_non_zero, 0, BITMAP_SIZE - max_non_zero);
728 maxval = reverse_lut(td->bitmap, td->lut);
730 ret = huf_uncompress(&gb, tmp, dsize / sizeof(int16_t));
735 for (i = 0; i < s->nb_channels; i++) {
736 EXRChannel *channel = &s->channels[i];
737 int size = channel->pixel_type;
739 for (j = 0; j < size; j++)
740 wav_decode(ptr + j, s->xdelta, size, s->ysize, s->xdelta * size, maxval);
741 ptr += s->xdelta * s->ysize * size;
744 apply_lut(td->lut, tmp, dsize / sizeof(int16_t));
746 out = td->uncompressed_data;
747 for (i = 0; i < s->ysize; i++) {
748 for (j = 0; j < s->nb_channels; j++) {
749 uint16_t *in = tmp + j * s->xdelta * s->ysize + i * s->xdelta;
750 memcpy(out, in, s->xdelta * 2);
751 out += s->xdelta * 2;
758 static int pxr24_uncompress(EXRContext *s, const uint8_t *src,
759 int compressed_size, int uncompressed_size,
762 unsigned long dest_len = uncompressed_size;
763 const uint8_t *in = td->tmp;
767 if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK ||
768 dest_len != uncompressed_size)
769 return AVERROR(EINVAL);
771 out = td->uncompressed_data;
772 for (i = 0; i < s->ysize; i++) {
773 for (c = 0; c < s->nb_channels; c++) {
774 EXRChannel *channel = &s->channels[c];
775 const uint8_t *ptr[4];
778 switch (channel->pixel_type) {
781 ptr[1] = ptr[0] + s->xdelta;
782 ptr[2] = ptr[1] + s->xdelta;
783 in = ptr[2] + s->xdelta;
785 for (j = 0; j < s->xdelta; ++j) {
786 uint32_t diff = (*(ptr[0]++) << 24) |
787 (*(ptr[1]++) << 16) |
790 bytestream_put_le32(&out, pixel);
795 ptr[1] = ptr[0] + s->xdelta;
796 in = ptr[1] + s->xdelta;
797 for (j = 0; j < s->xdelta; j++) {
798 uint32_t diff = (*(ptr[0]++) << 8) | *(ptr[1]++);
801 bytestream_put_le16(&out, pixel);
813 static int decode_block(AVCodecContext *avctx, void *tdata,
814 int jobnr, int threadnr)
816 EXRContext *s = avctx->priv_data;
817 AVFrame *const p = s->picture;
818 EXRThreadData *td = &s->thread_data[threadnr];
819 const uint8_t *channel_buffer[4] = { 0 };
820 const uint8_t *buf = s->buf;
821 uint64_t line_offset, uncompressed_size;
822 uint32_t xdelta = s->xdelta;
825 int32_t data_size, line;
827 int axmax = (avctx->width - (s->xmax + 1)) * 2 * s->desc->nb_components;
828 int bxmin = s->xmin * 2 * s->desc->nb_components;
829 int i, x, buf_size = s->buf_size;
832 line_offset = AV_RL64(s->table + jobnr * 8);
833 // Check if the buffer has the required bytes needed from the offset
834 if (line_offset > buf_size - 8)
835 return AVERROR_INVALIDDATA;
837 src = buf + line_offset + 8;
838 line = AV_RL32(src - 8);
839 if (line < s->ymin || line > s->ymax)
840 return AVERROR_INVALIDDATA;
842 data_size = AV_RL32(src - 4);
843 if (data_size <= 0 || data_size > buf_size)
844 return AVERROR_INVALIDDATA;
846 s->ysize = FFMIN(s->scan_lines_per_block, s->ymax - line + 1);
847 uncompressed_size = s->scan_line_size * s->ysize;
848 if ((s->compr == EXR_RAW && (data_size != uncompressed_size ||
849 line_offset > buf_size - uncompressed_size)) ||
850 (s->compr != EXR_RAW && (data_size > uncompressed_size ||
851 line_offset > buf_size - data_size))) {
852 return AVERROR_INVALIDDATA;
855 if (data_size < uncompressed_size) {
856 av_fast_padded_malloc(&td->uncompressed_data, &td->uncompressed_size, uncompressed_size);
857 av_fast_padded_malloc(&td->tmp, &td->tmp_size, uncompressed_size);
858 if (!td->uncompressed_data || !td->tmp)
859 return AVERROR(ENOMEM);
864 ret = zip_uncompress(src, data_size, uncompressed_size, td);
867 ret = piz_uncompress(s, src, data_size, uncompressed_size, td);
870 ret = pxr24_uncompress(s, src, data_size, uncompressed_size, td);
873 ret = rle_uncompress(src, data_size, uncompressed_size, td);
876 src = td->uncompressed_data;
879 channel_buffer[0] = src + xdelta * s->channel_offsets[0];
880 channel_buffer[1] = src + xdelta * s->channel_offsets[1];
881 channel_buffer[2] = src + xdelta * s->channel_offsets[2];
882 if (s->channel_offsets[3] >= 0)
883 channel_buffer[3] = src + xdelta * s->channel_offsets[3];
885 ptr = p->data[0] + line * p->linesize[0];
886 for (i = 0; i < s->scan_lines_per_block && line + i <= s->ymax; i++, ptr += p->linesize[0]) {
887 const uint8_t *r, *g, *b, *a;
889 r = channel_buffer[0];
890 g = channel_buffer[1];
891 b = channel_buffer[2];
892 if (channel_buffer[3])
893 a = channel_buffer[3];
895 ptr_x = (uint16_t *)ptr;
897 // Zero out the start if xmin is not 0
898 memset(ptr_x, 0, bxmin);
899 ptr_x += s->xmin * s->desc->nb_components;
900 if (s->pixel_type == EXR_FLOAT) {
902 for (x = 0; x < xdelta; x++) {
903 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&r));
904 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&g));
905 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&b));
906 if (channel_buffer[3])
907 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a));
911 for (x = 0; x < xdelta; x++) {
912 *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&r));
913 *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&g));
914 *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&b));
915 if (channel_buffer[3])
916 *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&a));
920 // Zero out the end if xmax+1 is not w
921 memset(ptr_x, 0, axmax);
923 channel_buffer[0] += s->scan_line_size;
924 channel_buffer[1] += s->scan_line_size;
925 channel_buffer[2] += s->scan_line_size;
926 if (channel_buffer[3])
927 channel_buffer[3] += s->scan_line_size;
933 static int decode_frame(AVCodecContext *avctx,
938 const uint8_t *buf = avpkt->data;
939 unsigned int buf_size = avpkt->size;
940 const uint8_t *buf_end = buf + buf_size;
942 EXRContext *const s = avctx->priv_data;
943 ThreadFrame frame = { .f = data };
944 AVFrame *picture = data;
947 int i, y, magic_number, version, flags, ret;
952 int scan_line_blocks;
954 unsigned int current_channel_offset = 0;
962 s->channel_offsets[0] = -1;
963 s->channel_offsets[1] = -1;
964 s->channel_offsets[2] = -1;
965 s->channel_offsets[3] = -1;
970 s->buf_size = buf_size;
973 av_log(avctx, AV_LOG_ERROR, "Too short header to parse\n");
974 return AVERROR_INVALIDDATA;
977 magic_number = bytestream_get_le32(&buf);
978 if (magic_number != 20000630) { // As per documentation of OpenEXR it's supposed to be int 20000630 little-endian
979 av_log(avctx, AV_LOG_ERROR, "Wrong magic number %d\n", magic_number);
980 return AVERROR_INVALIDDATA;
983 version = bytestream_get_byte(&buf);
985 avpriv_report_missing_feature(avctx, "Version %d", version);
986 return AVERROR_PATCHWELCOME;
989 flags = bytestream_get_le24(&buf);
991 avpriv_report_missing_feature(avctx, "Tile support");
992 return AVERROR_PATCHWELCOME;
996 while (buf < buf_end && buf[0]) {
997 unsigned int variable_buffer_data_size;
998 // Process the channel list
999 if (check_header_variable(avctx, &buf, buf_end, "channels", "chlist", 38, &variable_buffer_data_size) >= 0) {
1000 const uint8_t *channel_list_end;
1001 if (!variable_buffer_data_size)
1002 return AVERROR_INVALIDDATA;
1004 channel_list_end = buf + variable_buffer_data_size;
1005 while (channel_list_end - buf >= 19) {
1006 EXRChannel *channel;
1007 enum ExrPixelType current_pixel_type;
1008 int channel_index = -1;
1011 const char* b = buf;
1013 if ( strcmp( s->layer, "" ) != 0 ) {
1014 if ( strncmp( b, s->layer, strlen(s->layer) ) == 0 ) {
1015 b += strlen(s->layer);
1016 if ( *b == '.' ) ++b; /* skip dot if not given */
1017 av_log( avctx, AV_LOG_INFO, "Layer %s.%s matched\n",
1023 if (!strcmp(b, "R")||!strcmp(b, "X")||!strcmp(b,"U"))
1025 else if (!strcmp(b, "G")||!strcmp(b, "Y")||!strcmp(b,"V"))
1027 else if (!strcmp(b, "B")||!strcmp(b, "Z")||!strcmp(b,"W"))
1029 else if (!strcmp(b, "A"))
1032 av_log(avctx, AV_LOG_WARNING, "Unsupported channel %.256s\n", buf);
1034 while (bytestream_get_byte(&buf) && buf < channel_list_end)
1035 continue; /* skip */
1037 if (channel_list_end - * &buf < 4) {
1038 av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
1039 return AVERROR_INVALIDDATA;
1042 current_pixel_type = bytestream_get_le32(&buf);
1043 if (current_pixel_type > 2) {
1044 av_log(avctx, AV_LOG_ERROR, "Unknown pixel type\n");
1045 return AVERROR_INVALIDDATA;
1049 xsub = bytestream_get_le32(&buf);
1050 ysub = bytestream_get_le32(&buf);
1051 if (xsub != 1 || ysub != 1) {
1052 avpriv_report_missing_feature(avctx, "Subsampling %dx%d", xsub, ysub);
1053 return AVERROR_PATCHWELCOME;
1056 if (channel_index >= 0) {
1057 if (s->pixel_type != -1 && s->pixel_type != current_pixel_type) {
1058 av_log(avctx, AV_LOG_ERROR, "RGB channels not of the same depth\n");
1059 return AVERROR_INVALIDDATA;
1061 s->pixel_type = current_pixel_type;
1062 s->channel_offsets[channel_index] = current_channel_offset;
1065 s->channels = av_realloc_f(s->channels, ++s->nb_channels, sizeof(EXRChannel));
1067 return AVERROR(ENOMEM);
1068 channel = &s->channels[s->nb_channels - 1];
1069 channel->pixel_type = current_pixel_type;
1070 channel->xsub = xsub;
1071 channel->ysub = ysub;
1073 current_channel_offset += 1 << current_pixel_type;
1076 /* Check if all channels are set with an offset or if the channels
1077 * are causing an overflow */
1079 if (FFMIN3(s->channel_offsets[0],
1080 s->channel_offsets[1],
1081 s->channel_offsets[2]) < 0) {
1082 if (s->channel_offsets[0] < 0)
1083 av_log(avctx, AV_LOG_ERROR, "Missing red channel\n");
1084 if (s->channel_offsets[1] < 0)
1085 av_log(avctx, AV_LOG_ERROR, "Missing green channel\n");
1086 if (s->channel_offsets[2] < 0)
1087 av_log(avctx, AV_LOG_ERROR, "Missing blue channel\n");
1088 return AVERROR_INVALIDDATA;
1091 buf = channel_list_end;
1093 } else if (check_header_variable(avctx, &buf, buf_end, "dataWindow", "box2i", 31, &variable_buffer_data_size) >= 0) {
1094 if (!variable_buffer_data_size)
1095 return AVERROR_INVALIDDATA;
1097 s->xmin = AV_RL32(buf);
1098 s->ymin = AV_RL32(buf + 4);
1099 s->xmax = AV_RL32(buf + 8);
1100 s->ymax = AV_RL32(buf + 12);
1101 s->xdelta = (s->xmax - s->xmin) + 1;
1102 s->ydelta = (s->ymax - s->ymin) + 1;
1104 buf += variable_buffer_data_size;
1106 } else if (check_header_variable(avctx, &buf, buf_end, "displayWindow", "box2i", 34, &variable_buffer_data_size) >= 0) {
1107 if (!variable_buffer_data_size)
1108 return AVERROR_INVALIDDATA;
1110 w = AV_RL32(buf + 8) + 1;
1111 h = AV_RL32(buf + 12) + 1;
1113 buf += variable_buffer_data_size;
1115 } else if (check_header_variable(avctx, &buf, buf_end, "lineOrder", "lineOrder", 25, &variable_buffer_data_size) >= 0) {
1116 if (!variable_buffer_data_size)
1117 return AVERROR_INVALIDDATA;
1119 av_log(avctx, AV_LOG_DEBUG, "line order : %d\n", *buf);
1121 av_log(avctx, AV_LOG_ERROR, "Unknown line order\n");
1122 return AVERROR_INVALIDDATA;
1125 buf += variable_buffer_data_size;
1127 } else if (check_header_variable(avctx, &buf, buf_end, "pixelAspectRatio", "float", 31, &variable_buffer_data_size) >= 0) {
1128 if (!variable_buffer_data_size)
1129 return AVERROR_INVALIDDATA;
1131 avctx->sample_aspect_ratio = av_d2q(av_int2float(AV_RL32(buf)), 255);
1133 buf += variable_buffer_data_size;
1135 } else if (check_header_variable(avctx, &buf, buf_end, "compression", "compression", 29, &variable_buffer_data_size) >= 0) {
1136 if (!variable_buffer_data_size)
1137 return AVERROR_INVALIDDATA;
1142 av_log(avctx, AV_LOG_WARNING, "Found more than one compression attribute\n");
1144 buf += variable_buffer_data_size;
1148 // Check if there is enough bytes for a header
1149 if (buf_end - buf <= 9) {
1150 av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
1151 return AVERROR_INVALIDDATA;
1154 // Process unknown variables
1155 for (i = 0; i < 2; i++) {
1156 // Skip variable name/type
1157 while (++buf < buf_end)
1162 // Skip variable length
1163 if (buf_end - buf >= 5) {
1164 variable_buffer_data_size = get_header_variable_length(&buf, buf_end);
1165 if (!variable_buffer_data_size) {
1166 av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
1167 return AVERROR_INVALIDDATA;
1169 buf += variable_buffer_data_size;
1173 if (s->compr == -1) {
1174 av_log(avctx, AV_LOG_ERROR, "Missing compression attribute\n");
1175 return AVERROR_INVALIDDATA;
1178 if (buf >= buf_end) {
1179 av_log(avctx, AV_LOG_ERROR, "Incomplete frame\n");
1180 return AVERROR_INVALIDDATA;
1184 switch (s->pixel_type) {
1187 if (s->channel_offsets[3] >= 0)
1188 avctx->pix_fmt = AV_PIX_FMT_RGBA64;
1190 avctx->pix_fmt = AV_PIX_FMT_RGB48;
1193 avpriv_request_sample(avctx, "32-bit unsigned int");
1194 return AVERROR_PATCHWELCOME;
1196 av_log(avctx, AV_LOG_ERROR, "Missing channel list\n");
1197 return AVERROR_INVALIDDATA;
1204 s->scan_lines_per_block = 1;
1208 s->scan_lines_per_block = 16;
1211 s->scan_lines_per_block = 32;
1214 avpriv_report_missing_feature(avctx, "Compression %d", s->compr);
1215 return AVERROR_PATCHWELCOME;
1218 // Verify the xmin, xmax, ymin, ymax and xdelta before setting the actual image size
1219 if (s->xmin > s->xmax ||
1220 s->ymin > s->ymax ||
1221 s->xdelta != s->xmax - s->xmin + 1 ||
1222 s->xmax >= w || s->ymax >= h) {
1223 av_log(avctx, AV_LOG_ERROR, "Wrong sizing or missing size information\n");
1224 return AVERROR_INVALIDDATA;
1227 if ((ret = ff_set_dimensions(avctx, w, h)) < 0)
1230 s->desc = av_pix_fmt_desc_get(avctx->pix_fmt);
1231 out_line_size = avctx->width * 2 * s->desc->nb_components;
1232 s->scan_line_size = s->xdelta * current_channel_offset;
1233 scan_line_blocks = (s->ydelta + s->scan_lines_per_block - 1) / s->scan_lines_per_block;
1235 if (s->compr != EXR_RAW) {
1236 size_t thread_data_size, prev_size;
1239 prev_size = s->thread_data_size;
1240 if (av_size_mult(avctx->thread_count, sizeof(EXRThreadData), &thread_data_size))
1241 return AVERROR(EINVAL);
1243 m = av_fast_realloc(s->thread_data, &s->thread_data_size, thread_data_size);
1245 return AVERROR(ENOMEM);
1247 memset(s->thread_data + prev_size, 0, s->thread_data_size - prev_size);
1250 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
1253 if (buf_end - buf < scan_line_blocks * 8)
1254 return AVERROR_INVALIDDATA;
1256 ptr = picture->data[0];
1258 // Zero out the start if ymin is not 0
1259 for (y = 0; y < s->ymin; y++) {
1260 memset(ptr, 0, out_line_size);
1261 ptr += picture->linesize[0];
1264 s->picture = picture;
1265 avctx->execute2(avctx, decode_block, s->thread_data, NULL, scan_line_blocks);
1267 // Zero out the end if ymax+1 is not h
1268 for (y = s->ymax + 1; y < avctx->height; y++) {
1269 memset(ptr, 0, out_line_size);
1270 ptr += picture->linesize[0];
1273 picture->pict_type = AV_PICTURE_TYPE_I;
1279 static av_cold int decode_end(AVCodecContext *avctx)
1281 EXRContext *s = avctx->priv_data;
1284 for (i = 0; i < s->thread_data_size / sizeof(EXRThreadData); i++) {
1285 EXRThreadData *td = &s->thread_data[i];
1286 av_freep(&td->uncompressed_data);
1288 av_freep(&td->bitmap);
1292 av_freep(&s->thread_data);
1293 s->thread_data_size = 0;
1294 av_freep(&s->channels);
1299 AVCodec ff_exr_decoder = {
1301 .long_name = NULL_IF_CONFIG_SMALL("OpenEXR image"),
1302 .type = AVMEDIA_TYPE_VIDEO,
1303 .id = AV_CODEC_ID_EXR,
1304 .priv_data_size = sizeof(EXRContext),
1305 .close = decode_end,
1306 .decode = decode_frame,
1307 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS | CODEC_CAP_SLICE_THREADS,
1308 .priv_class = &exr_class,