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
35 #include "libavutil/imgutils.h"
36 #include "libavutil/opt.h"
39 #include "bytestream.h"
64 typedef struct EXRChannel {
66 enum ExrPixelType pixel_type;
69 typedef struct EXRThreadData {
70 uint8_t *uncompressed_data;
71 int uncompressed_size;
80 typedef struct EXRContext {
83 AVCodecContext *avctx;
85 enum ExrCompr compression;
86 enum ExrPixelType pixel_type;
87 int channel_offsets[4]; // 0 = red, 1 = green, 2 = blue and 3 = alpha
88 const AVPixFmtDescriptor *desc;
93 uint32_t xdelta, ydelta;
96 uint64_t scan_line_size;
97 int scan_lines_per_block;
103 EXRChannel *channels;
106 EXRThreadData *thread_data;
112 * Convert from 32-bit float as uint32_t to uint16_t.
114 * @param v 32-bit float
116 * @return normalized 16-bit unsigned int
118 static inline uint16_t exr_flt2uint(uint32_t v)
120 unsigned int exp = v >> 23;
121 // "HACK": negative values result in exp< 0, so clipping them to 0
122 // is also handled by this condition, avoids explicit check for sign bit.
123 if (exp <= 127 + 7 - 24) // we would shift out all bits anyway
128 return (v + (1 << 23)) >> (127 + 7 - exp);
132 * Convert from 16-bit float as uint16_t to uint16_t.
134 * @param v 16-bit float
136 * @return normalized 16-bit unsigned int
138 static inline uint16_t exr_halflt2uint(uint16_t v)
140 unsigned exp = 14 - (v >> 10);
145 return (v & 0x8000) ? 0 : 0xffff;
148 return (v + (1 << 16)) >> (exp + 1);
151 static void predictor(uint8_t *src, int size)
153 uint8_t *t = src + 1;
154 uint8_t *stop = src + size;
157 int d = (int) t[-1] + (int) t[0] - 128;
163 static void reorder_pixels(uint8_t *src, uint8_t *dst, int size)
165 const int8_t *t1 = src;
166 const int8_t *t2 = src + (size + 1) / 2;
168 int8_t *stop = s + size;
183 static int zip_uncompress(const uint8_t *src, int compressed_size,
184 int uncompressed_size, EXRThreadData *td)
186 unsigned long dest_len = uncompressed_size;
188 if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK ||
189 dest_len != uncompressed_size)
190 return AVERROR_INVALIDDATA;
192 predictor(td->tmp, uncompressed_size);
193 reorder_pixels(td->tmp, td->uncompressed_data, uncompressed_size);
198 static int rle_uncompress(const uint8_t *src, int compressed_size,
199 int uncompressed_size, EXRThreadData *td)
201 uint8_t *d = td->tmp;
202 const int8_t *s = src;
203 int ssize = compressed_size;
204 int dsize = uncompressed_size;
205 uint8_t *dend = d + dsize;
214 if ((dsize -= count) < 0 ||
215 (ssize -= count + 1) < 0)
216 return AVERROR_INVALIDDATA;
223 if ((dsize -= count) < 0 ||
225 return AVERROR_INVALIDDATA;
235 return AVERROR_INVALIDDATA;
237 predictor(td->tmp, uncompressed_size);
238 reorder_pixels(td->tmp, td->uncompressed_data, uncompressed_size);
243 #define USHORT_RANGE (1 << 16)
244 #define BITMAP_SIZE (1 << 13)
246 static uint16_t reverse_lut(const uint8_t *bitmap, uint16_t *lut)
250 for (i = 0; i < USHORT_RANGE; i++)
251 if ((i == 0) || (bitmap[i >> 3] & (1 << (i & 7))))
256 memset(lut + k, 0, (USHORT_RANGE - k) * 2);
261 static void apply_lut(const uint16_t *lut, uint16_t *dst, int dsize)
265 for (i = 0; i < dsize; ++i)
266 dst[i] = lut[dst[i]];
269 #define HUF_ENCBITS 16 // literal (value) bit length
270 #define HUF_DECBITS 14 // decoding bit size (>= 8)
272 #define HUF_ENCSIZE ((1 << HUF_ENCBITS) + 1) // encoding table size
273 #define HUF_DECSIZE (1 << HUF_DECBITS) // decoding table size
274 #define HUF_DECMASK (HUF_DECSIZE - 1)
276 typedef struct HufDec {
282 static void huf_canonical_code_table(uint64_t *hcode)
284 uint64_t c, n[59] = { 0 };
287 for (i = 0; i < HUF_ENCSIZE; ++i)
291 for (i = 58; i > 0; --i) {
292 uint64_t nc = ((c + n[i]) >> 1);
297 for (i = 0; i < HUF_ENCSIZE; ++i) {
301 hcode[i] = l | (n[l]++ << 6);
305 #define SHORT_ZEROCODE_RUN 59
306 #define LONG_ZEROCODE_RUN 63
307 #define SHORTEST_LONG_RUN (2 + LONG_ZEROCODE_RUN - SHORT_ZEROCODE_RUN)
308 #define LONGEST_LONG_RUN (255 + SHORTEST_LONG_RUN)
310 static int huf_unpack_enc_table(GetByteContext *gb,
311 int32_t im, int32_t iM, uint64_t *hcode)
315 init_get_bits8(&gbit, gb->buffer, bytestream2_get_bytes_left(gb));
317 for (; im <= iM; im++) {
318 uint64_t l = hcode[im] = get_bits(&gbit, 6);
320 if (l == LONG_ZEROCODE_RUN) {
321 int zerun = get_bits(&gbit, 8) + SHORTEST_LONG_RUN;
323 if (im + zerun > iM + 1)
324 return AVERROR_INVALIDDATA;
330 } else if (l >= SHORT_ZEROCODE_RUN) {
331 int zerun = l - SHORT_ZEROCODE_RUN + 2;
333 if (im + zerun > iM + 1)
334 return AVERROR_INVALIDDATA;
343 bytestream2_skip(gb, (get_bits_count(&gbit) + 7) / 8);
344 huf_canonical_code_table(hcode);
349 static int huf_build_dec_table(const uint64_t *hcode, int im,
350 int iM, HufDec *hdecod)
352 for (; im <= iM; im++) {
353 uint64_t c = hcode[im] >> 6;
354 int i, l = hcode[im] & 63;
357 return AVERROR_INVALIDDATA;
359 if (l > HUF_DECBITS) {
360 HufDec *pl = hdecod + (c >> (l - HUF_DECBITS));
362 return AVERROR_INVALIDDATA;
366 pl->p = av_realloc(pl->p, pl->lit * sizeof(int));
368 return AVERROR(ENOMEM);
370 pl->p[pl->lit - 1] = im;
372 HufDec *pl = hdecod + (c << (HUF_DECBITS - l));
374 for (i = 1 << (HUF_DECBITS - l); i > 0; i--, pl++) {
375 if (pl->len || pl->p)
376 return AVERROR_INVALIDDATA;
386 #define get_char(c, lc, gb) \
388 c = (c << 8) | bytestream2_get_byte(gb); \
392 #define get_code(po, rlc, c, lc, gb, out, oe) \
396 get_char(c, lc, gb); \
402 return AVERROR_INVALIDDATA; \
408 } else if (out < oe) { \
411 return AVERROR_INVALIDDATA; \
415 static int huf_decode(const uint64_t *hcode, const HufDec *hdecod,
416 GetByteContext *gb, int nbits,
417 int rlc, int no, uint16_t *out)
420 uint16_t *outb = out;
421 uint16_t *oe = out + no;
422 const uint8_t *ie = gb->buffer + (nbits + 7) / 8; // input byte size
426 while (gb->buffer < ie) {
429 while (lc >= HUF_DECBITS) {
430 const HufDec pl = hdecod[(c >> (lc - HUF_DECBITS)) & HUF_DECMASK];
434 get_code(pl.lit, rlc, c, lc, gb, out, oe);
439 return AVERROR_INVALIDDATA;
441 for (j = 0; j < pl.lit; j++) {
442 int l = hcode[pl.p[j]] & 63;
444 while (lc < l && bytestream2_get_bytes_left(gb) > 0)
448 if ((hcode[pl.p[j]] >> 6) ==
449 ((c >> (lc - l)) & ((1LL << l) - 1))) {
451 get_code(pl.p[j], rlc, c, lc, gb, out, oe);
458 return AVERROR_INVALIDDATA;
468 const HufDec pl = hdecod[(c << (HUF_DECBITS - lc)) & HUF_DECMASK];
472 get_code(pl.lit, rlc, c, lc, gb, out, oe);
474 return AVERROR_INVALIDDATA;
478 if (out - outb != no)
479 return AVERROR_INVALIDDATA;
483 static int huf_uncompress(GetByteContext *gb,
484 uint16_t *dst, int dst_size)
486 int32_t src_size, im, iM;
492 src_size = bytestream2_get_le32(gb);
493 im = bytestream2_get_le32(gb);
494 iM = bytestream2_get_le32(gb);
495 bytestream2_skip(gb, 4);
496 nBits = bytestream2_get_le32(gb);
497 if (im < 0 || im >= HUF_ENCSIZE ||
498 iM < 0 || iM >= HUF_ENCSIZE ||
500 return AVERROR_INVALIDDATA;
502 bytestream2_skip(gb, 4);
504 freq = av_mallocz_array(HUF_ENCSIZE, sizeof(*freq));
505 hdec = av_mallocz_array(HUF_DECSIZE, sizeof(*hdec));
506 if (!freq || !hdec) {
507 ret = AVERROR(ENOMEM);
511 if ((ret = huf_unpack_enc_table(gb, im, iM, freq)) < 0)
514 if (nBits > 8 * bytestream2_get_bytes_left(gb)) {
515 ret = AVERROR_INVALIDDATA;
519 if ((ret = huf_build_dec_table(freq, im, iM, hdec)) < 0)
521 ret = huf_decode(freq, hdec, gb, nBits, iM, dst_size, dst);
524 for (i = 0; i < HUF_DECSIZE; i++)
526 av_freep(&hdec[i].p);
534 static inline void wdec14(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
539 int ai = ls + (hi & 1) + (hi >> 1);
541 int16_t bs = ai - hi;
548 #define A_OFFSET (1 << (NBITS - 1))
549 #define MOD_MASK ((1 << NBITS) - 1)
551 static inline void wdec16(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
555 int bb = (m - (d >> 1)) & MOD_MASK;
556 int aa = (d + bb - A_OFFSET) & MOD_MASK;
561 static void wav_decode(uint16_t *in, int nx, int ox,
562 int ny, int oy, uint16_t mx)
564 int w14 = (mx < (1 << 14));
565 int n = (nx > ny) ? ny : nx;
578 uint16_t *ey = in + oy * (ny - p2);
579 uint16_t i00, i01, i10, i11;
585 for (; py <= ey; py += oy2) {
587 uint16_t *ex = py + ox * (nx - p2);
589 for (; px <= ex; px += ox2) {
590 uint16_t *p01 = px + ox1;
591 uint16_t *p10 = px + oy1;
592 uint16_t *p11 = p10 + ox1;
595 wdec14(*px, *p10, &i00, &i10);
596 wdec14(*p01, *p11, &i01, &i11);
597 wdec14(i00, i01, px, p01);
598 wdec14(i10, i11, p10, p11);
600 wdec16(*px, *p10, &i00, &i10);
601 wdec16(*p01, *p11, &i01, &i11);
602 wdec16(i00, i01, px, p01);
603 wdec16(i10, i11, p10, p11);
608 uint16_t *p10 = px + oy1;
611 wdec14(*px, *p10, &i00, p10);
613 wdec16(*px, *p10, &i00, p10);
621 uint16_t *ex = py + ox * (nx - p2);
623 for (; px <= ex; px += ox2) {
624 uint16_t *p01 = px + ox1;
627 wdec14(*px, *p01, &i00, p01);
629 wdec16(*px, *p01, &i00, p01);
640 static int piz_uncompress(EXRContext *s, const uint8_t *src, int ssize,
641 int dsize, EXRThreadData *td)
644 uint16_t maxval, min_non_zero, max_non_zero;
646 uint16_t *tmp = (uint16_t *)td->tmp;
651 td->bitmap = av_malloc(BITMAP_SIZE);
653 td->lut = av_malloc(1 << 17);
654 if (!td->bitmap || !td->lut) {
655 av_freep(&td->bitmap);
657 return AVERROR(ENOMEM);
660 bytestream2_init(&gb, src, ssize);
661 min_non_zero = bytestream2_get_le16(&gb);
662 max_non_zero = bytestream2_get_le16(&gb);
664 if (max_non_zero >= BITMAP_SIZE)
665 return AVERROR_INVALIDDATA;
667 memset(td->bitmap, 0, FFMIN(min_non_zero, BITMAP_SIZE));
668 if (min_non_zero <= max_non_zero)
669 bytestream2_get_buffer(&gb, td->bitmap + min_non_zero,
670 max_non_zero - min_non_zero + 1);
671 memset(td->bitmap + max_non_zero, 0, BITMAP_SIZE - max_non_zero);
673 maxval = reverse_lut(td->bitmap, td->lut);
675 ret = huf_uncompress(&gb, tmp, dsize / sizeof(uint16_t));
680 for (i = 0; i < s->nb_channels; i++) {
681 EXRChannel *channel = &s->channels[i];
682 int size = channel->pixel_type;
684 for (j = 0; j < size; j++)
685 wav_decode(ptr + j, s->xdelta, size, s->ysize,
686 s->xdelta * size, maxval);
687 ptr += s->xdelta * s->ysize * size;
690 apply_lut(td->lut, tmp, dsize / sizeof(uint16_t));
692 out = td->uncompressed_data;
693 for (i = 0; i < s->ysize; i++)
694 for (j = 0; j < s->nb_channels; j++) {
695 uint16_t *in = tmp + j * s->xdelta * s->ysize + i * s->xdelta;
696 memcpy(out, in, s->xdelta * 2);
697 out += s->xdelta * 2;
703 static int pxr24_uncompress(EXRContext *s, const uint8_t *src,
704 int compressed_size, int uncompressed_size,
707 unsigned long dest_len = uncompressed_size;
708 const uint8_t *in = td->tmp;
712 if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK ||
713 dest_len != uncompressed_size)
714 return AVERROR_INVALIDDATA;
716 out = td->uncompressed_data;
717 for (i = 0; i < s->ysize; i++)
718 for (c = 0; c < s->nb_channels; c++) {
719 EXRChannel *channel = &s->channels[c];
720 const uint8_t *ptr[4];
723 switch (channel->pixel_type) {
726 ptr[1] = ptr[0] + s->xdelta;
727 ptr[2] = ptr[1] + s->xdelta;
728 in = ptr[2] + s->xdelta;
730 for (j = 0; j < s->xdelta; ++j) {
731 uint32_t diff = (*(ptr[0]++) << 24) |
732 (*(ptr[1]++) << 16) |
735 bytestream_put_le32(&out, pixel);
740 ptr[1] = ptr[0] + s->xdelta;
741 in = ptr[1] + s->xdelta;
742 for (j = 0; j < s->xdelta; j++) {
743 uint32_t diff = (*(ptr[0]++) << 8) | *(ptr[1]++);
746 bytestream_put_le16(&out, pixel);
750 return AVERROR_INVALIDDATA;
757 static int decode_block(AVCodecContext *avctx, void *tdata,
758 int jobnr, int threadnr)
760 EXRContext *s = avctx->priv_data;
761 AVFrame *const p = s->picture;
762 EXRThreadData *td = &s->thread_data[threadnr];
763 const uint8_t *channel_buffer[4] = { 0 };
764 const uint8_t *buf = s->buf;
765 uint64_t line_offset, uncompressed_size;
766 uint32_t xdelta = s->xdelta;
769 uint32_t data_size, line;
771 int axmax = (avctx->width - (s->xmax + 1)) * 2 * s->desc->nb_components;
772 int bxmin = s->xmin * 2 * s->desc->nb_components;
773 int i, x, buf_size = s->buf_size;
776 line_offset = AV_RL64(s->gb.buffer + jobnr * 8);
777 // Check if the buffer has the required bytes needed from the offset
778 if (line_offset > buf_size - 8)
779 return AVERROR_INVALIDDATA;
781 src = buf + line_offset + 8;
782 line = AV_RL32(src - 8);
783 if (line < s->ymin || line > s->ymax)
784 return AVERROR_INVALIDDATA;
786 data_size = AV_RL32(src - 4);
787 if (data_size <= 0 || data_size > buf_size)
788 return AVERROR_INVALIDDATA;
790 s->ysize = FFMIN(s->scan_lines_per_block, s->ymax - line + 1);
791 uncompressed_size = s->scan_line_size * s->ysize;
792 if ((s->compression == EXR_RAW && (data_size != uncompressed_size ||
793 line_offset > buf_size - uncompressed_size)) ||
794 (s->compression != EXR_RAW && (data_size > uncompressed_size ||
795 line_offset > buf_size - data_size))) {
796 return AVERROR_INVALIDDATA;
799 if (data_size < uncompressed_size) {
800 av_fast_padded_malloc(&td->uncompressed_data,
801 &td->uncompressed_size, uncompressed_size);
802 av_fast_padded_malloc(&td->tmp, &td->tmp_size, uncompressed_size);
803 if (!td->uncompressed_data || !td->tmp)
804 return AVERROR(ENOMEM);
806 ret = AVERROR_INVALIDDATA;
807 switch (s->compression) {
810 ret = zip_uncompress(src, data_size, uncompressed_size, td);
813 ret = piz_uncompress(s, src, data_size, uncompressed_size, td);
816 ret = pxr24_uncompress(s, src, data_size, uncompressed_size, td);
819 ret = rle_uncompress(src, data_size, uncompressed_size, td);
822 av_log(avctx, AV_LOG_ERROR, "decode_block() failed.\n");
825 src = td->uncompressed_data;
828 channel_buffer[0] = src + xdelta * s->channel_offsets[0];
829 channel_buffer[1] = src + xdelta * s->channel_offsets[1];
830 channel_buffer[2] = src + xdelta * s->channel_offsets[2];
831 if (s->channel_offsets[3] >= 0)
832 channel_buffer[3] = src + xdelta * s->channel_offsets[3];
834 ptr = p->data[0] + line * p->linesize[0];
836 i < s->scan_lines_per_block && line + i <= s->ymax;
837 i++, ptr += p->linesize[0]) {
838 const uint8_t *r, *g, *b, *a;
840 r = channel_buffer[0];
841 g = channel_buffer[1];
842 b = channel_buffer[2];
843 if (channel_buffer[3])
844 a = channel_buffer[3];
846 ptr_x = (uint16_t *) ptr;
848 // Zero out the start if xmin is not 0
849 memset(ptr_x, 0, bxmin);
850 ptr_x += s->xmin * s->desc->nb_components;
851 if (s->pixel_type == EXR_FLOAT) {
853 for (x = 0; x < xdelta; x++) {
854 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&r));
855 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&g));
856 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&b));
857 if (channel_buffer[3])
858 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a));
862 for (x = 0; x < xdelta; x++) {
863 *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&r));
864 *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&g));
865 *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&b));
866 if (channel_buffer[3])
867 *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&a));
871 // Zero out the end if xmax+1 is not w
872 memset(ptr_x, 0, axmax);
874 channel_buffer[0] += s->scan_line_size;
875 channel_buffer[1] += s->scan_line_size;
876 channel_buffer[2] += s->scan_line_size;
877 if (channel_buffer[3])
878 channel_buffer[3] += s->scan_line_size;
885 * Check if the variable name corresponds to its data type.
887 * @param s the EXRContext
888 * @param value_name name of the variable to check
889 * @param value_type type of the variable to check
890 * @param minimum_length minimum length of the variable data
892 * @return bytes to read containing variable data
893 * -1 if variable is not found
894 * 0 if buffer ended prematurely
896 static int check_header_variable(EXRContext *s,
897 const char *value_name,
898 const char *value_type,
899 unsigned int minimum_length)
903 if (bytestream2_get_bytes_left(&s->gb) >= minimum_length &&
904 !strcmp(s->gb.buffer, value_name)) {
905 // found value_name, jump to value_type (null terminated strings)
906 s->gb.buffer += strlen(value_name) + 1;
907 if (!strcmp(s->gb.buffer, value_type)) {
908 s->gb.buffer += strlen(value_type) + 1;
909 var_size = bytestream2_get_le32(&s->gb);
910 // don't go read past boundaries
911 if (var_size > bytestream2_get_bytes_left(&s->gb))
914 // value_type not found, reset the buffer
915 s->gb.buffer -= strlen(value_name) + 1;
916 av_log(s->avctx, AV_LOG_WARNING,
917 "Unknown data type %s for header variable %s.\n",
918 value_type, value_name);
925 static int decode_header(EXRContext *s)
927 int current_channel_offset = 0;
928 int magic_number, version, flags, i;
930 if (bytestream2_get_bytes_left(&s->gb) < 10) {
931 av_log(s->avctx, AV_LOG_ERROR, "Header too short to parse.\n");
932 return AVERROR_INVALIDDATA;
935 magic_number = bytestream2_get_le32(&s->gb);
936 if (magic_number != 20000630) {
937 /* As per documentation of OpenEXR, it is supposed to be
938 * int 20000630 little-endian */
939 av_log(s->avctx, AV_LOG_ERROR, "Wrong magic number %d.\n", magic_number);
940 return AVERROR_INVALIDDATA;
943 version = bytestream2_get_byte(&s->gb);
945 avpriv_report_missing_feature(s->avctx, "Version %d", version);
946 return AVERROR_PATCHWELCOME;
949 flags = bytestream2_get_le24(&s->gb);
951 avpriv_report_missing_feature(s->avctx, "Tile support");
952 return AVERROR_PATCHWELCOME;
956 while (bytestream2_get_bytes_left(&s->gb) > 0 && *s->gb.buffer) {
958 if ((var_size = check_header_variable(s, "channels",
959 "chlist", 38)) >= 0) {
960 GetByteContext ch_gb;
962 return AVERROR_INVALIDDATA;
964 bytestream2_init(&ch_gb, s->gb.buffer, var_size);
966 while (bytestream2_get_bytes_left(&ch_gb) >= 19) {
968 enum ExrPixelType current_pixel_type;
969 int channel_index = -1;
972 if (strcmp(s->layer, "") != 0) {
973 if (strncmp(ch_gb.buffer, s->layer, strlen(s->layer)) == 0) {
974 ch_gb.buffer += strlen(s->layer);
975 if (*ch_gb.buffer == '.')
976 ch_gb.buffer++; /* skip dot if not given */
977 av_log(s->avctx, AV_LOG_INFO,
978 "Layer %s.%s matched.\n", s->layer, ch_gb.buffer);
982 if (!strcmp(ch_gb.buffer, "R") ||
983 !strcmp(ch_gb.buffer, "X") ||
984 !strcmp(ch_gb.buffer, "U"))
986 else if (!strcmp(ch_gb.buffer, "G") ||
987 !strcmp(ch_gb.buffer, "Y") ||
988 !strcmp(ch_gb.buffer, "V"))
990 else if (!strcmp(ch_gb.buffer, "B") ||
991 !strcmp(ch_gb.buffer, "Z") ||
992 !strcmp(ch_gb.buffer, "W"))
994 else if (!strcmp(ch_gb.buffer, "A"))
997 av_log(s->avctx, AV_LOG_WARNING,
998 "Unsupported channel %.256s.\n", ch_gb.buffer);
1000 /* skip until you get a 0 */
1001 while (bytestream2_get_bytes_left(&ch_gb) > 0 &&
1002 bytestream2_get_byte(&ch_gb))
1005 if (bytestream2_get_bytes_left(&ch_gb) < 4) {
1006 av_log(s->avctx, AV_LOG_ERROR, "Incomplete header.\n");
1007 return AVERROR_INVALIDDATA;
1010 current_pixel_type = bytestream2_get_le32(&ch_gb);
1011 if (current_pixel_type >= EXR_UNKNOWN) {
1012 avpriv_report_missing_feature(s->avctx,
1014 current_pixel_type);
1015 return AVERROR_PATCHWELCOME;
1018 bytestream2_skip(&ch_gb, 4);
1019 xsub = bytestream2_get_le32(&ch_gb);
1020 ysub = bytestream2_get_le32(&ch_gb);
1021 if (xsub != 1 || ysub != 1) {
1022 avpriv_report_missing_feature(s->avctx,
1023 "Subsampling %dx%d",
1025 return AVERROR_PATCHWELCOME;
1028 if (channel_index >= 0) {
1029 if (s->pixel_type != EXR_UNKNOWN &&
1030 s->pixel_type != current_pixel_type) {
1031 av_log(s->avctx, AV_LOG_ERROR,
1032 "RGB channels not of the same depth.\n");
1033 return AVERROR_INVALIDDATA;
1035 s->pixel_type = current_pixel_type;
1036 s->channel_offsets[channel_index] = current_channel_offset;
1039 s->channels = av_realloc(s->channels,
1040 ++s->nb_channels * sizeof(EXRChannel));
1042 return AVERROR(ENOMEM);
1043 channel = &s->channels[s->nb_channels - 1];
1044 channel->pixel_type = current_pixel_type;
1045 channel->xsub = xsub;
1046 channel->ysub = ysub;
1048 current_channel_offset += 1 << current_pixel_type;
1051 /* Check if all channels are set with an offset or if the channels
1052 * are causing an overflow */
1053 if (FFMIN3(s->channel_offsets[0],
1054 s->channel_offsets[1],
1055 s->channel_offsets[2]) < 0) {
1056 if (s->channel_offsets[0] < 0)
1057 av_log(s->avctx, AV_LOG_ERROR, "Missing red channel.\n");
1058 if (s->channel_offsets[1] < 0)
1059 av_log(s->avctx, AV_LOG_ERROR, "Missing green channel.\n");
1060 if (s->channel_offsets[2] < 0)
1061 av_log(s->avctx, AV_LOG_ERROR, "Missing blue channel.\n");
1062 return AVERROR_INVALIDDATA;
1065 // skip one last byte and update main gb
1066 s->gb.buffer = ch_gb.buffer + 1;
1068 } else if ((var_size = check_header_variable(s, "dataWindow", "box2i",
1071 return AVERROR_INVALIDDATA;
1073 s->xmin = bytestream2_get_le32(&s->gb);
1074 s->ymin = bytestream2_get_le32(&s->gb);
1075 s->xmax = bytestream2_get_le32(&s->gb);
1076 s->ymax = bytestream2_get_le32(&s->gb);
1077 s->xdelta = (s->xmax - s->xmin) + 1;
1078 s->ydelta = (s->ymax - s->ymin) + 1;
1081 } else if ((var_size = check_header_variable(s, "displayWindow",
1082 "box2i", 34)) >= 0) {
1084 return AVERROR_INVALIDDATA;
1086 bytestream2_skip(&s->gb, 8);
1087 s->w = bytestream2_get_le32(&s->gb) + 1;
1088 s->h = bytestream2_get_le32(&s->gb) + 1;
1091 } else if ((var_size = check_header_variable(s, "lineOrder",
1092 "lineOrder", 25)) >= 0) {
1095 return AVERROR_INVALIDDATA;
1097 line_order = bytestream2_get_byte(&s->gb);
1098 av_log(s->avctx, AV_LOG_DEBUG, "line order: %d.\n", line_order);
1099 if (line_order > 2) {
1100 av_log(s->avctx, AV_LOG_ERROR, "Unknown line order.\n");
1101 return AVERROR_INVALIDDATA;
1105 } else if ((var_size = check_header_variable(s, "pixelAspectRatio",
1106 "float", 31)) >= 0) {
1108 return AVERROR_INVALIDDATA;
1110 s->avctx->sample_aspect_ratio =
1111 av_d2q(av_int2float(bytestream2_get_le32(&s->gb)), 255);
1114 } else if ((var_size = check_header_variable(s, "compression",
1115 "compression", 29)) >= 0) {
1117 return AVERROR_INVALIDDATA;
1119 if (s->compression == EXR_UNKN)
1120 s->compression = bytestream2_get_byte(&s->gb);
1122 av_log(s->avctx, AV_LOG_WARNING,
1123 "Found more than one compression attribute.\n");
1128 // Check if there are enough bytes for a header
1129 if (bytestream2_get_bytes_left(&s->gb) <= 9) {
1130 av_log(s->avctx, AV_LOG_ERROR, "Incomplete header\n");
1131 return AVERROR_INVALIDDATA;
1134 // Process unknown variables
1135 for (i = 0; i < 2; i++) // value_name and value_type
1136 while (bytestream2_get_byte(&s->gb) != 0);
1138 // Skip variable length
1139 bytestream2_skip(&s->gb, bytestream2_get_le32(&s->gb));
1142 if (s->compression == EXR_UNKN) {
1143 av_log(s->avctx, AV_LOG_ERROR, "Missing compression attribute.\n");
1144 return AVERROR_INVALIDDATA;
1146 s->scan_line_size = s->xdelta * current_channel_offset;
1148 if (bytestream2_get_bytes_left(&s->gb) <= 0) {
1149 av_log(s->avctx, AV_LOG_ERROR, "Incomplete frame.\n");
1150 return AVERROR_INVALIDDATA;
1153 // aaand we are done
1154 bytestream2_skip(&s->gb, 1);
1158 static int decode_frame(AVCodecContext *avctx, void *data,
1159 int *got_frame, AVPacket *avpkt)
1161 EXRContext *s = avctx->priv_data;
1162 ThreadFrame frame = { .f = data };
1163 AVFrame *picture = data;
1168 int scan_line_blocks;
1170 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
1172 if ((ret = decode_header(s)) < 0)
1175 switch (s->pixel_type) {
1178 if (s->channel_offsets[3] >= 0)
1179 avctx->pix_fmt = AV_PIX_FMT_RGBA64;
1181 avctx->pix_fmt = AV_PIX_FMT_RGB48;
1184 avpriv_request_sample(avctx, "32-bit unsigned int");
1185 return AVERROR_PATCHWELCOME;
1187 av_log(avctx, AV_LOG_ERROR, "Missing channel list.\n");
1188 return AVERROR_INVALIDDATA;
1191 switch (s->compression) {
1195 s->scan_lines_per_block = 1;
1199 s->scan_lines_per_block = 16;
1202 s->scan_lines_per_block = 32;
1205 avpriv_report_missing_feature(avctx, "Compression %d", s->compression);
1206 return AVERROR_PATCHWELCOME;
1209 /* Verify the xmin, xmax, ymin, ymax and xdelta before setting
1210 * the actual image size. */
1211 if (s->xmin > s->xmax ||
1212 s->ymin > s->ymax ||
1213 s->xdelta != s->xmax - s->xmin + 1 ||
1216 av_log(avctx, AV_LOG_ERROR, "Wrong or missing size information.\n");
1217 return AVERROR_INVALIDDATA;
1220 if ((ret = ff_set_dimensions(avctx, s->w, s->h)) < 0)
1223 s->desc = av_pix_fmt_desc_get(avctx->pix_fmt);
1225 return AVERROR_INVALIDDATA;
1226 out_line_size = avctx->width * 2 * s->desc->nb_components;
1227 scan_line_blocks = (s->ydelta + s->scan_lines_per_block - 1) /
1228 s->scan_lines_per_block;
1230 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
1233 if (bytestream2_get_bytes_left(&s->gb) < scan_line_blocks * 8)
1234 return AVERROR_INVALIDDATA;
1236 // save pointer we are going to use in decode_block
1237 s->buf = avpkt->data;
1238 s->buf_size = avpkt->size;
1239 ptr = picture->data[0];
1241 // Zero out the start if ymin is not 0
1242 for (y = 0; y < s->ymin; y++) {
1243 memset(ptr, 0, out_line_size);
1244 ptr += picture->linesize[0];
1247 s->picture = picture;
1248 avctx->execute2(avctx, decode_block, s->thread_data, NULL, scan_line_blocks);
1250 // Zero out the end if ymax+1 is not h
1251 for (y = s->ymax + 1; y < avctx->height; y++) {
1252 memset(ptr, 0, out_line_size);
1253 ptr += picture->linesize[0];
1256 picture->pict_type = AV_PICTURE_TYPE_I;
1262 static av_cold int decode_init(AVCodecContext *avctx)
1264 EXRContext *s = avctx->priv_data;
1273 s->channel_offsets[0] = -1;
1274 s->channel_offsets[1] = -1;
1275 s->channel_offsets[2] = -1;
1276 s->channel_offsets[3] = -1;
1277 s->pixel_type = EXR_UNKNOWN;
1278 s->compression = EXR_UNKN;
1283 // allocate thread data, used for non EXR_RAW compreesion types
1284 s->thread_data = av_mallocz_array(avctx->thread_count, sizeof(EXRThreadData));
1285 if (!s->thread_data)
1286 return AVERROR_INVALIDDATA;
1291 static int decode_init_thread_copy(AVCodecContext *avctx)
1292 { EXRContext *s = avctx->priv_data;
1294 // allocate thread data, used for non EXR_RAW compreesion types
1295 s->thread_data = av_mallocz_array(avctx->thread_count, sizeof(EXRThreadData));
1296 if (!s->thread_data)
1297 return AVERROR_INVALIDDATA;
1302 static av_cold int decode_end(AVCodecContext *avctx)
1304 EXRContext *s = avctx->priv_data;
1306 for (i = 0; i < avctx->thread_count; i++) {
1307 EXRThreadData *td = &s->thread_data[i];
1308 av_freep(&td->uncompressed_data);
1310 av_freep(&td->bitmap);
1314 av_freep(&s->thread_data);
1315 av_freep(&s->channels);
1320 #define OFFSET(x) offsetof(EXRContext, x)
1321 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1322 static const AVOption options[] = {
1323 { "layer", "Set the decoding layer", OFFSET(layer),
1324 AV_OPT_TYPE_STRING, { .str = "" }, 0, 0, VD },
1328 static const AVClass exr_class = {
1329 .class_name = "EXR",
1330 .item_name = av_default_item_name,
1332 .version = LIBAVUTIL_VERSION_INT,
1335 AVCodec ff_exr_decoder = {
1337 .long_name = NULL_IF_CONFIG_SMALL("OpenEXR image"),
1338 .type = AVMEDIA_TYPE_VIDEO,
1339 .id = AV_CODEC_ID_EXR,
1340 .priv_data_size = sizeof(EXRContext),
1341 .init = decode_init,
1342 .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
1343 .close = decode_end,
1344 .decode = decode_frame,
1345 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS |
1346 CODEC_CAP_SLICE_THREADS,
1347 .priv_class = &exr_class,