]> git.sesse.net Git - ffmpeg/blob - libavcodec/exr.c
53b8e5c7dbea2e8b41a9112a379556c01bb80458
[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     int 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     const int8_t *s = (const 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 *got_frame,
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     const AVPixFmtDescriptor *desc;
234     EXRContext *const s = avctx->priv_data;
235     AVFrame *picture  = data;
236     AVFrame *const p = &s->picture;
237     uint8_t *ptr;
238
239     int i, x, y, stride, magic_number, version, flags, ret;
240     int w = 0;
241     int h = 0;
242     unsigned int xmin   = ~0;
243     unsigned int xmax   = ~0;
244     unsigned int ymin   = ~0;
245     unsigned int ymax   = ~0;
246     unsigned int xdelta = ~0;
247
248     int out_line_size;
249     int bxmin, axmax;
250     int scan_lines_per_block;
251     unsigned long scan_line_size;
252     unsigned long uncompressed_size;
253
254     unsigned int current_channel_offset = 0;
255
256     s->channel_offsets[0] = -1;
257     s->channel_offsets[1] = -1;
258     s->channel_offsets[2] = -1;
259     s->channel_offsets[3] = -1;
260     s->bits_per_color_id = -1;
261     s->compr = -1;
262
263     if (buf_size < 10) {
264         av_log(avctx, AV_LOG_ERROR, "Too short header to parse\n");
265         return AVERROR_INVALIDDATA;
266     }
267
268     magic_number = bytestream_get_le32(&buf);
269     if (magic_number != 20000630) { // As per documentation of OpenEXR it's supposed to be int 20000630 little-endian
270         av_log(avctx, AV_LOG_ERROR, "Wrong magic number %d\n", magic_number);
271         return AVERROR_INVALIDDATA;
272     }
273
274     version = bytestream_get_byte(&buf);
275     if (version != 2) {
276         av_log(avctx, AV_LOG_ERROR, "Unsupported version %d\n", version);
277         return AVERROR_PATCHWELCOME;
278     }
279
280     flags = bytestream_get_le24(&buf);
281     if (flags & 0x2) {
282         av_log(avctx, AV_LOG_ERROR, "Tile based images are not supported\n");
283         return AVERROR_PATCHWELCOME;
284     }
285
286     // Parse the header
287     while (buf < buf_end && buf[0]) {
288         unsigned int variable_buffer_data_size;
289         // Process the channel list
290         if (check_header_variable(avctx, &buf, buf_end, "channels", "chlist", 38, &variable_buffer_data_size) >= 0) {
291             const uint8_t *channel_list_end;
292             if (!variable_buffer_data_size)
293                 return AVERROR_INVALIDDATA;
294
295             channel_list_end = buf + variable_buffer_data_size;
296             while (channel_list_end - buf >= 19) {
297                 int current_bits_per_color_id = -1;
298                 int channel_index = -1;
299
300                 if (!strcmp(buf, "R"))
301                     channel_index = 0;
302                 else if (!strcmp(buf, "G"))
303                     channel_index = 1;
304                 else if (!strcmp(buf, "B"))
305                     channel_index = 2;
306                 else if (!strcmp(buf, "A"))
307                     channel_index = 3;
308                 else
309                     av_log(avctx, AV_LOG_WARNING, "Unsupported channel %.256s\n", buf);
310
311                 while (bytestream_get_byte(&buf) && buf < channel_list_end)
312                     continue; /* skip */
313
314                 if (channel_list_end - * &buf < 4) {
315                     av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
316                     return AVERROR_INVALIDDATA;
317                 }
318
319                 current_bits_per_color_id = bytestream_get_le32(&buf);
320                 if (current_bits_per_color_id > 2) {
321                     av_log(avctx, AV_LOG_ERROR, "Unknown color format\n");
322                     return AVERROR_INVALIDDATA;
323                 }
324
325                 if (channel_index >= 0) {
326                     if (s->bits_per_color_id != -1 && s->bits_per_color_id != current_bits_per_color_id) {
327                         av_log(avctx, AV_LOG_ERROR, "RGB channels not of the same depth\n");
328                         return AVERROR_INVALIDDATA;
329                     }
330                     s->bits_per_color_id  = current_bits_per_color_id;
331                     s->channel_offsets[channel_index] = current_channel_offset;
332                 }
333
334                 current_channel_offset += 1 << current_bits_per_color_id;
335                 buf += 12;
336             }
337
338             /* Check if all channels are set with an offset or if the channels
339              * are causing an overflow  */
340
341             if (FFMIN3(s->channel_offsets[0],
342                        s->channel_offsets[1],
343                        s->channel_offsets[2]) < 0) {
344                 if (s->channel_offsets[0] < 0)
345                     av_log(avctx, AV_LOG_ERROR, "Missing red channel\n");
346                 if (s->channel_offsets[1] < 0)
347                     av_log(avctx, AV_LOG_ERROR, "Missing green channel\n");
348                 if (s->channel_offsets[2] < 0)
349                     av_log(avctx, AV_LOG_ERROR, "Missing blue channel\n");
350                 return AVERROR_INVALIDDATA;
351             }
352
353             buf = channel_list_end;
354             continue;
355         }
356
357         // Process the dataWindow variable
358         if (check_header_variable(avctx, &buf, buf_end, "dataWindow", "box2i", 31, &variable_buffer_data_size) >= 0) {
359             if (!variable_buffer_data_size)
360                 return AVERROR_INVALIDDATA;
361
362             xmin = AV_RL32(buf);
363             ymin = AV_RL32(buf + 4);
364             xmax = AV_RL32(buf + 8);
365             ymax = AV_RL32(buf + 12);
366             xdelta = (xmax-xmin) + 1;
367
368             buf += variable_buffer_data_size;
369             continue;
370         }
371
372         // Process the displayWindow variable
373         if (check_header_variable(avctx, &buf, buf_end, "displayWindow", "box2i", 34, &variable_buffer_data_size) >= 0) {
374             if (!variable_buffer_data_size)
375                 return AVERROR_INVALIDDATA;
376
377             w = AV_RL32(buf + 8) + 1;
378             h = AV_RL32(buf + 12) + 1;
379
380             buf += variable_buffer_data_size;
381             continue;
382         }
383
384         // Process the lineOrder variable
385         if (check_header_variable(avctx, &buf, buf_end, "lineOrder", "lineOrder", 25, &variable_buffer_data_size) >= 0) {
386             if (!variable_buffer_data_size)
387                 return AVERROR_INVALIDDATA;
388
389             if (*buf) {
390                 av_log(avctx, AV_LOG_ERROR, "Doesn't support this line order : %d\n", *buf);
391                 return AVERROR_PATCHWELCOME;
392             }
393
394             buf += variable_buffer_data_size;
395             continue;
396         }
397
398         // Process the pixelAspectRatio variable
399         if (check_header_variable(avctx, &buf, buf_end, "pixelAspectRatio", "float", 31, &variable_buffer_data_size) >= 0) {
400             if (!variable_buffer_data_size)
401                 return AVERROR_INVALIDDATA;
402
403             avctx->sample_aspect_ratio = av_d2q(av_int2float(AV_RL32(buf)), 255);
404
405             buf += variable_buffer_data_size;
406             continue;
407         }
408
409         // Process the compression variable
410         if (check_header_variable(avctx, &buf, buf_end, "compression", "compression", 29, &variable_buffer_data_size) >= 0) {
411             if (!variable_buffer_data_size)
412                 return AVERROR_INVALIDDATA;
413
414             if (s->compr == -1)
415                 s->compr = *buf;
416             else
417                 av_log(avctx, AV_LOG_WARNING, "Found more than one compression attribute\n");
418
419             buf += variable_buffer_data_size;
420             continue;
421         }
422
423         // Check if there is enough bytes for a header
424         if (buf_end - buf <= 9) {
425             av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
426             return AVERROR_INVALIDDATA;
427         }
428
429         // Process unknown variables
430         for (i = 0; i < 2; i++) {
431             // Skip variable name/type
432             while (++buf < buf_end)
433                 if (buf[0] == 0x0)
434                     break;
435         }
436         buf++;
437         // Skip variable length
438         if (buf_end - buf >= 5) {
439             variable_buffer_data_size = get_header_variable_length(&buf, buf_end);
440             if (!variable_buffer_data_size) {
441                 av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
442                 return AVERROR_INVALIDDATA;
443             }
444             buf += variable_buffer_data_size;
445         }
446     }
447
448     if (s->compr == -1) {
449         av_log(avctx, AV_LOG_ERROR, "Missing compression attribute\n");
450         return AVERROR_INVALIDDATA;
451     }
452
453     if (buf >= buf_end) {
454         av_log(avctx, AV_LOG_ERROR, "Incomplete frame\n");
455         return AVERROR_INVALIDDATA;
456     }
457     buf++;
458
459     switch (s->bits_per_color_id) {
460     case 2: // 32-bit
461     case 1: // 16-bit
462         if (s->channel_offsets[3] >= 0)
463             avctx->pix_fmt = AV_PIX_FMT_RGBA64;
464         else
465             avctx->pix_fmt = AV_PIX_FMT_RGB48;
466         break;
467     // 8-bit
468     case 0:
469         av_log_missing_feature(avctx, "8-bit OpenEXR", 1);
470         return AVERROR_PATCHWELCOME;
471     default:
472         av_log(avctx, AV_LOG_ERROR, "Unknown color format : %d\n", s->bits_per_color_id);
473         return AVERROR_INVALIDDATA;
474     }
475
476     switch (s->compr) {
477     case EXR_RAW:
478     case EXR_RLE:
479     case EXR_ZIP1:
480         scan_lines_per_block = 1;
481         break;
482     case EXR_ZIP16:
483         scan_lines_per_block = 16;
484         break;
485     default:
486         av_log(avctx, AV_LOG_ERROR, "Compression type %d is not supported\n", s->compr);
487         return AVERROR_PATCHWELCOME;
488     }
489
490     if (s->picture.data[0])
491         ff_thread_release_buffer(avctx, &s->picture);
492     if (av_image_check_size(w, h, 0, avctx))
493         return AVERROR_INVALIDDATA;
494
495     // Verify the xmin, xmax, ymin, ymax and xdelta before setting the actual image size
496     if (xmin > xmax || ymin > ymax || xdelta != xmax - xmin + 1 || xmax >= w || ymax >= h) {
497         av_log(avctx, AV_LOG_ERROR, "Wrong sizing or missing size information\n");
498         return AVERROR_INVALIDDATA;
499     }
500
501     if (w != avctx->width || h != avctx->height) {
502         avcodec_set_dimensions(avctx, w, h);
503     }
504
505     desc = av_pix_fmt_desc_get(avctx->pix_fmt);
506     bxmin = xmin * 2 * desc->nb_components;
507     axmax = (avctx->width - (xmax + 1)) * 2 * desc->nb_components;
508     out_line_size = avctx->width * 2 * desc->nb_components;
509     scan_line_size = xdelta * current_channel_offset;
510     uncompressed_size = scan_line_size * scan_lines_per_block;
511
512     if (s->compr != EXR_RAW) {
513         av_fast_padded_malloc(&s->uncompressed_data, &s->uncompressed_size, uncompressed_size);
514         av_fast_padded_malloc(&s->tmp, &s->tmp_size, uncompressed_size);
515         if (!s->uncompressed_data || !s->tmp)
516             return AVERROR(ENOMEM);
517     }
518
519     if ((ret = ff_thread_get_buffer(avctx, p)) < 0) {
520         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
521         return ret;
522     }
523
524     ptr    = p->data[0];
525     stride = p->linesize[0];
526
527     // Zero out the start if ymin is not 0
528     for (y = 0; y < ymin; y++) {
529         memset(ptr, 0, out_line_size);
530         ptr += stride;
531     }
532
533     // Process the actual scan line blocks
534     for (y = ymin; y <= ymax; y += scan_lines_per_block) {
535         uint16_t *ptr_x = (uint16_t *)ptr;
536         if (buf_end - buf > 8) {
537             /* Read the lineoffset from the line offset table and add 8 bytes
538                to skip the coordinates and data size fields */
539             const uint64_t line_offset = bytestream_get_le64(&buf) + 8;
540             int32_t data_size;
541
542             // Check if the buffer has the required bytes needed from the offset
543             if ((line_offset > buf_size) ||
544                 (s->compr == EXR_RAW && line_offset > avpkt->size - xdelta * current_channel_offset) ||
545                 (s->compr != EXR_RAW && line_offset > buf_size - (data_size = AV_RL32(avpkt->data + line_offset - 4)))) {
546                 // Line offset is probably wrong and not inside the buffer
547                 av_log(avctx, AV_LOG_WARNING, "Line offset for line %d is out of reach setting it to black\n", y);
548                 for (i = 0; i < scan_lines_per_block && y + i <= ymax; i++, ptr += stride) {
549                     ptr_x = (uint16_t *)ptr;
550                     memset(ptr_x, 0, out_line_size);
551                 }
552             } else {
553                 const uint8_t *red_channel_buffer, *green_channel_buffer, *blue_channel_buffer, *alpha_channel_buffer = 0;
554
555                 if (scan_lines_per_block > 1)
556                     uncompressed_size = scan_line_size * FFMIN(scan_lines_per_block, ymax - y + 1);
557                 if ((s->compr == EXR_ZIP1 || s->compr == EXR_ZIP16) && data_size < uncompressed_size) {
558                     unsigned long dest_len = uncompressed_size;
559
560                     if (uncompress(s->tmp, &dest_len, avpkt->data + line_offset, data_size) != Z_OK ||
561                         dest_len != uncompressed_size) {
562                         av_log(avctx, AV_LOG_ERROR, "error during zlib decompression\n");
563                         return AVERROR(EINVAL);
564                     }
565                 } else if (s->compr == EXR_RLE && data_size < uncompressed_size) {
566                     if (rle_uncompress(avpkt->data + line_offset, data_size, s->tmp, uncompressed_size)) {
567                         av_log(avctx, AV_LOG_ERROR, "error during rle decompression\n");
568                         return AVERROR(EINVAL);
569                     }
570                 }
571
572                 if (s->compr != EXR_RAW && data_size < uncompressed_size) {
573                     predictor(s->tmp, uncompressed_size);
574                     reorder_pixels(s->tmp, s->uncompressed_data, uncompressed_size);
575
576                     red_channel_buffer   = s->uncompressed_data + xdelta * s->channel_offsets[0];
577                     green_channel_buffer = s->uncompressed_data + xdelta * s->channel_offsets[1];
578                     blue_channel_buffer  = s->uncompressed_data + xdelta * s->channel_offsets[2];
579                     if (s->channel_offsets[3] >= 0)
580                         alpha_channel_buffer = s->uncompressed_data + xdelta * s->channel_offsets[3];
581                 } else {
582                     red_channel_buffer   = avpkt->data + line_offset + xdelta * s->channel_offsets[0];
583                     green_channel_buffer = avpkt->data + line_offset + xdelta * s->channel_offsets[1];
584                     blue_channel_buffer  = avpkt->data + line_offset + xdelta * s->channel_offsets[2];
585                     if (s->channel_offsets[3] >= 0)
586                         alpha_channel_buffer = avpkt->data + line_offset + xdelta * s->channel_offsets[3];
587                 }
588
589                 for (i = 0; i < scan_lines_per_block && y + i <= ymax; i++, ptr += stride) {
590                     const uint8_t *r, *g, *b, *a;
591
592                     r = red_channel_buffer;
593                     g = green_channel_buffer;
594                     b = blue_channel_buffer;
595                     if (alpha_channel_buffer)
596                         a = alpha_channel_buffer;
597
598                     ptr_x = (uint16_t *)ptr;
599
600                     // Zero out the start if xmin is not 0
601                     memset(ptr_x, 0, bxmin);
602                     ptr_x += xmin * desc->nb_components;
603                     if (s->bits_per_color_id == 2) {
604                         // 32-bit
605                         for (x = 0; x < xdelta; x++) {
606                             *ptr_x++ = exr_flt2uint(bytestream_get_le32(&r));
607                             *ptr_x++ = exr_flt2uint(bytestream_get_le32(&g));
608                             *ptr_x++ = exr_flt2uint(bytestream_get_le32(&b));
609                             if (alpha_channel_buffer)
610                                 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a));
611                         }
612                     } else {
613                         // 16-bit
614                         for (x = 0; x < xdelta; x++) {
615                             *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&r));
616                             *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&g));
617                             *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&b));
618                             if (alpha_channel_buffer)
619                                 *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&a));
620                         }
621                     }
622
623                     // Zero out the end if xmax+1 is not w
624                     memset(ptr_x, 0, axmax);
625
626                     red_channel_buffer   += scan_line_size;
627                     green_channel_buffer += scan_line_size;
628                     blue_channel_buffer  += scan_line_size;
629                     if (alpha_channel_buffer)
630                         alpha_channel_buffer += scan_line_size;
631                 }
632             }
633         }
634     }
635
636     // Zero out the end if ymax+1 is not h
637     for (y = ymax + 1; y < avctx->height; y++) {
638         memset(ptr, 0, out_line_size);
639         ptr += stride;
640     }
641
642     *picture   = s->picture;
643     *got_frame = 1;
644
645     return buf_size;
646 }
647
648 static av_cold int decode_init(AVCodecContext *avctx)
649 {
650     EXRContext *s = avctx->priv_data;
651
652     avcodec_get_frame_defaults(&s->picture);
653     avctx->coded_frame = &s->picture;
654
655     return 0;
656 }
657
658 static av_cold int decode_end(AVCodecContext *avctx)
659 {
660     EXRContext *s = avctx->priv_data;
661
662     if (s->picture.data[0])
663         avctx->release_buffer(avctx, &s->picture);
664
665     av_freep(&s->uncompressed_data);
666     av_freep(&s->tmp);
667
668     return 0;
669 }
670
671 AVCodec ff_exr_decoder = {
672     .name               = "exr",
673     .type               = AVMEDIA_TYPE_VIDEO,
674     .id                 = AV_CODEC_ID_EXR,
675     .priv_data_size     = sizeof(EXRContext),
676     .init               = decode_init,
677     .close              = decode_end,
678     .decode             = decode_frame,
679     .capabilities       = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
680     .long_name          = NULL_IF_CONFIG_SMALL("OpenEXR image"),
681 };