]> git.sesse.net Git - ffmpeg/blob - libavcodec/exr.c
exr: fix scan_line_size calculation
[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 EXRContext {
52     AVFrame picture;
53     int compr;
54     int bits_per_color_id;
55     int8_t channel_offsets[4]; // 0 = red, 1 = green, 2 = blue and 3 = alpha
56
57     uint8_t *uncompressed_data;
58     int uncompressed_size;
59
60     uint8_t *tmp;
61     int tmp_size;
62 } EXRContext;
63
64 /**
65  * Converts from 32-bit float as uint32_t to uint16_t
66  *
67  * @param v 32-bit float
68  * @return normalized 16-bit unsigned int
69  */
70 static inline uint16_t exr_flt2uint(uint32_t v)
71 {
72     unsigned int exp = v >> 23;
73     // "HACK": negative values result in exp<  0, so clipping them to 0
74     // is also handled by this condition, avoids explicit check for sign bit.
75     if (exp<= 127 + 7 - 24) // we would shift out all bits anyway
76         return 0;
77     if (exp >= 127)
78         return 0xffff;
79     v &= 0x007fffff;
80     return (v + (1 << 23)) >> (127 + 7 - exp);
81 }
82
83 /**
84  * Converts from 16-bit float as uint16_t to uint16_t
85  *
86  * @param v 16-bit float
87  * @return normalized 16-bit unsigned int
88  */
89 static inline uint16_t exr_halflt2uint(uint16_t v)
90 {
91     unsigned exp = 14 - (v >> 10);
92     if (exp >= 14) {
93         if (exp == 14) return (v >> 9) & 1;
94         else           return (v & 0x8000) ? 0 : 0xffff;
95     }
96     v <<= 6;
97     return (v + (1 << 16)) >> (exp + 1);
98 }
99
100 /**
101  * Gets the size of the header variable
102  *
103  * @param **buf the current pointer location in the header where
104  * the variable data starts
105  * @param *buf_end pointer location of the end of the buffer
106  * @return size of variable data
107  */
108 static unsigned int get_header_variable_length(const uint8_t **buf,
109                                                const uint8_t *buf_end)
110 {
111     unsigned int variable_buffer_data_size = bytestream_get_le32(buf);
112     if (variable_buffer_data_size >= buf_end - *buf)
113         return 0;
114     return variable_buffer_data_size;
115 }
116
117 /**
118  * Checks if the variable name corresponds with it's data type
119  *
120  * @param *avctx the AVCodecContext
121  * @param **buf the current pointer location in the header where
122  * the variable name starts
123  * @param *buf_end pointer location of the end of the buffer
124  * @param *value_name name of the varible to check
125  * @param *value_type type of the varible to check
126  * @param minimum_length minimum length of the variable data
127  * @param variable_buffer_data_size variable length read from the header
128  * after it's checked
129  * @return negative if variable is invalid
130  */
131 static int check_header_variable(AVCodecContext *avctx,
132                                               const uint8_t **buf,
133                                               const uint8_t *buf_end,
134                                               const char *value_name,
135                                               const char *value_type,
136                                               unsigned int minimum_length,
137                                               unsigned int *variable_buffer_data_size)
138 {
139     if (buf_end - *buf >= minimum_length && !strcmp(*buf, value_name)) {
140         *buf += strlen(value_name)+1;
141         if (!strcmp(*buf, value_type)) {
142             *buf += strlen(value_type)+1;
143             *variable_buffer_data_size = get_header_variable_length(buf, buf_end);
144             if (!*variable_buffer_data_size)
145                 av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
146             if (*variable_buffer_data_size > buf_end - *buf)
147                 return -1;
148             return 1;
149         }
150         *buf -= strlen(value_name)+1;
151         av_log(avctx, AV_LOG_WARNING, "Unknown data type for header variable %s\n", value_name);
152     }
153     return -1;
154 }
155
156 static void predictor(uint8_t *src, int size)
157 {
158     uint8_t *t = src + 1;
159     uint8_t *stop = src + size;
160
161     while (t < stop) {
162         int d = (int)t[-1] + (int)t[0] - 128;
163         t[0] = d;
164         ++t;
165     }
166 }
167
168 static void reorder_pixels(uint8_t *src, uint8_t *dst, int size)
169 {
170     const int8_t *t1 = src;
171     const int8_t *t2 = src + (size + 1) / 2;
172     int8_t *s = dst;
173     int8_t *stop = s + size;
174
175     while (1) {
176         if (s < stop)
177             *(s++) = *(t1++);
178         else
179             break;
180
181         if (s < stop)
182             *(s++) = *(t2++);
183         else
184             break;
185     }
186 }
187
188 static int rle_uncompress(const uint8_t *src, int ssize, uint8_t *dst, int dsize)
189 {
190     int8_t *d = (int8_t *)dst;
191     int8_t *s = (int8_t *)src;
192     int8_t *dend = d + dsize;
193     int count;
194
195     while (ssize > 0) {
196         count = *s++;
197
198         if (count < 0) {
199             count = -count;
200
201             if ((dsize -= count    ) < 0 ||
202                 (ssize -= count + 1) < 0)
203                 return -1;
204
205             while (count--)
206                 *d++ = *s++;
207         } else {
208             count++;
209
210             if ((dsize -= count) < 0 ||
211                 (ssize -= 2    ) < 0)
212                 return -1;
213
214             while (count--)
215                 *d++ = *s;
216
217             s++;
218         }
219     }
220
221     return dend != d;
222 }
223
224 static int decode_frame(AVCodecContext *avctx,
225                         void *data,
226                         int *data_size,
227                         AVPacket *avpkt)
228 {
229     const uint8_t *buf      = avpkt->data;
230     unsigned int   buf_size = avpkt->size;
231     const uint8_t *buf_end  = buf + buf_size;
232
233     EXRContext *const s = avctx->priv_data;
234     AVFrame *picture  = data;
235     AVFrame *const p = &s->picture;
236     uint8_t *ptr;
237
238     int i, x, y, stride, magic_number, version_flag, ret;
239     int w = 0;
240     int h = 0;
241     unsigned int xmin   = ~0;
242     unsigned int xmax   = ~0;
243     unsigned int ymin   = ~0;
244     unsigned int ymax   = ~0;
245     unsigned int xdelta = ~0;
246
247     int out_line_size;
248     int bxmin, axmax;
249     int scan_lines_per_block;
250     unsigned long scan_line_size;
251     unsigned long uncompressed_size;
252
253     unsigned int current_channel_offset = 0;
254
255     s->channel_offsets[0] = -1;
256     s->channel_offsets[1] = -1;
257     s->channel_offsets[2] = -1;
258     s->channel_offsets[3] = -1;
259     s->bits_per_color_id = -1;
260
261     if (buf_size < 10) {
262         av_log(avctx, AV_LOG_ERROR, "Too short header to parse\n");
263         return AVERROR_INVALIDDATA;
264     }
265
266     magic_number = bytestream_get_le32(&buf);
267     if (magic_number != 20000630) { // As per documentation of OpenEXR it's supposed to be int 20000630 little-endian
268         av_log(avctx, AV_LOG_ERROR, "Wrong magic number %d\n", magic_number);
269         return AVERROR_INVALIDDATA;
270     }
271
272     version_flag = bytestream_get_le32(&buf);
273     if ((version_flag & 0x200) == 0x200) {
274         av_log(avctx, AV_LOG_ERROR, "Tile based images are not supported\n");
275         return AVERROR_PATCHWELCOME;
276     }
277
278     // Parse the header
279     while (buf < buf_end && buf[0]) {
280         unsigned int variable_buffer_data_size;
281         // Process the channel list
282         if (check_header_variable(avctx, &buf, buf_end, "channels", "chlist", 38, &variable_buffer_data_size) >= 0) {
283             const uint8_t *channel_list_end;
284             if (!variable_buffer_data_size)
285                 return AVERROR_INVALIDDATA;
286
287             channel_list_end = buf + variable_buffer_data_size;
288             while (channel_list_end - buf >= 19) {
289                 int current_bits_per_color_id = -1;
290                 int channel_index = -1;
291
292                 if (!strcmp(buf, "R"))
293                     channel_index = 0;
294                 else if (!strcmp(buf, "G"))
295                     channel_index = 1;
296                 else if (!strcmp(buf, "B"))
297                     channel_index = 2;
298                 else if (!strcmp(buf, "A"))
299                     channel_index = 3;
300                 else
301                     av_log(avctx, AV_LOG_WARNING, "Unsupported channel %.256s\n", buf);
302
303                 while (bytestream_get_byte(&buf) && buf < channel_list_end)
304                     continue; /* skip */
305
306                 if (channel_list_end - * &buf < 4) {
307                     av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
308                     return AVERROR_INVALIDDATA;
309                 }
310
311                 current_bits_per_color_id = bytestream_get_le32(&buf);
312                 if (current_bits_per_color_id > 2) {
313                     av_log(avctx, AV_LOG_ERROR, "Unknown color format\n");
314                     return AVERROR_INVALIDDATA;
315                 }
316
317                 if (channel_index >= 0) {
318                     if (s->bits_per_color_id != -1 && s->bits_per_color_id != current_bits_per_color_id) {
319                         av_log(avctx, AV_LOG_ERROR, "RGB channels not of the same depth\n");
320                         return AVERROR_INVALIDDATA;
321                     }
322                     s->bits_per_color_id  = current_bits_per_color_id;
323                     s->channel_offsets[channel_index] = current_channel_offset;
324                 }
325
326                 current_channel_offset += 1 << current_bits_per_color_id;
327                 buf += 12;
328             }
329
330             /* Check if all channels are set with an offset or if the channels
331              * are causing an overflow  */
332
333             if (FFMIN3(s->channel_offsets[0],
334                        s->channel_offsets[1],
335                        s->channel_offsets[2]) < 0) {
336                 if (s->channel_offsets[0] < 0)
337                     av_log(avctx, AV_LOG_ERROR, "Missing red channel\n");
338                 if (s->channel_offsets[1] < 0)
339                     av_log(avctx, AV_LOG_ERROR, "Missing green channel\n");
340                 if (s->channel_offsets[2] < 0)
341                     av_log(avctx, AV_LOG_ERROR, "Missing blue channel\n");
342                 return AVERROR_INVALIDDATA;
343             }
344
345             buf = channel_list_end;
346             continue;
347         }
348
349         // Process the dataWindow variable
350         if (check_header_variable(avctx, &buf, buf_end, "dataWindow", "box2i", 31, &variable_buffer_data_size) >= 0) {
351             if (!variable_buffer_data_size)
352                 return AVERROR_INVALIDDATA;
353
354             xmin = AV_RL32(buf);
355             ymin = AV_RL32(buf + 4);
356             xmax = AV_RL32(buf + 8);
357             ymax = AV_RL32(buf + 12);
358             xdelta = (xmax-xmin) + 1;
359
360             buf += variable_buffer_data_size;
361             continue;
362         }
363
364         // Process the displayWindow variable
365         if (check_header_variable(avctx, &buf, buf_end, "displayWindow", "box2i", 34, &variable_buffer_data_size) >= 0) {
366             if (!variable_buffer_data_size)
367                 return AVERROR_INVALIDDATA;
368
369             w = AV_RL32(buf + 8) + 1;
370             h = AV_RL32(buf + 12) + 1;
371
372             buf += variable_buffer_data_size;
373             continue;
374         }
375
376         // Process the lineOrder variable
377         if (check_header_variable(avctx, &buf, buf_end, "lineOrder", "lineOrder", 25, &variable_buffer_data_size) >= 0) {
378             if (!variable_buffer_data_size)
379                 return AVERROR_INVALIDDATA;
380
381             if (*buf) {
382                 av_log(avctx, AV_LOG_ERROR, "Doesn't support this line order : %d\n", *buf);
383                 return AVERROR_PATCHWELCOME;
384             }
385
386             buf += variable_buffer_data_size;
387             continue;
388         }
389
390         // Process the pixelAspectRatio variable
391         if (check_header_variable(avctx, &buf, buf_end, "pixelAspectRatio", "float", 31, &variable_buffer_data_size) >= 0) {
392             if (!variable_buffer_data_size)
393                 return AVERROR_INVALIDDATA;
394
395             avctx->sample_aspect_ratio = av_d2q(av_int2float(AV_RL32(buf)), 255);
396
397             buf += variable_buffer_data_size;
398             continue;
399         }
400
401         // Process the compression variable
402         if (check_header_variable(avctx, &buf, buf_end, "compression", "compression", 29, &variable_buffer_data_size) >= 0) {
403             if (!variable_buffer_data_size)
404                 return AVERROR_INVALIDDATA;
405
406             s->compr = *buf;
407             switch (s->compr) {
408             case EXR_RAW:
409             case EXR_RLE:
410             case EXR_ZIP1:
411             case EXR_ZIP16:
412                 break;
413             case EXR_PIZ:
414             case EXR_B44:
415             default:
416                 av_log(avctx, AV_LOG_ERROR, "Compression type %d is not supported\n", s->compr);
417                 return AVERROR_PATCHWELCOME;
418             }
419
420             buf += variable_buffer_data_size;
421             continue;
422         }
423
424         // Check if there is enough bytes for a header
425         if (buf_end - buf <= 9) {
426             av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
427             return AVERROR_INVALIDDATA;
428         }
429
430         // Process unknown variables
431         for (i = 0; i < 2; i++) {
432             // Skip variable name/type
433             while (++buf < buf_end)
434                 if (buf[0] == 0x0)
435                     break;
436         }
437         buf++;
438         // Skip variable length
439         if (buf_end - buf >= 5) {
440             variable_buffer_data_size = get_header_variable_length(&buf, buf_end);
441             if (!variable_buffer_data_size) {
442                 av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
443                 return AVERROR_INVALIDDATA;
444             }
445             buf += variable_buffer_data_size;
446         }
447     }
448
449     if (buf >= buf_end) {
450         av_log(avctx, AV_LOG_ERROR, "Incomplete frame\n");
451         return AVERROR_INVALIDDATA;
452     }
453     buf++;
454
455     switch (s->bits_per_color_id) {
456     case 2: // 32-bit
457     case 1: // 16-bit
458         if (s->channel_offsets[3] >= 0)
459             avctx->pix_fmt = PIX_FMT_RGBA64;
460         else
461             avctx->pix_fmt = PIX_FMT_RGB48;
462         break;
463     // 8-bit
464     case 0:
465         av_log_missing_feature(avctx, "8-bit OpenEXR", 1);
466         return AVERROR_PATCHWELCOME;
467     default:
468         av_log(avctx, AV_LOG_ERROR, "Unknown color format : %d\n", s->bits_per_color_id);
469         return AVERROR_INVALIDDATA;
470     }
471
472     switch (s->compr) {
473     case EXR_RAW:
474     case EXR_RLE:
475     case EXR_ZIP1:
476         scan_lines_per_block = 1;
477         break;
478     case EXR_ZIP16:
479         scan_lines_per_block = 16;
480         break;
481     }
482
483     if (s->picture.data[0])
484         ff_thread_release_buffer(avctx, &s->picture);
485     if (av_image_check_size(w, h, 0, avctx))
486         return AVERROR_INVALIDDATA;
487
488     // Verify the xmin, xmax, ymin, ymax and xdelta before setting the actual image size
489     if (xmin > xmax || ymin > ymax || xdelta != xmax - xmin + 1 || xmax >= w || ymax >= h) {
490         av_log(avctx, AV_LOG_ERROR, "Wrong sizing or missing size information\n");
491         return AVERROR_INVALIDDATA;
492     }
493
494     if (w != avctx->width || h != avctx->height) {
495         avcodec_set_dimensions(avctx, w, h);
496     }
497
498     bxmin = xmin * 2 * av_pix_fmt_descriptors[avctx->pix_fmt].nb_components;
499     axmax = (avctx->width - (xmax + 1)) * 2 * av_pix_fmt_descriptors[avctx->pix_fmt].nb_components;
500     out_line_size = avctx->width * 2 * av_pix_fmt_descriptors[avctx->pix_fmt].nb_components;
501     scan_line_size = xdelta * current_channel_offset;
502     uncompressed_size = scan_line_size * scan_lines_per_block;
503
504     if (s->compr != EXR_RAW) {
505         av_fast_padded_malloc(&s->uncompressed_data, &s->uncompressed_size, uncompressed_size);
506         av_fast_padded_malloc(&s->tmp, &s->tmp_size, uncompressed_size);
507         if (!s->uncompressed_data || !s->tmp)
508             return AVERROR(ENOMEM);
509     }
510
511     if ((ret = ff_thread_get_buffer(avctx, p)) < 0) {
512         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
513         return ret;
514     }
515
516     ptr    = p->data[0];
517     stride = p->linesize[0];
518
519     // Zero out the start if ymin is not 0
520     for (y = 0; y < ymin; y++) {
521         memset(ptr, 0, out_line_size);
522         ptr += stride;
523     }
524
525     // Process the actual scan line blocks
526     for (y = ymin; y <= ymax; y += scan_lines_per_block) {
527         uint16_t *ptr_x = (uint16_t *)ptr;
528         if (buf_end - buf > 8) {
529             /* Read the lineoffset from the line offset table and add 8 bytes
530                to skip the coordinates and data size fields */
531             const uint64_t line_offset = bytestream_get_le64(&buf) + 8;
532             int32_t data_size;
533
534             // Check if the buffer has the required bytes needed from the offset
535             if ((line_offset > buf_size) ||
536                 (s->compr == EXR_RAW && line_offset > avpkt->size - xdelta * current_channel_offset) ||
537                 (s->compr != EXR_RAW && line_offset > buf_size - (data_size = AV_RL32(avpkt->data + line_offset - 4)))) {
538                 // Line offset is probably wrong and not inside the buffer
539                 av_log(avctx, AV_LOG_WARNING, "Line offset for line %d is out of reach setting it to black\n", y);
540                 for (i = 0; i < scan_lines_per_block && y + i <= ymax; i++, ptr += stride) {
541                     ptr_x = (uint16_t *)ptr;
542                     memset(ptr_x, 0, out_line_size);
543                 }
544             } else {
545                 const uint8_t *red_channel_buffer, *green_channel_buffer, *blue_channel_buffer, *alpha_channel_buffer = 0;
546
547                 if ((s->compr == EXR_ZIP1 || s->compr == EXR_ZIP16) && data_size < uncompressed_size) {
548                     if (uncompress(s->tmp, &uncompressed_size, avpkt->data + line_offset, data_size) != Z_OK) {
549                         av_log(avctx, AV_LOG_ERROR, "error during zlib decompression\n");
550                         return AVERROR(EINVAL);
551                     }
552                 } else if (s->compr == EXR_RLE && data_size < uncompressed_size) {
553                     if (rle_uncompress(avpkt->data + line_offset, data_size, s->tmp, uncompressed_size)) {
554                         av_log(avctx, AV_LOG_ERROR, "error during rle decompression\n");
555                         return AVERROR(EINVAL);
556                     }
557                 }
558
559                 if (s->compr != EXR_RAW && data_size < uncompressed_size) {
560                     predictor(s->tmp, uncompressed_size);
561                     reorder_pixels(s->tmp, s->uncompressed_data, uncompressed_size);
562
563                     red_channel_buffer   = s->uncompressed_data + xdelta * s->channel_offsets[0];
564                     green_channel_buffer = s->uncompressed_data + xdelta * s->channel_offsets[1];
565                     blue_channel_buffer  = s->uncompressed_data + xdelta * s->channel_offsets[2];
566                     if (s->channel_offsets[3] >= 0)
567                         alpha_channel_buffer = s->uncompressed_data + xdelta * s->channel_offsets[3];
568                 } else {
569                     red_channel_buffer   = avpkt->data + line_offset + xdelta * s->channel_offsets[0];
570                     green_channel_buffer = avpkt->data + line_offset + xdelta * s->channel_offsets[1];
571                     blue_channel_buffer  = avpkt->data + line_offset + xdelta * s->channel_offsets[2];
572                     if (s->channel_offsets[3] >= 0)
573                         alpha_channel_buffer = avpkt->data + line_offset + xdelta * s->channel_offsets[3];
574                 }
575
576                 for (i = 0; i < scan_lines_per_block && y + i <= ymax; i++, ptr += stride) {
577                     const uint8_t *r, *g, *b, *a;
578
579                     r = red_channel_buffer;
580                     g = green_channel_buffer;
581                     b = blue_channel_buffer;
582                     if (alpha_channel_buffer)
583                         a = alpha_channel_buffer;
584
585                     ptr_x = (uint16_t *)ptr;
586
587                     // Zero out the start if xmin is not 0
588                     memset(ptr_x, 0, bxmin);
589                     ptr_x += xmin * av_pix_fmt_descriptors[avctx->pix_fmt].nb_components;
590                     if (s->bits_per_color_id == 2) {
591                         // 32-bit
592                         for (x = 0; x < xdelta; x++) {
593                             *ptr_x++ = exr_flt2uint(bytestream_get_le32(&r));
594                             *ptr_x++ = exr_flt2uint(bytestream_get_le32(&g));
595                             *ptr_x++ = exr_flt2uint(bytestream_get_le32(&b));
596                             if (alpha_channel_buffer)
597                                 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a));
598                         }
599                     } else {
600                         // 16-bit
601                         for (x = 0; x < xdelta; x++) {
602                             *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&r));
603                             *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&g));
604                             *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&b));
605                             if (alpha_channel_buffer)
606                                 *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&a));
607                         }
608                     }
609
610                     // Zero out the end if xmax+1 is not w
611                     memset(ptr_x, 0, axmax);
612
613                     red_channel_buffer   += scan_line_size;
614                     green_channel_buffer += scan_line_size;
615                     blue_channel_buffer  += scan_line_size;
616                     if (alpha_channel_buffer)
617                         alpha_channel_buffer += scan_line_size;
618                 }
619             }
620         }
621     }
622
623     // Zero out the end if ymax+1 is not h
624     for (y = ymax + 1; y < avctx->height; y++) {
625         memset(ptr, 0, out_line_size);
626         ptr += stride;
627     }
628
629     *picture   = s->picture;
630     *data_size = sizeof(AVPicture);
631
632     return buf_size;
633 }
634
635 static av_cold int decode_init(AVCodecContext *avctx)
636 {
637     EXRContext *s = avctx->priv_data;
638
639     avcodec_get_frame_defaults(&s->picture);
640     avctx->coded_frame = &s->picture;
641
642     return 0;
643 }
644
645 static av_cold int decode_end(AVCodecContext *avctx)
646 {
647     EXRContext *s = avctx->priv_data;
648
649     if (s->picture.data[0])
650         avctx->release_buffer(avctx, &s->picture);
651
652     av_freep(&s->uncompressed_data);
653     av_freep(&s->tmp);
654
655     return 0;
656 }
657
658 AVCodec ff_exr_decoder = {
659     .name               = "exr",
660     .type               = AVMEDIA_TYPE_VIDEO,
661     .id                 = CODEC_ID_EXR,
662     .priv_data_size     = sizeof(EXRContext),
663     .init               = decode_init,
664     .close              = decode_end,
665     .decode             = decode_frame,
666     .capabilities       = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
667     .long_name          = NULL_IF_CONFIG_SMALL("OpenEXR image"),
668 };