Imported Debian version 2.4.3~trusty1
[deb_ffmpeg.git] / ffmpeg / libavcodec / exr.c
CommitLineData
2ba45a60
DM
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 * exr_half2float() is credited to Aaftab Munshi; Dan Ginsburg, Dave Shreiner.
32 *
33 */
34
35#include <zlib.h>
36#include <float.h>
37
38#include "libavutil/imgutils.h"
39#include "libavutil/opt.h"
40#include "libavutil/intfloat.h"
41
42#include "avcodec.h"
43#include "bytestream.h"
44#include "get_bits.h"
45#include "internal.h"
46#include "mathops.h"
47#include "thread.h"
48
49enum ExrCompr {
50 EXR_RAW,
51 EXR_RLE,
52 EXR_ZIP1,
53 EXR_ZIP16,
54 EXR_PIZ,
55 EXR_PXR24,
56 EXR_B44,
57 EXR_B44A,
58 EXR_UNKN,
59};
60
61enum ExrPixelType {
62 EXR_UINT,
63 EXR_HALF,
64 EXR_FLOAT,
65 EXR_UNKNOWN,
66};
67
68typedef struct EXRChannel {
69 int xsub, ysub;
70 enum ExrPixelType pixel_type;
71} EXRChannel;
72
73typedef struct EXRThreadData {
74 uint8_t *uncompressed_data;
75 int uncompressed_size;
76
77 uint8_t *tmp;
78 int tmp_size;
79
80 uint8_t *bitmap;
81 uint16_t *lut;
82} EXRThreadData;
83
84typedef struct EXRContext {
85 AVClass *class;
86 AVFrame *picture;
87 AVCodecContext *avctx;
88
89 enum ExrCompr compression;
90 enum ExrPixelType pixel_type;
91 int channel_offsets[4]; // 0 = red, 1 = green, 2 = blue and 3 = alpha
92 const AVPixFmtDescriptor *desc;
93
94 int w, h;
95 uint32_t xmax, xmin;
96 uint32_t ymax, ymin;
97 uint32_t xdelta, ydelta;
98 int ysize;
99
100 uint64_t scan_line_size;
101 int scan_lines_per_block;
102
103 GetByteContext gb;
104 const uint8_t *buf;
105 int buf_size;
106
107 EXRChannel *channels;
108 int nb_channels;
109
110 EXRThreadData *thread_data;
111
112 const char *layer;
113
114 float gamma;
115
116 uint16_t gamma_table[65536];
117
118} EXRContext;
119
120/* -15 stored using a single precision bias of 127 */
121#define HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP 0x38000000
122/* max exponent value in single precision that will be converted
123 * to Inf or Nan when stored as a half-float */
124#define HALF_FLOAT_MAX_BIASED_EXP_AS_SINGLE_FP_EXP 0x47800000
125
126/* 255 is the max exponent biased value */
127#define FLOAT_MAX_BIASED_EXP (0xFF << 23)
128
129#define HALF_FLOAT_MAX_BIASED_EXP (0x1F << 10)
130
131/*
132 * Convert a half float as a uint16_t into a full float.
133 *
134 * @param hf half float as uint16_t
135 *
136 * @return float value
137 */
138static union av_intfloat32 exr_half2float(uint16_t hf)
139{
140 unsigned int sign = (unsigned int)(hf >> 15);
141 unsigned int mantissa = (unsigned int)(hf & ((1 << 10) - 1));
142 unsigned int exp = (unsigned int)(hf & HALF_FLOAT_MAX_BIASED_EXP);
143 union av_intfloat32 f;
144
145 if (exp == HALF_FLOAT_MAX_BIASED_EXP) {
146 // we have a half-float NaN or Inf
147 // half-float NaNs will be converted to a single precision NaN
148 // half-float Infs will be converted to a single precision Inf
149 exp = FLOAT_MAX_BIASED_EXP;
150 if (mantissa)
151 mantissa = (1 << 23) - 1; // set all bits to indicate a NaN
152 } else if (exp == 0x0) {
153 // convert half-float zero/denorm to single precision value
154 if (mantissa) {
155 mantissa <<= 1;
156 exp = HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP;
157 // check for leading 1 in denorm mantissa
158 while ((mantissa & (1 << 10))) {
159 // for every leading 0, decrement single precision exponent by 1
160 // and shift half-float mantissa value to the left
161 mantissa <<= 1;
162 exp -= (1 << 23);
163 }
164 // clamp the mantissa to 10-bits
165 mantissa &= ((1 << 10) - 1);
166 // shift left to generate single-precision mantissa of 23-bits
167 mantissa <<= 13;
168 }
169 } else {
170 // shift left to generate single-precision mantissa of 23-bits
171 mantissa <<= 13;
172 // generate single precision biased exponent value
173 exp = (exp << 13) + HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP;
174 }
175
176 f.i = (sign << 31) | exp | mantissa;
177
178 return f;
179}
180
181
182/**
183 * Convert from 32-bit float as uint32_t to uint16_t.
184 *
185 * @param v 32-bit float
186 *
187 * @return normalized 16-bit unsigned int
188 */
189static inline uint16_t exr_flt2uint(uint32_t v)
190{
191 unsigned int exp = v >> 23;
192 // "HACK": negative values result in exp< 0, so clipping them to 0
193 // is also handled by this condition, avoids explicit check for sign bit.
194 if (exp <= 127 + 7 - 24) // we would shift out all bits anyway
195 return 0;
196 if (exp >= 127)
197 return 0xffff;
198 v &= 0x007fffff;
199 return (v + (1 << 23)) >> (127 + 7 - exp);
200}
201
202/**
203 * Convert from 16-bit float as uint16_t to uint16_t.
204 *
205 * @param v 16-bit float
206 *
207 * @return normalized 16-bit unsigned int
208 */
209static inline uint16_t exr_halflt2uint(uint16_t v)
210{
211 unsigned exp = 14 - (v >> 10);
212 if (exp >= 14) {
213 if (exp == 14)
214 return (v >> 9) & 1;
215 else
216 return (v & 0x8000) ? 0 : 0xffff;
217 }
218 v <<= 6;
219 return (v + (1 << 16)) >> (exp + 1);
220}
221
222static void predictor(uint8_t *src, int size)
223{
224 uint8_t *t = src + 1;
225 uint8_t *stop = src + size;
226
227 while (t < stop) {
228 int d = (int) t[-1] + (int) t[0] - 128;
229 t[0] = d;
230 ++t;
231 }
232}
233
234static void reorder_pixels(uint8_t *src, uint8_t *dst, int size)
235{
236 const int8_t *t1 = src;
237 const int8_t *t2 = src + (size + 1) / 2;
238 int8_t *s = dst;
239 int8_t *stop = s + size;
240
241 while (1) {
242 if (s < stop)
243 *(s++) = *(t1++);
244 else
245 break;
246
247 if (s < stop)
248 *(s++) = *(t2++);
249 else
250 break;
251 }
252}
253
254static int zip_uncompress(const uint8_t *src, int compressed_size,
255 int uncompressed_size, EXRThreadData *td)
256{
257 unsigned long dest_len = uncompressed_size;
258
259 if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK ||
260 dest_len != uncompressed_size)
261 return AVERROR_INVALIDDATA;
262
263 predictor(td->tmp, uncompressed_size);
264 reorder_pixels(td->tmp, td->uncompressed_data, uncompressed_size);
265
266 return 0;
267}
268
269static int rle_uncompress(const uint8_t *src, int compressed_size,
270 int uncompressed_size, EXRThreadData *td)
271{
272 uint8_t *d = td->tmp;
273 const int8_t *s = src;
274 int ssize = compressed_size;
275 int dsize = uncompressed_size;
276 uint8_t *dend = d + dsize;
277 int count;
278
279 while (ssize > 0) {
280 count = *s++;
281
282 if (count < 0) {
283 count = -count;
284
285 if ((dsize -= count) < 0 ||
286 (ssize -= count + 1) < 0)
287 return AVERROR_INVALIDDATA;
288
289 while (count--)
290 *d++ = *s++;
291 } else {
292 count++;
293
294 if ((dsize -= count) < 0 ||
295 (ssize -= 2) < 0)
296 return AVERROR_INVALIDDATA;
297
298 while (count--)
299 *d++ = *s;
300
301 s++;
302 }
303 }
304
305 if (dend != d)
306 return AVERROR_INVALIDDATA;
307
308 predictor(td->tmp, uncompressed_size);
309 reorder_pixels(td->tmp, td->uncompressed_data, uncompressed_size);
310
311 return 0;
312}
313
314#define USHORT_RANGE (1 << 16)
315#define BITMAP_SIZE (1 << 13)
316
317static uint16_t reverse_lut(const uint8_t *bitmap, uint16_t *lut)
318{
319 int i, k = 0;
320
321 for (i = 0; i < USHORT_RANGE; i++)
322 if ((i == 0) || (bitmap[i >> 3] & (1 << (i & 7))))
323 lut[k++] = i;
324
325 i = k - 1;
326
327 memset(lut + k, 0, (USHORT_RANGE - k) * 2);
328
329 return i;
330}
331
332static void apply_lut(const uint16_t *lut, uint16_t *dst, int dsize)
333{
334 int i;
335
336 for (i = 0; i < dsize; ++i)
337 dst[i] = lut[dst[i]];
338}
339
340#define HUF_ENCBITS 16 // literal (value) bit length
341#define HUF_DECBITS 14 // decoding bit size (>= 8)
342
343#define HUF_ENCSIZE ((1 << HUF_ENCBITS) + 1) // encoding table size
344#define HUF_DECSIZE (1 << HUF_DECBITS) // decoding table size
345#define HUF_DECMASK (HUF_DECSIZE - 1)
346
347typedef struct HufDec {
348 int len;
349 int lit;
350 int *p;
351} HufDec;
352
353static void huf_canonical_code_table(uint64_t *hcode)
354{
355 uint64_t c, n[59] = { 0 };
356 int i;
357
358 for (i = 0; i < HUF_ENCSIZE; ++i)
359 n[hcode[i]] += 1;
360
361 c = 0;
362 for (i = 58; i > 0; --i) {
363 uint64_t nc = ((c + n[i]) >> 1);
364 n[i] = c;
365 c = nc;
366 }
367
368 for (i = 0; i < HUF_ENCSIZE; ++i) {
369 int l = hcode[i];
370
371 if (l > 0)
372 hcode[i] = l | (n[l]++ << 6);
373 }
374}
375
376#define SHORT_ZEROCODE_RUN 59
377#define LONG_ZEROCODE_RUN 63
378#define SHORTEST_LONG_RUN (2 + LONG_ZEROCODE_RUN - SHORT_ZEROCODE_RUN)
379#define LONGEST_LONG_RUN (255 + SHORTEST_LONG_RUN)
380
381static int huf_unpack_enc_table(GetByteContext *gb,
382 int32_t im, int32_t iM, uint64_t *hcode)
383{
384 GetBitContext gbit;
385
386 init_get_bits8(&gbit, gb->buffer, bytestream2_get_bytes_left(gb));
387
388 for (; im <= iM; im++) {
389 uint64_t l = hcode[im] = get_bits(&gbit, 6);
390
391 if (l == LONG_ZEROCODE_RUN) {
392 int zerun = get_bits(&gbit, 8) + SHORTEST_LONG_RUN;
393
394 if (im + zerun > iM + 1)
395 return AVERROR_INVALIDDATA;
396
397 while (zerun--)
398 hcode[im++] = 0;
399
400 im--;
401 } else if (l >= SHORT_ZEROCODE_RUN) {
402 int zerun = l - SHORT_ZEROCODE_RUN + 2;
403
404 if (im + zerun > iM + 1)
405 return AVERROR_INVALIDDATA;
406
407 while (zerun--)
408 hcode[im++] = 0;
409
410 im--;
411 }
412 }
413
414 bytestream2_skip(gb, (get_bits_count(&gbit) + 7) / 8);
415 huf_canonical_code_table(hcode);
416
417 return 0;
418}
419
420static int huf_build_dec_table(const uint64_t *hcode, int im,
421 int iM, HufDec *hdecod)
422{
423 for (; im <= iM; im++) {
424 uint64_t c = hcode[im] >> 6;
425 int i, l = hcode[im] & 63;
426
427 if (c >> l)
428 return AVERROR_INVALIDDATA;
429
430 if (l > HUF_DECBITS) {
431 HufDec *pl = hdecod + (c >> (l - HUF_DECBITS));
432 if (pl->len)
433 return AVERROR_INVALIDDATA;
434
435 pl->lit++;
436
437 pl->p = av_realloc(pl->p, pl->lit * sizeof(int));
438 if (!pl->p)
439 return AVERROR(ENOMEM);
440
441 pl->p[pl->lit - 1] = im;
442 } else if (l) {
443 HufDec *pl = hdecod + (c << (HUF_DECBITS - l));
444
445 for (i = 1 << (HUF_DECBITS - l); i > 0; i--, pl++) {
446 if (pl->len || pl->p)
447 return AVERROR_INVALIDDATA;
448 pl->len = l;
449 pl->lit = im;
450 }
451 }
452 }
453
454 return 0;
455}
456
457#define get_char(c, lc, gb) \
458{ \
459 c = (c << 8) | bytestream2_get_byte(gb); \
460 lc += 8; \
461}
462
463#define get_code(po, rlc, c, lc, gb, out, oe) \
464{ \
465 if (po == rlc) { \
466 if (lc < 8) \
467 get_char(c, lc, gb); \
468 lc -= 8; \
469 \
470 cs = c >> lc; \
471 \
472 if (out + cs > oe) \
473 return AVERROR_INVALIDDATA; \
474 \
475 s = out[-1]; \
476 \
477 while (cs-- > 0) \
478 *out++ = s; \
479 } else if (out < oe) { \
480 *out++ = po; \
481 } else { \
482 return AVERROR_INVALIDDATA; \
483 } \
484}
485
486static int huf_decode(const uint64_t *hcode, const HufDec *hdecod,
487 GetByteContext *gb, int nbits,
488 int rlc, int no, uint16_t *out)
489{
490 uint64_t c = 0;
491 uint16_t *outb = out;
492 uint16_t *oe = out + no;
493 const uint8_t *ie = gb->buffer + (nbits + 7) / 8; // input byte size
494 uint8_t cs, s;
495 int i, lc = 0;
496
497 while (gb->buffer < ie) {
498 get_char(c, lc, gb);
499
500 while (lc >= HUF_DECBITS) {
501 const HufDec pl = hdecod[(c >> (lc - HUF_DECBITS)) & HUF_DECMASK];
502
503 if (pl.len) {
504 lc -= pl.len;
505 get_code(pl.lit, rlc, c, lc, gb, out, oe);
506 } else {
507 int j;
508
509 if (!pl.p)
510 return AVERROR_INVALIDDATA;
511
512 for (j = 0; j < pl.lit; j++) {
513 int l = hcode[pl.p[j]] & 63;
514
515 while (lc < l && bytestream2_get_bytes_left(gb) > 0)
516 get_char(c, lc, gb);
517
518 if (lc >= l) {
519 if ((hcode[pl.p[j]] >> 6) ==
520 ((c >> (lc - l)) & ((1LL << l) - 1))) {
521 lc -= l;
522 get_code(pl.p[j], rlc, c, lc, gb, out, oe);
523 break;
524 }
525 }
526 }
527
528 if (j == pl.lit)
529 return AVERROR_INVALIDDATA;
530 }
531 }
532 }
533
534 i = (8 - nbits) & 7;
535 c >>= i;
536 lc -= i;
537
538 while (lc > 0) {
539 const HufDec pl = hdecod[(c << (HUF_DECBITS - lc)) & HUF_DECMASK];
540
541 if (pl.len) {
542 lc -= pl.len;
543 get_code(pl.lit, rlc, c, lc, gb, out, oe);
544 } else {
545 return AVERROR_INVALIDDATA;
546 }
547 }
548
549 if (out - outb != no)
550 return AVERROR_INVALIDDATA;
551 return 0;
552}
553
554static int huf_uncompress(GetByteContext *gb,
555 uint16_t *dst, int dst_size)
556{
557 int32_t src_size, im, iM;
558 uint32_t nBits;
559 uint64_t *freq;
560 HufDec *hdec;
561 int ret, i;
562
563 src_size = bytestream2_get_le32(gb);
564 im = bytestream2_get_le32(gb);
565 iM = bytestream2_get_le32(gb);
566 bytestream2_skip(gb, 4);
567 nBits = bytestream2_get_le32(gb);
568 if (im < 0 || im >= HUF_ENCSIZE ||
569 iM < 0 || iM >= HUF_ENCSIZE ||
570 src_size < 0)
571 return AVERROR_INVALIDDATA;
572
573 bytestream2_skip(gb, 4);
574
575 freq = av_mallocz_array(HUF_ENCSIZE, sizeof(*freq));
576 hdec = av_mallocz_array(HUF_DECSIZE, sizeof(*hdec));
577 if (!freq || !hdec) {
578 ret = AVERROR(ENOMEM);
579 goto fail;
580 }
581
582 if ((ret = huf_unpack_enc_table(gb, im, iM, freq)) < 0)
583 goto fail;
584
585 if (nBits > 8 * bytestream2_get_bytes_left(gb)) {
586 ret = AVERROR_INVALIDDATA;
587 goto fail;
588 }
589
590 if ((ret = huf_build_dec_table(freq, im, iM, hdec)) < 0)
591 goto fail;
592 ret = huf_decode(freq, hdec, gb, nBits, iM, dst_size, dst);
593
594fail:
595 for (i = 0; i < HUF_DECSIZE; i++)
596 if (hdec)
597 av_freep(&hdec[i].p);
598
599 av_free(freq);
600 av_free(hdec);
601
602 return ret;
603}
604
605static inline void wdec14(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
606{
607 int16_t ls = l;
608 int16_t hs = h;
609 int hi = hs;
610 int ai = ls + (hi & 1) + (hi >> 1);
611 int16_t as = ai;
612 int16_t bs = ai - hi;
613
614 *a = as;
615 *b = bs;
616}
617
618#define NBITS 16
619#define A_OFFSET (1 << (NBITS - 1))
620#define MOD_MASK ((1 << NBITS) - 1)
621
622static inline void wdec16(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
623{
624 int m = l;
625 int d = h;
626 int bb = (m - (d >> 1)) & MOD_MASK;
627 int aa = (d + bb - A_OFFSET) & MOD_MASK;
628 *b = bb;
629 *a = aa;
630}
631
632static void wav_decode(uint16_t *in, int nx, int ox,
633 int ny, int oy, uint16_t mx)
634{
635 int w14 = (mx < (1 << 14));
636 int n = (nx > ny) ? ny : nx;
637 int p = 1;
638 int p2;
639
640 while (p <= n)
641 p <<= 1;
642
643 p >>= 1;
644 p2 = p;
645 p >>= 1;
646
647 while (p >= 1) {
648 uint16_t *py = in;
649 uint16_t *ey = in + oy * (ny - p2);
650 uint16_t i00, i01, i10, i11;
651 int oy1 = oy * p;
652 int oy2 = oy * p2;
653 int ox1 = ox * p;
654 int ox2 = ox * p2;
655
656 for (; py <= ey; py += oy2) {
657 uint16_t *px = py;
658 uint16_t *ex = py + ox * (nx - p2);
659
660 for (; px <= ex; px += ox2) {
661 uint16_t *p01 = px + ox1;
662 uint16_t *p10 = px + oy1;
663 uint16_t *p11 = p10 + ox1;
664
665 if (w14) {
666 wdec14(*px, *p10, &i00, &i10);
667 wdec14(*p01, *p11, &i01, &i11);
668 wdec14(i00, i01, px, p01);
669 wdec14(i10, i11, p10, p11);
670 } else {
671 wdec16(*px, *p10, &i00, &i10);
672 wdec16(*p01, *p11, &i01, &i11);
673 wdec16(i00, i01, px, p01);
674 wdec16(i10, i11, p10, p11);
675 }
676 }
677
678 if (nx & p) {
679 uint16_t *p10 = px + oy1;
680
681 if (w14)
682 wdec14(*px, *p10, &i00, p10);
683 else
684 wdec16(*px, *p10, &i00, p10);
685
686 *px = i00;
687 }
688 }
689
690 if (ny & p) {
691 uint16_t *px = py;
692 uint16_t *ex = py + ox * (nx - p2);
693
694 for (; px <= ex; px += ox2) {
695 uint16_t *p01 = px + ox1;
696
697 if (w14)
698 wdec14(*px, *p01, &i00, p01);
699 else
700 wdec16(*px, *p01, &i00, p01);
701
702 *px = i00;
703 }
704 }
705
706 p2 = p;
707 p >>= 1;
708 }
709}
710
711static int piz_uncompress(EXRContext *s, const uint8_t *src, int ssize,
712 int dsize, EXRThreadData *td)
713{
714 GetByteContext gb;
715 uint16_t maxval, min_non_zero, max_non_zero;
716 uint16_t *ptr;
717 uint16_t *tmp = (uint16_t *)td->tmp;
718 uint8_t *out;
719 int ret, i, j;
720
721 if (!td->bitmap)
722 td->bitmap = av_malloc(BITMAP_SIZE);
723 if (!td->lut)
724 td->lut = av_malloc(1 << 17);
725 if (!td->bitmap || !td->lut) {
726 av_freep(&td->bitmap);
727 av_freep(&td->lut);
728 return AVERROR(ENOMEM);
729 }
730
731 bytestream2_init(&gb, src, ssize);
732 min_non_zero = bytestream2_get_le16(&gb);
733 max_non_zero = bytestream2_get_le16(&gb);
734
735 if (max_non_zero >= BITMAP_SIZE)
736 return AVERROR_INVALIDDATA;
737
738 memset(td->bitmap, 0, FFMIN(min_non_zero, BITMAP_SIZE));
739 if (min_non_zero <= max_non_zero)
740 bytestream2_get_buffer(&gb, td->bitmap + min_non_zero,
741 max_non_zero - min_non_zero + 1);
742 memset(td->bitmap + max_non_zero, 0, BITMAP_SIZE - max_non_zero);
743
744 maxval = reverse_lut(td->bitmap, td->lut);
745
746 ret = huf_uncompress(&gb, tmp, dsize / sizeof(uint16_t));
747 if (ret)
748 return ret;
749
750 ptr = tmp;
751 for (i = 0; i < s->nb_channels; i++) {
752 EXRChannel *channel = &s->channels[i];
753 int size = channel->pixel_type;
754
755 for (j = 0; j < size; j++)
756 wav_decode(ptr + j, s->xdelta, size, s->ysize,
757 s->xdelta * size, maxval);
758 ptr += s->xdelta * s->ysize * size;
759 }
760
761 apply_lut(td->lut, tmp, dsize / sizeof(uint16_t));
762
763 out = td->uncompressed_data;
764 for (i = 0; i < s->ysize; i++)
765 for (j = 0; j < s->nb_channels; j++) {
766 uint16_t *in = tmp + j * s->xdelta * s->ysize + i * s->xdelta;
767 memcpy(out, in, s->xdelta * 2);
768 out += s->xdelta * 2;
769 }
770
771 return 0;
772}
773
774static int pxr24_uncompress(EXRContext *s, const uint8_t *src,
775 int compressed_size, int uncompressed_size,
776 EXRThreadData *td)
777{
778 unsigned long dest_len = uncompressed_size;
779 const uint8_t *in = td->tmp;
780 uint8_t *out;
781 int c, i, j;
782
783 if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK ||
784 dest_len != uncompressed_size)
785 return AVERROR_INVALIDDATA;
786
787 out = td->uncompressed_data;
788 for (i = 0; i < s->ysize; i++)
789 for (c = 0; c < s->nb_channels; c++) {
790 EXRChannel *channel = &s->channels[c];
791 const uint8_t *ptr[4];
792 uint32_t pixel = 0;
793
794 switch (channel->pixel_type) {
795 case EXR_FLOAT:
796 ptr[0] = in;
797 ptr[1] = ptr[0] + s->xdelta;
798 ptr[2] = ptr[1] + s->xdelta;
799 in = ptr[2] + s->xdelta;
800
801 for (j = 0; j < s->xdelta; ++j) {
802 uint32_t diff = (*(ptr[0]++) << 24) |
803 (*(ptr[1]++) << 16) |
804 (*(ptr[2]++) << 8);
805 pixel += diff;
806 bytestream_put_le32(&out, pixel);
807 }
808 break;
809 case EXR_HALF:
810 ptr[0] = in;
811 ptr[1] = ptr[0] + s->xdelta;
812 in = ptr[1] + s->xdelta;
813 for (j = 0; j < s->xdelta; j++) {
814 uint32_t diff = (*(ptr[0]++) << 8) | *(ptr[1]++);
815
816 pixel += diff;
817 bytestream_put_le16(&out, pixel);
818 }
819 break;
820 default:
821 return AVERROR_INVALIDDATA;
822 }
823 }
824
825 return 0;
826}
827
828static int decode_block(AVCodecContext *avctx, void *tdata,
829 int jobnr, int threadnr)
830{
831 EXRContext *s = avctx->priv_data;
832 AVFrame *const p = s->picture;
833 EXRThreadData *td = &s->thread_data[threadnr];
834 const uint8_t *channel_buffer[4] = { 0 };
835 const uint8_t *buf = s->buf;
836 uint64_t line_offset, uncompressed_size;
837 uint32_t xdelta = s->xdelta;
838 uint16_t *ptr_x;
839 uint8_t *ptr;
840 uint32_t data_size, line;
841 const uint8_t *src;
842 int axmax = (avctx->width - (s->xmax + 1)) * 2 * s->desc->nb_components;
843 int bxmin = s->xmin * 2 * s->desc->nb_components;
844 int i, x, buf_size = s->buf_size;
845 int ret;
846 float one_gamma = 1.0f / s->gamma;
847
848 line_offset = AV_RL64(s->gb.buffer + jobnr * 8);
849 // Check if the buffer has the required bytes needed from the offset
850 if (line_offset > buf_size - 8)
851 return AVERROR_INVALIDDATA;
852
853 src = buf + line_offset + 8;
854 line = AV_RL32(src - 8);
855 if (line < s->ymin || line > s->ymax)
856 return AVERROR_INVALIDDATA;
857
858 data_size = AV_RL32(src - 4);
859 if (data_size <= 0 || data_size > buf_size)
860 return AVERROR_INVALIDDATA;
861
862 s->ysize = FFMIN(s->scan_lines_per_block, s->ymax - line + 1);
863 uncompressed_size = s->scan_line_size * s->ysize;
864 if ((s->compression == EXR_RAW && (data_size != uncompressed_size ||
865 line_offset > buf_size - uncompressed_size)) ||
866 (s->compression != EXR_RAW && (data_size > uncompressed_size ||
867 line_offset > buf_size - data_size))) {
868 return AVERROR_INVALIDDATA;
869 }
870
871 if (data_size < uncompressed_size) {
872 av_fast_padded_malloc(&td->uncompressed_data,
873 &td->uncompressed_size, uncompressed_size);
874 av_fast_padded_malloc(&td->tmp, &td->tmp_size, uncompressed_size);
875 if (!td->uncompressed_data || !td->tmp)
876 return AVERROR(ENOMEM);
877
878 ret = AVERROR_INVALIDDATA;
879 switch (s->compression) {
880 case EXR_ZIP1:
881 case EXR_ZIP16:
882 ret = zip_uncompress(src, data_size, uncompressed_size, td);
883 break;
884 case EXR_PIZ:
885 ret = piz_uncompress(s, src, data_size, uncompressed_size, td);
886 break;
887 case EXR_PXR24:
888 ret = pxr24_uncompress(s, src, data_size, uncompressed_size, td);
889 break;
890 case EXR_RLE:
891 ret = rle_uncompress(src, data_size, uncompressed_size, td);
892 }
893 if (ret < 0) {
894 av_log(avctx, AV_LOG_ERROR, "decode_block() failed.\n");
895 return ret;
896 }
897 src = td->uncompressed_data;
898 }
899
900 channel_buffer[0] = src + xdelta * s->channel_offsets[0];
901 channel_buffer[1] = src + xdelta * s->channel_offsets[1];
902 channel_buffer[2] = src + xdelta * s->channel_offsets[2];
903 if (s->channel_offsets[3] >= 0)
904 channel_buffer[3] = src + xdelta * s->channel_offsets[3];
905
906 ptr = p->data[0] + line * p->linesize[0];
907 for (i = 0;
908 i < s->scan_lines_per_block && line + i <= s->ymax;
909 i++, ptr += p->linesize[0]) {
910 const uint8_t *r, *g, *b, *a;
911
912 r = channel_buffer[0];
913 g = channel_buffer[1];
914 b = channel_buffer[2];
915 if (channel_buffer[3])
916 a = channel_buffer[3];
917
918 ptr_x = (uint16_t *) ptr;
919
920 // Zero out the start if xmin is not 0
921 memset(ptr_x, 0, bxmin);
922 ptr_x += s->xmin * s->desc->nb_components;
923 if (s->pixel_type == EXR_FLOAT) {
924 // 32-bit
925 for (x = 0; x < xdelta; x++) {
926 union av_intfloat32 t;
927 t.i = bytestream_get_le32(&r);
928 if ( t.f > 0.0f ) /* avoid negative values */
929 t.f = powf(t.f, one_gamma);
930 *ptr_x++ = exr_flt2uint(t.i);
931
932 t.i = bytestream_get_le32(&g);
933 if ( t.f > 0.0f )
934 t.f = powf(t.f, one_gamma);
935 *ptr_x++ = exr_flt2uint(t.i);
936
937 t.i = bytestream_get_le32(&b);
938 if ( t.f > 0.0f )
939 t.f = powf(t.f, one_gamma);
940 *ptr_x++ = exr_flt2uint(t.i);
941 if (channel_buffer[3])
942 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a));
943 }
944 } else {
945 // 16-bit
946 for (x = 0; x < xdelta; x++) {
947 *ptr_x++ = s->gamma_table[bytestream_get_le16(&r)];
948 *ptr_x++ = s->gamma_table[bytestream_get_le16(&g)];
949 *ptr_x++ = s->gamma_table[bytestream_get_le16(&b)];
950 if (channel_buffer[3])
951 *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&a));
952 }
953 }
954
955 // Zero out the end if xmax+1 is not w
956 memset(ptr_x, 0, axmax);
957
958 channel_buffer[0] += s->scan_line_size;
959 channel_buffer[1] += s->scan_line_size;
960 channel_buffer[2] += s->scan_line_size;
961 if (channel_buffer[3])
962 channel_buffer[3] += s->scan_line_size;
963 }
964
965 return 0;
966}
967
968/**
969 * Check if the variable name corresponds to its data type.
970 *
971 * @param s the EXRContext
972 * @param value_name name of the variable to check
973 * @param value_type type of the variable to check
974 * @param minimum_length minimum length of the variable data
975 *
976 * @return bytes to read containing variable data
977 * -1 if variable is not found
978 * 0 if buffer ended prematurely
979 */
980static int check_header_variable(EXRContext *s,
981 const char *value_name,
982 const char *value_type,
983 unsigned int minimum_length)
984{
985 int var_size = -1;
986
987 if (bytestream2_get_bytes_left(&s->gb) >= minimum_length &&
988 !strcmp(s->gb.buffer, value_name)) {
989 // found value_name, jump to value_type (null terminated strings)
990 s->gb.buffer += strlen(value_name) + 1;
991 if (!strcmp(s->gb.buffer, value_type)) {
992 s->gb.buffer += strlen(value_type) + 1;
993 var_size = bytestream2_get_le32(&s->gb);
994 // don't go read past boundaries
995 if (var_size > bytestream2_get_bytes_left(&s->gb))
996 var_size = 0;
997 } else {
998 // value_type not found, reset the buffer
999 s->gb.buffer -= strlen(value_name) + 1;
1000 av_log(s->avctx, AV_LOG_WARNING,
1001 "Unknown data type %s for header variable %s.\n",
1002 value_type, value_name);
1003 }
1004 }
1005
1006 return var_size;
1007}
1008
1009static int decode_header(EXRContext *s)
1010{
1011 int current_channel_offset = 0;
1012 int magic_number, version, flags, i;
1013
1014 if (bytestream2_get_bytes_left(&s->gb) < 10) {
1015 av_log(s->avctx, AV_LOG_ERROR, "Header too short to parse.\n");
1016 return AVERROR_INVALIDDATA;
1017 }
1018
1019 magic_number = bytestream2_get_le32(&s->gb);
1020 if (magic_number != 20000630) {
1021 /* As per documentation of OpenEXR, it is supposed to be
1022 * int 20000630 little-endian */
1023 av_log(s->avctx, AV_LOG_ERROR, "Wrong magic number %d.\n", magic_number);
1024 return AVERROR_INVALIDDATA;
1025 }
1026
1027 version = bytestream2_get_byte(&s->gb);
1028 if (version != 2) {
1029 avpriv_report_missing_feature(s->avctx, "Version %d", version);
1030 return AVERROR_PATCHWELCOME;
1031 }
1032
1033 flags = bytestream2_get_le24(&s->gb);
1034 if (flags & 0x02) {
1035 avpriv_report_missing_feature(s->avctx, "Tile support");
1036 return AVERROR_PATCHWELCOME;
1037 }
1038
1039 // Parse the header
1040 while (bytestream2_get_bytes_left(&s->gb) > 0 && *s->gb.buffer) {
1041 int var_size;
1042 if ((var_size = check_header_variable(s, "channels",
1043 "chlist", 38)) >= 0) {
1044 GetByteContext ch_gb;
1045 if (!var_size)
1046 return AVERROR_INVALIDDATA;
1047
1048 bytestream2_init(&ch_gb, s->gb.buffer, var_size);
1049
1050 while (bytestream2_get_bytes_left(&ch_gb) >= 19) {
1051 EXRChannel *channel;
1052 enum ExrPixelType current_pixel_type;
1053 int channel_index = -1;
1054 int xsub, ysub;
1055
1056 if (strcmp(s->layer, "") != 0) {
1057 if (strncmp(ch_gb.buffer, s->layer, strlen(s->layer)) == 0) {
1058 ch_gb.buffer += strlen(s->layer);
1059 if (*ch_gb.buffer == '.')
1060 ch_gb.buffer++; /* skip dot if not given */
1061 av_log(s->avctx, AV_LOG_INFO,
1062 "Layer %s.%s matched.\n", s->layer, ch_gb.buffer);
1063 }
1064 }
1065
1066 if (!strcmp(ch_gb.buffer, "R") ||
1067 !strcmp(ch_gb.buffer, "X") ||
1068 !strcmp(ch_gb.buffer, "U"))
1069 channel_index = 0;
1070 else if (!strcmp(ch_gb.buffer, "G") ||
1071 !strcmp(ch_gb.buffer, "Y") ||
1072 !strcmp(ch_gb.buffer, "V"))
1073 channel_index = 1;
1074 else if (!strcmp(ch_gb.buffer, "B") ||
1075 !strcmp(ch_gb.buffer, "Z") ||
1076 !strcmp(ch_gb.buffer, "W"))
1077 channel_index = 2;
1078 else if (!strcmp(ch_gb.buffer, "A"))
1079 channel_index = 3;
1080 else
1081 av_log(s->avctx, AV_LOG_WARNING,
1082 "Unsupported channel %.256s.\n", ch_gb.buffer);
1083
1084 /* skip until you get a 0 */
1085 while (bytestream2_get_bytes_left(&ch_gb) > 0 &&
1086 bytestream2_get_byte(&ch_gb))
1087 continue;
1088
1089 if (bytestream2_get_bytes_left(&ch_gb) < 4) {
1090 av_log(s->avctx, AV_LOG_ERROR, "Incomplete header.\n");
1091 return AVERROR_INVALIDDATA;
1092 }
1093
1094 current_pixel_type = bytestream2_get_le32(&ch_gb);
1095 if (current_pixel_type >= EXR_UNKNOWN) {
1096 avpriv_report_missing_feature(s->avctx,
1097 "Pixel type %d.\n",
1098 current_pixel_type);
1099 return AVERROR_PATCHWELCOME;
1100 }
1101
1102 bytestream2_skip(&ch_gb, 4);
1103 xsub = bytestream2_get_le32(&ch_gb);
1104 ysub = bytestream2_get_le32(&ch_gb);
1105 if (xsub != 1 || ysub != 1) {
1106 avpriv_report_missing_feature(s->avctx,
1107 "Subsampling %dx%d",
1108 xsub, ysub);
1109 return AVERROR_PATCHWELCOME;
1110 }
1111
1112 if (channel_index >= 0) {
1113 if (s->pixel_type != EXR_UNKNOWN &&
1114 s->pixel_type != current_pixel_type) {
1115 av_log(s->avctx, AV_LOG_ERROR,
1116 "RGB channels not of the same depth.\n");
1117 return AVERROR_INVALIDDATA;
1118 }
1119 s->pixel_type = current_pixel_type;
1120 s->channel_offsets[channel_index] = current_channel_offset;
1121 }
1122
1123 s->channels = av_realloc(s->channels,
1124 ++s->nb_channels * sizeof(EXRChannel));
1125 if (!s->channels)
1126 return AVERROR(ENOMEM);
1127 channel = &s->channels[s->nb_channels - 1];
1128 channel->pixel_type = current_pixel_type;
1129 channel->xsub = xsub;
1130 channel->ysub = ysub;
1131
1132 current_channel_offset += 1 << current_pixel_type;
1133 }
1134
1135 /* Check if all channels are set with an offset or if the channels
1136 * are causing an overflow */
1137 if (FFMIN3(s->channel_offsets[0],
1138 s->channel_offsets[1],
1139 s->channel_offsets[2]) < 0) {
1140 if (s->channel_offsets[0] < 0)
1141 av_log(s->avctx, AV_LOG_ERROR, "Missing red channel.\n");
1142 if (s->channel_offsets[1] < 0)
1143 av_log(s->avctx, AV_LOG_ERROR, "Missing green channel.\n");
1144 if (s->channel_offsets[2] < 0)
1145 av_log(s->avctx, AV_LOG_ERROR, "Missing blue channel.\n");
1146 return AVERROR_INVALIDDATA;
1147 }
1148
1149 // skip one last byte and update main gb
1150 s->gb.buffer = ch_gb.buffer + 1;
1151 continue;
1152 } else if ((var_size = check_header_variable(s, "dataWindow", "box2i",
1153 31)) >= 0) {
1154 if (!var_size)
1155 return AVERROR_INVALIDDATA;
1156
1157 s->xmin = bytestream2_get_le32(&s->gb);
1158 s->ymin = bytestream2_get_le32(&s->gb);
1159 s->xmax = bytestream2_get_le32(&s->gb);
1160 s->ymax = bytestream2_get_le32(&s->gb);
1161 s->xdelta = (s->xmax - s->xmin) + 1;
1162 s->ydelta = (s->ymax - s->ymin) + 1;
1163
1164 continue;
1165 } else if ((var_size = check_header_variable(s, "displayWindow",
1166 "box2i", 34)) >= 0) {
1167 if (!var_size)
1168 return AVERROR_INVALIDDATA;
1169
1170 bytestream2_skip(&s->gb, 8);
1171 s->w = bytestream2_get_le32(&s->gb) + 1;
1172 s->h = bytestream2_get_le32(&s->gb) + 1;
1173
1174 continue;
1175 } else if ((var_size = check_header_variable(s, "lineOrder",
1176 "lineOrder", 25)) >= 0) {
1177 int line_order;
1178 if (!var_size)
1179 return AVERROR_INVALIDDATA;
1180
1181 line_order = bytestream2_get_byte(&s->gb);
1182 av_log(s->avctx, AV_LOG_DEBUG, "line order: %d.\n", line_order);
1183 if (line_order > 2) {
1184 av_log(s->avctx, AV_LOG_ERROR, "Unknown line order.\n");
1185 return AVERROR_INVALIDDATA;
1186 }
1187
1188 continue;
1189 } else if ((var_size = check_header_variable(s, "pixelAspectRatio",
1190 "float", 31)) >= 0) {
1191 if (!var_size)
1192 return AVERROR_INVALIDDATA;
1193
1194 ff_set_sar(s->avctx,
1195 av_d2q(av_int2float(bytestream2_get_le32(&s->gb)), 255));
1196
1197 continue;
1198 } else if ((var_size = check_header_variable(s, "compression",
1199 "compression", 29)) >= 0) {
1200 if (!var_size)
1201 return AVERROR_INVALIDDATA;
1202
1203 if (s->compression == EXR_UNKN)
1204 s->compression = bytestream2_get_byte(&s->gb);
1205 else
1206 av_log(s->avctx, AV_LOG_WARNING,
1207 "Found more than one compression attribute.\n");
1208
1209 continue;
1210 }
1211
1212 // Check if there are enough bytes for a header
1213 if (bytestream2_get_bytes_left(&s->gb) <= 9) {
1214 av_log(s->avctx, AV_LOG_ERROR, "Incomplete header\n");
1215 return AVERROR_INVALIDDATA;
1216 }
1217
1218 // Process unknown variables
1219 for (i = 0; i < 2; i++) // value_name and value_type
1220 while (bytestream2_get_byte(&s->gb) != 0);
1221
1222 // Skip variable length
1223 bytestream2_skip(&s->gb, bytestream2_get_le32(&s->gb));
1224 }
1225
1226 if (s->compression == EXR_UNKN) {
1227 av_log(s->avctx, AV_LOG_ERROR, "Missing compression attribute.\n");
1228 return AVERROR_INVALIDDATA;
1229 }
1230 s->scan_line_size = s->xdelta * current_channel_offset;
1231
1232 if (bytestream2_get_bytes_left(&s->gb) <= 0) {
1233 av_log(s->avctx, AV_LOG_ERROR, "Incomplete frame.\n");
1234 return AVERROR_INVALIDDATA;
1235 }
1236
1237 // aaand we are done
1238 bytestream2_skip(&s->gb, 1);
1239 return 0;
1240}
1241
1242static int decode_frame(AVCodecContext *avctx, void *data,
1243 int *got_frame, AVPacket *avpkt)
1244{
1245 EXRContext *s = avctx->priv_data;
1246 ThreadFrame frame = { .f = data };
1247 AVFrame *picture = data;
1248 uint8_t *ptr;
1249
1250 int y, ret;
1251 int out_line_size;
1252 int scan_line_blocks;
1253
1254 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
1255
1256 if ((ret = decode_header(s)) < 0)
1257 return ret;
1258
1259 switch (s->pixel_type) {
1260 case EXR_FLOAT:
1261 case EXR_HALF:
1262 if (s->channel_offsets[3] >= 0)
1263 avctx->pix_fmt = AV_PIX_FMT_RGBA64;
1264 else
1265 avctx->pix_fmt = AV_PIX_FMT_RGB48;
1266 break;
1267 case EXR_UINT:
1268 avpriv_request_sample(avctx, "32-bit unsigned int");
1269 return AVERROR_PATCHWELCOME;
1270 default:
1271 av_log(avctx, AV_LOG_ERROR, "Missing channel list.\n");
1272 return AVERROR_INVALIDDATA;
1273 }
1274
1275 switch (s->compression) {
1276 case EXR_RAW:
1277 case EXR_RLE:
1278 case EXR_ZIP1:
1279 s->scan_lines_per_block = 1;
1280 break;
1281 case EXR_PXR24:
1282 case EXR_ZIP16:
1283 s->scan_lines_per_block = 16;
1284 break;
1285 case EXR_PIZ:
1286 s->scan_lines_per_block = 32;
1287 break;
1288 default:
1289 avpriv_report_missing_feature(avctx, "Compression %d", s->compression);
1290 return AVERROR_PATCHWELCOME;
1291 }
1292
1293 /* Verify the xmin, xmax, ymin, ymax and xdelta before setting
1294 * the actual image size. */
1295 if (s->xmin > s->xmax ||
1296 s->ymin > s->ymax ||
1297 s->xdelta != s->xmax - s->xmin + 1 ||
1298 s->xmax >= s->w ||
1299 s->ymax >= s->h) {
1300 av_log(avctx, AV_LOG_ERROR, "Wrong or missing size information.\n");
1301 return AVERROR_INVALIDDATA;
1302 }
1303
1304 if ((ret = ff_set_dimensions(avctx, s->w, s->h)) < 0)
1305 return ret;
1306
1307 s->desc = av_pix_fmt_desc_get(avctx->pix_fmt);
1308 if (!s->desc)
1309 return AVERROR_INVALIDDATA;
1310 out_line_size = avctx->width * 2 * s->desc->nb_components;
1311 scan_line_blocks = (s->ydelta + s->scan_lines_per_block - 1) /
1312 s->scan_lines_per_block;
1313
1314 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
1315 return ret;
1316
1317 if (bytestream2_get_bytes_left(&s->gb) < scan_line_blocks * 8)
1318 return AVERROR_INVALIDDATA;
1319
1320 // save pointer we are going to use in decode_block
1321 s->buf = avpkt->data;
1322 s->buf_size = avpkt->size;
1323 ptr = picture->data[0];
1324
1325 // Zero out the start if ymin is not 0
1326 for (y = 0; y < s->ymin; y++) {
1327 memset(ptr, 0, out_line_size);
1328 ptr += picture->linesize[0];
1329 }
1330
1331 s->picture = picture;
1332 avctx->execute2(avctx, decode_block, s->thread_data, NULL, scan_line_blocks);
1333
1334 // Zero out the end if ymax+1 is not h
1335 for (y = s->ymax + 1; y < avctx->height; y++) {
1336 memset(ptr, 0, out_line_size);
1337 ptr += picture->linesize[0];
1338 }
1339
1340 picture->pict_type = AV_PICTURE_TYPE_I;
1341 *got_frame = 1;
1342
1343 return avpkt->size;
1344}
1345
1346static av_cold int decode_init(AVCodecContext *avctx)
1347{
1348 uint32_t i;
1349 union av_intfloat32 t;
1350 EXRContext *s = avctx->priv_data;
1351 float one_gamma = 1.0f / s->gamma;
1352
1353 s->avctx = avctx;
1354 s->xmin = ~0;
1355 s->xmax = ~0;
1356 s->ymin = ~0;
1357 s->ymax = ~0;
1358 s->xdelta = ~0;
1359 s->ydelta = ~0;
1360 s->channel_offsets[0] = -1;
1361 s->channel_offsets[1] = -1;
1362 s->channel_offsets[2] = -1;
1363 s->channel_offsets[3] = -1;
1364 s->pixel_type = EXR_UNKNOWN;
1365 s->compression = EXR_UNKN;
1366 s->nb_channels = 0;
1367 s->w = 0;
1368 s->h = 0;
1369
1370 if ( one_gamma > 0.9999f && one_gamma < 1.0001f ) {
1371 for ( i = 0; i < 65536; ++i ) {
1372 s->gamma_table[i] = exr_halflt2uint(i);
1373 }
1374 } else {
1375 for ( i = 0; i < 65536; ++i ) {
1376 t = exr_half2float(i);
1377 /* If negative value we reuse half value */
1378 if ( t.f <= 0.0f ) {
1379 s->gamma_table[i] = exr_halflt2uint(i);
1380 } else {
1381 t.f = powf(t.f, one_gamma);
1382 s->gamma_table[i] = exr_flt2uint(t.i);
1383 }
1384 }
1385 }
1386
1387 // allocate thread data, used for non EXR_RAW compreesion types
1388 s->thread_data = av_mallocz_array(avctx->thread_count, sizeof(EXRThreadData));
1389 if (!s->thread_data)
1390 return AVERROR_INVALIDDATA;
1391
1392 return 0;
1393}
1394
1395static int decode_init_thread_copy(AVCodecContext *avctx)
1396{ EXRContext *s = avctx->priv_data;
1397
1398 // allocate thread data, used for non EXR_RAW compreesion types
1399 s->thread_data = av_mallocz_array(avctx->thread_count, sizeof(EXRThreadData));
1400 if (!s->thread_data)
1401 return AVERROR_INVALIDDATA;
1402
1403 return 0;
1404}
1405
1406static av_cold int decode_end(AVCodecContext *avctx)
1407{
1408 EXRContext *s = avctx->priv_data;
1409 int i;
1410 for (i = 0; i < avctx->thread_count; i++) {
1411 EXRThreadData *td = &s->thread_data[i];
1412 av_freep(&td->uncompressed_data);
1413 av_freep(&td->tmp);
1414 av_freep(&td->bitmap);
1415 av_freep(&td->lut);
1416 }
1417
1418 av_freep(&s->thread_data);
1419 av_freep(&s->channels);
1420
1421 return 0;
1422}
1423
1424#define OFFSET(x) offsetof(EXRContext, x)
1425#define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1426static const AVOption options[] = {
1427 { "layer", "Set the decoding layer", OFFSET(layer),
1428 AV_OPT_TYPE_STRING, { .str = "" }, 0, 0, VD },
1429 { "gamma", "Set the float gamma value when decoding (experimental/unsupported)", OFFSET(gamma),
1430 AV_OPT_TYPE_FLOAT, { .dbl = 1.0f }, 0.001, FLT_MAX, VD },
1431 { NULL },
1432};
1433
1434static const AVClass exr_class = {
1435 .class_name = "EXR",
1436 .item_name = av_default_item_name,
1437 .option = options,
1438 .version = LIBAVUTIL_VERSION_INT,
1439};
1440
1441AVCodec ff_exr_decoder = {
1442 .name = "exr",
1443 .long_name = NULL_IF_CONFIG_SMALL("OpenEXR image"),
1444 .type = AVMEDIA_TYPE_VIDEO,
1445 .id = AV_CODEC_ID_EXR,
1446 .priv_data_size = sizeof(EXRContext),
1447 .init = decode_init,
1448 .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
1449 .close = decode_end,
1450 .decode = decode_frame,
1451 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS |
1452 CODEC_CAP_SLICE_THREADS,
1453 .priv_class = &exr_class,
1454};