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");
146 if (*variable_buffer_data_size > buf_end - *buf)
150 *buf -= strlen(value_name)+1;
151 av_log(avctx, AV_LOG_WARNING, "Unknown data type for header variable %s\n", value_name);
156 static void predictor(uint8_t *src, int size)
158 uint8_t *t = src + 1;
159 uint8_t *stop = src + size;
162 int d = (int)t[-1] + (int)t[0] - 128;
168 static void reorder_pixels(uint8_t *src, uint8_t *dst, int size)
170 const int8_t *t1 = src;
171 const int8_t *t2 = src + (size + 1) / 2;
173 int8_t *stop = s + size;
188 static int rle_uncompress(const uint8_t *src, int ssize, uint8_t *dst, int dsize)
190 int8_t *d = (int8_t *)dst;
191 int8_t *s = (int8_t *)src;
192 int8_t *dend = d + dsize;
201 if ((dsize -= count ) < 0 ||
202 (ssize -= count + 1) < 0)
210 if ((dsize -= count) < 0 ||
224 static int decode_frame(AVCodecContext *avctx,
229 const uint8_t *buf = avpkt->data;
230 unsigned int buf_size = avpkt->size;
231 const uint8_t *buf_end = buf + buf_size;
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_flag, 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;
249 int scan_lines_per_block;
250 unsigned long scan_line_size;
251 unsigned long uncompressed_size;
253 unsigned int current_channel_offset = 0;
255 s->channel_offsets[0] = -1;
256 s->channel_offsets[1] = -1;
257 s->channel_offsets[2] = -1;
258 s->channel_offsets[3] = -1;
259 s->bits_per_color_id = -1;
262 av_log(avctx, AV_LOG_ERROR, "Too short header to parse\n");
263 return AVERROR_INVALIDDATA;
266 magic_number = bytestream_get_le32(&buf);
267 if (magic_number != 20000630) { // As per documentation of OpenEXR it's supposed to be int 20000630 little-endian
268 av_log(avctx, AV_LOG_ERROR, "Wrong magic number %d\n", magic_number);
269 return AVERROR_INVALIDDATA;
272 version_flag = bytestream_get_le32(&buf);
273 if ((version_flag & 0x200) == 0x200) {
274 av_log(avctx, AV_LOG_ERROR, "Tile based images are not supported\n");
275 return AVERROR_PATCHWELCOME;
279 while (buf < buf_end && buf[0]) {
280 unsigned int variable_buffer_data_size;
281 // Process the channel list
282 if (check_header_variable(avctx, &buf, buf_end, "channels", "chlist", 38, &variable_buffer_data_size) >= 0) {
283 const uint8_t *channel_list_end;
284 if (!variable_buffer_data_size)
285 return AVERROR_INVALIDDATA;
287 channel_list_end = buf + variable_buffer_data_size;
288 while (channel_list_end - buf >= 19) {
289 int current_bits_per_color_id = -1;
290 int channel_index = -1;
292 if (!strcmp(buf, "R"))
294 else if (!strcmp(buf, "G"))
296 else if (!strcmp(buf, "B"))
298 else if (!strcmp(buf, "A"))
301 av_log(avctx, AV_LOG_WARNING, "Unsupported channel %.256s\n", buf);
303 while (bytestream_get_byte(&buf) && buf < channel_list_end)
306 if (channel_list_end - * &buf < 4) {
307 av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
308 return AVERROR_INVALIDDATA;
311 current_bits_per_color_id = bytestream_get_le32(&buf);
312 if (current_bits_per_color_id > 2) {
313 av_log(avctx, AV_LOG_ERROR, "Unknown color format\n");
314 return AVERROR_INVALIDDATA;
317 if (channel_index >= 0) {
318 if (s->bits_per_color_id != -1 && s->bits_per_color_id != current_bits_per_color_id) {
319 av_log(avctx, AV_LOG_ERROR, "RGB channels not of the same depth\n");
320 return AVERROR_INVALIDDATA;
322 s->bits_per_color_id = current_bits_per_color_id;
323 s->channel_offsets[channel_index] = current_channel_offset;
326 current_channel_offset += 1 << current_bits_per_color_id;
330 /* Check if all channels are set with an offset or if the channels
331 * are causing an overflow */
333 if (FFMIN3(s->channel_offsets[0],
334 s->channel_offsets[1],
335 s->channel_offsets[2]) < 0) {
336 if (s->channel_offsets[0] < 0)
337 av_log(avctx, AV_LOG_ERROR, "Missing red channel\n");
338 if (s->channel_offsets[1] < 0)
339 av_log(avctx, AV_LOG_ERROR, "Missing green channel\n");
340 if (s->channel_offsets[2] < 0)
341 av_log(avctx, AV_LOG_ERROR, "Missing blue channel\n");
342 return AVERROR_INVALIDDATA;
345 buf = channel_list_end;
349 // Process the dataWindow variable
350 if (check_header_variable(avctx, &buf, buf_end, "dataWindow", "box2i", 31, &variable_buffer_data_size) >= 0) {
351 if (!variable_buffer_data_size)
352 return AVERROR_INVALIDDATA;
355 ymin = AV_RL32(buf + 4);
356 xmax = AV_RL32(buf + 8);
357 ymax = AV_RL32(buf + 12);
358 xdelta = (xmax-xmin) + 1;
360 buf += variable_buffer_data_size;
364 // Process the displayWindow variable
365 if (check_header_variable(avctx, &buf, buf_end, "displayWindow", "box2i", 34, &variable_buffer_data_size) >= 0) {
366 if (!variable_buffer_data_size)
367 return AVERROR_INVALIDDATA;
369 w = AV_RL32(buf + 8) + 1;
370 h = AV_RL32(buf + 12) + 1;
372 buf += variable_buffer_data_size;
376 // Process the lineOrder variable
377 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;
382 av_log(avctx, AV_LOG_ERROR, "Doesn't support this line order : %d\n", *buf);
383 return AVERROR_PATCHWELCOME;
386 buf += variable_buffer_data_size;
390 // Process the pixelAspectRatio variable
391 if (check_header_variable(avctx, &buf, buf_end, "pixelAspectRatio", "float", 31, &variable_buffer_data_size) >= 0) {
392 if (!variable_buffer_data_size)
393 return AVERROR_INVALIDDATA;
395 avctx->sample_aspect_ratio = av_d2q(av_int2float(AV_RL32(buf)), 255);
397 buf += variable_buffer_data_size;
401 // Process the compression variable
402 if (check_header_variable(avctx, &buf, buf_end, "compression", "compression", 29, &variable_buffer_data_size) >= 0) {
403 if (!variable_buffer_data_size)
404 return AVERROR_INVALIDDATA;
409 av_log(avctx, AV_LOG_WARNING, "Found more than one compression attribute\n");
411 buf += variable_buffer_data_size;
415 // Check if there is enough bytes for a header
416 if (buf_end - buf <= 9) {
417 av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
418 return AVERROR_INVALIDDATA;
421 // Process unknown variables
422 for (i = 0; i < 2; i++) {
423 // Skip variable name/type
424 while (++buf < buf_end)
429 // Skip variable length
430 if (buf_end - buf >= 5) {
431 variable_buffer_data_size = get_header_variable_length(&buf, buf_end);
432 if (!variable_buffer_data_size) {
433 av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
434 return AVERROR_INVALIDDATA;
436 buf += variable_buffer_data_size;
440 if (s->compr == -1) {
441 av_log(avctx, AV_LOG_ERROR, "Missing compression attribute\n");
442 return AVERROR_INVALIDDATA;
445 if (buf >= buf_end) {
446 av_log(avctx, AV_LOG_ERROR, "Incomplete frame\n");
447 return AVERROR_INVALIDDATA;
451 switch (s->bits_per_color_id) {
454 if (s->channel_offsets[3] >= 0)
455 avctx->pix_fmt = PIX_FMT_RGBA64;
457 avctx->pix_fmt = PIX_FMT_RGB48;
461 av_log_missing_feature(avctx, "8-bit OpenEXR", 1);
462 return AVERROR_PATCHWELCOME;
464 av_log(avctx, AV_LOG_ERROR, "Unknown color format : %d\n", s->bits_per_color_id);
465 return AVERROR_INVALIDDATA;
472 scan_lines_per_block = 1;
475 scan_lines_per_block = 16;
478 av_log(avctx, AV_LOG_ERROR, "Compression type %d is not supported\n", s->compr);
479 return AVERROR_PATCHWELCOME;
482 if (s->picture.data[0])
483 ff_thread_release_buffer(avctx, &s->picture);
484 if (av_image_check_size(w, h, 0, avctx))
485 return AVERROR_INVALIDDATA;
487 // Verify the xmin, xmax, ymin, ymax and xdelta before setting the actual image size
488 if (xmin > xmax || ymin > ymax || xdelta != xmax - xmin + 1 || xmax >= w || ymax >= h) {
489 av_log(avctx, AV_LOG_ERROR, "Wrong sizing or missing size information\n");
490 return AVERROR_INVALIDDATA;
493 if (w != avctx->width || h != avctx->height) {
494 avcodec_set_dimensions(avctx, w, h);
497 bxmin = xmin * 2 * av_pix_fmt_descriptors[avctx->pix_fmt].nb_components;
498 axmax = (avctx->width - (xmax + 1)) * 2 * av_pix_fmt_descriptors[avctx->pix_fmt].nb_components;
499 out_line_size = avctx->width * 2 * av_pix_fmt_descriptors[avctx->pix_fmt].nb_components;
500 scan_line_size = xdelta * current_channel_offset;
501 uncompressed_size = scan_line_size * scan_lines_per_block;
503 if (s->compr != EXR_RAW) {
504 av_fast_padded_malloc(&s->uncompressed_data, &s->uncompressed_size, uncompressed_size);
505 av_fast_padded_malloc(&s->tmp, &s->tmp_size, uncompressed_size);
506 if (!s->uncompressed_data || !s->tmp)
507 return AVERROR(ENOMEM);
510 if ((ret = ff_thread_get_buffer(avctx, p)) < 0) {
511 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
516 stride = p->linesize[0];
518 // Zero out the start if ymin is not 0
519 for (y = 0; y < ymin; y++) {
520 memset(ptr, 0, out_line_size);
524 // Process the actual scan line blocks
525 for (y = ymin; y <= ymax; y += scan_lines_per_block) {
526 uint16_t *ptr_x = (uint16_t *)ptr;
527 if (buf_end - buf > 8) {
528 /* Read the lineoffset from the line offset table and add 8 bytes
529 to skip the coordinates and data size fields */
530 const uint64_t line_offset = bytestream_get_le64(&buf) + 8;
533 // Check if the buffer has the required bytes needed from the offset
534 if ((line_offset > buf_size) ||
535 (s->compr == EXR_RAW && line_offset > avpkt->size - xdelta * current_channel_offset) ||
536 (s->compr != EXR_RAW && line_offset > buf_size - (data_size = AV_RL32(avpkt->data + line_offset - 4)))) {
537 // Line offset is probably wrong and not inside the buffer
538 av_log(avctx, AV_LOG_WARNING, "Line offset for line %d is out of reach setting it to black\n", y);
539 for (i = 0; i < scan_lines_per_block && y + i <= ymax; i++, ptr += stride) {
540 ptr_x = (uint16_t *)ptr;
541 memset(ptr_x, 0, out_line_size);
544 const uint8_t *red_channel_buffer, *green_channel_buffer, *blue_channel_buffer, *alpha_channel_buffer = 0;
546 if ((s->compr == EXR_ZIP1 || s->compr == EXR_ZIP16) && data_size < uncompressed_size) {
547 unsigned long dest_len = uncompressed_size;
549 if (uncompress(s->tmp, &dest_len, avpkt->data + line_offset, data_size) != Z_OK ||
550 dest_len != uncompressed_size) {
551 av_log(avctx, AV_LOG_ERROR, "error during zlib decompression\n");
552 return AVERROR(EINVAL);
554 } else if (s->compr == EXR_RLE && data_size < uncompressed_size) {
555 if (rle_uncompress(avpkt->data + line_offset, data_size, s->tmp, uncompressed_size)) {
556 av_log(avctx, AV_LOG_ERROR, "error during rle decompression\n");
557 return AVERROR(EINVAL);
561 if (s->compr != EXR_RAW && data_size < uncompressed_size) {
562 predictor(s->tmp, uncompressed_size);
563 reorder_pixels(s->tmp, s->uncompressed_data, uncompressed_size);
565 red_channel_buffer = s->uncompressed_data + xdelta * s->channel_offsets[0];
566 green_channel_buffer = s->uncompressed_data + xdelta * s->channel_offsets[1];
567 blue_channel_buffer = s->uncompressed_data + xdelta * s->channel_offsets[2];
568 if (s->channel_offsets[3] >= 0)
569 alpha_channel_buffer = s->uncompressed_data + xdelta * s->channel_offsets[3];
571 red_channel_buffer = avpkt->data + line_offset + xdelta * s->channel_offsets[0];
572 green_channel_buffer = avpkt->data + line_offset + xdelta * s->channel_offsets[1];
573 blue_channel_buffer = avpkt->data + line_offset + xdelta * s->channel_offsets[2];
574 if (s->channel_offsets[3] >= 0)
575 alpha_channel_buffer = avpkt->data + line_offset + xdelta * s->channel_offsets[3];
578 for (i = 0; i < scan_lines_per_block && y + i <= ymax; i++, ptr += stride) {
579 const uint8_t *r, *g, *b, *a;
581 r = red_channel_buffer;
582 g = green_channel_buffer;
583 b = blue_channel_buffer;
584 if (alpha_channel_buffer)
585 a = alpha_channel_buffer;
587 ptr_x = (uint16_t *)ptr;
589 // Zero out the start if xmin is not 0
590 memset(ptr_x, 0, bxmin);
591 ptr_x += xmin * av_pix_fmt_descriptors[avctx->pix_fmt].nb_components;
592 if (s->bits_per_color_id == 2) {
594 for (x = 0; x < xdelta; x++) {
595 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&r));
596 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&g));
597 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&b));
598 if (alpha_channel_buffer)
599 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a));
603 for (x = 0; x < xdelta; x++) {
604 *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&r));
605 *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&g));
606 *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&b));
607 if (alpha_channel_buffer)
608 *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&a));
612 // Zero out the end if xmax+1 is not w
613 memset(ptr_x, 0, axmax);
615 red_channel_buffer += scan_line_size;
616 green_channel_buffer += scan_line_size;
617 blue_channel_buffer += scan_line_size;
618 if (alpha_channel_buffer)
619 alpha_channel_buffer += scan_line_size;
625 // Zero out the end if ymax+1 is not h
626 for (y = ymax + 1; y < avctx->height; y++) {
627 memset(ptr, 0, out_line_size);
631 *picture = s->picture;
632 *data_size = sizeof(AVPicture);
637 static av_cold int decode_init(AVCodecContext *avctx)
639 EXRContext *s = avctx->priv_data;
641 avcodec_get_frame_defaults(&s->picture);
642 avctx->coded_frame = &s->picture;
649 static av_cold int decode_end(AVCodecContext *avctx)
651 EXRContext *s = avctx->priv_data;
653 if (s->picture.data[0])
654 avctx->release_buffer(avctx, &s->picture);
656 av_freep(&s->uncompressed_data);
662 AVCodec ff_exr_decoder = {
664 .type = AVMEDIA_TYPE_VIDEO,
666 .priv_data_size = sizeof(EXRContext),
669 .decode = decode_frame,
670 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
671 .long_name = NULL_IF_CONFIG_SMALL("OpenEXR image"),