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 EXRContext {
54 int bits_per_color_id;
55 int channel_offsets[4]; // 0 = red, 1 = green, 2 = blue and 3 = alpha
57 uint8_t *uncompressed_data;
58 int uncompressed_size;
65 * Converts from 32-bit float as uint32_t to uint16_t
67 * @param v 32-bit float
68 * @return normalized 16-bit unsigned int
70 static inline uint16_t exr_flt2uint(uint32_t v)
72 unsigned int exp = v >> 23;
73 // "HACK": negative values result in exp< 0, so clipping them to 0
74 // is also handled by this condition, avoids explicit check for sign bit.
75 if (exp<= 127 + 7 - 24) // we would shift out all bits anyway
80 return (v + (1 << 23)) >> (127 + 7 - exp);
84 * Converts from 16-bit float as uint16_t to uint16_t
86 * @param v 16-bit float
87 * @return normalized 16-bit unsigned int
89 static inline uint16_t exr_halflt2uint(uint16_t v)
91 unsigned exp = 14 - (v >> 10);
93 if (exp == 14) return (v >> 9) & 1;
94 else return (v & 0x8000) ? 0 : 0xffff;
97 return (v + (1 << 16)) >> (exp + 1);
101 * Gets the size of the header variable
103 * @param **buf the current pointer location in the header where
104 * the variable data starts
105 * @param *buf_end pointer location of the end of the buffer
106 * @return size of variable data
108 static unsigned int get_header_variable_length(const uint8_t **buf,
109 const uint8_t *buf_end)
111 unsigned int variable_buffer_data_size = bytestream_get_le32(buf);
112 if (variable_buffer_data_size >= buf_end - *buf)
114 return variable_buffer_data_size;
118 * Checks if the variable name corresponds with it's data type
120 * @param *avctx the AVCodecContext
121 * @param **buf the current pointer location in the header where
122 * the variable name starts
123 * @param *buf_end pointer location of the end of the buffer
124 * @param *value_name name of the varible to check
125 * @param *value_type type of the varible to check
126 * @param minimum_length minimum length of the variable data
127 * @param variable_buffer_data_size variable length read from the header
129 * @return negative if variable is invalid
131 static int check_header_variable(AVCodecContext *avctx,
133 const uint8_t *buf_end,
134 const char *value_name,
135 const char *value_type,
136 unsigned int minimum_length,
137 unsigned int *variable_buffer_data_size)
139 if (buf_end - *buf >= minimum_length && !strcmp(*buf, value_name)) {
140 *buf += strlen(value_name)+1;
141 if (!strcmp(*buf, value_type)) {
142 *buf += strlen(value_type)+1;
143 *variable_buffer_data_size = get_header_variable_length(buf, buf_end);
144 if (!*variable_buffer_data_size)
145 av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
148 *buf -= strlen(value_name)+1;
149 av_log(avctx, AV_LOG_WARNING, "Unknown data type for header variable %s\n", value_name);
154 static void predictor(uint8_t *src, int size)
156 uint8_t *t = src + 1;
157 uint8_t *stop = src + size;
160 int d = (int)t[-1] + (int)t[0] - 128;
166 static void reorder_pixels(uint8_t *src, uint8_t *dst, int size)
168 const int8_t *t1 = src;
169 const int8_t *t2 = src + (size + 1) / 2;
171 int8_t *stop = s + size;
186 static int rle_uncompress(const uint8_t *src, int ssize, uint8_t *dst, int dsize)
188 int8_t *d = (int8_t *)dst;
189 const int8_t *s = (const int8_t *)src;
190 int8_t *dend = d + dsize;
199 if ((dsize -= count ) < 0 ||
200 (ssize -= count + 1) < 0)
208 if ((dsize -= count) < 0 ||
222 static int decode_frame(AVCodecContext *avctx,
227 const uint8_t *buf = avpkt->data;
228 unsigned int buf_size = avpkt->size;
229 const uint8_t *buf_end = buf + buf_size;
232 const AVPixFmtDescriptor *desc;
233 EXRContext *const s = avctx->priv_data;
234 AVFrame *picture = data;
235 AVFrame *const p = &s->picture;
238 int i, x, y, stride, magic_number, version, flags, ret;
241 unsigned int xmin = ~0;
242 unsigned int xmax = ~0;
243 unsigned int ymin = ~0;
244 unsigned int ymax = ~0;
245 unsigned int xdelta = ~0;
246 unsigned int ydelta = ~0;
250 int scan_lines_per_block;
251 unsigned long scan_line_size;
252 unsigned long uncompressed_size;
254 unsigned int current_channel_offset = 0;
256 s->channel_offsets[0] = -1;
257 s->channel_offsets[1] = -1;
258 s->channel_offsets[2] = -1;
259 s->channel_offsets[3] = -1;
260 s->bits_per_color_id = -1;
264 av_log(avctx, AV_LOG_ERROR, "Too short header to parse\n");
265 return AVERROR_INVALIDDATA;
268 magic_number = bytestream_get_le32(&buf);
269 if (magic_number != 20000630) { // As per documentation of OpenEXR it's supposed to be int 20000630 little-endian
270 av_log(avctx, AV_LOG_ERROR, "Wrong magic number %d\n", magic_number);
271 return AVERROR_INVALIDDATA;
274 version = bytestream_get_byte(&buf);
276 av_log(avctx, AV_LOG_ERROR, "Unsupported version %d\n", version);
277 return AVERROR_PATCHWELCOME;
280 flags = bytestream_get_le24(&buf);
282 av_log(avctx, AV_LOG_ERROR, "Tile based images are not supported\n");
283 return AVERROR_PATCHWELCOME;
287 while (buf < buf_end && buf[0]) {
288 unsigned int variable_buffer_data_size;
289 // Process the channel list
290 if (check_header_variable(avctx, &buf, buf_end, "channels", "chlist", 38, &variable_buffer_data_size) >= 0) {
291 const uint8_t *channel_list_end;
292 if (!variable_buffer_data_size)
293 return AVERROR_INVALIDDATA;
295 channel_list_end = buf + variable_buffer_data_size;
296 while (channel_list_end - buf >= 19) {
297 int current_bits_per_color_id = -1;
298 int channel_index = -1;
300 if (!strcmp(buf, "R"))
302 else if (!strcmp(buf, "G"))
304 else if (!strcmp(buf, "B"))
306 else if (!strcmp(buf, "A"))
309 av_log(avctx, AV_LOG_WARNING, "Unsupported channel %.256s\n", buf);
311 while (bytestream_get_byte(&buf) && buf < channel_list_end)
314 if (channel_list_end - * &buf < 4) {
315 av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
316 return AVERROR_INVALIDDATA;
319 current_bits_per_color_id = bytestream_get_le32(&buf);
320 if (current_bits_per_color_id > 2) {
321 av_log(avctx, AV_LOG_ERROR, "Unknown color format\n");
322 return AVERROR_INVALIDDATA;
325 if (channel_index >= 0) {
326 if (s->bits_per_color_id != -1 && s->bits_per_color_id != current_bits_per_color_id) {
327 av_log(avctx, AV_LOG_ERROR, "RGB channels not of the same depth\n");
328 return AVERROR_INVALIDDATA;
330 s->bits_per_color_id = current_bits_per_color_id;
331 s->channel_offsets[channel_index] = current_channel_offset;
334 current_channel_offset += 1 << current_bits_per_color_id;
338 /* Check if all channels are set with an offset or if the channels
339 * are causing an overflow */
341 if (FFMIN3(s->channel_offsets[0],
342 s->channel_offsets[1],
343 s->channel_offsets[2]) < 0) {
344 if (s->channel_offsets[0] < 0)
345 av_log(avctx, AV_LOG_ERROR, "Missing red channel\n");
346 if (s->channel_offsets[1] < 0)
347 av_log(avctx, AV_LOG_ERROR, "Missing green channel\n");
348 if (s->channel_offsets[2] < 0)
349 av_log(avctx, AV_LOG_ERROR, "Missing blue channel\n");
350 return AVERROR_INVALIDDATA;
353 buf = channel_list_end;
355 } else if (check_header_variable(avctx, &buf, buf_end, "dataWindow", "box2i", 31, &variable_buffer_data_size) >= 0) {
356 if (!variable_buffer_data_size)
357 return AVERROR_INVALIDDATA;
360 ymin = AV_RL32(buf + 4);
361 xmax = AV_RL32(buf + 8);
362 ymax = AV_RL32(buf + 12);
363 xdelta = (xmax-xmin) + 1;
364 ydelta = (ymax-ymin) + 1;
366 buf += variable_buffer_data_size;
368 } else if (check_header_variable(avctx, &buf, buf_end, "displayWindow", "box2i", 34, &variable_buffer_data_size) >= 0) {
369 if (!variable_buffer_data_size)
370 return AVERROR_INVALIDDATA;
372 w = AV_RL32(buf + 8) + 1;
373 h = AV_RL32(buf + 12) + 1;
375 buf += variable_buffer_data_size;
377 } else if (check_header_variable(avctx, &buf, buf_end, "lineOrder", "lineOrder", 25, &variable_buffer_data_size) >= 0) {
378 if (!variable_buffer_data_size)
379 return AVERROR_INVALIDDATA;
381 av_log(avctx, AV_LOG_DEBUG, "line order : %d\n", *buf);
383 av_log(avctx, AV_LOG_ERROR, "Unknown line order\n");
384 return AVERROR_INVALIDDATA;
387 buf += variable_buffer_data_size;
389 } else if (check_header_variable(avctx, &buf, buf_end, "pixelAspectRatio", "float", 31, &variable_buffer_data_size) >= 0) {
390 if (!variable_buffer_data_size)
391 return AVERROR_INVALIDDATA;
393 avctx->sample_aspect_ratio = av_d2q(av_int2float(AV_RL32(buf)), 255);
395 buf += variable_buffer_data_size;
397 } else if (check_header_variable(avctx, &buf, buf_end, "compression", "compression", 29, &variable_buffer_data_size) >= 0) {
398 if (!variable_buffer_data_size)
399 return AVERROR_INVALIDDATA;
404 av_log(avctx, AV_LOG_WARNING, "Found more than one compression attribute\n");
406 buf += variable_buffer_data_size;
410 // Check if there is enough bytes for a header
411 if (buf_end - buf <= 9) {
412 av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
413 return AVERROR_INVALIDDATA;
416 // Process unknown variables
417 for (i = 0; i < 2; i++) {
418 // Skip variable name/type
419 while (++buf < buf_end)
424 // Skip variable length
425 if (buf_end - buf >= 5) {
426 variable_buffer_data_size = get_header_variable_length(&buf, buf_end);
427 if (!variable_buffer_data_size) {
428 av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
429 return AVERROR_INVALIDDATA;
431 buf += variable_buffer_data_size;
435 if (s->compr == -1) {
436 av_log(avctx, AV_LOG_ERROR, "Missing compression attribute\n");
437 return AVERROR_INVALIDDATA;
440 if (buf >= buf_end) {
441 av_log(avctx, AV_LOG_ERROR, "Incomplete frame\n");
442 return AVERROR_INVALIDDATA;
446 switch (s->bits_per_color_id) {
449 if (s->channel_offsets[3] >= 0)
450 avctx->pix_fmt = AV_PIX_FMT_RGBA64;
452 avctx->pix_fmt = AV_PIX_FMT_RGB48;
456 av_log_missing_feature(avctx, "8-bit OpenEXR", 1);
457 return AVERROR_PATCHWELCOME;
459 av_log(avctx, AV_LOG_ERROR, "Unknown color format : %d\n", s->bits_per_color_id);
460 return AVERROR_INVALIDDATA;
467 scan_lines_per_block = 1;
470 scan_lines_per_block = 16;
473 av_log(avctx, AV_LOG_ERROR, "Compression type %d is not supported\n", s->compr);
474 return AVERROR_PATCHWELCOME;
477 if (s->picture.data[0])
478 ff_thread_release_buffer(avctx, &s->picture);
479 if (av_image_check_size(w, h, 0, avctx))
480 return AVERROR_INVALIDDATA;
482 // Verify the xmin, xmax, ymin, ymax and xdelta before setting the actual image size
483 if (xmin > xmax || ymin > ymax || xdelta != xmax - xmin + 1 || xmax >= w || ymax >= h) {
484 av_log(avctx, AV_LOG_ERROR, "Wrong sizing or missing size information\n");
485 return AVERROR_INVALIDDATA;
488 if (w != avctx->width || h != avctx->height) {
489 avcodec_set_dimensions(avctx, w, h);
492 desc = av_pix_fmt_desc_get(avctx->pix_fmt);
493 bxmin = xmin * 2 * desc->nb_components;
494 axmax = (avctx->width - (xmax + 1)) * 2 * desc->nb_components;
495 out_line_size = avctx->width * 2 * desc->nb_components;
496 scan_line_size = xdelta * current_channel_offset;
497 uncompressed_size = scan_line_size * scan_lines_per_block;
499 if (s->compr != EXR_RAW) {
500 av_fast_padded_malloc(&s->uncompressed_data, &s->uncompressed_size, uncompressed_size);
501 av_fast_padded_malloc(&s->tmp, &s->tmp_size, uncompressed_size);
502 if (!s->uncompressed_data || !s->tmp)
503 return AVERROR(ENOMEM);
506 if ((ret = ff_thread_get_buffer(avctx, p)) < 0) {
507 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
512 stride = p->linesize[0];
514 // Zero out the start if ymin is not 0
515 for (y = 0; y < ymin; y++) {
516 memset(ptr, 0, out_line_size);
520 if (buf_end - buf < (ydelta + scan_lines_per_block - 1) / scan_lines_per_block * 8)
521 return AVERROR_INVALIDDATA;
523 // Process the actual scan line blocks
524 for (y = ymin; y <= ymax; y += scan_lines_per_block) {
526 const uint8_t *channel_buffer[4] = { 0 };
527 const uint64_t line_offset = bytestream_get_le64(&buf);
528 int32_t data_size, line;
530 // Check if the buffer has the required bytes needed from the offset
531 if (line_offset > (uint64_t)buf_size - 8)
532 return AVERROR_INVALIDDATA;
534 src = avpkt->data + line_offset + 8;
535 line = AV_RL32(src - 8);
536 if (line < ymin || line > ymax)
537 return AVERROR_INVALIDDATA;
539 data_size = AV_RL32(src - 4);
540 if (data_size <= 0 || data_size > buf_size)
541 return AVERROR_INVALIDDATA;
543 if ((s->compr == EXR_RAW && (data_size != uncompressed_size ||
544 line_offset > buf_size - uncompressed_size)) ||
545 (s->compr != EXR_RAW && line_offset > buf_size - data_size)) {
546 return AVERROR_INVALIDDATA;
549 if (scan_lines_per_block > 1)
550 uncompressed_size = scan_line_size * FFMIN(scan_lines_per_block, ymax - y + 1);
551 if ((s->compr == EXR_ZIP1 || s->compr == EXR_ZIP16) && data_size < uncompressed_size) {
552 unsigned long dest_len = uncompressed_size;
554 if (uncompress(s->tmp, &dest_len, src, data_size) != Z_OK ||
555 dest_len != uncompressed_size) {
556 av_log(avctx, AV_LOG_ERROR, "error during zlib decompression\n");
557 return AVERROR(EINVAL);
559 } else if (s->compr == EXR_RLE && data_size < uncompressed_size) {
560 if (rle_uncompress(src, data_size, s->tmp, uncompressed_size)) {
561 av_log(avctx, AV_LOG_ERROR, "error during rle decompression\n");
562 return AVERROR(EINVAL);
566 if (s->compr != EXR_RAW && data_size < uncompressed_size) {
567 predictor(s->tmp, uncompressed_size);
568 reorder_pixels(s->tmp, s->uncompressed_data, uncompressed_size);
570 channel_buffer[0] = s->uncompressed_data + xdelta * s->channel_offsets[0];
571 channel_buffer[1] = s->uncompressed_data + xdelta * s->channel_offsets[1];
572 channel_buffer[2] = s->uncompressed_data + xdelta * s->channel_offsets[2];
573 if (s->channel_offsets[3] >= 0)
574 channel_buffer[3] = s->uncompressed_data + xdelta * s->channel_offsets[3];
576 channel_buffer[0] = src + xdelta * s->channel_offsets[0];
577 channel_buffer[1] = src + xdelta * s->channel_offsets[1];
578 channel_buffer[2] = src + xdelta * s->channel_offsets[2];
579 if (s->channel_offsets[3] >= 0)
580 channel_buffer[3] = src + xdelta * s->channel_offsets[3];
583 ptr = p->data[0] + line * stride;
584 for (i = 0; i < scan_lines_per_block && y + i <= ymax; i++, ptr += stride) {
585 const uint8_t *r, *g, *b, *a;
587 r = channel_buffer[0];
588 g = channel_buffer[1];
589 b = channel_buffer[2];
590 if (channel_buffer[3])
591 a = channel_buffer[3];
593 ptr_x = (uint16_t *)ptr;
595 // Zero out the start if xmin is not 0
596 memset(ptr_x, 0, bxmin);
597 ptr_x += xmin * desc->nb_components;
598 if (s->bits_per_color_id == 2) {
600 for (x = 0; x < xdelta; x++) {
601 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&r));
602 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&g));
603 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&b));
604 if (channel_buffer[3])
605 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a));
609 for (x = 0; x < xdelta; x++) {
610 *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&r));
611 *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&g));
612 *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&b));
613 if (channel_buffer[3])
614 *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&a));
618 // Zero out the end if xmax+1 is not w
619 memset(ptr_x, 0, axmax);
621 channel_buffer[0] += scan_line_size;
622 channel_buffer[1] += scan_line_size;
623 channel_buffer[2] += scan_line_size;
624 if (channel_buffer[3])
625 channel_buffer[3] += scan_line_size;
629 // Zero out the end if ymax+1 is not h
630 for (y = ymax + 1; y < avctx->height; y++) {
631 memset(ptr, 0, out_line_size);
635 *picture = s->picture;
641 static av_cold int decode_init(AVCodecContext *avctx)
643 EXRContext *s = avctx->priv_data;
645 avcodec_get_frame_defaults(&s->picture);
646 avctx->coded_frame = &s->picture;
651 static av_cold int decode_end(AVCodecContext *avctx)
653 EXRContext *s = avctx->priv_data;
655 if (s->picture.data[0])
656 avctx->release_buffer(avctx, &s->picture);
658 av_freep(&s->uncompressed_data);
664 AVCodec ff_exr_decoder = {
666 .type = AVMEDIA_TYPE_VIDEO,
667 .id = AV_CODEC_ID_EXR,
668 .priv_data_size = sizeof(EXRContext),
671 .decode = decode_frame,
672 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
673 .long_name = NULL_IF_CONFIG_SMALL("OpenEXR image"),