]> git.sesse.net Git - ffmpeg/blob - libavcodec/exr.c
Merge commit '215cdd35efd625ec28ef5846f1692b18f7c2c230'
[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 rle_uncompress(const uint8_t *src, int ssize, uint8_t *dst, int dsize)
203 {
204     int8_t *d = (int8_t *)dst;
205     const int8_t *s = (const int8_t *)src;
206     int8_t *dend = d + dsize;
207     int count;
208
209     while (ssize > 0) {
210         count = *s++;
211
212         if (count < 0) {
213             count = -count;
214
215             if ((dsize -= count    ) < 0 ||
216                 (ssize -= count + 1) < 0)
217                 return -1;
218
219             while (count--)
220                 *d++ = *s++;
221         } else {
222             count++;
223
224             if ((dsize -= count) < 0 ||
225                 (ssize -= 2    ) < 0)
226                 return -1;
227
228             while (count--)
229                 *d++ = *s;
230
231             s++;
232         }
233     }
234
235     return dend != d;
236 }
237
238 static int decode_block(AVCodecContext *avctx, void *tdata,
239                         int jobnr, int threadnr)
240 {
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;
248     uint16_t *ptr_x;
249     uint8_t *ptr;
250     int32_t data_size, line;
251     const uint8_t *src;
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;
255
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;
260
261     src = buf + line_offset + 8;
262     line = AV_RL32(src - 8);
263     if (line < s->ymin || line > s->ymax)
264         return AVERROR_INVALIDDATA;
265
266     data_size = AV_RL32(src - 4);
267     if (data_size <= 0 || data_size > buf_size)
268         return AVERROR_INVALIDDATA;
269
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;
276     }
277
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);
283     }
284     if ((s->compr == EXR_ZIP1 || s->compr == EXR_ZIP16) && data_size < uncompressed_size) {
285         unsigned long dest_len = uncompressed_size;
286
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);
291         }
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);
296         }
297     }
298
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;
303     }
304
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];
310
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;
314
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];
320
321         ptr_x = (uint16_t *)ptr;
322
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) {
327             // 32-bit
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));
334             }
335         } else {
336             // 16-bit
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));
343             }
344         }
345
346         // Zero out the end if xmax+1 is not w
347         memset(ptr_x, 0, axmax);
348
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;
354     }
355
356     return 0;
357 }
358
359 static int decode_frame(AVCodecContext *avctx,
360                         void *data,
361                         int *got_frame,
362                         AVPacket *avpkt)
363 {
364     const uint8_t *buf      = avpkt->data;
365     unsigned int   buf_size = avpkt->size;
366     const uint8_t *buf_end  = buf + buf_size;
367
368     EXRContext *const s = avctx->priv_data;
369     AVFrame *picture  = data;
370     AVFrame *const p = &s->picture;
371     uint8_t *ptr;
372
373     int i, y, magic_number, version, flags, ret;
374     int w = 0;
375     int h = 0;
376
377     int out_line_size;
378     int scan_line_blocks;
379
380     unsigned int current_channel_offset = 0;
381
382     s->xmin = ~0;
383     s->xmax = ~0;
384     s->ymin = ~0;
385     s->ymax = ~0;
386     s->xdelta = ~0;
387     s->ydelta = ~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;
393     s->compr = -1;
394     s->buf = buf;
395     s->buf_size = buf_size;
396
397     if (buf_size < 10) {
398         av_log(avctx, AV_LOG_ERROR, "Too short header to parse\n");
399         return AVERROR_INVALIDDATA;
400     }
401
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;
406     }
407
408     version = bytestream_get_byte(&buf);
409     if (version != 2) {
410         av_log(avctx, AV_LOG_ERROR, "Unsupported version %d\n", version);
411         return AVERROR_PATCHWELCOME;
412     }
413
414     flags = bytestream_get_le24(&buf);
415     if (flags & 0x2) {
416         av_log(avctx, AV_LOG_ERROR, "Tile based images are not supported\n");
417         return AVERROR_PATCHWELCOME;
418     }
419
420     // Parse the header
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;
428
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;
433                 int xsub, ysub;
434
435                 if (!strcmp(buf, "R"))
436                     channel_index = 0;
437                 else if (!strcmp(buf, "G"))
438                     channel_index = 1;
439                 else if (!strcmp(buf, "B"))
440                     channel_index = 2;
441                 else if (!strcmp(buf, "A"))
442                     channel_index = 3;
443                 else
444                     av_log(avctx, AV_LOG_WARNING, "Unsupported channel %.256s\n", buf);
445
446                 while (bytestream_get_byte(&buf) && buf < channel_list_end)
447                     continue; /* skip */
448
449                 if (channel_list_end - * &buf < 4) {
450                     av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
451                     return AVERROR_INVALIDDATA;
452                 }
453
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;
458                 }
459
460                 buf += 4;
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;
466                 }
467
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;
472                     }
473                     s->bits_per_color_id  = current_bits_per_color_id;
474                     s->channel_offsets[channel_index] = current_channel_offset;
475                 }
476
477                 current_channel_offset += 1 << current_bits_per_color_id;
478             }
479
480             /* Check if all channels are set with an offset or if the channels
481              * are causing an overflow  */
482
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;
493             }
494
495             buf = channel_list_end;
496             continue;
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;
500
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;
507
508             buf += variable_buffer_data_size;
509             continue;
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;
513
514             w = AV_RL32(buf + 8) + 1;
515             h = AV_RL32(buf + 12) + 1;
516
517             buf += variable_buffer_data_size;
518             continue;
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;
522
523             av_log(avctx, AV_LOG_DEBUG, "line order : %d\n", *buf);
524             if (*buf > 2) {
525                 av_log(avctx, AV_LOG_ERROR, "Unknown line order\n");
526                 return AVERROR_INVALIDDATA;
527             }
528
529             buf += variable_buffer_data_size;
530             continue;
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;
534
535             avctx->sample_aspect_ratio = av_d2q(av_int2float(AV_RL32(buf)), 255);
536
537             buf += variable_buffer_data_size;
538             continue;
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;
542
543             if (s->compr == -1)
544                 s->compr = *buf;
545             else
546                 av_log(avctx, AV_LOG_WARNING, "Found more than one compression attribute\n");
547
548             buf += variable_buffer_data_size;
549             continue;
550         }
551
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;
556         }
557
558         // Process unknown variables
559         for (i = 0; i < 2; i++) {
560             // Skip variable name/type
561             while (++buf < buf_end)
562                 if (buf[0] == 0x0)
563                     break;
564         }
565         buf++;
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;
572             }
573             buf += variable_buffer_data_size;
574         }
575     }
576
577     if (s->compr == -1) {
578         av_log(avctx, AV_LOG_ERROR, "Missing compression attribute\n");
579         return AVERROR_INVALIDDATA;
580     }
581
582     if (buf >= buf_end) {
583         av_log(avctx, AV_LOG_ERROR, "Incomplete frame\n");
584         return AVERROR_INVALIDDATA;
585     }
586     buf++;
587
588     switch (s->bits_per_color_id) {
589     case 2: // 32-bit
590     case 1: // 16-bit
591         if (s->channel_offsets[3] >= 0)
592             avctx->pix_fmt = AV_PIX_FMT_RGBA64;
593         else
594             avctx->pix_fmt = AV_PIX_FMT_RGB48;
595         break;
596     // 8-bit
597     case 0:
598         av_log_missing_feature(avctx, "8-bit OpenEXR", 1);
599         return AVERROR_PATCHWELCOME;
600     default:
601         av_log(avctx, AV_LOG_ERROR, "Unknown color format : %d\n", s->bits_per_color_id);
602         return AVERROR_INVALIDDATA;
603     }
604
605     switch (s->compr) {
606     case EXR_RAW:
607     case EXR_RLE:
608     case EXR_ZIP1:
609         s->scan_lines_per_block = 1;
610         break;
611     case EXR_ZIP16:
612         s->scan_lines_per_block = 16;
613         break;
614     default:
615         av_log(avctx, AV_LOG_ERROR, "Compression type %d is not supported\n", s->compr);
616         return AVERROR_PATCHWELCOME;
617     }
618
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;
623
624     // Verify the xmin, xmax, ymin, ymax and xdelta before setting the actual image size
625     if (s->xmin > s->xmax ||
626         s->ymin > s->ymax ||
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;
631     }
632
633     if (w != avctx->width || h != avctx->height) {
634         avcodec_set_dimensions(avctx, w, h);
635     }
636
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;
641
642     if (s->compr != EXR_RAW) {
643         int thread_data_size, prev_size;
644         EXRThreadData *m;
645
646         prev_size = s->thread_data_size;
647         if (av_size_mult(avctx->thread_count, sizeof(EXRThreadData), &thread_data_size))
648             return AVERROR(EINVAL);
649
650         m = av_fast_realloc(s->thread_data, &s->thread_data_size, thread_data_size);
651         if (!m)
652             return AVERROR(ENOMEM);
653         s->thread_data = m;
654         memset(s->thread_data + prev_size, 0, s->thread_data_size - prev_size);
655     }
656
657     if ((ret = ff_thread_get_buffer(avctx, p)) < 0) {
658         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
659         return ret;
660     }
661
662     if (buf_end - buf < scan_line_blocks * 8)
663         return AVERROR_INVALIDDATA;
664     s->table = buf;
665     ptr = p->data[0];
666
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];
671     }
672
673     avctx->execute2(avctx, decode_block, s->thread_data, NULL, scan_line_blocks);
674
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];
679     }
680
681     *picture   = s->picture;
682     *got_frame = 1;
683
684     return buf_size;
685 }
686
687 static av_cold int decode_init(AVCodecContext *avctx)
688 {
689     EXRContext *s = avctx->priv_data;
690
691     avcodec_get_frame_defaults(&s->picture);
692     avctx->coded_frame = &s->picture;
693
694     return 0;
695 }
696
697 static av_cold int decode_end(AVCodecContext *avctx)
698 {
699     EXRContext *s = avctx->priv_data;
700     int i;
701
702     if (s->picture.data[0])
703         avctx->release_buffer(avctx, &s->picture);
704
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);
708         av_free(td->tmp);
709     }
710
711     av_freep(&s->thread_data);
712     s->thread_data_size = 0;
713
714     return 0;
715 }
716
717 AVCodec ff_exr_decoder = {
718     .name               = "exr",
719     .type               = AVMEDIA_TYPE_VIDEO,
720     .id                 = AV_CODEC_ID_EXR,
721     .priv_data_size     = sizeof(EXRContext),
722     .init               = decode_init,
723     .close              = decode_end,
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"),
727 };