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 negative if variable is invalid
122 static 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, AV_LOG_ERROR, "Incomplete header\n");
137 if (*variable_buffer_data_size > buf_end - *buf)
141 *buf -= strlen(value_name)+1;
142 av_log(avctx, AV_LOG_WARNING, "Unknown data type for header variable %s\n", value_name);
147 static int decode_frame(AVCodecContext *avctx,
152 const uint8_t *buf = avpkt->data;
153 unsigned int buf_size = avpkt->size;
154 const uint8_t *buf_end = buf + buf_size;
156 EXRContext *const s = avctx->priv_data;
157 AVFrame *picture = data;
158 AVFrame *const p = &s->picture;
161 int i, x, y, stride, magic_number, version_flag;
164 unsigned int xmin = ~0;
165 unsigned int xmax = ~0;
166 unsigned int ymin = ~0;
167 unsigned int ymax = ~0;
168 unsigned int xdelta = ~0;
170 unsigned int current_channel_offset = 0;
172 s->channel_offsets[0] = -1;
173 s->channel_offsets[1] = -1;
174 s->channel_offsets[2] = -1;
175 s->bits_per_color_id = -1;
177 if (buf_end - buf < 10) {
178 av_log(avctx, AV_LOG_ERROR, "Too short header to parse\n");
182 magic_number = bytestream_get_le32(&buf);
183 if (magic_number != 20000630) { // As per documentation of OpenEXR it's supposed to be int 20000630 little-endian
184 av_log(avctx, AV_LOG_ERROR, "Wrong magic number %d\n", magic_number);
188 version_flag = bytestream_get_le32(&buf);
189 if ((version_flag & 0x200) == 0x200) {
190 av_log(avctx, AV_LOG_ERROR, "Tile based images are not supported\n");
195 while (buf < buf_end && buf[0]) {
196 unsigned int variable_buffer_data_size;
197 // Process the channel list
198 if (check_header_variable(avctx, &buf, buf_end, "channels", "chlist", 38, &variable_buffer_data_size) >= 0) {
199 const uint8_t *channel_list_end;
200 if (!variable_buffer_data_size)
203 channel_list_end = buf + variable_buffer_data_size + 4;
204 while (channel_list_end - buf >= 19) {
205 int current_bits_per_color_id = -1;
206 int channel_index = -1;
208 if (!strcmp(buf, "R"))
210 if (!strcmp(buf, "G"))
212 if (!strcmp(buf, "B"))
215 while (bytestream_get_byte(&buf) && buf < channel_list_end)
218 if (channel_list_end - * &buf < 4) {
219 av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
223 current_bits_per_color_id = bytestream_get_le32(&buf);
224 if (current_bits_per_color_id > 2) {
225 av_log(avctx, AV_LOG_ERROR, "Unknown color format\n");
229 if (channel_index >= 0) {
230 if (s->bits_per_color_id != -1 && s->bits_per_color_id != current_bits_per_color_id) {
231 av_log(avctx, AV_LOG_ERROR, "RGB channels not of the same depth\n");
234 s->bits_per_color_id = current_bits_per_color_id;
235 s->channel_offsets[channel_index] = current_channel_offset;
238 current_channel_offset += 1 << current_bits_per_color_id;
242 /* Check if all channels are set with an offset or if the channels
243 * are causing an overflow */
245 if (FFMIN3(s->channel_offsets[0],
246 s->channel_offsets[1],
247 s->channel_offsets[2]) < 0) {
248 if (s->channel_offsets[0] < 0)
249 av_log(avctx, AV_LOG_ERROR, "Missing red channel\n");
250 if (s->channel_offsets[1] < 0)
251 av_log(avctx, AV_LOG_ERROR, "Missing green channel\n");
252 if (s->channel_offsets[2] < 0)
253 av_log(avctx, AV_LOG_ERROR, "Missing blue channel\n");
257 buf = channel_list_end;
261 // Process the dataWindow variable
262 if (check_header_variable(avctx, &buf, buf_end, "dataWindow", "box2i", 31, &variable_buffer_data_size) >= 0) {
263 if (!variable_buffer_data_size)
267 ymin = AV_RL32(buf + 4);
268 xmax = AV_RL32(buf + 8);
269 ymax = AV_RL32(buf + 12);
270 xdelta = (xmax-xmin) + 1;
272 buf += variable_buffer_data_size;
276 // Process the displayWindow variable
277 if (check_header_variable(avctx, &buf, buf_end, "displayWindow", "box2i", 34, &variable_buffer_data_size) >= 0) {
278 if (!variable_buffer_data_size)
281 w = AV_RL32(buf + 8) + 1;
282 h = AV_RL32(buf + 12) + 1;
284 buf += variable_buffer_data_size;
288 // Process the lineOrder variable
289 if (check_header_variable(avctx, &buf, buf_end, "lineOrder", "lineOrder", 25, &variable_buffer_data_size) >= 0) {
290 if (!variable_buffer_data_size)
294 av_log(avctx, AV_LOG_ERROR, "Doesn't support this line order : %d\n", *buf);
298 buf += variable_buffer_data_size;
302 // Process the compression variable
303 if (check_header_variable(avctx, &buf, buf_end, "compression", "compression", 29, &variable_buffer_data_size) >= 0) {
304 if (!variable_buffer_data_size)
317 av_log(avctx, AV_LOG_ERROR, "This type of compression is not supported\n");
321 buf += variable_buffer_data_size;
325 // Check if there is enough bytes for a header
326 if (buf_end - buf <= 9) {
327 av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
331 // Process unknown variables
332 for (i = 0; i < 2; i++) {
333 // Skip variable name/type
334 while (++buf < buf_end)
339 // Skip variable length
340 if (buf_end - buf >= 5) {
341 variable_buffer_data_size = get_header_variable_length(&buf, buf_end);
342 if (!variable_buffer_data_size) {
343 av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
346 buf += variable_buffer_data_size;
350 if (buf >= buf_end) {
351 av_log(avctx, AV_LOG_ERROR, "Incomplete frame\n");
356 switch (s->bits_per_color_id) {
359 avctx->pix_fmt = PIX_FMT_RGB48;
363 av_log_missing_feature(avctx, "8-bit OpenEXR", 1);
366 av_log(avctx, AV_LOG_ERROR, "Unknown color format : %d\n", s->bits_per_color_id);
370 if (s->picture.data[0])
371 avctx->release_buffer(avctx, &s->picture);
372 if (av_image_check_size(w, h, 0, avctx))
375 // Verify the xmin, xmax, ymin, ymax and xdelta before setting the actual image size
376 if (xmin > xmax || ymin > ymax || xdelta != xmax - xmin + 1 || xmax >= w || ymax >= h) {
377 av_log(avctx, AV_LOG_ERROR, "Wrong sizing or missing size information\n");
381 if (w != avctx->width || h != avctx->height) {
382 avcodec_set_dimensions(avctx, w, h);
385 if (avctx->get_buffer(avctx, p) < 0) {
386 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
391 stride = p->linesize[0];
393 // Zero out the start if ymin is not 0
394 for (y = 0; y < ymin; y++) {
395 memset(ptr, 0, avctx->width * 6);
399 // Process the actual lines
400 for (y = ymin; y <= ymax; y++) {
401 uint16_t *ptr_x = (uint16_t *)ptr;
402 if (buf_end - buf > 8) {
403 /* Read the lineoffset from the line offset table and add 8 bytes
404 to skip the coordinates and data size fields */
405 const uint64_t line_offset = bytestream_get_le64(&buf) + 8;
406 // Check if the buffer has the required bytes needed from the offset
407 if (line_offset > avpkt->size - xdelta * current_channel_offset) {
408 // Line offset is probably wrong and not inside the buffer
409 av_log(avctx, AV_LOG_WARNING, "Line offset for line %d is out of reach setting it to black\n", y);
410 memset(ptr_x, 0, avctx->width * 6);
412 const uint8_t *red_channel_buffer = avpkt->data + line_offset + xdelta * s->channel_offsets[0];
413 const uint8_t *green_channel_buffer = avpkt->data + line_offset + xdelta * s->channel_offsets[1];
414 const uint8_t *blue_channel_buffer = avpkt->data + line_offset + xdelta * s->channel_offsets[2];
416 // Zero out the start if xmin is not 0
417 memset(ptr_x, 0, xmin * 6);
419 if (s->bits_per_color_id == 2) {
421 for (x = 0; x < xdelta; x++) {
422 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&red_channel_buffer));
423 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&green_channel_buffer));
424 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&blue_channel_buffer));
428 for (x = 0; x < xdelta; x++) {
429 *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&red_channel_buffer));
430 *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&green_channel_buffer));
431 *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&blue_channel_buffer));
435 // Zero out the end if xmax+1 is not w
436 memset(ptr_x, 0, (avctx->width - (xmax + 1)) * 6);
437 ptr_x += (avctx->width - (xmax + 1)) * 3;
445 // Zero out the end if ymax+1 is not h
446 for (y = ymax + 1; y < avctx->height; y++) {
447 memset(ptr, 0, avctx->width * 6);
451 *picture = s->picture;
452 *data_size = sizeof(AVPicture);
457 static av_cold int decode_init(AVCodecContext *avctx)
459 EXRContext *s = avctx->priv_data;
460 avcodec_get_frame_defaults(&s->picture);
461 avctx->coded_frame = &s->picture;
465 static av_cold int decode_end(AVCodecContext *avctx)
467 EXRContext *s = avctx->priv_data;
468 if (s->picture.data[0])
469 avctx->release_buffer(avctx, &s->picture);
474 AVCodec ff_exr_decoder = {
476 .type = AVMEDIA_TYPE_VIDEO,
478 .priv_data_size = sizeof(EXRContext),
481 .decode = decode_frame,
482 .long_name = NULL_IF_CONFIG_SMALL("OpenEXR image"),