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
23 * @file libavcodec/exr.c
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
34 #include "bytestream.h"
35 #include "libavutil/imgutils.h"
46 typedef struct EXRContext {
49 int bits_per_color_id;
50 int8_t channel_offsets[3]; // 0 = red, 1 = green and 2 = blue
54 * Converts from 32-bit float as uint32_t to uint16_t
56 * @param v 32-bit float
57 * @return normalized 16-bit unsigned int
59 static inline uint16_t exr_flt2uint(uint32_t v)
61 unsigned int exp = v >> 23;
62 // "HACK": negative values result in exp< 0, so clipping them to 0
63 // is also handled by this condition, avoids explicit check for sign bit.
64 if (exp<= 127 + 7 - 24) // we would shift out all bits anyway
69 return (v + (1 << 23)) >> (127 + 7 - exp);
73 * Converts from 16-bit float as uint16_t to uint16_t
75 * @param v 16-bit float
76 * @return normalized 16-bit unsigned int
78 static inline uint16_t exr_halflt2uint(uint16_t v)
88 return (v + (1 << 16)) >> (15 - exp);
92 * Gets the size of the header variable
94 * @param **buf the current pointer location in the header where
95 * the variable data starts
96 * @param *buf_end pointer location of the end of the buffer
97 * @return size of variable data
99 static unsigned int get_header_variable_length(const uint8_t **buf,
100 const uint8_t *buf_end)
102 unsigned int variable_buffer_data_size = bytestream_get_le32(buf);
103 if (variable_buffer_data_size >= buf_end - *buf)
105 return variable_buffer_data_size;
109 * Checks if the variable name corresponds with it's data type
111 * @param *avctx the AVCodecContext
112 * @param **buf the current pointer location in the header where
113 * the variable name starts
114 * @param *buf_end pointer location of the end of the buffer
115 * @param *value_name name of the varible to check
116 * @param *value_type type of the varible to check
117 * @param minimum_length minimum length of the variable data
118 * @param variable_buffer_data_size variable length read from the header
120 * @return zero if variable is invalid and 1 if good
122 static unsigned int check_header_variable(AVCodecContext *avctx,
124 const uint8_t *buf_end,
125 const char *value_name,
126 const char *value_type,
127 unsigned int minimum_length,
128 unsigned int *variable_buffer_data_size)
130 if (buf_end - *buf >= minimum_length && !strcmp(*buf, value_name)) {
131 *buf += strlen(value_name)+1;
132 if (!strcmp(*buf, value_type)) {
133 *buf += strlen(value_type)+1;
134 *variable_buffer_data_size = get_header_variable_length(buf, buf_end);
135 if (!*variable_buffer_data_size)
136 av_log(avctx, AVERROR_INVALIDDATA, "Incomplete header\n");
139 *buf -= strlen(value_name)+1;
140 av_log(avctx, AV_LOG_WARNING, "Unknown data type for header variable %s\n", value_name);
145 static int decode_frame(AVCodecContext *avctx,
150 const uint8_t *buf = avpkt->data;
151 unsigned int buf_size = avpkt->size;
152 const uint8_t *buf_end = buf + buf_size;
154 EXRContext *const s = avctx->priv_data;
155 AVFrame *picture = data;
156 AVFrame *const p = &s->picture;
159 int x, y, stride, magic_number, version_flag;
162 unsigned int xmin = ~0;
163 unsigned int xmax = ~0;
164 unsigned int ymin = ~0;
165 unsigned int ymax = ~0;
166 unsigned int xdelta = ~0;
168 unsigned int current_channel_offset = 0;
170 s->channel_offsets[0] = -1;
171 s->channel_offsets[1] = -1;
172 s->channel_offsets[2] = -1;
173 s->bits_per_color_id = -1;
175 magic_number = bytestream_get_le32(&buf);
176 if (magic_number != 20000630) { // As per documentation of OpenEXR it's supposed to be int 20000630 little-endian
177 av_log(avctx, AVERROR_INVALIDDATA, "Wrong magic number %d\n", magic_number);
181 version_flag = bytestream_get_le32(&buf);
182 if ((version_flag & 0x200) == 0x200) {
183 av_log(avctx, AVERROR_NOFMT, "Tile based images are not supported\n");
187 if (buf_end - buf < 10) {
188 av_log(avctx, AVERROR_INVALIDDATA, "Too short header to parse\n");
193 while (buf[0] != 0x0) {
194 unsigned int variable_buffer_data_size;
195 // Process the channel list
196 if (check_header_variable(avctx, &buf, buf_end, "channels", "chlist", 38, &variable_buffer_data_size)) {
197 const uint8_t *channel_list_end;
198 if (!variable_buffer_data_size)
201 channel_list_end = buf + variable_buffer_data_size + 4;
202 while (channel_list_end - buf >= 19) {
203 int current_bits_per_color_id = -1;
204 int channel_index = -1;
206 if (!strcmp(buf, "R"))
208 if (!strcmp(buf, "G"))
210 if (!strcmp(buf, "B"))
213 while (bytestream_get_byte(&buf) && buf < channel_list_end)
216 if (channel_list_end - * &buf < 4) {
217 av_log(avctx, AVERROR_INVALIDDATA, "Incomplete header\n");
221 current_bits_per_color_id = bytestream_get_le32(&buf);
222 if (current_bits_per_color_id > 2) {
223 av_log(avctx, AVERROR_NOFMT, "Unknown color format\n");
227 if (channel_index >= 0) {
228 if (s->bits_per_color_id != -1 && s->bits_per_color_id != current_bits_per_color_id) {
229 av_log(avctx, AVERROR_NOFMT, "RGB channels not of the same depth\n");
232 s->bits_per_color_id = current_bits_per_color_id;
233 s->channel_offsets[channel_index] = current_channel_offset;
236 current_channel_offset += 1 << current_bits_per_color_id;
240 /* Check if all channels are set with an offset or if the channels
241 * are causing an overflow */
243 if (FFMIN3(s->channel_offsets[0],
244 s->channel_offsets[1],
245 s->channel_offsets[2]) < 0) {
246 if (s->channel_offsets[0] < 0)
247 av_log(avctx, AVERROR_NOFMT, "Missing red channel\n");
248 if (s->channel_offsets[1] < 0)
249 av_log(avctx, AVERROR_NOFMT, "Missing green channel\n");
250 if (s->channel_offsets[2] < 0)
251 av_log(avctx, AVERROR_NOFMT, "Missing blue channel\n");
255 buf = channel_list_end;
259 // Process the dataWindow variable
260 if (check_header_variable(avctx, &buf, buf_end, "dataWindow", "box2i", 31, &variable_buffer_data_size)) {
261 if (!variable_buffer_data_size)
265 ymin = AV_RL32(buf + 4);
266 xmax = AV_RL32(buf + 8);
267 ymax = AV_RL32(buf + 12);
268 xdelta = (xmax-xmin) + 1;
270 buf += variable_buffer_data_size;
274 // Process the displayWindow variable
275 if (check_header_variable(avctx, &buf, buf_end, "displayWindow", "box2i", 34, &variable_buffer_data_size)) {
276 if (!variable_buffer_data_size)
279 w = AV_RL32(buf + 8) + 1;
280 h = AV_RL32(buf + 12) + 1;
282 buf += variable_buffer_data_size;
286 // Process the lineOrder variable
287 if (check_header_variable(avctx, &buf, buf_end, "lineOrder", "lineOrder", 25, &variable_buffer_data_size)) {
288 if (!variable_buffer_data_size)
292 av_log(avctx, AVERROR_NOFMT, "Doesn't support this line order : %d\n", *buf);
296 buf += variable_buffer_data_size;
300 // Process the compression variable
301 if (check_header_variable(avctx, &buf, buf_end, "compression", "compression", 29, &variable_buffer_data_size)) {
302 if (!variable_buffer_data_size)
315 av_log(avctx, AVERROR_NOFMT, "This type of compression is not supported\n");
319 buf += variable_buffer_data_size;
323 // Check if there is enough bytes for a header
324 if (buf_end - buf <= 9) {
325 av_log(avctx, AVERROR_INVALIDDATA, "Incomplete header\n");
329 // Process unknown variables
330 for (int i = 0; i < 2; i++) {
331 // Skip variable name/type
332 while (buf++ < buf_end)
337 // Skip variable length
338 if (buf_end - buf >= 5) {
339 variable_buffer_data_size = get_header_variable_length(&buf, buf_end);
340 if (!variable_buffer_data_size) {
341 av_log(avctx, AVERROR_INVALIDDATA, "Incomplete header\n");
344 buf += variable_buffer_data_size;
348 if (buf >= buf_end) {
349 av_log(avctx, AVERROR_EOF, "Incomplete file\n");
354 switch (s->bits_per_color_id) {
357 avctx->pix_fmt = PIX_FMT_RGB48;
361 av_log_missing_feature(avctx, "8-bit OpenEXR", 1);
364 av_log(avctx, AVERROR_NOFMT, "Unknown color format : %d\n", s->bits_per_color_id);
368 if (s->picture.data[0])
369 avctx->release_buffer(avctx, &s->picture);
370 if (av_image_check_size(w, h, 0, avctx))
373 // Verify the xmin, xmax, ymin, ymax and xdelta before setting the actual image size
374 if (xmin > w || xmin == ~0 ||
375 xmax > w || xmax == ~0 ||
376 ymin > h || ymin == ~0 ||
377 ymax > h || ymax == ~0 ||
379 av_log(avctx, AVERROR_INVALIDDATA, "Wrong sizing or missing size information\n");
383 if (w != avctx->width || h != avctx->height) {
384 avcodec_set_dimensions(avctx, w, h);
387 if (avctx->get_buffer(avctx, p) < 0) {
388 av_log(avctx, AVERROR_IO, "get_buffer() failed\n");
393 stride = p->linesize[0];
395 // Zero out the start if ymin is not 0
396 for (y = 0; y < ymin; y++) {
397 memset(ptr, 0, avctx->width * 6);
401 // Process the actual lines
402 for (y = ymin; y <= ymax; y++) {
403 uint16_t *ptr_x = (uint16_t *)ptr;
404 if (buf_end - buf > 8) {
405 /* Read the lineoffset from the line offset table and add 8 bytes
406 to skip the coordinates and data size fields */
407 const uint64_t line_offset = bytestream_get_le64(&buf) + 8;
408 // Check if the buffer has the required bytes needed from the offset
409 if (line_offset > avpkt->size - xdelta * current_channel_offset) {
410 // Line offset is probably wrong and not inside the buffer
411 av_log(avctx, AV_LOG_WARNING, "Line offset for line %d is out of reach setting it to black\n", y);
412 memset(ptr_x, 0, avctx->width * 6);
414 const uint8_t *red_channel_buffer = avpkt->data + line_offset + xdelta * s->channel_offsets[0];
415 const uint8_t *green_channel_buffer = avpkt->data + line_offset + xdelta * s->channel_offsets[1];
416 const uint8_t *blue_channel_buffer = avpkt->data + line_offset + xdelta * s->channel_offsets[2];
418 // Zero out the start if xmin is not 0
419 memset(ptr_x, 0, xmin * 6);
421 if (s->bits_per_color_id == 2) {
423 for (x = 0; x < xdelta; x++) {
424 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&red_channel_buffer));
425 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&green_channel_buffer));
426 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&blue_channel_buffer));
430 for (x = 0; x < xdelta; x++) {
431 *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&red_channel_buffer));
432 *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&green_channel_buffer));
433 *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&blue_channel_buffer));
437 // Zero out the end if xmax+1 is not w
438 memset(ptr_x, 0, (avctx->width - (xmax + 1)) * 6);
439 ptr_x += (avctx->width - (xmax + 1)) * 3;
447 // Zero out the end if ymax+1 is not h
448 for (y = ymax; y < avctx->height; y++) {
449 memset(ptr, 0, avctx->width * 6);
453 *picture = s->picture;
454 *data_size = sizeof(AVPicture);
459 static av_cold int decode_init(AVCodecContext *avctx)
461 EXRContext *s = avctx->priv_data;
462 avcodec_get_frame_defaults(&s->picture);
463 avctx->coded_frame = &s->picture;
467 static av_cold int decode_end(AVCodecContext *avctx)
469 EXRContext *s = avctx->priv_data;
470 if (s->picture.data[0])
471 avctx->release_buffer(avctx, &s->picture);
476 AVCodec ff_exr_decoder = {
478 .type = AVMEDIA_TYPE_VIDEO,
480 .priv_data_size = sizeof(EXRContext),
483 .decode = decode_frame,
484 .long_name = NULL_IF_CONFIG_SMALL("OpenEXR image"),