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"
51 typedef struct EXRThreadData {
52 uint8_t *uncompressed_data;
53 int uncompressed_size;
59 typedef struct EXRContext {
62 int bits_per_color_id;
63 int channel_offsets[4]; // 0 = red, 1 = green, 2 = blue and 3 = alpha
64 const AVPixFmtDescriptor *desc;
68 uint32_t xdelta, ydelta;
70 uint64_t scan_line_size;
71 int scan_lines_per_block;
73 const uint8_t *buf, *table;
76 EXRThreadData *thread_data;
81 * Converts from 32-bit float as uint32_t to uint16_t
83 * @param v 32-bit float
84 * @return normalized 16-bit unsigned int
86 static inline uint16_t exr_flt2uint(uint32_t v)
88 unsigned int exp = v >> 23;
89 // "HACK": negative values result in exp< 0, so clipping them to 0
90 // is also handled by this condition, avoids explicit check for sign bit.
91 if (exp<= 127 + 7 - 24) // we would shift out all bits anyway
96 return (v + (1 << 23)) >> (127 + 7 - exp);
100 * Converts from 16-bit float as uint16_t to uint16_t
102 * @param v 16-bit float
103 * @return normalized 16-bit unsigned int
105 static inline uint16_t exr_halflt2uint(uint16_t v)
107 unsigned exp = 14 - (v >> 10);
109 if (exp == 14) return (v >> 9) & 1;
110 else return (v & 0x8000) ? 0 : 0xffff;
113 return (v + (1 << 16)) >> (exp + 1);
117 * Gets the size of the header variable
119 * @param **buf the current pointer location in the header where
120 * the variable data starts
121 * @param *buf_end pointer location of the end of the buffer
122 * @return size of variable data
124 static unsigned int get_header_variable_length(const uint8_t **buf,
125 const uint8_t *buf_end)
127 unsigned int variable_buffer_data_size = bytestream_get_le32(buf);
128 if (variable_buffer_data_size >= buf_end - *buf)
130 return variable_buffer_data_size;
134 * Checks if the variable name corresponds with it's data type
136 * @param *avctx the AVCodecContext
137 * @param **buf the current pointer location in the header where
138 * the variable name starts
139 * @param *buf_end pointer location of the end of the buffer
140 * @param *value_name name of the varible to check
141 * @param *value_type type of the varible to check
142 * @param minimum_length minimum length of the variable data
143 * @param variable_buffer_data_size variable length read from the header
145 * @return negative if variable is invalid
147 static int check_header_variable(AVCodecContext *avctx,
149 const uint8_t *buf_end,
150 const char *value_name,
151 const char *value_type,
152 unsigned int minimum_length,
153 unsigned int *variable_buffer_data_size)
155 if (buf_end - *buf >= minimum_length && !strcmp(*buf, value_name)) {
156 *buf += strlen(value_name)+1;
157 if (!strcmp(*buf, value_type)) {
158 *buf += strlen(value_type)+1;
159 *variable_buffer_data_size = get_header_variable_length(buf, buf_end);
160 if (!*variable_buffer_data_size)
161 av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
164 *buf -= strlen(value_name)+1;
165 av_log(avctx, AV_LOG_WARNING, "Unknown data type for header variable %s\n", value_name);
170 static void predictor(uint8_t *src, int size)
172 uint8_t *t = src + 1;
173 uint8_t *stop = src + size;
176 int d = (int)t[-1] + (int)t[0] - 128;
182 static void reorder_pixels(uint8_t *src, uint8_t *dst, int size)
184 const int8_t *t1 = src;
185 const int8_t *t2 = src + (size + 1) / 2;
187 int8_t *stop = s + size;
202 static int zip_uncompress(const uint8_t *src, int compressed_size,
203 int uncompressed_size, EXRThreadData *td)
205 unsigned long dest_len = uncompressed_size;
207 if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK ||
208 dest_len != uncompressed_size)
209 return AVERROR(EINVAL);
211 predictor(td->tmp, uncompressed_size);
212 reorder_pixels(td->tmp, td->uncompressed_data, uncompressed_size);
217 static int rle_uncompress(const uint8_t *src, int compressed_size,
218 int uncompressed_size, EXRThreadData *td)
220 int8_t *d = (int8_t *)td->tmp;
221 const int8_t *s = (const int8_t *)src;
222 int ssize = compressed_size;
223 int dsize = uncompressed_size;
224 int8_t *dend = d + dsize;
233 if ((dsize -= count ) < 0 ||
234 (ssize -= count + 1) < 0)
242 if ((dsize -= count) < 0 ||
254 return AVERROR_INVALIDDATA;
256 predictor(td->tmp, uncompressed_size);
257 reorder_pixels(td->tmp, td->uncompressed_data, uncompressed_size);
262 static int decode_block(AVCodecContext *avctx, void *tdata,
263 int jobnr, int threadnr)
265 EXRContext *s = avctx->priv_data;
266 AVFrame *const p = &s->picture;
267 EXRThreadData *td = &s->thread_data[threadnr];
268 const uint8_t *channel_buffer[4] = { 0 };
269 const uint8_t *buf = s->buf;
270 uint64_t line_offset, uncompressed_size;
271 uint32_t xdelta = s->xdelta;
274 int32_t data_size, line;
276 int axmax = (avctx->width - (s->xmax + 1)) * 2 * s->desc->nb_components;
277 int bxmin = s->xmin * 2 * s->desc->nb_components;
278 int ret, i, x, buf_size = s->buf_size;
280 line_offset = AV_RL64(s->table + jobnr * 8);
281 // Check if the buffer has the required bytes needed from the offset
282 if (line_offset > buf_size - 8)
283 return AVERROR_INVALIDDATA;
285 src = buf + line_offset + 8;
286 line = AV_RL32(src - 8);
287 if (line < s->ymin || line > s->ymax)
288 return AVERROR_INVALIDDATA;
290 data_size = AV_RL32(src - 4);
291 if (data_size <= 0 || data_size > buf_size)
292 return AVERROR_INVALIDDATA;
294 uncompressed_size = s->scan_line_size * FFMIN(s->scan_lines_per_block, s->ymax - line + 1);
295 if ((s->compr == EXR_RAW && (data_size != uncompressed_size ||
296 line_offset > buf_size - uncompressed_size)) ||
297 (s->compr != EXR_RAW && (data_size > uncompressed_size ||
298 line_offset > buf_size - data_size))) {
299 return AVERROR_INVALIDDATA;
302 if (data_size < uncompressed_size) {
303 av_fast_padded_malloc(&td->uncompressed_data, &td->uncompressed_size, uncompressed_size);
304 av_fast_padded_malloc(&td->tmp, &td->tmp_size, uncompressed_size);
305 if (!td->uncompressed_data || !td->tmp)
306 return AVERROR(ENOMEM);
311 ret = zip_uncompress(src, data_size, uncompressed_size, td);
314 ret = rle_uncompress(src, data_size, uncompressed_size, td);
317 src = td->uncompressed_data;
320 channel_buffer[0] = src + xdelta * s->channel_offsets[0];
321 channel_buffer[1] = src + xdelta * s->channel_offsets[1];
322 channel_buffer[2] = src + xdelta * s->channel_offsets[2];
323 if (s->channel_offsets[3] >= 0)
324 channel_buffer[3] = src + xdelta * s->channel_offsets[3];
326 ptr = p->data[0] + line * p->linesize[0];
327 for (i = 0; i < s->scan_lines_per_block && line + i <= s->ymax; i++, ptr += p->linesize[0]) {
328 const uint8_t *r, *g, *b, *a;
330 r = channel_buffer[0];
331 g = channel_buffer[1];
332 b = channel_buffer[2];
333 if (channel_buffer[3])
334 a = channel_buffer[3];
336 ptr_x = (uint16_t *)ptr;
338 // Zero out the start if xmin is not 0
339 memset(ptr_x, 0, bxmin);
340 ptr_x += s->xmin * s->desc->nb_components;
341 if (s->bits_per_color_id == 2) {
343 for (x = 0; x < xdelta; x++) {
344 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&r));
345 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&g));
346 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&b));
347 if (channel_buffer[3])
348 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a));
352 for (x = 0; x < xdelta; x++) {
353 *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&r));
354 *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&g));
355 *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&b));
356 if (channel_buffer[3])
357 *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&a));
361 // Zero out the end if xmax+1 is not w
362 memset(ptr_x, 0, axmax);
364 channel_buffer[0] += s->scan_line_size;
365 channel_buffer[1] += s->scan_line_size;
366 channel_buffer[2] += s->scan_line_size;
367 if (channel_buffer[3])
368 channel_buffer[3] += s->scan_line_size;
374 static int decode_frame(AVCodecContext *avctx,
379 const uint8_t *buf = avpkt->data;
380 unsigned int buf_size = avpkt->size;
381 const uint8_t *buf_end = buf + buf_size;
383 EXRContext *const s = avctx->priv_data;
384 AVFrame *picture = data;
385 AVFrame *const p = &s->picture;
388 int i, y, magic_number, version, flags, ret;
393 int scan_line_blocks;
395 unsigned int current_channel_offset = 0;
403 s->channel_offsets[0] = -1;
404 s->channel_offsets[1] = -1;
405 s->channel_offsets[2] = -1;
406 s->channel_offsets[3] = -1;
407 s->bits_per_color_id = -1;
410 s->buf_size = buf_size;
413 av_log(avctx, AV_LOG_ERROR, "Too short header to parse\n");
414 return AVERROR_INVALIDDATA;
417 magic_number = bytestream_get_le32(&buf);
418 if (magic_number != 20000630) { // As per documentation of OpenEXR it's supposed to be int 20000630 little-endian
419 av_log(avctx, AV_LOG_ERROR, "Wrong magic number %d\n", magic_number);
420 return AVERROR_INVALIDDATA;
423 version = bytestream_get_byte(&buf);
425 av_log(avctx, AV_LOG_ERROR, "Unsupported version %d\n", version);
426 return AVERROR_PATCHWELCOME;
429 flags = bytestream_get_le24(&buf);
431 av_log(avctx, AV_LOG_ERROR, "Tile based images are not supported\n");
432 return AVERROR_PATCHWELCOME;
436 while (buf < buf_end && buf[0]) {
437 unsigned int variable_buffer_data_size;
438 // Process the channel list
439 if (check_header_variable(avctx, &buf, buf_end, "channels", "chlist", 38, &variable_buffer_data_size) >= 0) {
440 const uint8_t *channel_list_end;
441 if (!variable_buffer_data_size)
442 return AVERROR_INVALIDDATA;
444 channel_list_end = buf + variable_buffer_data_size;
445 while (channel_list_end - buf >= 19) {
446 int current_bits_per_color_id = -1;
447 int channel_index = -1;
450 if (!strcmp(buf, "R"))
452 else if (!strcmp(buf, "G"))
454 else if (!strcmp(buf, "B"))
456 else if (!strcmp(buf, "A"))
459 av_log(avctx, AV_LOG_WARNING, "Unsupported channel %.256s\n", buf);
461 while (bytestream_get_byte(&buf) && buf < channel_list_end)
464 if (channel_list_end - * &buf < 4) {
465 av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
466 return AVERROR_INVALIDDATA;
469 current_bits_per_color_id = bytestream_get_le32(&buf);
470 if (current_bits_per_color_id > 2) {
471 av_log(avctx, AV_LOG_ERROR, "Unknown color format\n");
472 return AVERROR_INVALIDDATA;
476 xsub = bytestream_get_le32(&buf);
477 ysub = bytestream_get_le32(&buf);
478 if (xsub != 1 || ysub != 1) {
479 av_log(avctx, AV_LOG_ERROR, "Unsupported subsampling %dx%d\n", xsub, ysub);
480 return AVERROR_PATCHWELCOME;
483 if (channel_index >= 0) {
484 if (s->bits_per_color_id != -1 && s->bits_per_color_id != current_bits_per_color_id) {
485 av_log(avctx, AV_LOG_ERROR, "RGB channels not of the same depth\n");
486 return AVERROR_INVALIDDATA;
488 s->bits_per_color_id = current_bits_per_color_id;
489 s->channel_offsets[channel_index] = current_channel_offset;
492 current_channel_offset += 1 << current_bits_per_color_id;
495 /* Check if all channels are set with an offset or if the channels
496 * are causing an overflow */
498 if (FFMIN3(s->channel_offsets[0],
499 s->channel_offsets[1],
500 s->channel_offsets[2]) < 0) {
501 if (s->channel_offsets[0] < 0)
502 av_log(avctx, AV_LOG_ERROR, "Missing red channel\n");
503 if (s->channel_offsets[1] < 0)
504 av_log(avctx, AV_LOG_ERROR, "Missing green channel\n");
505 if (s->channel_offsets[2] < 0)
506 av_log(avctx, AV_LOG_ERROR, "Missing blue channel\n");
507 return AVERROR_INVALIDDATA;
510 buf = channel_list_end;
512 } else if (check_header_variable(avctx, &buf, buf_end, "dataWindow", "box2i", 31, &variable_buffer_data_size) >= 0) {
513 if (!variable_buffer_data_size)
514 return AVERROR_INVALIDDATA;
516 s->xmin = AV_RL32(buf);
517 s->ymin = AV_RL32(buf + 4);
518 s->xmax = AV_RL32(buf + 8);
519 s->ymax = AV_RL32(buf + 12);
520 s->xdelta = (s->xmax - s->xmin) + 1;
521 s->ydelta = (s->ymax - s->ymin) + 1;
523 buf += variable_buffer_data_size;
525 } else if (check_header_variable(avctx, &buf, buf_end, "displayWindow", "box2i", 34, &variable_buffer_data_size) >= 0) {
526 if (!variable_buffer_data_size)
527 return AVERROR_INVALIDDATA;
529 w = AV_RL32(buf + 8) + 1;
530 h = AV_RL32(buf + 12) + 1;
532 buf += variable_buffer_data_size;
534 } else if (check_header_variable(avctx, &buf, buf_end, "lineOrder", "lineOrder", 25, &variable_buffer_data_size) >= 0) {
535 if (!variable_buffer_data_size)
536 return AVERROR_INVALIDDATA;
538 av_log(avctx, AV_LOG_DEBUG, "line order : %d\n", *buf);
540 av_log(avctx, AV_LOG_ERROR, "Unknown line order\n");
541 return AVERROR_INVALIDDATA;
544 buf += variable_buffer_data_size;
546 } else if (check_header_variable(avctx, &buf, buf_end, "pixelAspectRatio", "float", 31, &variable_buffer_data_size) >= 0) {
547 if (!variable_buffer_data_size)
548 return AVERROR_INVALIDDATA;
550 avctx->sample_aspect_ratio = av_d2q(av_int2float(AV_RL32(buf)), 255);
552 buf += variable_buffer_data_size;
554 } else if (check_header_variable(avctx, &buf, buf_end, "compression", "compression", 29, &variable_buffer_data_size) >= 0) {
555 if (!variable_buffer_data_size)
556 return AVERROR_INVALIDDATA;
561 av_log(avctx, AV_LOG_WARNING, "Found more than one compression attribute\n");
563 buf += variable_buffer_data_size;
567 // Check if there is enough bytes for a header
568 if (buf_end - buf <= 9) {
569 av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
570 return AVERROR_INVALIDDATA;
573 // Process unknown variables
574 for (i = 0; i < 2; i++) {
575 // Skip variable name/type
576 while (++buf < buf_end)
581 // Skip variable length
582 if (buf_end - buf >= 5) {
583 variable_buffer_data_size = get_header_variable_length(&buf, buf_end);
584 if (!variable_buffer_data_size) {
585 av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
586 return AVERROR_INVALIDDATA;
588 buf += variable_buffer_data_size;
592 if (s->compr == -1) {
593 av_log(avctx, AV_LOG_ERROR, "Missing compression attribute\n");
594 return AVERROR_INVALIDDATA;
597 if (buf >= buf_end) {
598 av_log(avctx, AV_LOG_ERROR, "Incomplete frame\n");
599 return AVERROR_INVALIDDATA;
603 switch (s->bits_per_color_id) {
606 if (s->channel_offsets[3] >= 0)
607 avctx->pix_fmt = AV_PIX_FMT_RGBA64;
609 avctx->pix_fmt = AV_PIX_FMT_RGB48;
613 av_log_missing_feature(avctx, "8-bit OpenEXR", 1);
614 return AVERROR_PATCHWELCOME;
616 av_log(avctx, AV_LOG_ERROR, "Unknown color format : %d\n", s->bits_per_color_id);
617 return AVERROR_INVALIDDATA;
624 s->scan_lines_per_block = 1;
627 s->scan_lines_per_block = 16;
630 av_log(avctx, AV_LOG_ERROR, "Compression type %d is not supported\n", s->compr);
631 return AVERROR_PATCHWELCOME;
634 if (s->picture.data[0])
635 ff_thread_release_buffer(avctx, &s->picture);
636 if (av_image_check_size(w, h, 0, avctx))
637 return AVERROR_INVALIDDATA;
639 // Verify the xmin, xmax, ymin, ymax and xdelta before setting the actual image size
640 if (s->xmin > s->xmax ||
642 s->xdelta != s->xmax - s->xmin + 1 ||
643 s->xmax >= w || s->ymax >= h) {
644 av_log(avctx, AV_LOG_ERROR, "Wrong sizing or missing size information\n");
645 return AVERROR_INVALIDDATA;
648 if (w != avctx->width || h != avctx->height) {
649 avcodec_set_dimensions(avctx, w, h);
652 s->desc = av_pix_fmt_desc_get(avctx->pix_fmt);
653 out_line_size = avctx->width * 2 * s->desc->nb_components;
654 s->scan_line_size = s->xdelta * current_channel_offset;
655 scan_line_blocks = (s->ydelta + s->scan_lines_per_block - 1) / s->scan_lines_per_block;
657 if (s->compr != EXR_RAW) {
658 int thread_data_size, prev_size;
661 prev_size = s->thread_data_size;
662 if (av_size_mult(avctx->thread_count, sizeof(EXRThreadData), &thread_data_size))
663 return AVERROR(EINVAL);
665 m = av_fast_realloc(s->thread_data, &s->thread_data_size, thread_data_size);
667 return AVERROR(ENOMEM);
669 memset(s->thread_data + prev_size, 0, s->thread_data_size - prev_size);
672 if ((ret = ff_thread_get_buffer(avctx, p)) < 0) {
673 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
677 if (buf_end - buf < scan_line_blocks * 8)
678 return AVERROR_INVALIDDATA;
682 // Zero out the start if ymin is not 0
683 for (y = 0; y < s->ymin; y++) {
684 memset(ptr, 0, out_line_size);
685 ptr += p->linesize[0];
688 avctx->execute2(avctx, decode_block, s->thread_data, NULL, scan_line_blocks);
690 // Zero out the end if ymax+1 is not h
691 for (y = s->ymax + 1; y < avctx->height; y++) {
692 memset(ptr, 0, out_line_size);
693 ptr += p->linesize[0];
696 *picture = s->picture;
702 static av_cold int decode_init(AVCodecContext *avctx)
704 EXRContext *s = avctx->priv_data;
706 avcodec_get_frame_defaults(&s->picture);
707 avctx->coded_frame = &s->picture;
712 static av_cold int decode_end(AVCodecContext *avctx)
714 EXRContext *s = avctx->priv_data;
717 if (s->picture.data[0])
718 avctx->release_buffer(avctx, &s->picture);
720 for (i = 0; i < s->thread_data_size / sizeof(EXRThreadData); i++) {
721 EXRThreadData *td = &s->thread_data[i];
722 av_free(td->uncompressed_data);
726 av_freep(&s->thread_data);
727 s->thread_data_size = 0;
732 AVCodec ff_exr_decoder = {
734 .type = AVMEDIA_TYPE_VIDEO,
735 .id = AV_CODEC_ID_EXR,
736 .priv_data_size = sizeof(EXRContext),
739 .decode = decode_frame,
740 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS | CODEC_CAP_SLICE_THREADS,
741 .long_name = NULL_IF_CONFIG_SMALL("OpenEXR image"),