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
36 #include "bytestream.h"
39 #include "libavutil/imgutils.h"
40 #include "libavutil/avassert.h"
59 typedef struct EXRChannel {
61 enum ExrPixelType pixel_type;
64 typedef struct EXRThreadData {
65 uint8_t *uncompressed_data;
66 int uncompressed_size;
72 typedef struct EXRContext {
75 enum ExrPixelType pixel_type;
76 int channel_offsets[4]; // 0 = red, 1 = green, 2 = blue and 3 = alpha
77 const AVPixFmtDescriptor *desc;
81 uint32_t xdelta, ydelta;
85 uint64_t scan_line_size;
86 int scan_lines_per_block;
88 const uint8_t *buf, *table;
94 EXRThreadData *thread_data;
99 * Converts from 32-bit float as uint32_t to uint16_t
101 * @param v 32-bit float
102 * @return normalized 16-bit unsigned int
104 static inline uint16_t exr_flt2uint(uint32_t v)
106 unsigned int exp = v >> 23;
107 // "HACK": negative values result in exp< 0, so clipping them to 0
108 // is also handled by this condition, avoids explicit check for sign bit.
109 if (exp<= 127 + 7 - 24) // we would shift out all bits anyway
114 return (v + (1 << 23)) >> (127 + 7 - exp);
118 * Converts from 16-bit float as uint16_t to uint16_t
120 * @param v 16-bit float
121 * @return normalized 16-bit unsigned int
123 static inline uint16_t exr_halflt2uint(uint16_t v)
125 unsigned exp = 14 - (v >> 10);
127 if (exp == 14) return (v >> 9) & 1;
128 else return (v & 0x8000) ? 0 : 0xffff;
131 return (v + (1 << 16)) >> (exp + 1);
135 * Gets the size of the header variable
137 * @param **buf the current pointer location in the header where
138 * the variable data starts
139 * @param *buf_end pointer location of the end of the buffer
140 * @return size of variable data
142 static unsigned int get_header_variable_length(const uint8_t **buf,
143 const uint8_t *buf_end)
145 unsigned int variable_buffer_data_size = bytestream_get_le32(buf);
146 if (variable_buffer_data_size >= buf_end - *buf)
148 return variable_buffer_data_size;
152 * Checks if the variable name corresponds with it's data type
154 * @param *avctx the AVCodecContext
155 * @param **buf the current pointer location in the header where
156 * the variable name starts
157 * @param *buf_end pointer location of the end of the buffer
158 * @param *value_name name of the varible to check
159 * @param *value_type type of the varible to check
160 * @param minimum_length minimum length of the variable data
161 * @param variable_buffer_data_size variable length read from the header
163 * @return negative if variable is invalid
165 static int check_header_variable(AVCodecContext *avctx,
167 const uint8_t *buf_end,
168 const char *value_name,
169 const char *value_type,
170 unsigned int minimum_length,
171 unsigned int *variable_buffer_data_size)
173 if (buf_end - *buf >= minimum_length && !strcmp(*buf, value_name)) {
174 *buf += strlen(value_name)+1;
175 if (!strcmp(*buf, value_type)) {
176 *buf += strlen(value_type)+1;
177 *variable_buffer_data_size = get_header_variable_length(buf, buf_end);
178 if (!*variable_buffer_data_size)
179 av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
182 *buf -= strlen(value_name)+1;
183 av_log(avctx, AV_LOG_WARNING, "Unknown data type for header variable %s\n", value_name);
188 static void predictor(uint8_t *src, int size)
190 uint8_t *t = src + 1;
191 uint8_t *stop = src + size;
194 int d = (int)t[-1] + (int)t[0] - 128;
200 static void reorder_pixels(uint8_t *src, uint8_t *dst, int size)
202 const int8_t *t1 = src;
203 const int8_t *t2 = src + (size + 1) / 2;
205 int8_t *stop = s + size;
220 static int zip_uncompress(const uint8_t *src, int compressed_size,
221 int uncompressed_size, EXRThreadData *td)
223 unsigned long dest_len = uncompressed_size;
225 if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK ||
226 dest_len != uncompressed_size)
227 return AVERROR(EINVAL);
229 predictor(td->tmp, uncompressed_size);
230 reorder_pixels(td->tmp, td->uncompressed_data, uncompressed_size);
235 static int rle_uncompress(const uint8_t *src, int compressed_size,
236 int uncompressed_size, EXRThreadData *td)
238 int8_t *d = (int8_t *)td->tmp;
239 const int8_t *s = (const int8_t *)src;
240 int ssize = compressed_size;
241 int dsize = uncompressed_size;
242 int8_t *dend = d + dsize;
251 if ((dsize -= count ) < 0 ||
252 (ssize -= count + 1) < 0)
260 if ((dsize -= count) < 0 ||
272 return AVERROR_INVALIDDATA;
274 predictor(td->tmp, uncompressed_size);
275 reorder_pixels(td->tmp, td->uncompressed_data, uncompressed_size);
280 static int pxr24_uncompress(EXRContext *s, const uint8_t *src,
281 int compressed_size, int uncompressed_size,
284 unsigned long dest_len = uncompressed_size;
285 const uint8_t *in = td->tmp;
289 if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK ||
290 dest_len != uncompressed_size)
291 return AVERROR(EINVAL);
293 out = td->uncompressed_data;
294 for (i = 0; i < s->ysize; i++) {
295 for (c = 0; c < s->nb_channels; c++) {
296 EXRChannel *channel = &s->channels[c];
297 const uint8_t *ptr[4];
300 switch (channel->pixel_type) {
303 ptr[1] = ptr[0] + s->xdelta;
304 ptr[2] = ptr[1] + s->xdelta;
305 in = ptr[2] + s->xdelta;
307 for (j = 0; j < s->xdelta; ++j) {
308 uint32_t diff = (*(ptr[0]++) << 24) |
309 (*(ptr[1]++) << 16) |
312 bytestream_put_le32(&out, pixel);
317 ptr[1] = ptr[0] + s->xdelta;
318 in = ptr[1] + s->xdelta;
319 for (j = 0; j < s->xdelta; j++) {
320 uint32_t diff = (*(ptr[0]++) << 8) | *(ptr[1]++);
323 bytestream_put_le16(&out, pixel);
335 static int decode_block(AVCodecContext *avctx, void *tdata,
336 int jobnr, int threadnr)
338 EXRContext *s = avctx->priv_data;
339 AVFrame *const p = s->picture;
340 EXRThreadData *td = &s->thread_data[threadnr];
341 const uint8_t *channel_buffer[4] = { 0 };
342 const uint8_t *buf = s->buf;
343 uint64_t line_offset, uncompressed_size;
344 uint32_t xdelta = s->xdelta;
347 int32_t data_size, line;
349 int axmax = (avctx->width - (s->xmax + 1)) * 2 * s->desc->nb_components;
350 int bxmin = s->xmin * 2 * s->desc->nb_components;
351 int i, x, buf_size = s->buf_size;
354 line_offset = AV_RL64(s->table + jobnr * 8);
355 // Check if the buffer has the required bytes needed from the offset
356 if (line_offset > buf_size - 8)
357 return AVERROR_INVALIDDATA;
359 src = buf + line_offset + 8;
360 line = AV_RL32(src - 8);
361 if (line < s->ymin || line > s->ymax)
362 return AVERROR_INVALIDDATA;
364 data_size = AV_RL32(src - 4);
365 if (data_size <= 0 || data_size > buf_size)
366 return AVERROR_INVALIDDATA;
368 s->ysize = FFMIN(s->scan_lines_per_block, s->ymax - line + 1);
369 uncompressed_size = s->scan_line_size * s->ysize;
370 if ((s->compr == EXR_RAW && (data_size != uncompressed_size ||
371 line_offset > buf_size - uncompressed_size)) ||
372 (s->compr != EXR_RAW && (data_size > uncompressed_size ||
373 line_offset > buf_size - data_size))) {
374 return AVERROR_INVALIDDATA;
377 if (data_size < uncompressed_size) {
378 av_fast_padded_malloc(&td->uncompressed_data, &td->uncompressed_size, uncompressed_size);
379 av_fast_padded_malloc(&td->tmp, &td->tmp_size, uncompressed_size);
380 if (!td->uncompressed_data || !td->tmp)
381 return AVERROR(ENOMEM);
386 ret = zip_uncompress(src, data_size, uncompressed_size, td);
389 ret = pxr24_uncompress(s, src, data_size, uncompressed_size, td);
392 ret = rle_uncompress(src, data_size, uncompressed_size, td);
395 src = td->uncompressed_data;
398 channel_buffer[0] = src + xdelta * s->channel_offsets[0];
399 channel_buffer[1] = src + xdelta * s->channel_offsets[1];
400 channel_buffer[2] = src + xdelta * s->channel_offsets[2];
401 if (s->channel_offsets[3] >= 0)
402 channel_buffer[3] = src + xdelta * s->channel_offsets[3];
404 ptr = p->data[0] + line * p->linesize[0];
405 for (i = 0; i < s->scan_lines_per_block && line + i <= s->ymax; i++, ptr += p->linesize[0]) {
406 const uint8_t *r, *g, *b, *a;
408 r = channel_buffer[0];
409 g = channel_buffer[1];
410 b = channel_buffer[2];
411 if (channel_buffer[3])
412 a = channel_buffer[3];
414 ptr_x = (uint16_t *)ptr;
416 // Zero out the start if xmin is not 0
417 memset(ptr_x, 0, bxmin);
418 ptr_x += s->xmin * s->desc->nb_components;
419 if (s->pixel_type == EXR_FLOAT) {
421 for (x = 0; x < xdelta; x++) {
422 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&r));
423 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&g));
424 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&b));
425 if (channel_buffer[3])
426 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a));
430 for (x = 0; x < xdelta; x++) {
431 *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&r));
432 *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&g));
433 *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&b));
434 if (channel_buffer[3])
435 *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&a));
439 // Zero out the end if xmax+1 is not w
440 memset(ptr_x, 0, axmax);
442 channel_buffer[0] += s->scan_line_size;
443 channel_buffer[1] += s->scan_line_size;
444 channel_buffer[2] += s->scan_line_size;
445 if (channel_buffer[3])
446 channel_buffer[3] += s->scan_line_size;
452 static int decode_frame(AVCodecContext *avctx,
457 const uint8_t *buf = avpkt->data;
458 unsigned int buf_size = avpkt->size;
459 const uint8_t *buf_end = buf + buf_size;
461 EXRContext *const s = avctx->priv_data;
462 ThreadFrame frame = { .f = data };
463 AVFrame *picture = data;
466 int i, y, magic_number, version, flags, ret;
471 int scan_line_blocks;
473 unsigned int current_channel_offset = 0;
481 s->channel_offsets[0] = -1;
482 s->channel_offsets[1] = -1;
483 s->channel_offsets[2] = -1;
484 s->channel_offsets[3] = -1;
489 s->buf_size = buf_size;
492 av_log(avctx, AV_LOG_ERROR, "Too short header to parse\n");
493 return AVERROR_INVALIDDATA;
496 magic_number = bytestream_get_le32(&buf);
497 if (magic_number != 20000630) { // As per documentation of OpenEXR it's supposed to be int 20000630 little-endian
498 av_log(avctx, AV_LOG_ERROR, "Wrong magic number %d\n", magic_number);
499 return AVERROR_INVALIDDATA;
502 version = bytestream_get_byte(&buf);
504 av_log(avctx, AV_LOG_ERROR, "Unsupported version %d\n", version);
505 return AVERROR_PATCHWELCOME;
508 flags = bytestream_get_le24(&buf);
510 av_log(avctx, AV_LOG_ERROR, "Tile based images are not supported\n");
511 return AVERROR_PATCHWELCOME;
515 while (buf < buf_end && buf[0]) {
516 unsigned int variable_buffer_data_size;
517 // Process the channel list
518 if (check_header_variable(avctx, &buf, buf_end, "channels", "chlist", 38, &variable_buffer_data_size) >= 0) {
519 const uint8_t *channel_list_end;
520 if (!variable_buffer_data_size)
521 return AVERROR_INVALIDDATA;
523 channel_list_end = buf + variable_buffer_data_size;
524 while (channel_list_end - buf >= 19) {
526 int current_pixel_type = -1;
527 int channel_index = -1;
530 if (!strcmp(buf, "R"))
532 else if (!strcmp(buf, "G"))
534 else if (!strcmp(buf, "B"))
536 else if (!strcmp(buf, "A"))
539 av_log(avctx, AV_LOG_WARNING, "Unsupported channel %.256s\n", buf);
541 while (bytestream_get_byte(&buf) && buf < channel_list_end)
544 if (channel_list_end - * &buf < 4) {
545 av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
546 return AVERROR_INVALIDDATA;
549 current_pixel_type = bytestream_get_le32(&buf);
550 if (current_pixel_type > 2) {
551 av_log(avctx, AV_LOG_ERROR, "Unknown pixel type\n");
552 return AVERROR_INVALIDDATA;
556 xsub = bytestream_get_le32(&buf);
557 ysub = bytestream_get_le32(&buf);
558 if (xsub != 1 || ysub != 1) {
559 av_log(avctx, AV_LOG_ERROR, "Unsupported subsampling %dx%d\n", xsub, ysub);
560 return AVERROR_PATCHWELCOME;
563 if (channel_index >= 0) {
564 if (s->pixel_type != -1 && s->pixel_type != current_pixel_type) {
565 av_log(avctx, AV_LOG_ERROR, "RGB channels not of the same depth\n");
566 return AVERROR_INVALIDDATA;
568 s->pixel_type = current_pixel_type;
569 s->channel_offsets[channel_index] = current_channel_offset;
572 s->channels = av_realloc_f(s->channels, ++s->nb_channels, sizeof(EXRChannel));
574 return AVERROR(ENOMEM);
575 channel = &s->channels[s->nb_channels - 1];
576 channel->pixel_type = current_pixel_type;
577 channel->xsub = xsub;
578 channel->ysub = ysub;
580 current_channel_offset += 1 << current_pixel_type;
583 /* Check if all channels are set with an offset or if the channels
584 * are causing an overflow */
586 if (FFMIN3(s->channel_offsets[0],
587 s->channel_offsets[1],
588 s->channel_offsets[2]) < 0) {
589 if (s->channel_offsets[0] < 0)
590 av_log(avctx, AV_LOG_ERROR, "Missing red channel\n");
591 if (s->channel_offsets[1] < 0)
592 av_log(avctx, AV_LOG_ERROR, "Missing green channel\n");
593 if (s->channel_offsets[2] < 0)
594 av_log(avctx, AV_LOG_ERROR, "Missing blue channel\n");
595 return AVERROR_INVALIDDATA;
598 buf = channel_list_end;
600 } else if (check_header_variable(avctx, &buf, buf_end, "dataWindow", "box2i", 31, &variable_buffer_data_size) >= 0) {
601 if (!variable_buffer_data_size)
602 return AVERROR_INVALIDDATA;
604 s->xmin = AV_RL32(buf);
605 s->ymin = AV_RL32(buf + 4);
606 s->xmax = AV_RL32(buf + 8);
607 s->ymax = AV_RL32(buf + 12);
608 s->xdelta = (s->xmax - s->xmin) + 1;
609 s->ydelta = (s->ymax - s->ymin) + 1;
611 buf += variable_buffer_data_size;
613 } else if (check_header_variable(avctx, &buf, buf_end, "displayWindow", "box2i", 34, &variable_buffer_data_size) >= 0) {
614 if (!variable_buffer_data_size)
615 return AVERROR_INVALIDDATA;
617 w = AV_RL32(buf + 8) + 1;
618 h = AV_RL32(buf + 12) + 1;
620 buf += variable_buffer_data_size;
622 } else if (check_header_variable(avctx, &buf, buf_end, "lineOrder", "lineOrder", 25, &variable_buffer_data_size) >= 0) {
623 if (!variable_buffer_data_size)
624 return AVERROR_INVALIDDATA;
626 av_log(avctx, AV_LOG_DEBUG, "line order : %d\n", *buf);
628 av_log(avctx, AV_LOG_ERROR, "Unknown line order\n");
629 return AVERROR_INVALIDDATA;
632 buf += variable_buffer_data_size;
634 } else if (check_header_variable(avctx, &buf, buf_end, "pixelAspectRatio", "float", 31, &variable_buffer_data_size) >= 0) {
635 if (!variable_buffer_data_size)
636 return AVERROR_INVALIDDATA;
638 avctx->sample_aspect_ratio = av_d2q(av_int2float(AV_RL32(buf)), 255);
640 buf += variable_buffer_data_size;
642 } else if (check_header_variable(avctx, &buf, buf_end, "compression", "compression", 29, &variable_buffer_data_size) >= 0) {
643 if (!variable_buffer_data_size)
644 return AVERROR_INVALIDDATA;
649 av_log(avctx, AV_LOG_WARNING, "Found more than one compression attribute\n");
651 buf += variable_buffer_data_size;
655 // Check if there is enough bytes for a header
656 if (buf_end - buf <= 9) {
657 av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
658 return AVERROR_INVALIDDATA;
661 // Process unknown variables
662 for (i = 0; i < 2; i++) {
663 // Skip variable name/type
664 while (++buf < buf_end)
669 // Skip variable length
670 if (buf_end - buf >= 5) {
671 variable_buffer_data_size = get_header_variable_length(&buf, buf_end);
672 if (!variable_buffer_data_size) {
673 av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
674 return AVERROR_INVALIDDATA;
676 buf += variable_buffer_data_size;
680 if (s->compr == -1) {
681 av_log(avctx, AV_LOG_ERROR, "Missing compression attribute\n");
682 return AVERROR_INVALIDDATA;
685 if (buf >= buf_end) {
686 av_log(avctx, AV_LOG_ERROR, "Incomplete frame\n");
687 return AVERROR_INVALIDDATA;
691 switch (s->pixel_type) {
694 if (s->channel_offsets[3] >= 0)
695 avctx->pix_fmt = AV_PIX_FMT_RGBA64;
697 avctx->pix_fmt = AV_PIX_FMT_RGB48;
700 avpriv_request_sample(avctx, "32-bit unsigned int");
701 return AVERROR_PATCHWELCOME;
703 av_log(avctx, AV_LOG_ERROR, "Missing channel list\n");
704 return AVERROR_INVALIDDATA;
711 s->scan_lines_per_block = 1;
715 s->scan_lines_per_block = 16;
718 av_log(avctx, AV_LOG_ERROR, "Compression type %d is not supported\n", s->compr);
719 return AVERROR_PATCHWELCOME;
722 if (av_image_check_size(w, h, 0, avctx))
723 return AVERROR_INVALIDDATA;
725 // Verify the xmin, xmax, ymin, ymax and xdelta before setting the actual image size
726 if (s->xmin > s->xmax ||
728 s->xdelta != s->xmax - s->xmin + 1 ||
729 s->xmax >= w || s->ymax >= h) {
730 av_log(avctx, AV_LOG_ERROR, "Wrong sizing or missing size information\n");
731 return AVERROR_INVALIDDATA;
734 if (w != avctx->width || h != avctx->height) {
735 avcodec_set_dimensions(avctx, w, h);
738 s->desc = av_pix_fmt_desc_get(avctx->pix_fmt);
739 out_line_size = avctx->width * 2 * s->desc->nb_components;
740 s->scan_line_size = s->xdelta * current_channel_offset;
741 scan_line_blocks = (s->ydelta + s->scan_lines_per_block - 1) / s->scan_lines_per_block;
743 if (s->compr != EXR_RAW) {
744 size_t thread_data_size, prev_size;
747 prev_size = s->thread_data_size;
748 if (av_size_mult(avctx->thread_count, sizeof(EXRThreadData), &thread_data_size))
749 return AVERROR(EINVAL);
751 m = av_fast_realloc(s->thread_data, &s->thread_data_size, thread_data_size);
753 return AVERROR(ENOMEM);
755 memset(s->thread_data + prev_size, 0, s->thread_data_size - prev_size);
758 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
761 if (buf_end - buf < scan_line_blocks * 8)
762 return AVERROR_INVALIDDATA;
764 ptr = picture->data[0];
766 // Zero out the start if ymin is not 0
767 for (y = 0; y < s->ymin; y++) {
768 memset(ptr, 0, out_line_size);
769 ptr += picture->linesize[0];
772 s->picture = picture;
773 avctx->execute2(avctx, decode_block, s->thread_data, NULL, scan_line_blocks);
775 // Zero out the end if ymax+1 is not h
776 for (y = s->ymax + 1; y < avctx->height; y++) {
777 memset(ptr, 0, out_line_size);
778 ptr += picture->linesize[0];
781 picture->pict_type = AV_PICTURE_TYPE_I;
787 static av_cold int decode_end(AVCodecContext *avctx)
789 EXRContext *s = avctx->priv_data;
792 for (i = 0; i < s->thread_data_size / sizeof(EXRThreadData); i++) {
793 EXRThreadData *td = &s->thread_data[i];
794 av_free(td->uncompressed_data);
798 av_freep(&s->thread_data);
799 s->thread_data_size = 0;
800 av_freep(&s->channels);
805 AVCodec ff_exr_decoder = {
807 .type = AVMEDIA_TYPE_VIDEO,
808 .id = AV_CODEC_ID_EXR,
809 .priv_data_size = sizeof(EXRContext),
811 .decode = decode_frame,
812 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS | CODEC_CAP_SLICE_THREADS,
813 .long_name = NULL_IF_CONFIG_SMALL("OpenEXR image"),