]> git.sesse.net Git - ffmpeg/blob - libavcodec/exr.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavcodec / exr.c
1 /*
2  * OpenEXR (.exr) image decoder
3  * Copyright (c) 2009 Jimmy Christensen
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 /**
23  * @file libavcodec/exr.c
24  * OpenEXR decoder
25  * @author Jimmy Christensen
26  *
27  * For more information on the OpenEXR format, visit:
28  *  http://openexr.com/
29  *
30  * exr_flt2uint() and exr_halflt2uint() is credited to  Reimar Döffinger
31  */
32
33 #include "avcodec.h"
34 #include "bytestream.h"
35 #include "libavutil/imgutils.h"
36
37 enum ExrCompr {
38     EXR_RAW   = 0,
39     EXR_RLE   = 1,
40     EXR_ZIP1  = 2,
41     EXR_ZIP16 = 3,
42     EXR_PIZ   = 4,
43     EXR_B44   = 6
44 };
45
46 typedef struct EXRContext {
47     AVFrame picture;
48     int compr;
49     int bits_per_color_id;
50     int8_t channel_offsets[3]; // 0 = red, 1 = green and 2 = blue
51 } EXRContext;
52
53 /**
54  * Converts from 32-bit float as uint32_t to uint16_t
55  *
56  * @param v 32-bit float
57  * @return normalized 16-bit unsigned int
58  */
59 static inline uint16_t exr_flt2uint(uint32_t v)
60 {
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
65         return 0;
66     if (exp >= 127)
67         return 0xffff;
68     v &= 0x007fffff;
69     return (v + (1 << 23)) >> (127 + 7 - exp);
70 }
71
72 /**
73  * Converts from 16-bit float as uint16_t to uint16_t
74  *
75  * @param v 16-bit float
76  * @return normalized 16-bit unsigned int
77  */
78 static inline uint16_t exr_halflt2uint(uint16_t v)
79 {
80     int exp = v >> 10;
81     if (v & 0x8000)
82         return 0;
83     if (!exp)
84         return (v >> 9) & 1;
85     if (exp >= 15)
86         return 0xffff;
87     v <<= 6;
88     return (v + (1 << 16)) >> (15 - exp);
89 }
90
91 /**
92  * Gets the size of the header variable
93  *
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
98  */
99 static unsigned int get_header_variable_length(const uint8_t **buf,
100                                                const uint8_t *buf_end)
101 {
102     unsigned int variable_buffer_data_size = bytestream_get_le32(buf);
103     if (variable_buffer_data_size >= buf_end - *buf)
104         return 0;
105     return variable_buffer_data_size;
106 }
107
108 /**
109  * Checks if the variable name corresponds with it's data type
110  *
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
119  * after it's checked
120  * @return negative if variable is invalid
121  */
122 static int check_header_variable(AVCodecContext *avctx,
123                                               const uint8_t **buf,
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)
129 {
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)
138                 return -1;
139             return 1;
140         }
141         *buf -= strlen(value_name)+1;
142         av_log(avctx, AV_LOG_WARNING, "Unknown data type for header variable %s\n", value_name);
143     }
144     return -1;
145 }
146
147 static int decode_frame(AVCodecContext *avctx,
148                         void *data,
149                         int *data_size,
150                         AVPacket *avpkt)
151 {
152     const uint8_t *buf      = avpkt->data;
153     unsigned int   buf_size = avpkt->size;
154     const uint8_t *buf_end  = buf + buf_size;
155
156     EXRContext *const s = avctx->priv_data;
157     AVFrame *picture  = data;
158     AVFrame *const p = &s->picture;
159     uint8_t *ptr;
160
161     int i, x, y, stride, magic_number, version_flag;
162     int w = 0;
163     int h = 0;
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;
169
170     unsigned int current_channel_offset = 0;
171
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;
176
177     if (buf_end - buf < 10) {
178         av_log(avctx, AV_LOG_ERROR, "Too short header to parse\n");
179         return -1;
180     }
181
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);
185         return -1;
186     }
187
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");
191         return -1;
192     }
193
194     // Parse the header
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)
201                 return -1;
202
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;
207
208                 if (!strcmp(buf, "R"))
209                     channel_index = 0;
210                 if (!strcmp(buf, "G"))
211                     channel_index = 1;
212                 if (!strcmp(buf, "B"))
213                     channel_index = 2;
214
215                 while (bytestream_get_byte(&buf) && buf < channel_list_end)
216                     continue; /* skip */
217
218                 if (channel_list_end - * &buf < 4) {
219                     av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
220                     return -1;
221                 }
222
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");
226                     return -1;
227                 }
228
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");
232                         return -1;
233                     }
234                     s->bits_per_color_id  = current_bits_per_color_id;
235                     s->channel_offsets[channel_index] = current_channel_offset;
236                 }
237
238                 current_channel_offset += 1 << current_bits_per_color_id;
239                 buf += 12;
240             }
241
242             /* Check if all channels are set with an offset or if the channels
243              * are causing an overflow  */
244
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");
254                 return -1;
255             }
256
257             buf = channel_list_end;
258             continue;
259         }
260
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)
264                 return -1;
265
266             xmin = AV_RL32(buf);
267             ymin = AV_RL32(buf + 4);
268             xmax = AV_RL32(buf + 8);
269             ymax = AV_RL32(buf + 12);
270             xdelta = (xmax-xmin) + 1;
271
272             buf += variable_buffer_data_size;
273             continue;
274         }
275
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)
279                 return -1;
280
281             w = AV_RL32(buf + 8) + 1;
282             h = AV_RL32(buf + 12) + 1;
283
284             buf += variable_buffer_data_size;
285             continue;
286         }
287
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)
291                 return -1;
292
293             if (*buf) {
294                 av_log(avctx, AV_LOG_ERROR, "Doesn't support this line order : %d\n", *buf);
295                 return -1;
296             }
297
298             buf += variable_buffer_data_size;
299             continue;
300         }
301
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)
305                 return -1;
306
307             switch (*buf) {
308             case EXR_RAW:
309                 s->compr = *buf;
310                 break;
311             case EXR_RLE:
312             case EXR_ZIP1:
313             case EXR_ZIP16:
314             case EXR_PIZ:
315             case EXR_B44:
316             default:
317                 av_log(avctx, AV_LOG_ERROR, "This type of compression is not supported\n");
318                 return -1;
319             }
320
321             buf += variable_buffer_data_size;
322             continue;
323         }
324
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");
328             return -1;
329         }
330
331         // Process unknown variables
332         for (i = 0; i < 2; i++) {
333             // Skip variable name/type
334             while (++buf < buf_end)
335                 if (buf[0] == 0x0)
336                     break;
337         }
338         buf++;
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");
344                 return -1;
345             }
346             buf += variable_buffer_data_size;
347         }
348     }
349
350     if (buf >= buf_end) {
351         av_log(avctx, AV_LOG_ERROR, "Incomplete frame\n");
352         return -1;
353     }
354     buf++;
355
356     switch (s->bits_per_color_id) {
357     case 2: // 32-bit
358     case 1: // 16-bit
359         avctx->pix_fmt = PIX_FMT_RGB48;
360         break;
361     // 8-bit
362     case 0:
363         av_log_missing_feature(avctx, "8-bit OpenEXR", 1);
364         return -1;
365     default:
366         av_log(avctx, AV_LOG_ERROR, "Unknown color format : %d\n", s->bits_per_color_id);
367         return -1;
368     }
369
370     if (s->picture.data[0])
371         avctx->release_buffer(avctx, &s->picture);
372     if (av_image_check_size(w, h, 0, avctx))
373         return -1;
374
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");
378         return -1;
379     }
380
381     if (w != avctx->width || h != avctx->height) {
382         avcodec_set_dimensions(avctx, w, h);
383     }
384
385     if (avctx->get_buffer(avctx, p) < 0) {
386         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
387         return -1;
388     }
389
390     ptr    = p->data[0];
391     stride = p->linesize[0];
392
393     // Zero out the start if ymin is not 0
394     for (y = 0; y < ymin; y++) {
395         memset(ptr, 0, avctx->width * 6);
396         ptr += stride;
397     }
398
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);
411             } else {
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];
415
416                 // Zero out the start if xmin is not 0
417                 memset(ptr_x, 0, xmin * 6);
418                 ptr_x += xmin * 3;
419                 if (s->bits_per_color_id == 2) {
420                     // 32-bit
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));
425                     }
426                 } else {
427                     // 16-bit
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));
432                     }
433                 }
434
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;
438
439             }
440             // Move to next line
441             ptr += stride;
442         }
443     }
444
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);
448         ptr += stride;
449     }
450
451     *picture   = s->picture;
452     *data_size = sizeof(AVPicture);
453
454     return buf_size;
455 }
456
457 static av_cold int decode_init(AVCodecContext *avctx)
458 {
459     EXRContext *s = avctx->priv_data;
460     avcodec_get_frame_defaults(&s->picture);
461     avctx->coded_frame = &s->picture;
462     return 0;
463 }
464
465 static av_cold int decode_end(AVCodecContext *avctx)
466 {
467     EXRContext *s = avctx->priv_data;
468     if (s->picture.data[0])
469         avctx->release_buffer(avctx, &s->picture);
470
471     return 0;
472 }
473
474 AVCodec ff_exr_decoder = {
475     .name               = "exr",
476     .type               = AVMEDIA_TYPE_VIDEO,
477     .id                 = CODEC_ID_EXR,
478     .priv_data_size     = sizeof(EXRContext),
479     .init               = decode_init,
480     .close              = decode_end,
481     .decode             = decode_frame,
482     .long_name          = NULL_IF_CONFIG_SMALL("OpenEXR image"),
483 };