]> git.sesse.net Git - ffmpeg/blob - libavcodec/exr.c
7e9e68c17b965f3fca8fe0a49d36a3f0081fcca3
[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
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 <zlib.h>
34
35 #include "avcodec.h"
36 #include "bytestream.h"
37 #include "mathops.h"
38 #include "thread.h"
39 #include "libavutil/imgutils.h"
40
41 enum ExrCompr {
42     EXR_RAW   = 0,
43     EXR_RLE   = 1,
44     EXR_ZIP1  = 2,
45     EXR_ZIP16 = 3,
46     EXR_PIZ   = 4,
47     EXR_B44   = 6,
48     EXR_B44A  = 7,
49 };
50
51 typedef struct EXRThreadData {
52     uint8_t *uncompressed_data;
53     int uncompressed_size;
54
55     uint8_t *tmp;
56     int tmp_size;
57 } EXRThreadData;
58
59 typedef struct EXRContext {
60     AVFrame picture;
61     int compr;
62     int bits_per_color_id;
63     int channel_offsets[4]; // 0 = red, 1 = green, 2 = blue and 3 = alpha
64     const AVPixFmtDescriptor *desc;
65
66     uint32_t xmax, xmin;
67     uint32_t ymax, ymin;
68     uint32_t xdelta, ydelta;
69
70     uint64_t scan_line_size;
71     int scan_lines_per_block;
72
73     const uint8_t *buf, *table;
74     int buf_size;
75
76     EXRThreadData *thread_data;
77     int thread_data_size;
78 } EXRContext;
79
80 /**
81  * Converts from 32-bit float as uint32_t to uint16_t
82  *
83  * @param v 32-bit float
84  * @return normalized 16-bit unsigned int
85  */
86 static inline uint16_t exr_flt2uint(uint32_t v)
87 {
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
92         return 0;
93     if (exp >= 127)
94         return 0xffff;
95     v &= 0x007fffff;
96     return (v + (1 << 23)) >> (127 + 7 - exp);
97 }
98
99 /**
100  * Converts from 16-bit float as uint16_t to uint16_t
101  *
102  * @param v 16-bit float
103  * @return normalized 16-bit unsigned int
104  */
105 static inline uint16_t exr_halflt2uint(uint16_t v)
106 {
107     unsigned exp = 14 - (v >> 10);
108     if (exp >= 14) {
109         if (exp == 14) return (v >> 9) & 1;
110         else           return (v & 0x8000) ? 0 : 0xffff;
111     }
112     v <<= 6;
113     return (v + (1 << 16)) >> (exp + 1);
114 }
115
116 /**
117  * Gets the size of the header variable
118  *
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
123  */
124 static unsigned int get_header_variable_length(const uint8_t **buf,
125                                                const uint8_t *buf_end)
126 {
127     unsigned int variable_buffer_data_size = bytestream_get_le32(buf);
128     if (variable_buffer_data_size >= buf_end - *buf)
129         return 0;
130     return variable_buffer_data_size;
131 }
132
133 /**
134  * Checks if the variable name corresponds with it's data type
135  *
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
144  * after it's checked
145  * @return negative if variable is invalid
146  */
147 static int check_header_variable(AVCodecContext *avctx,
148                                               const uint8_t **buf,
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)
154 {
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");
162             return 1;
163         }
164         *buf -= strlen(value_name)+1;
165         av_log(avctx, AV_LOG_WARNING, "Unknown data type for header variable %s\n", value_name);
166     }
167     return -1;
168 }
169
170 static void predictor(uint8_t *src, int size)
171 {
172     uint8_t *t = src + 1;
173     uint8_t *stop = src + size;
174
175     while (t < stop) {
176         int d = (int)t[-1] + (int)t[0] - 128;
177         t[0] = d;
178         ++t;
179     }
180 }
181
182 static void reorder_pixels(uint8_t *src, uint8_t *dst, int size)
183 {
184     const int8_t *t1 = src;
185     const int8_t *t2 = src + (size + 1) / 2;
186     int8_t *s = dst;
187     int8_t *stop = s + size;
188
189     while (1) {
190         if (s < stop)
191             *(s++) = *(t1++);
192         else
193             break;
194
195         if (s < stop)
196             *(s++) = *(t2++);
197         else
198             break;
199     }
200 }
201
202 static int zip_uncompress(const uint8_t *src, int compressed_size,
203                           int uncompressed_size, EXRThreadData *td)
204 {
205     unsigned long dest_len = uncompressed_size;
206
207     if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK ||
208         dest_len != uncompressed_size)
209         return AVERROR(EINVAL);
210
211     predictor(td->tmp, uncompressed_size);
212     reorder_pixels(td->tmp, td->uncompressed_data, uncompressed_size);
213
214     return 0;
215 }
216
217 static int rle_uncompress(const uint8_t *src, int compressed_size,
218                           int uncompressed_size, EXRThreadData *td)
219 {
220     int8_t *d = (int8_t *)td->tmp;
221     const int8_t *s = (const int8_t *)src;
222     int ssize = compressed_size;
223     int dsize = uncompressed_size;
224     int8_t *dend = d + dsize;
225     int count;
226
227     while (ssize > 0) {
228         count = *s++;
229
230         if (count < 0) {
231             count = -count;
232
233             if ((dsize -= count    ) < 0 ||
234                 (ssize -= count + 1) < 0)
235                 return -1;
236
237             while (count--)
238                 *d++ = *s++;
239         } else {
240             count++;
241
242             if ((dsize -= count) < 0 ||
243                 (ssize -= 2    ) < 0)
244                 return -1;
245
246             while (count--)
247                 *d++ = *s;
248
249             s++;
250         }
251     }
252
253     if (dend != d)
254         return AVERROR_INVALIDDATA;
255
256     predictor(td->tmp, uncompressed_size);
257     reorder_pixels(td->tmp, td->uncompressed_data, uncompressed_size);
258
259     return 0;
260 }
261
262 static int decode_block(AVCodecContext *avctx, void *tdata,
263                         int jobnr, int threadnr)
264 {
265     EXRContext *s = avctx->priv_data;
266     AVFrame *const p = &s->picture;
267     EXRThreadData *td = &s->thread_data[threadnr];
268     const uint8_t *channel_buffer[4] = { 0 };
269     const uint8_t *buf = s->buf;
270     uint64_t line_offset, uncompressed_size;
271     uint32_t xdelta = s->xdelta;
272     uint16_t *ptr_x;
273     uint8_t *ptr;
274     int32_t data_size, line;
275     const uint8_t *src;
276     int axmax = (avctx->width - (s->xmax + 1)) * 2 * s->desc->nb_components;
277     int bxmin = s->xmin * 2 * s->desc->nb_components;
278     int ret, i, x, buf_size = s->buf_size;
279
280     line_offset = AV_RL64(s->table + jobnr * 8);
281     // Check if the buffer has the required bytes needed from the offset
282     if (line_offset > buf_size - 8)
283         return AVERROR_INVALIDDATA;
284
285     src = buf + line_offset + 8;
286     line = AV_RL32(src - 8);
287     if (line < s->ymin || line > s->ymax)
288         return AVERROR_INVALIDDATA;
289
290     data_size = AV_RL32(src - 4);
291     if (data_size <= 0 || data_size > buf_size)
292         return AVERROR_INVALIDDATA;
293
294     uncompressed_size = s->scan_line_size * FFMIN(s->scan_lines_per_block, s->ymax - line + 1);
295     if ((s->compr == EXR_RAW && (data_size != uncompressed_size ||
296                                  line_offset > buf_size - uncompressed_size)) ||
297         (s->compr != EXR_RAW && (data_size > uncompressed_size ||
298                                  line_offset > buf_size - data_size))) {
299         return AVERROR_INVALIDDATA;
300     }
301
302     if (data_size < uncompressed_size) {
303         av_fast_padded_malloc(&td->uncompressed_data, &td->uncompressed_size, uncompressed_size);
304         av_fast_padded_malloc(&td->tmp, &td->tmp_size, uncompressed_size);
305         if (!td->uncompressed_data || !td->tmp)
306             return AVERROR(ENOMEM);
307
308         switch (s->compr) {
309         case EXR_ZIP1:
310         case EXR_ZIP16:
311             ret = zip_uncompress(src, data_size, uncompressed_size, td);
312             break;
313         case EXR_RLE:
314             ret = rle_uncompress(src, data_size, uncompressed_size, td);
315         }
316
317         src = td->uncompressed_data;
318     }
319
320     channel_buffer[0] = src + xdelta * s->channel_offsets[0];
321     channel_buffer[1] = src + xdelta * s->channel_offsets[1];
322     channel_buffer[2] = src + xdelta * s->channel_offsets[2];
323     if (s->channel_offsets[3] >= 0)
324         channel_buffer[3] = src + xdelta * s->channel_offsets[3];
325
326     ptr = p->data[0] + line * p->linesize[0];
327     for (i = 0; i < s->scan_lines_per_block && line + i <= s->ymax; i++, ptr += p->linesize[0]) {
328         const uint8_t *r, *g, *b, *a;
329
330         r = channel_buffer[0];
331         g = channel_buffer[1];
332         b = channel_buffer[2];
333         if (channel_buffer[3])
334             a = channel_buffer[3];
335
336         ptr_x = (uint16_t *)ptr;
337
338         // Zero out the start if xmin is not 0
339         memset(ptr_x, 0, bxmin);
340         ptr_x += s->xmin * s->desc->nb_components;
341         if (s->bits_per_color_id == 2) {
342             // 32-bit
343             for (x = 0; x < xdelta; x++) {
344                 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&r));
345                 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&g));
346                 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&b));
347                 if (channel_buffer[3])
348                     *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a));
349             }
350         } else {
351             // 16-bit
352             for (x = 0; x < xdelta; x++) {
353                 *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&r));
354                 *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&g));
355                 *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&b));
356                 if (channel_buffer[3])
357                     *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&a));
358             }
359         }
360
361         // Zero out the end if xmax+1 is not w
362         memset(ptr_x, 0, axmax);
363
364         channel_buffer[0] += s->scan_line_size;
365         channel_buffer[1] += s->scan_line_size;
366         channel_buffer[2] += s->scan_line_size;
367         if (channel_buffer[3])
368             channel_buffer[3] += s->scan_line_size;
369     }
370
371     return 0;
372 }
373
374 static int decode_frame(AVCodecContext *avctx,
375                         void *data,
376                         int *got_frame,
377                         AVPacket *avpkt)
378 {
379     const uint8_t *buf      = avpkt->data;
380     unsigned int   buf_size = avpkt->size;
381     const uint8_t *buf_end  = buf + buf_size;
382
383     EXRContext *const s = avctx->priv_data;
384     AVFrame *picture  = data;
385     AVFrame *const p = &s->picture;
386     uint8_t *ptr;
387
388     int i, y, magic_number, version, flags, ret;
389     int w = 0;
390     int h = 0;
391
392     int out_line_size;
393     int scan_line_blocks;
394
395     unsigned int current_channel_offset = 0;
396
397     s->xmin = ~0;
398     s->xmax = ~0;
399     s->ymin = ~0;
400     s->ymax = ~0;
401     s->xdelta = ~0;
402     s->ydelta = ~0;
403     s->channel_offsets[0] = -1;
404     s->channel_offsets[1] = -1;
405     s->channel_offsets[2] = -1;
406     s->channel_offsets[3] = -1;
407     s->bits_per_color_id = -1;
408     s->compr = -1;
409     s->buf = buf;
410     s->buf_size = buf_size;
411
412     if (buf_size < 10) {
413         av_log(avctx, AV_LOG_ERROR, "Too short header to parse\n");
414         return AVERROR_INVALIDDATA;
415     }
416
417     magic_number = bytestream_get_le32(&buf);
418     if (magic_number != 20000630) { // As per documentation of OpenEXR it's supposed to be int 20000630 little-endian
419         av_log(avctx, AV_LOG_ERROR, "Wrong magic number %d\n", magic_number);
420         return AVERROR_INVALIDDATA;
421     }
422
423     version = bytestream_get_byte(&buf);
424     if (version != 2) {
425         av_log(avctx, AV_LOG_ERROR, "Unsupported version %d\n", version);
426         return AVERROR_PATCHWELCOME;
427     }
428
429     flags = bytestream_get_le24(&buf);
430     if (flags & 0x2) {
431         av_log(avctx, AV_LOG_ERROR, "Tile based images are not supported\n");
432         return AVERROR_PATCHWELCOME;
433     }
434
435     // Parse the header
436     while (buf < buf_end && buf[0]) {
437         unsigned int variable_buffer_data_size;
438         // Process the channel list
439         if (check_header_variable(avctx, &buf, buf_end, "channels", "chlist", 38, &variable_buffer_data_size) >= 0) {
440             const uint8_t *channel_list_end;
441             if (!variable_buffer_data_size)
442                 return AVERROR_INVALIDDATA;
443
444             channel_list_end = buf + variable_buffer_data_size;
445             while (channel_list_end - buf >= 19) {
446                 int current_bits_per_color_id = -1;
447                 int channel_index = -1;
448                 int xsub, ysub;
449
450                 if (!strcmp(buf, "R"))
451                     channel_index = 0;
452                 else if (!strcmp(buf, "G"))
453                     channel_index = 1;
454                 else if (!strcmp(buf, "B"))
455                     channel_index = 2;
456                 else if (!strcmp(buf, "A"))
457                     channel_index = 3;
458                 else
459                     av_log(avctx, AV_LOG_WARNING, "Unsupported channel %.256s\n", buf);
460
461                 while (bytestream_get_byte(&buf) && buf < channel_list_end)
462                     continue; /* skip */
463
464                 if (channel_list_end - * &buf < 4) {
465                     av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
466                     return AVERROR_INVALIDDATA;
467                 }
468
469                 current_bits_per_color_id = bytestream_get_le32(&buf);
470                 if (current_bits_per_color_id > 2) {
471                     av_log(avctx, AV_LOG_ERROR, "Unknown color format\n");
472                     return AVERROR_INVALIDDATA;
473                 }
474
475                 buf += 4;
476                 xsub = bytestream_get_le32(&buf);
477                 ysub = bytestream_get_le32(&buf);
478                 if (xsub != 1 || ysub != 1) {
479                     av_log(avctx, AV_LOG_ERROR, "Unsupported subsampling %dx%d\n", xsub, ysub);
480                     return AVERROR_PATCHWELCOME;
481                 }
482
483                 if (channel_index >= 0) {
484                     if (s->bits_per_color_id != -1 && s->bits_per_color_id != current_bits_per_color_id) {
485                         av_log(avctx, AV_LOG_ERROR, "RGB channels not of the same depth\n");
486                         return AVERROR_INVALIDDATA;
487                     }
488                     s->bits_per_color_id  = current_bits_per_color_id;
489                     s->channel_offsets[channel_index] = current_channel_offset;
490                 }
491
492                 current_channel_offset += 1 << current_bits_per_color_id;
493             }
494
495             /* Check if all channels are set with an offset or if the channels
496              * are causing an overflow  */
497
498             if (FFMIN3(s->channel_offsets[0],
499                        s->channel_offsets[1],
500                        s->channel_offsets[2]) < 0) {
501                 if (s->channel_offsets[0] < 0)
502                     av_log(avctx, AV_LOG_ERROR, "Missing red channel\n");
503                 if (s->channel_offsets[1] < 0)
504                     av_log(avctx, AV_LOG_ERROR, "Missing green channel\n");
505                 if (s->channel_offsets[2] < 0)
506                     av_log(avctx, AV_LOG_ERROR, "Missing blue channel\n");
507                 return AVERROR_INVALIDDATA;
508             }
509
510             buf = channel_list_end;
511             continue;
512         } else if (check_header_variable(avctx, &buf, buf_end, "dataWindow", "box2i", 31, &variable_buffer_data_size) >= 0) {
513             if (!variable_buffer_data_size)
514                 return AVERROR_INVALIDDATA;
515
516             s->xmin = AV_RL32(buf);
517             s->ymin = AV_RL32(buf + 4);
518             s->xmax = AV_RL32(buf + 8);
519             s->ymax = AV_RL32(buf + 12);
520             s->xdelta = (s->xmax - s->xmin) + 1;
521             s->ydelta = (s->ymax - s->ymin) + 1;
522
523             buf += variable_buffer_data_size;
524             continue;
525         } else if (check_header_variable(avctx, &buf, buf_end, "displayWindow", "box2i", 34, &variable_buffer_data_size) >= 0) {
526             if (!variable_buffer_data_size)
527                 return AVERROR_INVALIDDATA;
528
529             w = AV_RL32(buf + 8) + 1;
530             h = AV_RL32(buf + 12) + 1;
531
532             buf += variable_buffer_data_size;
533             continue;
534         } else if (check_header_variable(avctx, &buf, buf_end, "lineOrder", "lineOrder", 25, &variable_buffer_data_size) >= 0) {
535             if (!variable_buffer_data_size)
536                 return AVERROR_INVALIDDATA;
537
538             av_log(avctx, AV_LOG_DEBUG, "line order : %d\n", *buf);
539             if (*buf > 2) {
540                 av_log(avctx, AV_LOG_ERROR, "Unknown line order\n");
541                 return AVERROR_INVALIDDATA;
542             }
543
544             buf += variable_buffer_data_size;
545             continue;
546         } else if (check_header_variable(avctx, &buf, buf_end, "pixelAspectRatio", "float", 31, &variable_buffer_data_size) >= 0) {
547             if (!variable_buffer_data_size)
548                 return AVERROR_INVALIDDATA;
549
550             avctx->sample_aspect_ratio = av_d2q(av_int2float(AV_RL32(buf)), 255);
551
552             buf += variable_buffer_data_size;
553             continue;
554         } else if (check_header_variable(avctx, &buf, buf_end, "compression", "compression", 29, &variable_buffer_data_size) >= 0) {
555             if (!variable_buffer_data_size)
556                 return AVERROR_INVALIDDATA;
557
558             if (s->compr == -1)
559                 s->compr = *buf;
560             else
561                 av_log(avctx, AV_LOG_WARNING, "Found more than one compression attribute\n");
562
563             buf += variable_buffer_data_size;
564             continue;
565         }
566
567         // Check if there is enough bytes for a header
568         if (buf_end - buf <= 9) {
569             av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
570             return AVERROR_INVALIDDATA;
571         }
572
573         // Process unknown variables
574         for (i = 0; i < 2; i++) {
575             // Skip variable name/type
576             while (++buf < buf_end)
577                 if (buf[0] == 0x0)
578                     break;
579         }
580         buf++;
581         // Skip variable length
582         if (buf_end - buf >= 5) {
583             variable_buffer_data_size = get_header_variable_length(&buf, buf_end);
584             if (!variable_buffer_data_size) {
585                 av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
586                 return AVERROR_INVALIDDATA;
587             }
588             buf += variable_buffer_data_size;
589         }
590     }
591
592     if (s->compr == -1) {
593         av_log(avctx, AV_LOG_ERROR, "Missing compression attribute\n");
594         return AVERROR_INVALIDDATA;
595     }
596
597     if (buf >= buf_end) {
598         av_log(avctx, AV_LOG_ERROR, "Incomplete frame\n");
599         return AVERROR_INVALIDDATA;
600     }
601     buf++;
602
603     switch (s->bits_per_color_id) {
604     case 2: // 32-bit
605     case 1: // 16-bit
606         if (s->channel_offsets[3] >= 0)
607             avctx->pix_fmt = AV_PIX_FMT_RGBA64;
608         else
609             avctx->pix_fmt = AV_PIX_FMT_RGB48;
610         break;
611     // 8-bit
612     case 0:
613         av_log_missing_feature(avctx, "8-bit OpenEXR", 1);
614         return AVERROR_PATCHWELCOME;
615     default:
616         av_log(avctx, AV_LOG_ERROR, "Unknown color format : %d\n", s->bits_per_color_id);
617         return AVERROR_INVALIDDATA;
618     }
619
620     switch (s->compr) {
621     case EXR_RAW:
622     case EXR_RLE:
623     case EXR_ZIP1:
624         s->scan_lines_per_block = 1;
625         break;
626     case EXR_ZIP16:
627         s->scan_lines_per_block = 16;
628         break;
629     default:
630         av_log(avctx, AV_LOG_ERROR, "Compression type %d is not supported\n", s->compr);
631         return AVERROR_PATCHWELCOME;
632     }
633
634     if (s->picture.data[0])
635         ff_thread_release_buffer(avctx, &s->picture);
636     if (av_image_check_size(w, h, 0, avctx))
637         return AVERROR_INVALIDDATA;
638
639     // Verify the xmin, xmax, ymin, ymax and xdelta before setting the actual image size
640     if (s->xmin > s->xmax ||
641         s->ymin > s->ymax ||
642         s->xdelta != s->xmax - s->xmin + 1 ||
643         s->xmax >= w || s->ymax >= h) {
644         av_log(avctx, AV_LOG_ERROR, "Wrong sizing or missing size information\n");
645         return AVERROR_INVALIDDATA;
646     }
647
648     if (w != avctx->width || h != avctx->height) {
649         avcodec_set_dimensions(avctx, w, h);
650     }
651
652     s->desc = av_pix_fmt_desc_get(avctx->pix_fmt);
653     out_line_size = avctx->width * 2 * s->desc->nb_components;
654     s->scan_line_size = s->xdelta * current_channel_offset;
655     scan_line_blocks = (s->ydelta + s->scan_lines_per_block - 1) / s->scan_lines_per_block;
656
657     if (s->compr != EXR_RAW) {
658         int thread_data_size, prev_size;
659         EXRThreadData *m;
660
661         prev_size = s->thread_data_size;
662         if (av_size_mult(avctx->thread_count, sizeof(EXRThreadData), &thread_data_size))
663             return AVERROR(EINVAL);
664
665         m = av_fast_realloc(s->thread_data, &s->thread_data_size, thread_data_size);
666         if (!m)
667             return AVERROR(ENOMEM);
668         s->thread_data = m;
669         memset(s->thread_data + prev_size, 0, s->thread_data_size - prev_size);
670     }
671
672     if ((ret = ff_thread_get_buffer(avctx, p)) < 0) {
673         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
674         return ret;
675     }
676
677     if (buf_end - buf < scan_line_blocks * 8)
678         return AVERROR_INVALIDDATA;
679     s->table = buf;
680     ptr = p->data[0];
681
682     // Zero out the start if ymin is not 0
683     for (y = 0; y < s->ymin; y++) {
684         memset(ptr, 0, out_line_size);
685         ptr += p->linesize[0];
686     }
687
688     avctx->execute2(avctx, decode_block, s->thread_data, NULL, scan_line_blocks);
689
690     // Zero out the end if ymax+1 is not h
691     for (y = s->ymax + 1; y < avctx->height; y++) {
692         memset(ptr, 0, out_line_size);
693         ptr += p->linesize[0];
694     }
695
696     *picture   = s->picture;
697     *got_frame = 1;
698
699     return buf_size;
700 }
701
702 static av_cold int decode_init(AVCodecContext *avctx)
703 {
704     EXRContext *s = avctx->priv_data;
705
706     avcodec_get_frame_defaults(&s->picture);
707     avctx->coded_frame = &s->picture;
708
709     return 0;
710 }
711
712 static av_cold int decode_end(AVCodecContext *avctx)
713 {
714     EXRContext *s = avctx->priv_data;
715     int i;
716
717     if (s->picture.data[0])
718         avctx->release_buffer(avctx, &s->picture);
719
720     for (i = 0; i < s->thread_data_size / sizeof(EXRThreadData); i++) {
721         EXRThreadData *td = &s->thread_data[i];
722         av_free(td->uncompressed_data);
723         av_free(td->tmp);
724     }
725
726     av_freep(&s->thread_data);
727     s->thread_data_size = 0;
728
729     return 0;
730 }
731
732 AVCodec ff_exr_decoder = {
733     .name               = "exr",
734     .type               = AVMEDIA_TYPE_VIDEO,
735     .id                 = AV_CODEC_ID_EXR,
736     .priv_data_size     = sizeof(EXRContext),
737     .init               = decode_init,
738     .close              = decode_end,
739     .decode             = decode_frame,
740     .capabilities       = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS | CODEC_CAP_SLICE_THREADS,
741     .long_name          = NULL_IF_CONFIG_SMALL("OpenEXR image"),
742 };