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