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 rle_uncompress(const uint8_t *src, int ssize, uint8_t *dst, int dsize)
204 int8_t *d = (int8_t *)dst;
205 const int8_t *s = (const int8_t *)src;
206 int8_t *dend = d + dsize;
215 if ((dsize -= count ) < 0 ||
216 (ssize -= count + 1) < 0)
224 if ((dsize -= count) < 0 ||
238 static int decode_block(AVCodecContext *avctx, void *tdata,
239 int jobnr, int threadnr)
241 EXRContext *s = avctx->priv_data;
242 AVFrame *const p = &s->picture;
243 EXRThreadData *td = &s->thread_data[threadnr];
244 const uint8_t *channel_buffer[4] = { 0 };
245 const uint8_t *buf = s->buf;
246 uint64_t line_offset, uncompressed_size;
247 uint32_t xdelta = s->xdelta;
250 int32_t data_size, line;
252 int axmax = (avctx->width - (s->xmax + 1)) * 2 * s->desc->nb_components;
253 int bxmin = s->xmin * 2 * s->desc->nb_components;
254 int i, x, buf_size = s->buf_size;
256 line_offset = AV_RL64(s->table + jobnr * 8);
257 // Check if the buffer has the required bytes needed from the offset
258 if (line_offset > buf_size - 8)
259 return AVERROR_INVALIDDATA;
261 src = buf + line_offset + 8;
262 line = AV_RL32(src - 8);
263 if (line < s->ymin || line > s->ymax)
264 return AVERROR_INVALIDDATA;
266 data_size = AV_RL32(src - 4);
267 if (data_size <= 0 || data_size > buf_size)
268 return AVERROR_INVALIDDATA;
270 uncompressed_size = s->scan_line_size * FFMIN(s->scan_lines_per_block, s->ymax - line + 1);
271 if ((s->compr == EXR_RAW && (data_size != uncompressed_size ||
272 line_offset > buf_size - uncompressed_size)) ||
273 (s->compr != EXR_RAW && (data_size > uncompressed_size ||
274 line_offset > buf_size - data_size))) {
275 return AVERROR_INVALIDDATA;
278 if (data_size < uncompressed_size) {
279 av_fast_padded_malloc(&td->uncompressed_data, &td->uncompressed_size, uncompressed_size);
280 av_fast_padded_malloc(&td->tmp, &td->tmp_size, uncompressed_size);
281 if (!td->uncompressed_data || !td->tmp)
282 return AVERROR(ENOMEM);
284 if ((s->compr == EXR_ZIP1 || s->compr == EXR_ZIP16) && data_size < uncompressed_size) {
285 unsigned long dest_len = uncompressed_size;
287 if (uncompress(td->tmp, &dest_len, src, data_size) != Z_OK ||
288 dest_len != uncompressed_size) {
289 av_log(avctx, AV_LOG_ERROR, "error during zlib decompression\n");
290 return AVERROR(EINVAL);
292 } else if (s->compr == EXR_RLE && data_size < uncompressed_size) {
293 if (rle_uncompress(src, data_size, td->tmp, uncompressed_size)) {
294 av_log(avctx, AV_LOG_ERROR, "error during rle decompression\n");
295 return AVERROR(EINVAL);
299 if (data_size < uncompressed_size) {
300 predictor(td->tmp, uncompressed_size);
301 reorder_pixels(td->tmp, td->uncompressed_data, uncompressed_size);
302 src = td->uncompressed_data;
305 channel_buffer[0] = src + xdelta * s->channel_offsets[0];
306 channel_buffer[1] = src + xdelta * s->channel_offsets[1];
307 channel_buffer[2] = src + xdelta * s->channel_offsets[2];
308 if (s->channel_offsets[3] >= 0)
309 channel_buffer[3] = src + xdelta * s->channel_offsets[3];
311 ptr = p->data[0] + line * p->linesize[0];
312 for (i = 0; i < s->scan_lines_per_block && line + i <= s->ymax; i++, ptr += p->linesize[0]) {
313 const uint8_t *r, *g, *b, *a;
315 r = channel_buffer[0];
316 g = channel_buffer[1];
317 b = channel_buffer[2];
318 if (channel_buffer[3])
319 a = channel_buffer[3];
321 ptr_x = (uint16_t *)ptr;
323 // Zero out the start if xmin is not 0
324 memset(ptr_x, 0, bxmin);
325 ptr_x += s->xmin * s->desc->nb_components;
326 if (s->bits_per_color_id == 2) {
328 for (x = 0; x < xdelta; x++) {
329 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&r));
330 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&g));
331 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&b));
332 if (channel_buffer[3])
333 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a));
337 for (x = 0; x < xdelta; x++) {
338 *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&r));
339 *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&g));
340 *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&b));
341 if (channel_buffer[3])
342 *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&a));
346 // Zero out the end if xmax+1 is not w
347 memset(ptr_x, 0, axmax);
349 channel_buffer[0] += s->scan_line_size;
350 channel_buffer[1] += s->scan_line_size;
351 channel_buffer[2] += s->scan_line_size;
352 if (channel_buffer[3])
353 channel_buffer[3] += s->scan_line_size;
359 static int decode_frame(AVCodecContext *avctx,
364 const uint8_t *buf = avpkt->data;
365 unsigned int buf_size = avpkt->size;
366 const uint8_t *buf_end = buf + buf_size;
368 EXRContext *const s = avctx->priv_data;
369 AVFrame *picture = data;
370 AVFrame *const p = &s->picture;
373 int i, y, magic_number, version, flags, ret;
378 int scan_line_blocks;
380 unsigned int current_channel_offset = 0;
388 s->channel_offsets[0] = -1;
389 s->channel_offsets[1] = -1;
390 s->channel_offsets[2] = -1;
391 s->channel_offsets[3] = -1;
392 s->bits_per_color_id = -1;
395 s->buf_size = buf_size;
398 av_log(avctx, AV_LOG_ERROR, "Too short header to parse\n");
399 return AVERROR_INVALIDDATA;
402 magic_number = bytestream_get_le32(&buf);
403 if (magic_number != 20000630) { // As per documentation of OpenEXR it's supposed to be int 20000630 little-endian
404 av_log(avctx, AV_LOG_ERROR, "Wrong magic number %d\n", magic_number);
405 return AVERROR_INVALIDDATA;
408 version = bytestream_get_byte(&buf);
410 av_log(avctx, AV_LOG_ERROR, "Unsupported version %d\n", version);
411 return AVERROR_PATCHWELCOME;
414 flags = bytestream_get_le24(&buf);
416 av_log(avctx, AV_LOG_ERROR, "Tile based images are not supported\n");
417 return AVERROR_PATCHWELCOME;
421 while (buf < buf_end && buf[0]) {
422 unsigned int variable_buffer_data_size;
423 // Process the channel list
424 if (check_header_variable(avctx, &buf, buf_end, "channels", "chlist", 38, &variable_buffer_data_size) >= 0) {
425 const uint8_t *channel_list_end;
426 if (!variable_buffer_data_size)
427 return AVERROR_INVALIDDATA;
429 channel_list_end = buf + variable_buffer_data_size;
430 while (channel_list_end - buf >= 19) {
431 int current_bits_per_color_id = -1;
432 int channel_index = -1;
435 if (!strcmp(buf, "R"))
437 else if (!strcmp(buf, "G"))
439 else if (!strcmp(buf, "B"))
441 else if (!strcmp(buf, "A"))
444 av_log(avctx, AV_LOG_WARNING, "Unsupported channel %.256s\n", buf);
446 while (bytestream_get_byte(&buf) && buf < channel_list_end)
449 if (channel_list_end - * &buf < 4) {
450 av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
451 return AVERROR_INVALIDDATA;
454 current_bits_per_color_id = bytestream_get_le32(&buf);
455 if (current_bits_per_color_id > 2) {
456 av_log(avctx, AV_LOG_ERROR, "Unknown color format\n");
457 return AVERROR_INVALIDDATA;
461 xsub = bytestream_get_le32(&buf);
462 ysub = bytestream_get_le32(&buf);
463 if (xsub != 1 || ysub != 1) {
464 av_log(avctx, AV_LOG_ERROR, "Unsupported subsampling %dx%d\n", xsub, ysub);
465 return AVERROR_PATCHWELCOME;
468 if (channel_index >= 0) {
469 if (s->bits_per_color_id != -1 && s->bits_per_color_id != current_bits_per_color_id) {
470 av_log(avctx, AV_LOG_ERROR, "RGB channels not of the same depth\n");
471 return AVERROR_INVALIDDATA;
473 s->bits_per_color_id = current_bits_per_color_id;
474 s->channel_offsets[channel_index] = current_channel_offset;
477 current_channel_offset += 1 << current_bits_per_color_id;
480 /* Check if all channels are set with an offset or if the channels
481 * are causing an overflow */
483 if (FFMIN3(s->channel_offsets[0],
484 s->channel_offsets[1],
485 s->channel_offsets[2]) < 0) {
486 if (s->channel_offsets[0] < 0)
487 av_log(avctx, AV_LOG_ERROR, "Missing red channel\n");
488 if (s->channel_offsets[1] < 0)
489 av_log(avctx, AV_LOG_ERROR, "Missing green channel\n");
490 if (s->channel_offsets[2] < 0)
491 av_log(avctx, AV_LOG_ERROR, "Missing blue channel\n");
492 return AVERROR_INVALIDDATA;
495 buf = channel_list_end;
497 } else if (check_header_variable(avctx, &buf, buf_end, "dataWindow", "box2i", 31, &variable_buffer_data_size) >= 0) {
498 if (!variable_buffer_data_size)
499 return AVERROR_INVALIDDATA;
501 s->xmin = AV_RL32(buf);
502 s->ymin = AV_RL32(buf + 4);
503 s->xmax = AV_RL32(buf + 8);
504 s->ymax = AV_RL32(buf + 12);
505 s->xdelta = (s->xmax - s->xmin) + 1;
506 s->ydelta = (s->ymax - s->ymin) + 1;
508 buf += variable_buffer_data_size;
510 } else if (check_header_variable(avctx, &buf, buf_end, "displayWindow", "box2i", 34, &variable_buffer_data_size) >= 0) {
511 if (!variable_buffer_data_size)
512 return AVERROR_INVALIDDATA;
514 w = AV_RL32(buf + 8) + 1;
515 h = AV_RL32(buf + 12) + 1;
517 buf += variable_buffer_data_size;
519 } else if (check_header_variable(avctx, &buf, buf_end, "lineOrder", "lineOrder", 25, &variable_buffer_data_size) >= 0) {
520 if (!variable_buffer_data_size)
521 return AVERROR_INVALIDDATA;
523 av_log(avctx, AV_LOG_DEBUG, "line order : %d\n", *buf);
525 av_log(avctx, AV_LOG_ERROR, "Unknown line order\n");
526 return AVERROR_INVALIDDATA;
529 buf += variable_buffer_data_size;
531 } else if (check_header_variable(avctx, &buf, buf_end, "pixelAspectRatio", "float", 31, &variable_buffer_data_size) >= 0) {
532 if (!variable_buffer_data_size)
533 return AVERROR_INVALIDDATA;
535 avctx->sample_aspect_ratio = av_d2q(av_int2float(AV_RL32(buf)), 255);
537 buf += variable_buffer_data_size;
539 } else if (check_header_variable(avctx, &buf, buf_end, "compression", "compression", 29, &variable_buffer_data_size) >= 0) {
540 if (!variable_buffer_data_size)
541 return AVERROR_INVALIDDATA;
546 av_log(avctx, AV_LOG_WARNING, "Found more than one compression attribute\n");
548 buf += variable_buffer_data_size;
552 // Check if there is enough bytes for a header
553 if (buf_end - buf <= 9) {
554 av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
555 return AVERROR_INVALIDDATA;
558 // Process unknown variables
559 for (i = 0; i < 2; i++) {
560 // Skip variable name/type
561 while (++buf < buf_end)
566 // Skip variable length
567 if (buf_end - buf >= 5) {
568 variable_buffer_data_size = get_header_variable_length(&buf, buf_end);
569 if (!variable_buffer_data_size) {
570 av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
571 return AVERROR_INVALIDDATA;
573 buf += variable_buffer_data_size;
577 if (s->compr == -1) {
578 av_log(avctx, AV_LOG_ERROR, "Missing compression attribute\n");
579 return AVERROR_INVALIDDATA;
582 if (buf >= buf_end) {
583 av_log(avctx, AV_LOG_ERROR, "Incomplete frame\n");
584 return AVERROR_INVALIDDATA;
588 switch (s->bits_per_color_id) {
591 if (s->channel_offsets[3] >= 0)
592 avctx->pix_fmt = AV_PIX_FMT_RGBA64;
594 avctx->pix_fmt = AV_PIX_FMT_RGB48;
598 av_log_missing_feature(avctx, "8-bit OpenEXR", 1);
599 return AVERROR_PATCHWELCOME;
601 av_log(avctx, AV_LOG_ERROR, "Unknown color format : %d\n", s->bits_per_color_id);
602 return AVERROR_INVALIDDATA;
609 s->scan_lines_per_block = 1;
612 s->scan_lines_per_block = 16;
615 av_log(avctx, AV_LOG_ERROR, "Compression type %d is not supported\n", s->compr);
616 return AVERROR_PATCHWELCOME;
619 if (s->picture.data[0])
620 ff_thread_release_buffer(avctx, &s->picture);
621 if (av_image_check_size(w, h, 0, avctx))
622 return AVERROR_INVALIDDATA;
624 // Verify the xmin, xmax, ymin, ymax and xdelta before setting the actual image size
625 if (s->xmin > s->xmax ||
627 s->xdelta != s->xmax - s->xmin + 1 ||
628 s->xmax >= w || s->ymax >= h) {
629 av_log(avctx, AV_LOG_ERROR, "Wrong sizing or missing size information\n");
630 return AVERROR_INVALIDDATA;
633 if (w != avctx->width || h != avctx->height) {
634 avcodec_set_dimensions(avctx, w, h);
637 s->desc = av_pix_fmt_desc_get(avctx->pix_fmt);
638 out_line_size = avctx->width * 2 * s->desc->nb_components;
639 s->scan_line_size = s->xdelta * current_channel_offset;
640 scan_line_blocks = (s->ydelta + s->scan_lines_per_block - 1) / s->scan_lines_per_block;
642 if (s->compr != EXR_RAW) {
643 int thread_data_size, prev_size;
646 prev_size = s->thread_data_size;
647 if (av_size_mult(avctx->thread_count, sizeof(EXRThreadData), &thread_data_size))
648 return AVERROR(EINVAL);
650 m = av_fast_realloc(s->thread_data, &s->thread_data_size, thread_data_size);
652 return AVERROR(ENOMEM);
654 memset(s->thread_data + prev_size, 0, s->thread_data_size - prev_size);
657 if ((ret = ff_thread_get_buffer(avctx, p)) < 0) {
658 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
662 if (buf_end - buf < scan_line_blocks * 8)
663 return AVERROR_INVALIDDATA;
667 // Zero out the start if ymin is not 0
668 for (y = 0; y < s->ymin; y++) {
669 memset(ptr, 0, out_line_size);
670 ptr += p->linesize[0];
673 avctx->execute2(avctx, decode_block, s->thread_data, NULL, scan_line_blocks);
675 // Zero out the end if ymax+1 is not h
676 for (y = s->ymax + 1; y < avctx->height; y++) {
677 memset(ptr, 0, out_line_size);
678 ptr += p->linesize[0];
681 *picture = s->picture;
687 static av_cold int decode_init(AVCodecContext *avctx)
689 EXRContext *s = avctx->priv_data;
691 avcodec_get_frame_defaults(&s->picture);
692 avctx->coded_frame = &s->picture;
697 static av_cold int decode_end(AVCodecContext *avctx)
699 EXRContext *s = avctx->priv_data;
702 if (s->picture.data[0])
703 avctx->release_buffer(avctx, &s->picture);
705 for (i = 0; i < s->thread_data_size / sizeof(EXRThreadData); i++) {
706 EXRThreadData *td = &s->thread_data[i];
707 av_free(td->uncompressed_data);
711 av_freep(&s->thread_data);
712 s->thread_data_size = 0;
717 AVCodec ff_exr_decoder = {
719 .type = AVMEDIA_TYPE_VIDEO,
720 .id = AV_CODEC_ID_EXR,
721 .priv_data_size = sizeof(EXRContext),
724 .decode = decode_frame,
725 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS | CODEC_CAP_SLICE_THREADS,
726 .long_name = NULL_IF_CONFIG_SMALL("OpenEXR image"),