FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/exr.c
Date: 2025-03-08 20:38:41
Exec Total Coverage
Lines: 856 1280 66.9%
Functions: 23 31 74.2%
Branches: 460 769 59.8%

Line Branch Exec Source
1 /*
2 * OpenEXR (.exr) image decoder
3 * Copyright (c) 2006 Industrial Light & Magic, a division of Lucas Digital Ltd. LLC
4 * Copyright (c) 2009 Jimmy Christensen
5 *
6 * B44/B44A, Tile, UINT32 added by Jokyo Images support by CNC - French National Center for Cinema
7 *
8 * This file is part of FFmpeg.
9 *
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25 /**
26 * @file
27 * OpenEXR decoder
28 * @author Jimmy Christensen
29 *
30 * For more information on the OpenEXR format, visit:
31 * http://openexr.com/
32 */
33
34 #include <float.h>
35 #include <zlib.h>
36
37 #include "libavutil/avassert.h"
38 #include "libavutil/common.h"
39 #include "libavutil/csp.h"
40 #include "libavutil/imgutils.h"
41 #include "libavutil/intfloat.h"
42 #include "libavutil/avstring.h"
43 #include "libavutil/mem.h"
44 #include "libavutil/opt.h"
45 #include "libavutil/half2float.h"
46
47 #include "avcodec.h"
48 #include "bytestream.h"
49
50 #if HAVE_BIGENDIAN
51 #include "bswapdsp.h"
52 #endif
53
54 #include "codec_internal.h"
55 #include "decode.h"
56 #include "exrdsp.h"
57 #include "get_bits.h"
58 #include "mathops.h"
59 #include "thread.h"
60
61 enum ExrCompr {
62 EXR_RAW,
63 EXR_RLE,
64 EXR_ZIP1,
65 EXR_ZIP16,
66 EXR_PIZ,
67 EXR_PXR24,
68 EXR_B44,
69 EXR_B44A,
70 EXR_DWAA,
71 EXR_DWAB,
72 EXR_UNKN,
73 };
74
75 enum ExrPixelType {
76 EXR_UINT,
77 EXR_HALF,
78 EXR_FLOAT,
79 EXR_UNKNOWN,
80 };
81
82 enum ExrTileLevelMode {
83 EXR_TILE_LEVEL_ONE,
84 EXR_TILE_LEVEL_MIPMAP,
85 EXR_TILE_LEVEL_RIPMAP,
86 EXR_TILE_LEVEL_UNKNOWN,
87 };
88
89 enum ExrTileLevelRound {
90 EXR_TILE_ROUND_UP,
91 EXR_TILE_ROUND_DOWN,
92 EXR_TILE_ROUND_UNKNOWN,
93 };
94
95 typedef struct HuffEntry {
96 uint8_t len;
97 uint16_t sym;
98 uint32_t code;
99 } HuffEntry;
100
101 typedef struct EXRChannel {
102 int xsub, ysub;
103 enum ExrPixelType pixel_type;
104 } EXRChannel;
105
106 typedef struct EXRTileAttribute {
107 int32_t xSize;
108 int32_t ySize;
109 enum ExrTileLevelMode level_mode;
110 enum ExrTileLevelRound level_round;
111 } EXRTileAttribute;
112
113 typedef struct EXRThreadData {
114 uint8_t *uncompressed_data;
115 int uncompressed_size;
116
117 uint8_t *tmp;
118 int tmp_size;
119
120 uint8_t *bitmap;
121 uint16_t *lut;
122
123 uint8_t *ac_data;
124 unsigned ac_size;
125
126 uint8_t *dc_data;
127 unsigned dc_size;
128
129 uint8_t *rle_data;
130 unsigned rle_size;
131
132 uint8_t *rle_raw_data;
133 unsigned rle_raw_size;
134
135 float block[3][64];
136
137 int ysize, xsize;
138
139 int channel_line_size;
140
141 int run_sym;
142 HuffEntry *he;
143 uint64_t *freq;
144 VLC vlc;
145 } EXRThreadData;
146
147 typedef struct EXRContext {
148 AVClass *class;
149 AVFrame *picture;
150 AVCodecContext *avctx;
151 ExrDSPContext dsp;
152
153 #if HAVE_BIGENDIAN
154 BswapDSPContext bbdsp;
155 #endif
156
157 enum ExrCompr compression;
158 enum ExrPixelType pixel_type;
159 int channel_offsets[4]; // 0 = red, 1 = green, 2 = blue and 3 = alpha
160 const AVPixFmtDescriptor *desc;
161
162 int w, h;
163 uint32_t sar;
164 int32_t xmax, xmin;
165 int32_t ymax, ymin;
166 uint32_t xdelta, ydelta;
167
168 int scan_lines_per_block;
169
170 EXRTileAttribute tile_attr; /* header data attribute of tile */
171 int is_tile; /* 0 if scanline, 1 if tile */
172 int is_multipart;
173 int current_part;
174
175 int is_luma;/* 1 if there is an Y plane */
176
177 GetByteContext gb;
178 const uint8_t *buf;
179 int buf_size;
180
181 EXRChannel *channels;
182 int nb_channels;
183 int current_channel_offset;
184 uint32_t chunk_count;
185
186 EXRThreadData *thread_data;
187
188 const char *layer;
189 int selected_part;
190
191 enum AVColorTransferCharacteristic apply_trc_type;
192 float gamma;
193
194 uint8_t *offset_table;
195
196 Half2FloatTables h2f_tables;
197 } EXRContext;
198
199 12853 static int zip_uncompress(const EXRContext *s, const uint8_t *src, int compressed_size,
200 int uncompressed_size, EXRThreadData *td)
201 {
202 12853 unsigned long dest_len = uncompressed_size;
203
204
1/2
✓ Branch 1 taken 12853 times.
✗ Branch 2 not taken.
12853 if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK ||
205
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12853 times.
12853 dest_len != uncompressed_size)
206 return AVERROR_INVALIDDATA;
207
208 av_assert1(uncompressed_size % 2 == 0);
209
210 12853 s->dsp.predictor(td->tmp, uncompressed_size);
211 12853 s->dsp.reorder_pixels(td->uncompressed_data, td->tmp, uncompressed_size);
212
213 12853 return 0;
214 }
215
216 4848 static int rle(uint8_t *dst, const uint8_t *src,
217 int compressed_size, int uncompressed_size)
218 {
219 4848 uint8_t *d = dst;
220 4848 const int8_t *s = src;
221 4848 int ssize = compressed_size;
222 4848 int dsize = uncompressed_size;
223 4848 uint8_t *dend = d + dsize;
224 int count;
225
226
2/2
✓ Branch 0 taken 671923 times.
✓ Branch 1 taken 4848 times.
676771 while (ssize > 0) {
227 671923 count = *s++;
228
229
2/2
✓ Branch 0 taken 501696 times.
✓ Branch 1 taken 170227 times.
671923 if (count < 0) {
230 501696 count = -count;
231
232
1/2
✓ Branch 0 taken 501696 times.
✗ Branch 1 not taken.
501696 if ((dsize -= count) < 0 ||
233
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 501696 times.
501696 (ssize -= count + 1) < 0)
234 return AVERROR_INVALIDDATA;
235
236
2/2
✓ Branch 0 taken 21206629 times.
✓ Branch 1 taken 501696 times.
21708325 while (count--)
237 21206629 *d++ = *s++;
238 } else {
239 170227 count++;
240
241
1/2
✓ Branch 0 taken 170227 times.
✗ Branch 1 not taken.
170227 if ((dsize -= count) < 0 ||
242
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 170227 times.
170227 (ssize -= 2) < 0)
243 return AVERROR_INVALIDDATA;
244
245
2/2
✓ Branch 0 taken 4743203 times.
✓ Branch 1 taken 170227 times.
4913430 while (count--)
246 4743203 *d++ = *s;
247
248 170227 s++;
249 }
250 }
251
252
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4848 times.
4848 if (dend != d)
253 return AVERROR_INVALIDDATA;
254
255 4848 return 0;
256 }
257
258 4848 static int rle_uncompress(const EXRContext *ctx, const uint8_t *src, int compressed_size,
259 int uncompressed_size, EXRThreadData *td)
260 {
261 4848 rle(td->tmp, src, compressed_size, uncompressed_size);
262
263 av_assert1(uncompressed_size % 2 == 0);
264
265 4848 ctx->dsp.predictor(td->tmp, uncompressed_size);
266 4848 ctx->dsp.reorder_pixels(td->uncompressed_data, td->tmp, uncompressed_size);
267
268 4848 return 0;
269 }
270
271 #define USHORT_RANGE (1 << 16)
272 #define BITMAP_SIZE (1 << 13)
273
274 30 static uint16_t reverse_lut(const uint8_t *bitmap, uint16_t *lut)
275 {
276 30 int i, k = 0;
277
278
2/2
✓ Branch 0 taken 1966080 times.
✓ Branch 1 taken 30 times.
1966110 for (i = 0; i < USHORT_RANGE; i++)
279
4/4
✓ Branch 0 taken 1966050 times.
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 15355 times.
✓ Branch 3 taken 1950695 times.
1966080 if ((i == 0) || (bitmap[i >> 3] & (1 << (i & 7))))
280 15385 lut[k++] = i;
281
282 30 i = k - 1;
283
284 30 memset(lut + k, 0, (USHORT_RANGE - k) * 2);
285
286 30 return i;
287 }
288
289 30 static void apply_lut(const uint16_t *lut, uint16_t *dst, int dsize)
290 {
291 int i;
292
293
2/2
✓ Branch 0 taken 820722 times.
✓ Branch 1 taken 30 times.
820752 for (i = 0; i < dsize; ++i)
294 820722 dst[i] = lut[dst[i]];
295 30 }
296
297 #define HUF_ENCBITS 16 // literal (value) bit length
298 #define HUF_ENCSIZE ((1 << HUF_ENCBITS) + 1) // encoding table size
299
300 30 static void huf_canonical_code_table(uint64_t *freq)
301 {
302 30 uint64_t c, n[59] = { 0 };
303 int i;
304
305
2/2
✓ Branch 0 taken 1966110 times.
✓ Branch 1 taken 30 times.
1966140 for (i = 0; i < HUF_ENCSIZE; i++)
306 1966110 n[freq[i]] += 1;
307
308 30 c = 0;
309
2/2
✓ Branch 0 taken 1740 times.
✓ Branch 1 taken 30 times.
1770 for (i = 58; i > 0; --i) {
310 1740 uint64_t nc = ((c + n[i]) >> 1);
311 1740 n[i] = c;
312 1740 c = nc;
313 }
314
315
2/2
✓ Branch 0 taken 1966110 times.
✓ Branch 1 taken 30 times.
1966140 for (i = 0; i < HUF_ENCSIZE; ++i) {
316 1966110 int l = freq[i];
317
318
2/2
✓ Branch 0 taken 6417 times.
✓ Branch 1 taken 1959693 times.
1966110 if (l > 0)
319 6417 freq[i] = l | (n[l]++ << 6);
320 }
321 30 }
322
323 #define SHORT_ZEROCODE_RUN 59
324 #define LONG_ZEROCODE_RUN 63
325 #define SHORTEST_LONG_RUN (2 + LONG_ZEROCODE_RUN - SHORT_ZEROCODE_RUN)
326 #define LONGEST_LONG_RUN (255 + SHORTEST_LONG_RUN)
327
328 30 static int huf_unpack_enc_table(GetByteContext *gb,
329 int32_t im, int32_t iM, uint64_t *freq)
330 {
331 GetBitContext gbit;
332 30 int ret = init_get_bits8(&gbit, gb->buffer, bytestream2_get_bytes_left(gb));
333
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (ret < 0)
334 return ret;
335
336
2/2
✓ Branch 0 taken 16606 times.
✓ Branch 1 taken 30 times.
16636 for (; im <= iM; im++) {
337 int l;
338
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16606 times.
16606 if (get_bits_left(&gbit) < 6)
339 return AVERROR_INVALIDDATA;
340 16606 l = freq[im] = get_bits(&gbit, 6);
341
342
2/2
✓ Branch 0 taken 8339 times.
✓ Branch 1 taken 8267 times.
16606 if (l == LONG_ZEROCODE_RUN) {
343 8339 int zerun = get_bits(&gbit, 8) + SHORTEST_LONG_RUN;
344
345
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8339 times.
8339 if (im + zerun > iM + 1)
346 return AVERROR_INVALIDDATA;
347
348
2/2
✓ Branch 0 taken 1955626 times.
✓ Branch 1 taken 8339 times.
1963965 while (zerun--)
349 1955626 freq[im++] = 0;
350
351 8339 im--;
352
2/2
✓ Branch 0 taken 1053 times.
✓ Branch 1 taken 7214 times.
8267 } else if (l >= SHORT_ZEROCODE_RUN) {
353 1053 int zerun = l - SHORT_ZEROCODE_RUN + 2;
354
355
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1053 times.
1053 if (im + zerun > iM + 1)
356 return AVERROR_INVALIDDATA;
357
358
2/2
✓ Branch 0 taken 3270 times.
✓ Branch 1 taken 1053 times.
4323 while (zerun--)
359 3270 freq[im++] = 0;
360
361 1053 im--;
362 }
363 }
364
365 30 bytestream2_skip(gb, (get_bits_count(&gbit) + 7) / 8);
366 30 huf_canonical_code_table(freq);
367
368 30 return 0;
369 }
370
371 30 static int huf_build_dec_table(const EXRContext *s,
372 EXRThreadData *td, int im, int iM)
373 {
374 30 int j = 0;
375
376 30 td->run_sym = -1;
377
2/2
✓ Branch 0 taken 1966080 times.
✓ Branch 1 taken 30 times.
1966110 for (int i = im; i < iM; i++) {
378 1966080 td->he[j].sym = i;
379 1966080 td->he[j].len = td->freq[i] & 63;
380 1966080 td->he[j].code = td->freq[i] >> 6;
381
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1966080 times.
1966080 if (td->he[j].len > 32) {
382 avpriv_request_sample(s->avctx, "Too big code length");
383 return AVERROR_PATCHWELCOME;
384 }
385
2/2
✓ Branch 0 taken 6387 times.
✓ Branch 1 taken 1959693 times.
1966080 if (td->he[j].len > 0)
386 6387 j++;
387 else
388 1959693 td->run_sym = i;
389 }
390
391
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (im > 0)
392 td->run_sym = 0;
393
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 else if (iM < 65535)
394 td->run_sym = 65535;
395
396
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (td->run_sym == -1) {
397 avpriv_request_sample(s->avctx, "No place for run symbol");
398 return AVERROR_PATCHWELCOME;
399 }
400
401 30 td->he[j].sym = td->run_sym;
402 30 td->he[j].len = td->freq[iM] & 63;
403
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (td->he[j].len > 32) {
404 avpriv_request_sample(s->avctx, "Too big code length");
405 return AVERROR_PATCHWELCOME;
406 }
407 30 td->he[j].code = td->freq[iM] >> 6;
408 30 j++;
409
410 30 ff_vlc_free(&td->vlc);
411 30 return ff_vlc_init_sparse(&td->vlc, 12, j,
412 30 &td->he[0].len, sizeof(td->he[0]), sizeof(td->he[0].len),
413 30 &td->he[0].code, sizeof(td->he[0]), sizeof(td->he[0].code),
414 30 &td->he[0].sym, sizeof(td->he[0]), sizeof(td->he[0].sym), 0);
415 }
416
417 30 static int huf_decode(VLC *vlc, GetByteContext *gb, int nbits, int run_sym,
418 int no, uint16_t *out)
419 {
420 GetBitContext gbit;
421 30 int oe = 0;
422
423 30 init_get_bits(&gbit, gb->buffer, nbits);
424
3/4
✓ Branch 1 taken 213481 times.
✓ Branch 2 taken 30 times.
✓ Branch 3 taken 213481 times.
✗ Branch 4 not taken.
213511 while (get_bits_left(&gbit) > 0 && oe < no) {
425 213481 uint16_t x = get_vlc2(&gbit, vlc->table, 12, 3);
426
427
2/2
✓ Branch 0 taken 3387 times.
✓ Branch 1 taken 210094 times.
213481 if (x == run_sym) {
428 3387 int run = get_bits(&gbit, 8);
429 uint16_t fill;
430
431
2/4
✓ Branch 0 taken 3387 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3387 times.
3387 if (oe == 0 || oe + run > no)
432 return AVERROR_INVALIDDATA;
433
434 3387 fill = out[oe - 1];
435
436
2/2
✓ Branch 0 taken 610628 times.
✓ Branch 1 taken 3387 times.
614015 while (run-- > 0)
437 610628 out[oe++] = fill;
438 } else {
439 210094 out[oe++] = x;
440 }
441 }
442
443 30 return 0;
444 }
445
446 30 static int huf_uncompress(const EXRContext *s,
447 EXRThreadData *td,
448 GetByteContext *gb,
449 uint16_t *dst, int dst_size)
450 {
451 int32_t im, iM;
452 uint32_t nBits;
453 int ret;
454
455 30 im = bytestream2_get_le32(gb);
456 30 iM = bytestream2_get_le32(gb);
457 30 bytestream2_skip(gb, 4);
458 30 nBits = bytestream2_get_le32(gb);
459
3/6
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 30 times.
✗ Branch 5 not taken.
30 if (im < 0 || im >= HUF_ENCSIZE ||
460
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 iM < 0 || iM >= HUF_ENCSIZE)
461 return AVERROR_INVALIDDATA;
462
463 30 bytestream2_skip(gb, 4);
464
465
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 23 times.
30 if (!td->freq)
466 7 td->freq = av_malloc_array(HUF_ENCSIZE, sizeof(*td->freq));
467
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 23 times.
30 if (!td->he)
468 7 td->he = av_calloc(HUF_ENCSIZE, sizeof(*td->he));
469
2/4
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 30 times.
30 if (!td->freq || !td->he) {
470 ret = AVERROR(ENOMEM);
471 return ret;
472 }
473
474 30 memset(td->freq, 0, sizeof(*td->freq) * HUF_ENCSIZE);
475
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
30 if ((ret = huf_unpack_enc_table(gb, im, iM, td->freq)) < 0)
476 return ret;
477
478
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
30 if (nBits > 8 * bytestream2_get_bytes_left(gb)) {
479 ret = AVERROR_INVALIDDATA;
480 return ret;
481 }
482
483
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
30 if ((ret = huf_build_dec_table(s, td, im, iM)) < 0)
484 return ret;
485 30 return huf_decode(&td->vlc, gb, nBits, td->run_sym, dst_size, dst);
486 }
487
488 1091709 static inline void wdec14(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
489 {
490 1091709 int16_t ls = l;
491 1091709 int16_t hs = h;
492 1091709 int hi = hs;
493 1091709 int ai = ls + (hi & 1) + (hi >> 1);
494 1091709 int16_t as = ai;
495 1091709 int16_t bs = ai - hi;
496
497 1091709 *a = as;
498 1091709 *b = bs;
499 1091709 }
500
501 #define NBITS 16
502 #define A_OFFSET (1 << (NBITS - 1))
503 #define MOD_MASK ((1 << NBITS) - 1)
504
505 static inline void wdec16(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
506 {
507 int m = l;
508 int d = h;
509 int bb = (m - (d >> 1)) & MOD_MASK;
510 int aa = (d + bb - A_OFFSET) & MOD_MASK;
511 *b = bb;
512 *a = aa;
513 }
514
515 102 static void wav_decode(uint16_t *in, int nx, int ox,
516 int ny, int oy, uint16_t mx)
517 {
518 102 int w14 = (mx < (1 << 14));
519 102 int n = (nx > ny) ? ny : nx;
520 102 int p = 1;
521 int p2;
522
523
2/2
✓ Branch 0 taken 591 times.
✓ Branch 1 taken 102 times.
693 while (p <= n)
524 591 p <<= 1;
525
526 102 p >>= 1;
527 102 p2 = p;
528 102 p >>= 1;
529
530
2/2
✓ Branch 0 taken 489 times.
✓ Branch 1 taken 102 times.
591 while (p >= 1) {
531 489 uint16_t *py = in;
532 489 uint16_t *ey = in + oy * (ny - p2);
533 uint16_t i00, i01, i10, i11;
534 489 int oy1 = oy * p;
535 489 int oy2 = oy * p2;
536 489 int ox1 = ox * p;
537 489 int ox2 = ox * p2;
538
539
2/2
✓ Branch 0 taken 2940 times.
✓ Branch 1 taken 489 times.
3429 for (; py <= ey; py += oy2) {
540 2940 uint16_t *px = py;
541 2940 uint16_t *ex = py + ox * (nx - p2);
542
543
2/2
✓ Branch 0 taken 272751 times.
✓ Branch 1 taken 2940 times.
275691 for (; px <= ex; px += ox2) {
544 272751 uint16_t *p01 = px + ox1;
545 272751 uint16_t *p10 = px + oy1;
546 272751 uint16_t *p11 = p10 + ox1;
547
548
1/2
✓ Branch 0 taken 272751 times.
✗ Branch 1 not taken.
272751 if (w14) {
549 272751 wdec14(*px, *p10, &i00, &i10);
550 272751 wdec14(*p01, *p11, &i01, &i11);
551 272751 wdec14(i00, i01, px, p01);
552 272751 wdec14(i10, i11, p10, p11);
553 } else {
554 wdec16(*px, *p10, &i00, &i10);
555 wdec16(*p01, *p11, &i01, &i11);
556 wdec16(i00, i01, px, p01);
557 wdec16(i10, i11, p10, p11);
558 }
559 }
560
561
2/2
✓ Branch 0 taken 324 times.
✓ Branch 1 taken 2616 times.
2940 if (nx & p) {
562 324 uint16_t *p10 = px + oy1;
563
564
1/2
✓ Branch 0 taken 324 times.
✗ Branch 1 not taken.
324 if (w14)
565 324 wdec14(*px, *p10, &i00, p10);
566 else
567 wdec16(*px, *p10, &i00, p10);
568
569 324 *px = i00;
570 }
571 }
572
573
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 474 times.
489 if (ny & p) {
574 15 uint16_t *px = py;
575 15 uint16_t *ex = py + ox * (nx - p2);
576
577
2/2
✓ Branch 0 taken 381 times.
✓ Branch 1 taken 15 times.
396 for (; px <= ex; px += ox2) {
578 381 uint16_t *p01 = px + ox1;
579
580
1/2
✓ Branch 0 taken 381 times.
✗ Branch 1 not taken.
381 if (w14)
581 381 wdec14(*px, *p01, &i00, p01);
582 else
583 wdec16(*px, *p01, &i00, p01);
584
585 381 *px = i00;
586 }
587 }
588
589 489 p2 = p;
590 489 p >>= 1;
591 }
592 102 }
593
594 30 static int piz_uncompress(const EXRContext *s, const uint8_t *src, int ssize,
595 int dsize, EXRThreadData *td)
596 {
597 GetByteContext gb;
598 uint16_t maxval, min_non_zero, max_non_zero;
599 uint16_t *ptr;
600 30 uint16_t *tmp = (uint16_t *)td->tmp;
601 uint16_t *out;
602 uint16_t *in;
603 int ret, i, j;
604 int pixel_half_size;/* 1 for half, 2 for float and uint32 */
605 EXRChannel *channel;
606 int tmp_offset;
607
608
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 23 times.
30 if (!td->bitmap)
609 7 td->bitmap = av_malloc(BITMAP_SIZE);
610
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 23 times.
30 if (!td->lut)
611 7 td->lut = av_malloc(1 << 17);
612
2/4
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 30 times.
30 if (!td->bitmap || !td->lut) {
613 av_freep(&td->bitmap);
614 av_freep(&td->lut);
615 return AVERROR(ENOMEM);
616 }
617
618 30 bytestream2_init(&gb, src, ssize);
619 30 min_non_zero = bytestream2_get_le16(&gb);
620 30 max_non_zero = bytestream2_get_le16(&gb);
621
622
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (max_non_zero >= BITMAP_SIZE)
623 return AVERROR_INVALIDDATA;
624
625 30 memset(td->bitmap, 0, FFMIN(min_non_zero, BITMAP_SIZE));
626
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 if (min_non_zero <= max_non_zero)
627 30 bytestream2_get_buffer(&gb, td->bitmap + min_non_zero,
628 30 max_non_zero - min_non_zero + 1);
629 30 memset(td->bitmap + max_non_zero + 1, 0, BITMAP_SIZE - max_non_zero - 1);
630
631 30 maxval = reverse_lut(td->bitmap, td->lut);
632
633 30 bytestream2_skip(&gb, 4);
634 30 ret = huf_uncompress(s, td, &gb, tmp, dsize / sizeof(uint16_t));
635
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (ret)
636 return ret;
637
638 30 ptr = tmp;
639
2/2
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 30 times.
120 for (i = 0; i < s->nb_channels; i++) {
640 90 channel = &s->channels[i];
641
642
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 12 times.
90 if (channel->pixel_type == EXR_HALF)
643 78 pixel_half_size = 1;
644 else
645 12 pixel_half_size = 2;
646
647
2/2
✓ Branch 0 taken 102 times.
✓ Branch 1 taken 90 times.
192 for (j = 0; j < pixel_half_size; j++)
648 102 wav_decode(ptr + j, td->xsize, pixel_half_size, td->ysize,
649 102 td->xsize * pixel_half_size, maxval);
650 90 ptr += td->xsize * td->ysize * pixel_half_size;
651 }
652
653 30 apply_lut(td->lut, tmp, dsize / sizeof(uint16_t));
654
655 30 out = (uint16_t *)td->uncompressed_data;
656
2/2
✓ Branch 0 taken 891 times.
✓ Branch 1 taken 30 times.
921 for (i = 0; i < td->ysize; i++) {
657 891 tmp_offset = 0;
658
2/2
✓ Branch 0 taken 2673 times.
✓ Branch 1 taken 891 times.
3564 for (j = 0; j < s->nb_channels; j++) {
659 2673 channel = &s->channels[j];
660
2/2
✓ Branch 0 taken 2289 times.
✓ Branch 1 taken 384 times.
2673 if (channel->pixel_type == EXR_HALF)
661 2289 pixel_half_size = 1;
662 else
663 384 pixel_half_size = 2;
664
665 2673 in = tmp + tmp_offset * td->xsize * td->ysize + i * td->xsize * pixel_half_size;
666 2673 tmp_offset += pixel_half_size;
667
668 #if HAVE_BIGENDIAN
669 s->bbdsp.bswap16_buf(out, in, td->xsize * pixel_half_size);
670 #else
671 2673 memcpy(out, in, td->xsize * 2 * pixel_half_size);
672 #endif
673 2673 out += td->xsize * pixel_half_size;
674 }
675 }
676
677 30 return 0;
678 }
679
680 61 static int pxr24_uncompress(const EXRContext *s, const uint8_t *src,
681 int compressed_size, int uncompressed_size,
682 EXRThreadData *td)
683 {
684 61 unsigned long dest_len, expected_len = 0;
685 61 const uint8_t *in = td->tmp;
686 uint8_t *out;
687 int c, i, j;
688
689
2/2
✓ Branch 0 taken 218 times.
✓ Branch 1 taken 61 times.
279 for (i = 0; i < s->nb_channels; i++) {
690
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 187 times.
218 if (s->channels[i].pixel_type == EXR_FLOAT) {
691 31 expected_len += (td->xsize * td->ysize * 3);/* PRX 24 store float in 24 bit instead of 32 */
692
2/2
✓ Branch 0 taken 181 times.
✓ Branch 1 taken 6 times.
187 } else if (s->channels[i].pixel_type == EXR_HALF) {
693 181 expected_len += (td->xsize * td->ysize * 2);
694 } else {//UINT 32
695 6 expected_len += (td->xsize * td->ysize * 4);
696 }
697 }
698
699 61 dest_len = expected_len;
700
701
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 61 times.
61 if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK) {
702 return AVERROR_INVALIDDATA;
703
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
61 } else if (dest_len != expected_len) {
704 return AVERROR_INVALIDDATA;
705 }
706
707 61 out = td->uncompressed_data;
708
2/2
✓ Branch 0 taken 889 times.
✓ Branch 1 taken 61 times.
950 for (i = 0; i < td->ysize; i++)
709
2/2
✓ Branch 0 taken 2950 times.
✓ Branch 1 taken 889 times.
3839 for (c = 0; c < s->nb_channels; c++) {
710 2950 EXRChannel *channel = &s->channels[c];
711 const uint8_t *ptr[4];
712 2950 uint32_t pixel = 0;
713
714
3/4
✓ Branch 0 taken 248 times.
✓ Branch 1 taken 2651 times.
✓ Branch 2 taken 51 times.
✗ Branch 3 not taken.
2950 switch (channel->pixel_type) {
715 248 case EXR_FLOAT:
716 248 ptr[0] = in;
717 248 ptr[1] = ptr[0] + td->xsize;
718 248 ptr[2] = ptr[1] + td->xsize;
719 248 in = ptr[2] + td->xsize;
720
721
2/2
✓ Branch 0 taken 2976 times.
✓ Branch 1 taken 248 times.
3224 for (j = 0; j < td->xsize; ++j) {
722 2976 uint32_t diff = ((unsigned)*(ptr[0]++) << 24) |
723 2976 (*(ptr[1]++) << 16) |
724 2976 (*(ptr[2]++) << 8);
725 2976 pixel += diff;
726 2976 bytestream_put_le32(&out, pixel);
727 }
728 248 break;
729 2651 case EXR_HALF:
730 2651 ptr[0] = in;
731 2651 ptr[1] = ptr[0] + td->xsize;
732 2651 in = ptr[1] + td->xsize;
733
2/2
✓ Branch 0 taken 1923039 times.
✓ Branch 1 taken 2651 times.
1925690 for (j = 0; j < td->xsize; j++) {
734 1923039 uint32_t diff = (*(ptr[0]++) << 8) | *(ptr[1]++);
735
736 1923039 pixel += diff;
737 1923039 bytestream_put_le16(&out, pixel);
738 }
739 2651 break;
740 51 case EXR_UINT:
741 51 ptr[0] = in;
742 51 ptr[1] = ptr[0] + s->xdelta;
743 51 ptr[2] = ptr[1] + s->xdelta;
744 51 ptr[3] = ptr[2] + s->xdelta;
745 51 in = ptr[3] + s->xdelta;
746
747
2/2
✓ Branch 0 taken 639 times.
✓ Branch 1 taken 51 times.
690 for (j = 0; j < s->xdelta; ++j) {
748 639 uint32_t diff = ((uint32_t)*(ptr[0]++) << 24) |
749 639 (*(ptr[1]++) << 16) |
750 639 (*(ptr[2]++) << 8 ) |
751 639 (*(ptr[3]++));
752 639 pixel += diff;
753 639 bytestream_put_le32(&out, pixel);
754 }
755 51 break;
756 default:
757 return AVERROR_INVALIDDATA;
758 }
759 }
760
761 61 return 0;
762 }
763
764 59279 static void unpack_14(const uint8_t b[14], uint16_t s[16])
765 {
766 59279 uint16_t shift = (b[ 2] >> 2) & 15;
767 59279 uint16_t bias = (0x20 << shift);
768 int i;
769
770 59279 s[ 0] = (b[0] << 8) | b[1];
771
772 59279 s[ 4] = s[ 0] + ((((b[ 2] << 4) | (b[ 3] >> 4)) & 0x3f) << shift) - bias;
773 59279 s[ 8] = s[ 4] + ((((b[ 3] << 2) | (b[ 4] >> 6)) & 0x3f) << shift) - bias;
774 59279 s[12] = s[ 8] + ((b[ 4] & 0x3f) << shift) - bias;
775
776 59279 s[ 1] = s[ 0] + ((b[ 5] >> 2) << shift) - bias;
777 59279 s[ 5] = s[ 4] + ((((b[ 5] << 4) | (b[ 6] >> 4)) & 0x3f) << shift) - bias;
778 59279 s[ 9] = s[ 8] + ((((b[ 6] << 2) | (b[ 7] >> 6)) & 0x3f) << shift) - bias;
779 59279 s[13] = s[12] + ((b[ 7] & 0x3f) << shift) - bias;
780
781 59279 s[ 2] = s[ 1] + ((b[ 8] >> 2) << shift) - bias;
782 59279 s[ 6] = s[ 5] + ((((b[ 8] << 4) | (b[ 9] >> 4)) & 0x3f) << shift) - bias;
783 59279 s[10] = s[ 9] + ((((b[ 9] << 2) | (b[10] >> 6)) & 0x3f) << shift) - bias;
784 59279 s[14] = s[13] + ((b[10] & 0x3f) << shift) - bias;
785
786 59279 s[ 3] = s[ 2] + ((b[11] >> 2) << shift) - bias;
787 59279 s[ 7] = s[ 6] + ((((b[11] << 4) | (b[12] >> 4)) & 0x3f) << shift) - bias;
788 59279 s[11] = s[10] + ((((b[12] << 2) | (b[13] >> 6)) & 0x3f) << shift) - bias;
789 59279 s[15] = s[14] + ((b[13] & 0x3f) << shift) - bias;
790
791
2/2
✓ Branch 0 taken 948464 times.
✓ Branch 1 taken 59279 times.
1007743 for (i = 0; i < 16; ++i) {
792
2/2
✓ Branch 0 taken 942347 times.
✓ Branch 1 taken 6117 times.
948464 if (s[i] & 0x8000)
793 942347 s[i] &= 0x7fff;
794 else
795 6117 s[i] = ~s[i];
796 }
797 59279 }
798
799 106 static void unpack_3(const uint8_t b[3], uint16_t s[16])
800 {
801 int i;
802
803 106 s[0] = (b[0] << 8) | b[1];
804
805
2/2
✓ Branch 0 taken 105 times.
✓ Branch 1 taken 1 times.
106 if (s[0] & 0x8000)
806 105 s[0] &= 0x7fff;
807 else
808 1 s[0] = ~s[0];
809
810
2/2
✓ Branch 0 taken 1590 times.
✓ Branch 1 taken 106 times.
1696 for (i = 1; i < 16; i++)
811 1590 s[i] = s[0];
812 106 }
813
814
815 23 static int b44_uncompress(const EXRContext *s, const uint8_t *src, int compressed_size,
816 int uncompressed_size, EXRThreadData *td) {
817 23 const int8_t *sr = src;
818 23 int stay_to_uncompress = compressed_size;
819 int nb_b44_block_w, nb_b44_block_h;
820 int index_tl_x, index_tl_y, index_out, index_tmp;
821 uint16_t tmp_buffer[16]; /* B44 use 4x4 half float pixel */
822 int c, iY, iX, y, x;
823 23 int target_channel_offset = 0;
824
825 /* calc B44 block count */
826 23 nb_b44_block_w = td->xsize / 4;
827
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 10 times.
23 if ((td->xsize % 4) != 0)
828 13 nb_b44_block_w++;
829
830 23 nb_b44_block_h = td->ysize / 4;
831
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 17 times.
23 if ((td->ysize % 4) != 0)
832 6 nb_b44_block_h++;
833
834
2/2
✓ Branch 0 taken 133 times.
✓ Branch 1 taken 23 times.
156 for (c = 0; c < s->nb_channels; c++) {
835
2/2
✓ Branch 0 taken 85 times.
✓ Branch 1 taken 48 times.
133 if (s->channels[c].pixel_type == EXR_HALF) {/* B44 only compress half float data */
836
2/2
✓ Branch 0 taken 374 times.
✓ Branch 1 taken 85 times.
459 for (iY = 0; iY < nb_b44_block_h; iY++) {
837
2/2
✓ Branch 0 taken 59385 times.
✓ Branch 1 taken 374 times.
59759 for (iX = 0; iX < nb_b44_block_w; iX++) {/* For each B44 block */
838
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59385 times.
59385 if (stay_to_uncompress < 3)
839 return AVERROR_INVALIDDATA;
840
841
2/2
✓ Branch 0 taken 106 times.
✓ Branch 1 taken 59279 times.
59385 if (src[compressed_size - stay_to_uncompress + 2] == 0xfc) { /* B44A block */
842 106 unpack_3(sr, tmp_buffer);
843 106 sr += 3;
844 106 stay_to_uncompress -= 3;
845 } else {/* B44 Block */
846
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59279 times.
59279 if (stay_to_uncompress < 14)
847 return AVERROR_INVALIDDATA;
848 59279 unpack_14(sr, tmp_buffer);
849 59279 sr += 14;
850 59279 stay_to_uncompress -= 14;
851 }
852
853 /* copy data to uncompress buffer (B44 block can exceed target resolution)*/
854 59385 index_tl_x = iX * 4;
855 59385 index_tl_y = iY * 4;
856
857
4/4
✓ Branch 0 taken 3896 times.
✓ Branch 1 taken 291865 times.
✓ Branch 2 taken 236376 times.
✓ Branch 3 taken 59385 times.
295761 for (y = index_tl_y; y < FFMIN(index_tl_y + 4, td->ysize); y++) {
858
4/4
✓ Branch 0 taken 4358 times.
✓ Branch 1 taken 1176005 times.
✓ Branch 2 taken 943987 times.
✓ Branch 3 taken 236376 times.
1180363 for (x = index_tl_x; x < FFMIN(index_tl_x + 4, td->xsize); x++) {
859 943987 index_out = target_channel_offset * td->xsize + y * td->channel_line_size + 2 * x;
860 943987 index_tmp = (y-index_tl_y) * 4 + (x-index_tl_x);
861 943987 td->uncompressed_data[index_out] = tmp_buffer[index_tmp] & 0xff;
862 943987 td->uncompressed_data[index_out + 1] = tmp_buffer[index_tmp] >> 8;
863 }
864 }
865 }
866 }
867 85 target_channel_offset += 2;
868 } else {/* Float or UINT 32 channel */
869
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
48 if (stay_to_uncompress < td->ysize * td->xsize * 4)
870 return AVERROR_INVALIDDATA;
871
872
2/2
✓ Branch 0 taken 400 times.
✓ Branch 1 taken 48 times.
448 for (y = 0; y < td->ysize; y++) {
873 400 index_out = target_channel_offset * td->xsize + y * td->channel_line_size;
874 400 memcpy(&td->uncompressed_data[index_out], sr, td->xsize * 4);
875 400 sr += td->xsize * 4;
876 }
877 48 target_channel_offset += 4;
878
879 48 stay_to_uncompress -= td->ysize * td->xsize * 4;
880 }
881 }
882
883 23 return 0;
884 }
885
886 static int ac_uncompress(const EXRContext *s, GetByteContext *gb, float *block)
887 {
888 int ret = 0, n = 1;
889
890 while (n < 64) {
891 uint16_t val = bytestream2_get_ne16(gb);
892
893 if (val == 0xff00) {
894 n = 64;
895 } else if ((val >> 8) == 0xff) {
896 n += val & 0xff;
897 } else {
898 ret = n;
899 block[ff_zigzag_direct[n]] = av_int2float(half2float(val, &s->h2f_tables));
900 n++;
901 }
902 }
903
904 return ret;
905 }
906
907 static void idct_1d(float *blk, int step)
908 {
909 const float a = .5f * cosf( M_PI / 4.f);
910 const float b = .5f * cosf( M_PI / 16.f);
911 const float c = .5f * cosf( M_PI / 8.f);
912 const float d = .5f * cosf(3.f*M_PI / 16.f);
913 const float e = .5f * cosf(5.f*M_PI / 16.f);
914 const float f = .5f * cosf(3.f*M_PI / 8.f);
915 const float g = .5f * cosf(7.f*M_PI / 16.f);
916
917 float alpha[4], beta[4], theta[4], gamma[4];
918
919 alpha[0] = c * blk[2 * step];
920 alpha[1] = f * blk[2 * step];
921 alpha[2] = c * blk[6 * step];
922 alpha[3] = f * blk[6 * step];
923
924 beta[0] = b * blk[1 * step] + d * blk[3 * step] + e * blk[5 * step] + g * blk[7 * step];
925 beta[1] = d * blk[1 * step] - g * blk[3 * step] - b * blk[5 * step] - e * blk[7 * step];
926 beta[2] = e * blk[1 * step] - b * blk[3 * step] + g * blk[5 * step] + d * blk[7 * step];
927 beta[3] = g * blk[1 * step] - e * blk[3 * step] + d * blk[5 * step] - b * blk[7 * step];
928
929 theta[0] = a * (blk[0 * step] + blk[4 * step]);
930 theta[3] = a * (blk[0 * step] - blk[4 * step]);
931
932 theta[1] = alpha[0] + alpha[3];
933 theta[2] = alpha[1] - alpha[2];
934
935 gamma[0] = theta[0] + theta[1];
936 gamma[1] = theta[3] + theta[2];
937 gamma[2] = theta[3] - theta[2];
938 gamma[3] = theta[0] - theta[1];
939
940 blk[0 * step] = gamma[0] + beta[0];
941 blk[1 * step] = gamma[1] + beta[1];
942 blk[2 * step] = gamma[2] + beta[2];
943 blk[3 * step] = gamma[3] + beta[3];
944
945 blk[4 * step] = gamma[3] - beta[3];
946 blk[5 * step] = gamma[2] - beta[2];
947 blk[6 * step] = gamma[1] - beta[1];
948 blk[7 * step] = gamma[0] - beta[0];
949 }
950
951 static void dct_inverse(float *block)
952 {
953 for (int i = 0; i < 8; i++)
954 idct_1d(block + i, 8);
955
956 for (int i = 0; i < 8; i++) {
957 idct_1d(block, 1);
958 block += 8;
959 }
960 }
961
962 static void convert(float y, float u, float v,
963 float *b, float *g, float *r)
964 {
965 *r = y + 1.5747f * v;
966 *g = y - 0.1873f * u - 0.4682f * v;
967 *b = y + 1.8556f * u;
968 }
969
970 static float to_linear(float x, float scale)
971 {
972 float ax = fabsf(x);
973
974 if (ax <= 1.f) {
975 return FFSIGN(x) * powf(ax, 2.2f * scale);
976 } else {
977 const float log_base = expf(2.2f * scale);
978
979 return FFSIGN(x) * powf(log_base, ax - 1.f);
980 }
981 }
982
983 static int dwa_uncompress(const EXRContext *s, const uint8_t *src, int compressed_size,
984 int uncompressed_size, EXRThreadData *td)
985 {
986 int64_t version, lo_usize, lo_size;
987 int64_t ac_size, dc_size, rle_usize, rle_csize, rle_raw_size;
988 int64_t ac_count, dc_count, ac_compression;
989 const int dc_w = td->xsize >> 3;
990 const int dc_h = td->ysize >> 3;
991 GetByteContext gb, agb;
992 int skip, ret;
993
994 if (compressed_size <= 88)
995 return AVERROR_INVALIDDATA;
996
997 version = AV_RL64(src + 0);
998 if (version != 2)
999 return AVERROR_INVALIDDATA;
1000
1001 lo_usize = AV_RL64(src + 8);
1002 lo_size = AV_RL64(src + 16);
1003 ac_size = AV_RL64(src + 24);
1004 dc_size = AV_RL64(src + 32);
1005 rle_csize = AV_RL64(src + 40);
1006 rle_usize = AV_RL64(src + 48);
1007 rle_raw_size = AV_RL64(src + 56);
1008 ac_count = AV_RL64(src + 64);
1009 dc_count = AV_RL64(src + 72);
1010 ac_compression = AV_RL64(src + 80);
1011
1012 if ( compressed_size < (uint64_t)(lo_size | ac_size | dc_size | rle_csize) || compressed_size < 88LL + lo_size + ac_size + dc_size + rle_csize
1013 || ac_count > (uint64_t)INT_MAX/2
1014 )
1015 return AVERROR_INVALIDDATA;
1016
1017 bytestream2_init(&gb, src + 88, compressed_size - 88);
1018 skip = bytestream2_get_le16(&gb);
1019 if (skip < 2)
1020 return AVERROR_INVALIDDATA;
1021
1022 bytestream2_skip(&gb, skip - 2);
1023
1024 if (lo_size > 0) {
1025 if (lo_usize > uncompressed_size)
1026 return AVERROR_INVALIDDATA;
1027 bytestream2_skip(&gb, lo_size);
1028 }
1029
1030 if (ac_size > 0) {
1031 unsigned long dest_len;
1032 GetByteContext agb = gb;
1033
1034 if (ac_count > 3LL * td->xsize * s->scan_lines_per_block)
1035 return AVERROR_INVALIDDATA;
1036
1037 dest_len = ac_count * 2LL;
1038
1039 av_fast_padded_malloc(&td->ac_data, &td->ac_size, dest_len);
1040 if (!td->ac_data)
1041 return AVERROR(ENOMEM);
1042
1043 switch (ac_compression) {
1044 case 0:
1045 ret = huf_uncompress(s, td, &agb, (int16_t *)td->ac_data, ac_count);
1046 if (ret < 0)
1047 return ret;
1048 break;
1049 case 1:
1050 if (uncompress(td->ac_data, &dest_len, agb.buffer, ac_size) != Z_OK ||
1051 dest_len != ac_count * 2LL)
1052 return AVERROR_INVALIDDATA;
1053 break;
1054 default:
1055 return AVERROR_INVALIDDATA;
1056 }
1057
1058 bytestream2_skip(&gb, ac_size);
1059 }
1060
1061 {
1062 unsigned long dest_len;
1063 GetByteContext agb = gb;
1064
1065 if (dc_count != dc_w * dc_h * 3)
1066 return AVERROR_INVALIDDATA;
1067
1068 dest_len = dc_count * 2LL;
1069
1070 av_fast_padded_malloc(&td->dc_data, &td->dc_size, FFALIGN(dest_len, 64) * 2);
1071 if (!td->dc_data)
1072 return AVERROR(ENOMEM);
1073
1074 if (uncompress(td->dc_data + FFALIGN(dest_len, 64), &dest_len, agb.buffer, dc_size) != Z_OK ||
1075 (dest_len != dc_count * 2LL))
1076 return AVERROR_INVALIDDATA;
1077
1078 s->dsp.predictor(td->dc_data + FFALIGN(dest_len, 64), dest_len);
1079 s->dsp.reorder_pixels(td->dc_data, td->dc_data + FFALIGN(dest_len, 64), dest_len);
1080
1081 bytestream2_skip(&gb, dc_size);
1082 }
1083
1084 if (rle_raw_size > 0 && rle_csize > 0 && rle_usize > 0) {
1085 unsigned long dest_len = rle_usize;
1086
1087 av_fast_padded_malloc(&td->rle_data, &td->rle_size, rle_usize);
1088 if (!td->rle_data)
1089 return AVERROR(ENOMEM);
1090
1091 av_fast_padded_malloc(&td->rle_raw_data, &td->rle_raw_size, rle_raw_size);
1092 if (!td->rle_raw_data)
1093 return AVERROR(ENOMEM);
1094
1095 if (uncompress(td->rle_data, &dest_len, gb.buffer, rle_csize) != Z_OK ||
1096 (dest_len != rle_usize))
1097 return AVERROR_INVALIDDATA;
1098
1099 ret = rle(td->rle_raw_data, td->rle_data, rle_usize, rle_raw_size);
1100 if (ret < 0)
1101 return ret;
1102 bytestream2_skip(&gb, rle_csize);
1103 }
1104
1105 bytestream2_init(&agb, td->ac_data, ac_count * 2);
1106
1107 for (int y = 0; y < td->ysize; y += 8) {
1108 for (int x = 0; x < td->xsize; x += 8) {
1109 memset(td->block, 0, sizeof(td->block));
1110
1111 for (int j = 0; j < 3; j++) {
1112 float *block = td->block[j];
1113 const int idx = (x >> 3) + (y >> 3) * dc_w + dc_w * dc_h * j;
1114 uint16_t *dc = (uint16_t *)td->dc_data;
1115 union av_intfloat32 dc_val;
1116
1117 dc_val.i = half2float(dc[idx], &s->h2f_tables);
1118
1119 block[0] = dc_val.f;
1120 ac_uncompress(s, &agb, block);
1121 dct_inverse(block);
1122 }
1123
1124 {
1125 const int o = s->nb_channels == 4;
1126 float *bo = ((float *)td->uncompressed_data) +
1127 y * td->xsize * s->nb_channels + td->xsize * (o + 0) + x;
1128 float *go = ((float *)td->uncompressed_data) +
1129 y * td->xsize * s->nb_channels + td->xsize * (o + 1) + x;
1130 float *ro = ((float *)td->uncompressed_data) +
1131 y * td->xsize * s->nb_channels + td->xsize * (o + 2) + x;
1132 float *yb = td->block[0];
1133 float *ub = td->block[1];
1134 float *vb = td->block[2];
1135
1136 for (int yy = 0; yy < 8; yy++) {
1137 for (int xx = 0; xx < 8; xx++) {
1138 const int idx = xx + yy * 8;
1139
1140 convert(yb[idx], ub[idx], vb[idx], &bo[xx], &go[xx], &ro[xx]);
1141
1142 bo[xx] = to_linear(bo[xx], 1.f);
1143 go[xx] = to_linear(go[xx], 1.f);
1144 ro[xx] = to_linear(ro[xx], 1.f);
1145 }
1146
1147 bo += td->xsize * s->nb_channels;
1148 go += td->xsize * s->nb_channels;
1149 ro += td->xsize * s->nb_channels;
1150 }
1151 }
1152 }
1153 }
1154
1155 if (s->nb_channels < 4)
1156 return 0;
1157
1158 for (int y = 0; y < td->ysize && td->rle_raw_data; y++) {
1159 uint32_t *ao = ((uint32_t *)td->uncompressed_data) + y * td->xsize * s->nb_channels;
1160 uint8_t *ai0 = td->rle_raw_data + y * td->xsize;
1161 uint8_t *ai1 = td->rle_raw_data + y * td->xsize + rle_raw_size / 2;
1162
1163 for (int x = 0; x < td->xsize; x++) {
1164 uint16_t ha = ai0[x] | (ai1[x] << 8);
1165
1166 ao[x] = half2float(ha, &s->h2f_tables);
1167 }
1168 }
1169
1170 return 0;
1171 }
1172
1173 37267 static int decode_block(AVCodecContext *avctx, void *tdata,
1174 int jobnr, int threadnr)
1175 {
1176 37267 const EXRContext *s = avctx->priv_data;
1177 37267 AVFrame *const p = s->picture;
1178 37267 EXRThreadData *td = &s->thread_data[threadnr];
1179 37267 const uint8_t *channel_buffer[4] = { 0 };
1180 37267 const uint8_t *buf = s->buf;
1181 uint64_t line_offset, uncompressed_size;
1182 uint8_t *ptr;
1183 uint32_t data_size;
1184 37267 int line, col = 0;
1185 uint64_t tile_x, tile_y, tile_level_x, tile_level_y;
1186 const uint8_t *src;
1187 37267 int step = s->desc->comp[0].step;
1188 37267 int bxmin = 0, axmax = 0, window_xoffset = 0;
1189 int window_xmin, window_xmax, window_ymin, window_ymax;
1190 int data_xoffset, data_yoffset, data_window_offset, xsize, ysize;
1191 37267 int i, x, buf_size = s->buf_size;
1192 int c, rgb_channel_count;
1193 37267 float one_gamma = 1.0f / s->gamma;
1194 37267 av_csp_trc_function trc_func = av_csp_trc_func_from_id(s->apply_trc_type);
1195 int ret;
1196
1197 37267 line_offset = AV_RL64(s->gb.buffer + jobnr * 8);
1198
1199
2/2
✓ Branch 0 taken 104 times.
✓ Branch 1 taken 37163 times.
37267 if (s->is_tile) {
1200
2/4
✓ Branch 0 taken 104 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 104 times.
104 if (buf_size < 20 || line_offset > buf_size - 20)
1201 return AVERROR_INVALIDDATA;
1202
1203 104 src = buf + line_offset + 20;
1204
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 104 times.
104 if (s->is_multipart)
1205 src += 4;
1206
1207 104 tile_x = AV_RL32(src - 20);
1208 104 tile_y = AV_RL32(src - 16);
1209 104 tile_level_x = AV_RL32(src - 12);
1210 104 tile_level_y = AV_RL32(src - 8);
1211
1212 104 data_size = AV_RL32(src - 4);
1213
2/4
✓ Branch 0 taken 104 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 104 times.
104 if (data_size <= 0 || data_size > buf_size - line_offset - 20)
1214 return AVERROR_INVALIDDATA;
1215
1216
2/4
✓ Branch 0 taken 104 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 104 times.
104 if (tile_level_x || tile_level_y) { /* tile level, is not the full res level */
1217 avpriv_report_missing_feature(s->avctx, "Subres tile before full res tile");
1218 return AVERROR_PATCHWELCOME;
1219 }
1220
1221
3/4
✓ Branch 0 taken 70 times.
✓ Branch 1 taken 34 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 70 times.
104 if (tile_x && s->tile_attr.xSize + (int64_t)FFMAX(s->xmin, 0) >= INT_MAX / tile_x )
1222 return AVERROR_INVALIDDATA;
1223
3/4
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 38 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 66 times.
104 if (tile_y && s->tile_attr.ySize + (int64_t)FFMAX(s->ymin, 0) >= INT_MAX / tile_y )
1224 return AVERROR_INVALIDDATA;
1225
1226 104 line = s->ymin + s->tile_attr.ySize * tile_y;
1227 104 col = s->tile_attr.xSize * tile_x;
1228
1229
3/6
✓ Branch 0 taken 104 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 104 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 104 times.
✗ Branch 5 not taken.
104 if (line < s->ymin || line > s->ymax ||
1230
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 104 times.
104 s->xmin + col < s->xmin || s->xmin + col > s->xmax)
1231 return AVERROR_INVALIDDATA;
1232
1233 104 td->ysize = FFMIN(s->tile_attr.ySize, s->ydelta - tile_y * s->tile_attr.ySize);
1234 104 td->xsize = FFMIN(s->tile_attr.xSize, s->xdelta - tile_x * s->tile_attr.xSize);
1235
1236
2/4
✓ Branch 0 taken 104 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 104 times.
208 if (td->xsize * (uint64_t)s->current_channel_offset > INT_MAX ||
1237 104 av_image_check_size2(td->xsize, td->ysize, s->avctx->max_pixels, AV_PIX_FMT_NONE, 0, s->avctx) < 0)
1238 return AVERROR_INVALIDDATA;
1239
1240 104 td->channel_line_size = td->xsize * s->current_channel_offset;/* uncompress size of one line */
1241 104 uncompressed_size = td->channel_line_size * (uint64_t)td->ysize;/* uncompress size of the block */
1242 } else {
1243
2/4
✓ Branch 0 taken 37163 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 37163 times.
37163 if (buf_size < 8 || line_offset > buf_size - 8)
1244 return AVERROR_INVALIDDATA;
1245
1246 37163 src = buf + line_offset + 8;
1247
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37163 times.
37163 if (s->is_multipart)
1248 src += 4;
1249 37163 line = AV_RL32(src - 8);
1250
1251
2/4
✓ Branch 0 taken 37163 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 37163 times.
37163 if (line < s->ymin || line > s->ymax)
1252 return AVERROR_INVALIDDATA;
1253
1254 37163 data_size = AV_RL32(src - 4);
1255
2/4
✓ Branch 0 taken 37163 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 37163 times.
37163 if (data_size <= 0 || data_size > buf_size - line_offset - 8)
1256 return AVERROR_INVALIDDATA;
1257
1258 37163 td->ysize = FFMIN(s->scan_lines_per_block, s->ymax - line + 1); /* s->ydelta - line ?? */
1259 37163 td->xsize = s->xdelta;
1260
1261
2/4
✓ Branch 0 taken 37163 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 37163 times.
74326 if (td->xsize * (uint64_t)s->current_channel_offset > INT_MAX ||
1262 37163 av_image_check_size2(td->xsize, td->ysize, s->avctx->max_pixels, AV_PIX_FMT_NONE, 0, s->avctx) < 0)
1263 return AVERROR_INVALIDDATA;
1264
1265 37163 td->channel_line_size = td->xsize * s->current_channel_offset;/* uncompress size of one line */
1266 37163 uncompressed_size = td->channel_line_size * (uint64_t)td->ysize;/* uncompress size of the block */
1267
1268
3/4
✓ Branch 0 taken 11927 times.
✓ Branch 1 taken 25236 times.
✓ Branch 2 taken 11927 times.
✗ Branch 3 not taken.
37163 if ((s->compression == EXR_RAW && (data_size != uncompressed_size ||
1269
1/2
✓ Branch 0 taken 11927 times.
✗ Branch 1 not taken.
11927 line_offset > buf_size - uncompressed_size)) ||
1270
3/4
✓ Branch 0 taken 25236 times.
✓ Branch 1 taken 11927 times.
✓ Branch 2 taken 25236 times.
✗ Branch 3 not taken.
37163 (s->compression != EXR_RAW && (data_size > uncompressed_size ||
1271
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25236 times.
25236 line_offset > buf_size - data_size))) {
1272 return AVERROR_INVALIDDATA;
1273 }
1274 }
1275
1276 37267 window_xmin = FFMIN(avctx->width, FFMAX(0, s->xmin + col));
1277 37267 window_xmax = FFMIN(avctx->width, FFMAX(0, s->xmin + col + td->xsize));
1278 37267 window_ymin = FFMIN(avctx->height, FFMAX(0, line ));
1279 37267 window_ymax = FFMIN(avctx->height, FFMAX(0, line + td->ysize));
1280 37267 xsize = window_xmax - window_xmin;
1281 37267 ysize = window_ymax - window_ymin;
1282
1283 /* tile or scanline not visible skip decoding */
1284
4/4
✓ Branch 0 taken 37237 times.
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 403 times.
✓ Branch 3 taken 36834 times.
37267 if (xsize <= 0 || ysize <= 0)
1285 433 return 0;
1286
1287 /* is the first tile or is a scanline */
1288
2/2
✓ Branch 0 taken 36794 times.
✓ Branch 1 taken 40 times.
36834 if(col == 0) {
1289 36794 window_xmin = 0;
1290 /* pixels to add at the left of the display window */
1291 36794 window_xoffset = FFMAX(0, s->xmin);
1292 /* bytes to add at the left of the display window */
1293 36794 bxmin = window_xoffset * step;
1294 }
1295
1296 /* is the last tile or is a scanline */
1297
2/2
✓ Branch 0 taken 36794 times.
✓ Branch 1 taken 40 times.
36834 if(col + td->xsize == s->xdelta) {
1298 36794 window_xmax = avctx->width;
1299 /* bytes to add at the right of the display window */
1300 36794 axmax = FFMAX(0, (avctx->width - (s->xmax + 1))) * step;
1301 }
1302
1303
2/4
✓ Branch 0 taken 36834 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 36834 times.
36834 if (avctx->max_pixels && uncompressed_size > avctx->max_pixels * 16LL)
1304 return AVERROR_INVALIDDATA;
1305
1306
4/4
✓ Branch 0 taken 19019 times.
✓ Branch 1 taken 17815 times.
✓ Branch 2 taken 13 times.
✓ Branch 3 taken 19006 times.
36834 if (data_size < uncompressed_size || s->is_tile) { /* td->tmp is use for tile reorganization */
1307 17828 av_fast_padded_malloc(&td->tmp, &td->tmp_size, uncompressed_size);
1308
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17828 times.
17828 if (!td->tmp)
1309 return AVERROR(ENOMEM);
1310 }
1311
1312
2/2
✓ Branch 0 taken 17815 times.
✓ Branch 1 taken 19019 times.
36834 if (data_size < uncompressed_size) {
1313 17815 av_fast_padded_malloc(&td->uncompressed_data,
1314 17815 &td->uncompressed_size, uncompressed_size + 64);/* Force 64 padding for AVX2 reorder_pixels dst */
1315
1316
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17815 times.
17815 if (!td->uncompressed_data)
1317 return AVERROR(ENOMEM);
1318
1319 17815 ret = AVERROR_INVALIDDATA;
1320
5/7
✓ Branch 0 taken 12853 times.
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 61 times.
✓ Branch 3 taken 4848 times.
✓ Branch 4 taken 23 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
17815 switch (s->compression) {
1321 12853 case EXR_ZIP1:
1322 case EXR_ZIP16:
1323 12853 ret = zip_uncompress(s, src, data_size, uncompressed_size, td);
1324 12853 break;
1325 30 case EXR_PIZ:
1326 30 ret = piz_uncompress(s, src, data_size, uncompressed_size, td);
1327 30 break;
1328 61 case EXR_PXR24:
1329 61 ret = pxr24_uncompress(s, src, data_size, uncompressed_size, td);
1330 61 break;
1331 4848 case EXR_RLE:
1332 4848 ret = rle_uncompress(s, src, data_size, uncompressed_size, td);
1333 4848 break;
1334 23 case EXR_B44:
1335 case EXR_B44A:
1336 23 ret = b44_uncompress(s, src, data_size, uncompressed_size, td);
1337 23 break;
1338 case EXR_DWAA:
1339 case EXR_DWAB:
1340 ret = dwa_uncompress(s, src, data_size, uncompressed_size, td);
1341 break;
1342 }
1343
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17815 times.
17815 if (ret < 0) {
1344 av_log(avctx, AV_LOG_ERROR, "decode_block() failed.\n");
1345 return ret;
1346 }
1347 17815 src = td->uncompressed_data;
1348 }
1349
1350 /* offsets to crop data outside display window */
1351
4/4
✓ Branch 0 taken 36724 times.
✓ Branch 1 taken 110 times.
✓ Branch 2 taken 2320 times.
✓ Branch 3 taken 34514 times.
36834 data_xoffset = FFABS(FFMIN(0, s->xmin + col)) * (s->pixel_type == EXR_HALF ? 2 : 4);
1352
2/2
✓ Branch 0 taken 36828 times.
✓ Branch 1 taken 6 times.
36834 data_yoffset = FFABS(FFMIN(0, line));
1353 36834 data_window_offset = (data_yoffset * td->channel_line_size) + data_xoffset;
1354
1355
2/2
✓ Branch 0 taken 25358 times.
✓ Branch 1 taken 11476 times.
36834 if (!s->is_luma) {
1356 25358 channel_buffer[0] = src + (td->xsize * s->channel_offsets[0]) + data_window_offset;
1357 25358 channel_buffer[1] = src + (td->xsize * s->channel_offsets[1]) + data_window_offset;
1358 25358 channel_buffer[2] = src + (td->xsize * s->channel_offsets[2]) + data_window_offset;
1359 25358 rgb_channel_count = 3;
1360 } else { /* put y data in the first channel_buffer */
1361 11476 channel_buffer[0] = src + (td->xsize * s->channel_offsets[1]) + data_window_offset;
1362 11476 rgb_channel_count = 1;
1363 }
1364
2/2
✓ Branch 0 taken 13595 times.
✓ Branch 1 taken 23239 times.
36834 if (s->channel_offsets[3] >= 0)
1365 13595 channel_buffer[3] = src + (td->xsize * s->channel_offsets[3]) + data_window_offset;
1366
1367
4/4
✓ Branch 0 taken 11478 times.
✓ Branch 1 taken 25356 times.
✓ Branch 2 taken 11468 times.
✓ Branch 3 taken 10 times.
73658 if (s->desc->flags & AV_PIX_FMT_FLAG_PLANAR || s->desc->nb_components == 1 ) {
1368 /* todo: change this when a floating point pixel format with luma with alpha is implemented */
1369
2/2
✓ Branch 0 taken 23229 times.
✓ Branch 1 taken 13595 times.
36824 int channel_count = s->channel_offsets[3] >= 0 ? 4 : rgb_channel_count;
1370
2/2
✓ Branch 0 taken 11476 times.
✓ Branch 1 taken 25348 times.
36824 if (s->is_luma) {
1371 11476 channel_buffer[1] = channel_buffer[0];
1372 11476 channel_buffer[2] = channel_buffer[0];
1373 }
1374
1375
2/2
✓ Branch 0 taken 101131 times.
✓ Branch 1 taken 36824 times.
137955 for (c = 0; c < channel_count; c++) {
1376 101131 int plane = s->desc->comp[c].plane;
1377 101131 ptr = p->data[plane] + window_ymin * p->linesize[plane] + (window_xmin * step);
1378
1379
2/2
✓ Branch 0 taken 146524 times.
✓ Branch 1 taken 101131 times.
247655 for (i = 0; i < ysize; i++, ptr += p->linesize[plane]) {
1380 const uint8_t *src;
1381
1382
2/2
✓ Branch 0 taken 24648 times.
✓ Branch 1 taken 121876 times.
146524 if (s->pixel_type == EXR_FLOAT ||
1383
1/2
✓ Branch 0 taken 24648 times.
✗ Branch 1 not taken.
24648 s->compression == EXR_DWAA ||
1384
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24648 times.
24648 s->compression == EXR_DWAB) {
1385 // 32-bit
1386 121876 uint8_t *ptr_x = ptr;
1387
1388 121876 src = channel_buffer[c];
1389
1390 // Zero out the start if xmin is not 0
1391 121876 memset(ptr_x, 0, bxmin);
1392 121876 ptr_x += 4 * window_xoffset;
1393
1394
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 121876 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
121876 if (trc_func && c < 3) {
1395 for (int x = 0; x < xsize; x++, ptr_x += 4) {
1396 float f = av_int2float(bytestream_get_le32(&src));
1397 AV_WN32A(ptr_x, av_float2int(trc_func(f)));
1398 }
1399
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 121876 times.
121876 } else if (one_gamma != 1.f) {
1400 for (int x = 0; x < xsize; x++, ptr_x += 4) {
1401 float f = av_int2float(bytestream_get_le32(&src));
1402 if (f > 0.0f && c < 3) /* avoid negative values */
1403 f = powf(f, one_gamma);
1404 AV_WN32A(ptr_x, av_float2int(f));
1405 }
1406 } else {
1407
2/2
✓ Branch 0 taken 42250824 times.
✓ Branch 1 taken 121876 times.
42372700 for (int x = 0; x < xsize; x++, ptr_x += 4)
1408 42250824 AV_WN32A(ptr_x, bytestream_get_le32(&src));
1409 }
1410 121876 memset(ptr_x, 0, axmax);
1411
1/2
✓ Branch 0 taken 24648 times.
✗ Branch 1 not taken.
24648 } else if (s->pixel_type == EXR_HALF) {
1412 24648 src = channel_buffer[c];
1413
1414 // Zero out the start if xmin is not 0
1415 24648 memset(ptr, 0, bxmin);
1416
1417 // 16-bit
1418
2/2
✓ Branch 0 taken 10514097 times.
✓ Branch 1 taken 24648 times.
10538745 for (x = window_xoffset; x < xsize + window_xoffset; x++) {
1419 10514097 int v = bytestream_get_le16(&src);
1420 10514097 AV_WN16(ptr + x * sizeof(uint16_t), v);
1421 }
1422 24648 memset(ptr + x * sizeof(uint16_t), 0, axmax);
1423 }
1424
1425 // Zero out the end if xmax+1 is not w
1426 146524 channel_buffer[c] += td->channel_line_size;
1427 }
1428 }
1429 } else {
1430
1431 av_assert1(s->pixel_type == EXR_UINT);
1432 10 ptr = p->data[0] + window_ymin * p->linesize[0] + (window_xmin * s->desc->nb_components * 2);
1433
1434
2/2
✓ Branch 0 taken 175 times.
✓ Branch 1 taken 10 times.
185 for (i = 0; i < ysize; i++, ptr += p->linesize[0]) {
1435
1436 const uint8_t * a;
1437 const uint8_t *rgb[3];
1438 uint16_t *ptr_x;
1439
1440
2/2
✓ Branch 0 taken 525 times.
✓ Branch 1 taken 175 times.
700 for (c = 0; c < rgb_channel_count; c++) {
1441 525 rgb[c] = channel_buffer[c];
1442 }
1443
1444
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 175 times.
175 if (channel_buffer[3])
1445 a = channel_buffer[3];
1446
1447 175 ptr_x = (uint16_t *) ptr;
1448
1449 // Zero out the start if xmin is not 0
1450 175 memset(ptr_x, 0, bxmin);
1451 175 ptr_x += window_xoffset * s->desc->nb_components;
1452
1453
2/2
✓ Branch 0 taken 5309 times.
✓ Branch 1 taken 175 times.
5484 for (x = 0; x < xsize; x++) {
1454
2/2
✓ Branch 0 taken 15927 times.
✓ Branch 1 taken 5309 times.
21236 for (c = 0; c < rgb_channel_count; c++) {
1455 15927 *ptr_x++ = bytestream_get_le32(&rgb[c]) >> 16;
1456 }
1457
1458
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5309 times.
5309 if (channel_buffer[3])
1459 *ptr_x++ = bytestream_get_le32(&a) >> 16;
1460 }
1461
1462 // Zero out the end if xmax+1 is not w
1463 175 memset(ptr_x, 0, axmax);
1464
1465 175 channel_buffer[0] += td->channel_line_size;
1466 175 channel_buffer[1] += td->channel_line_size;
1467 175 channel_buffer[2] += td->channel_line_size;
1468
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 175 times.
175 if (channel_buffer[3])
1469 channel_buffer[3] += td->channel_line_size;
1470 }
1471 }
1472
1473 36834 return 0;
1474 }
1475
1476 static void skip_header_chunk(EXRContext *s)
1477 {
1478 GetByteContext *gb = &s->gb;
1479
1480 while (bytestream2_get_bytes_left(gb) > 0) {
1481 if (!bytestream2_peek_byte(gb))
1482 break;
1483
1484 // Process unknown variables
1485 for (int i = 0; i < 2; i++) // value_name and value_type
1486 while (bytestream2_get_byte(gb) != 0);
1487
1488 // Skip variable length
1489 bytestream2_skip(gb, bytestream2_get_le32(gb));
1490 }
1491 }
1492
1493 /**
1494 * Check if the variable name corresponds to its data type.
1495 *
1496 * @param s the EXRContext
1497 * @param value_name name of the variable to check
1498 * @param value_type type of the variable to check
1499 * @param minimum_length minimum length of the variable data
1500 *
1501 * @return bytes to read containing variable data
1502 * -1 if variable is not found
1503 * 0 if buffer ended prematurely
1504 */
1505 32478 static int check_header_variable(EXRContext *s,
1506 const char *value_name,
1507 const char *value_type,
1508 unsigned int minimum_length)
1509 {
1510 32478 GetByteContext *gb = &s->gb;
1511 32478 int var_size = -1;
1512
1513
1/2
✓ Branch 1 taken 32478 times.
✗ Branch 2 not taken.
32478 if (bytestream2_get_bytes_left(gb) >= minimum_length &&
1514
2/2
✓ Branch 0 taken 2166 times.
✓ Branch 1 taken 30312 times.
32478 !strcmp(gb->buffer, value_name)) {
1515 // found value_name, jump to value_type (null terminated strings)
1516 2166 gb->buffer += strlen(value_name) + 1;
1517
1/2
✓ Branch 0 taken 2166 times.
✗ Branch 1 not taken.
2166 if (!strcmp(gb->buffer, value_type)) {
1518 2166 gb->buffer += strlen(value_type) + 1;
1519 2166 var_size = bytestream2_get_le32(gb);
1520 // don't go read past boundaries
1521
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2166 times.
2166 if (var_size > bytestream2_get_bytes_left(gb))
1522 var_size = 0;
1523 } else {
1524 // value_type not found, reset the buffer
1525 gb->buffer -= strlen(value_name) + 1;
1526 av_log(s->avctx, AV_LOG_WARNING,
1527 "Unknown data type %s for header variable %s.\n",
1528 value_type, value_name);
1529 }
1530 }
1531
1532 32478 return var_size;
1533 }
1534
1535 316 static int decode_header(EXRContext *s, AVFrame *frame)
1536 {
1537 316 AVDictionary *metadata = NULL;
1538 316 GetByteContext *gb = &s->gb;
1539 int magic_number, version, flags;
1540 316 int layer_match = 0;
1541 int ret;
1542 316 int dup_channels = 0;
1543
1544 316 s->current_channel_offset = 0;
1545 316 s->xmin = ~0;
1546 316 s->xmax = ~0;
1547 316 s->ymin = ~0;
1548 316 s->ymax = ~0;
1549 316 s->xdelta = ~0;
1550 316 s->ydelta = ~0;
1551 316 s->channel_offsets[0] = -1;
1552 316 s->channel_offsets[1] = -1;
1553 316 s->channel_offsets[2] = -1;
1554 316 s->channel_offsets[3] = -1;
1555 316 s->pixel_type = EXR_UNKNOWN;
1556 316 s->compression = EXR_UNKN;
1557 316 s->nb_channels = 0;
1558 316 s->w = 0;
1559 316 s->h = 0;
1560 316 s->tile_attr.xSize = -1;
1561 316 s->tile_attr.ySize = -1;
1562 316 s->is_tile = 0;
1563 316 s->is_multipart = 0;
1564 316 s->is_luma = 0;
1565 316 s->current_part = 0;
1566
1567
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 316 times.
316 if (bytestream2_get_bytes_left(gb) < 10) {
1568 av_log(s->avctx, AV_LOG_ERROR, "Header too short to parse.\n");
1569 return AVERROR_INVALIDDATA;
1570 }
1571
1572 316 magic_number = bytestream2_get_le32(gb);
1573
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 316 times.
316 if (magic_number != 20000630) {
1574 /* As per documentation of OpenEXR, it is supposed to be
1575 * int 20000630 little-endian */
1576 av_log(s->avctx, AV_LOG_ERROR, "Wrong magic number %d.\n", magic_number);
1577 return AVERROR_INVALIDDATA;
1578 }
1579
1580 316 version = bytestream2_get_byte(gb);
1581
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 316 times.
316 if (version != 2) {
1582 avpriv_report_missing_feature(s->avctx, "Version %d", version);
1583 return AVERROR_PATCHWELCOME;
1584 }
1585
1586 316 flags = bytestream2_get_le24(gb);
1587
1588
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 272 times.
316 if (flags & 0x02)
1589 44 s->is_tile = 1;
1590
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 316 times.
316 if (flags & 0x10)
1591 s->is_multipart = 1;
1592
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 316 times.
316 if (flags & 0x08) {
1593 avpriv_report_missing_feature(s->avctx, "deep data");
1594 return AVERROR_PATCHWELCOME;
1595 }
1596
1597 // Parse the header
1598
1/2
✓ Branch 1 taken 4396 times.
✗ Branch 2 not taken.
4396 while (bytestream2_get_bytes_left(gb) > 0) {
1599 int var_size;
1600
1601
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 4396 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
4396 while (s->is_multipart && s->current_part < s->selected_part &&
1602 bytestream2_get_bytes_left(gb) > 0) {
1603 if (bytestream2_peek_byte(gb)) {
1604 skip_header_chunk(s);
1605 } else {
1606 bytestream2_skip(gb, 1);
1607 if (!bytestream2_peek_byte(gb))
1608 break;
1609 }
1610 bytestream2_skip(gb, 1);
1611 s->current_part++;
1612 }
1613
1614
2/2
✓ Branch 1 taken 316 times.
✓ Branch 2 taken 4080 times.
4396 if (!bytestream2_peek_byte(gb)) {
1615
1/2
✓ Branch 0 taken 316 times.
✗ Branch 1 not taken.
316 if (!s->is_multipart)
1616 316 break;
1617 bytestream2_skip(gb, 1);
1618 if (s->current_part == s->selected_part) {
1619 while (bytestream2_get_bytes_left(gb) > 0) {
1620 if (bytestream2_peek_byte(gb)) {
1621 skip_header_chunk(s);
1622 } else {
1623 bytestream2_skip(gb, 1);
1624 if (!bytestream2_peek_byte(gb))
1625 break;
1626 }
1627 }
1628 }
1629 if (!bytestream2_peek_byte(gb))
1630 break;
1631 s->current_part++;
1632 }
1633
1634
2/2
✓ Branch 1 taken 316 times.
✓ Branch 2 taken 3764 times.
4080 if ((var_size = check_header_variable(s, "channels",
1635 316 "chlist", 38)) >= 0) {
1636 GetByteContext ch_gb;
1637
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 316 times.
316 if (!var_size) {
1638 ret = AVERROR_INVALIDDATA;
1639 goto fail;
1640 }
1641
1642 316 bytestream2_init(&ch_gb, gb->buffer, var_size);
1643
1644
2/2
✓ Branch 1 taken 1212 times.
✓ Branch 2 taken 316 times.
1528 while (bytestream2_get_bytes_left(&ch_gb) >= 19) {
1645 EXRChannel *channel;
1646 enum ExrPixelType current_pixel_type;
1647 1212 int channel_index = -1;
1648 int xsub, ysub;
1649
1650
2/2
✓ Branch 0 taken 272 times.
✓ Branch 1 taken 940 times.
1212 if (strcmp(s->layer, "") != 0) {
1651
2/2
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 122 times.
272 if (strncmp(ch_gb.buffer, s->layer, strlen(s->layer)) == 0) {
1652 150 layer_match = 1;
1653 150 av_log(s->avctx, AV_LOG_INFO,
1654 "Channel match layer : %s.\n", ch_gb.buffer);
1655 150 ch_gb.buffer += strlen(s->layer);
1656
1/2
✓ Branch 0 taken 150 times.
✗ Branch 1 not taken.
150 if (*ch_gb.buffer == '.')
1657 150 ch_gb.buffer++; /* skip dot if not given */
1658 } else {
1659 122 layer_match = 0;
1660 122 av_log(s->avctx, AV_LOG_INFO,
1661 "Channel doesn't match layer : %s.\n", ch_gb.buffer);
1662 }
1663 } else {
1664 940 layer_match = 1;
1665 }
1666
1667
2/2
✓ Branch 0 taken 1090 times.
✓ Branch 1 taken 122 times.
1212 if (layer_match) { /* only search channel if the layer match is valid */
1668
3/4
✓ Branch 1 taken 836 times.
✓ Branch 2 taken 254 times.
✓ Branch 3 taken 836 times.
✗ Branch 4 not taken.
1926 if (!av_strcasecmp(ch_gb.buffer, "R") ||
1669
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 836 times.
1672 !av_strcasecmp(ch_gb.buffer, "X") ||
1670 836 !av_strcasecmp(ch_gb.buffer, "U")) {
1671 254 channel_index = 0;
1672 254 s->is_luma = 0;
1673
3/4
✓ Branch 1 taken 582 times.
✓ Branch 2 taken 254 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 582 times.
1418 } else if (!av_strcasecmp(ch_gb.buffer, "G") ||
1674 582 !av_strcasecmp(ch_gb.buffer, "V")) {
1675 254 channel_index = 1;
1676 254 s->is_luma = 0;
1677
2/2
✓ Branch 1 taken 62 times.
✓ Branch 2 taken 520 times.
582 } else if (!av_strcasecmp(ch_gb.buffer, "Y")) {
1678 62 channel_index = 1;
1679 62 s->is_luma = 1;
1680
3/4
✓ Branch 1 taken 266 times.
✓ Branch 2 taken 254 times.
✓ Branch 3 taken 266 times.
✗ Branch 4 not taken.
786 } else if (!av_strcasecmp(ch_gb.buffer, "B") ||
1681
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 266 times.
532 !av_strcasecmp(ch_gb.buffer, "Z") ||
1682 266 !av_strcasecmp(ch_gb.buffer, "W")) {
1683 254 channel_index = 2;
1684 254 s->is_luma = 0;
1685
2/2
✓ Branch 1 taken 114 times.
✓ Branch 2 taken 152 times.
266 } else if (!av_strcasecmp(ch_gb.buffer, "A")) {
1686 114 channel_index = 3;
1687 } else {
1688 152 av_log(s->avctx, AV_LOG_WARNING,
1689 "Unsupported channel %.256s.\n", ch_gb.buffer);
1690 }
1691 }
1692
1693 /* skip until you get a 0 */
1694
3/4
✓ Branch 1 taken 4856 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3644 times.
✓ Branch 4 taken 1212 times.
9712 while (bytestream2_get_bytes_left(&ch_gb) > 0 &&
1695 4856 bytestream2_get_byte(&ch_gb))
1696 3644 continue;
1697
1698
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1212 times.
1212 if (bytestream2_get_bytes_left(&ch_gb) < 4) {
1699 av_log(s->avctx, AV_LOG_ERROR, "Incomplete header.\n");
1700 ret = AVERROR_INVALIDDATA;
1701 goto fail;
1702 }
1703
1704 1212 current_pixel_type = bytestream2_get_le32(&ch_gb);
1705
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1212 times.
1212 if (current_pixel_type >= EXR_UNKNOWN) {
1706 avpriv_report_missing_feature(s->avctx, "Pixel type %d",
1707 current_pixel_type);
1708 ret = AVERROR_PATCHWELCOME;
1709 goto fail;
1710 }
1711
1712 1212 bytestream2_skip(&ch_gb, 4);
1713 1212 xsub = bytestream2_get_le32(&ch_gb);
1714 1212 ysub = bytestream2_get_le32(&ch_gb);
1715
1716
2/4
✓ Branch 0 taken 1212 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1212 times.
1212 if (xsub != 1 || ysub != 1) {
1717 avpriv_report_missing_feature(s->avctx,
1718 "Subsampling %dx%d",
1719 xsub, ysub);
1720 ret = AVERROR_PATCHWELCOME;
1721 goto fail;
1722 }
1723
1724
3/4
✓ Branch 0 taken 938 times.
✓ Branch 1 taken 274 times.
✓ Branch 2 taken 938 times.
✗ Branch 3 not taken.
1212 if (channel_index >= 0 && s->channel_offsets[channel_index] == -1) { /* channel has not been previously assigned */
1725
2/2
✓ Branch 0 taken 622 times.
✓ Branch 1 taken 316 times.
938 if (s->pixel_type != EXR_UNKNOWN &&
1726
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 622 times.
622 s->pixel_type != current_pixel_type) {
1727 av_log(s->avctx, AV_LOG_ERROR,
1728 "RGB channels not of the same depth.\n");
1729 ret = AVERROR_INVALIDDATA;
1730 goto fail;
1731 }
1732 938 s->pixel_type = current_pixel_type;
1733 938 s->channel_offsets[channel_index] = s->current_channel_offset;
1734
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 274 times.
274 } else if (channel_index >= 0) {
1735 av_log(s->avctx, AV_LOG_WARNING,
1736 "Multiple channels with index %d.\n", channel_index);
1737 if (++dup_channels > 10) {
1738 ret = AVERROR_INVALIDDATA;
1739 goto fail;
1740 }
1741 }
1742
1743 2424 s->channels = av_realloc(s->channels,
1744 1212 ++s->nb_channels * sizeof(EXRChannel));
1745
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1212 times.
1212 if (!s->channels) {
1746 ret = AVERROR(ENOMEM);
1747 goto fail;
1748 }
1749 1212 channel = &s->channels[s->nb_channels - 1];
1750 1212 channel->pixel_type = current_pixel_type;
1751 1212 channel->xsub = xsub;
1752 1212 channel->ysub = ysub;
1753
1754
2/2
✓ Branch 0 taken 404 times.
✓ Branch 1 taken 808 times.
1212 if (current_pixel_type == EXR_HALF) {
1755 404 s->current_channel_offset += 2;
1756 } else {/* Float or UINT32 */
1757 808 s->current_channel_offset += 4;
1758 }
1759 }
1760
1761 /* Check if all channels are set with an offset or if the channels
1762 * are causing an overflow */
1763
2/2
✓ Branch 0 taken 254 times.
✓ Branch 1 taken 62 times.
316 if (!s->is_luma) {/* if we expected to have at least 3 channels */
1764
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 254 times.
254 if (FFMIN3(s->channel_offsets[0],
1765 s->channel_offsets[1],
1766 s->channel_offsets[2]) < 0) {
1767 if (s->channel_offsets[0] < 0)
1768 av_log(s->avctx, AV_LOG_ERROR, "Missing red channel.\n");
1769 if (s->channel_offsets[1] < 0)
1770 av_log(s->avctx, AV_LOG_ERROR, "Missing green channel.\n");
1771 if (s->channel_offsets[2] < 0)
1772 av_log(s->avctx, AV_LOG_ERROR, "Missing blue channel.\n");
1773 ret = AVERROR_INVALIDDATA;
1774 goto fail;
1775 }
1776 }
1777
1778 // skip one last byte and update main gb
1779 316 gb->buffer = ch_gb.buffer + 1;
1780 316 continue;
1781
2/2
✓ Branch 1 taken 316 times.
✓ Branch 2 taken 3448 times.
3764 } else if ((var_size = check_header_variable(s, "dataWindow", "box2i",
1782 316 31)) >= 0) {
1783 int xmin, ymin, xmax, ymax;
1784
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 316 times.
316 if (!var_size) {
1785 ret = AVERROR_INVALIDDATA;
1786 goto fail;
1787 }
1788
1789 316 xmin = bytestream2_get_le32(gb);
1790 316 ymin = bytestream2_get_le32(gb);
1791 316 xmax = bytestream2_get_le32(gb);
1792 316 ymax = bytestream2_get_le32(gb);
1793
1794
3/6
✓ Branch 0 taken 316 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 316 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 316 times.
✗ Branch 5 not taken.
316 if (xmin > xmax || ymin > ymax ||
1795
1/2
✓ Branch 0 taken 316 times.
✗ Branch 1 not taken.
316 ymax == INT_MAX || xmax == INT_MAX ||
1796
1/2
✓ Branch 0 taken 316 times.
✗ Branch 1 not taken.
316 (unsigned)xmax - xmin >= INT_MAX ||
1797
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 316 times.
316 (unsigned)ymax - ymin >= INT_MAX) {
1798 ret = AVERROR_INVALIDDATA;
1799 goto fail;
1800 }
1801 316 s->xmin = xmin;
1802 316 s->xmax = xmax;
1803 316 s->ymin = ymin;
1804 316 s->ymax = ymax;
1805 316 s->xdelta = (s->xmax - s->xmin) + 1;
1806 316 s->ydelta = (s->ymax - s->ymin) + 1;
1807
1808 316 continue;
1809
2/2
✓ Branch 1 taken 316 times.
✓ Branch 2 taken 3132 times.
3448 } else if ((var_size = check_header_variable(s, "displayWindow",
1810 316 "box2i", 34)) >= 0) {
1811 int32_t sx, sy, dx, dy;
1812
1813
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 316 times.
316 if (!var_size) {
1814 ret = AVERROR_INVALIDDATA;
1815 goto fail;
1816 }
1817
1818 316 sx = bytestream2_get_le32(gb);
1819 316 sy = bytestream2_get_le32(gb);
1820 316 dx = bytestream2_get_le32(gb);
1821 316 dy = bytestream2_get_le32(gb);
1822
1823 316 s->w = (unsigned)dx - sx + 1;
1824 316 s->h = (unsigned)dy - sy + 1;
1825
1826 316 continue;
1827
2/2
✓ Branch 1 taken 316 times.
✓ Branch 2 taken 2816 times.
3132 } else if ((var_size = check_header_variable(s, "lineOrder",
1828 316 "lineOrder", 25)) >= 0) {
1829 int line_order;
1830
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 316 times.
316 if (!var_size) {
1831 ret = AVERROR_INVALIDDATA;
1832 goto fail;
1833 }
1834
1835 316 line_order = bytestream2_get_byte(gb);
1836 316 av_log(s->avctx, AV_LOG_DEBUG, "line order: %d.\n", line_order);
1837
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 316 times.
316 if (line_order > 2) {
1838 av_log(s->avctx, AV_LOG_ERROR, "Unknown line order.\n");
1839 ret = AVERROR_INVALIDDATA;
1840 goto fail;
1841 }
1842
1843 316 continue;
1844
2/2
✓ Branch 1 taken 148 times.
✓ Branch 2 taken 2668 times.
2816 } else if ((var_size = check_header_variable(s, "pixelAspectRatio",
1845 "float", 31)) >= 0) {
1846
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 148 times.
148 if (!var_size) {
1847 ret = AVERROR_INVALIDDATA;
1848 goto fail;
1849 }
1850
1851 148 s->sar = bytestream2_get_le32(gb);
1852
1853 148 continue;
1854
2/2
✓ Branch 1 taken 316 times.
✓ Branch 2 taken 2352 times.
2668 } else if ((var_size = check_header_variable(s, "compression",
1855 "compression", 29)) >= 0) {
1856
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 316 times.
316 if (!var_size) {
1857 ret = AVERROR_INVALIDDATA;
1858 goto fail;
1859 }
1860
1861
1/2
✓ Branch 0 taken 316 times.
✗ Branch 1 not taken.
316 if (s->compression == EXR_UNKN)
1862 316 s->compression = bytestream2_get_byte(gb);
1863 else {
1864 bytestream2_skip(gb, 1);
1865 av_log(s->avctx, AV_LOG_WARNING,
1866 "Found more than one compression attribute.\n");
1867 }
1868
1869 316 continue;
1870
2/2
✓ Branch 1 taken 44 times.
✓ Branch 2 taken 2308 times.
2352 } else if ((var_size = check_header_variable(s, "tiles",
1871 44 "tiledesc", 22)) >= 0) {
1872 uint8_t tileLevel;
1873
1874
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
44 if (!s->is_tile)
1875 av_log(s->avctx, AV_LOG_WARNING,
1876 "Found tile attribute and scanline flags. Exr will be interpreted as scanline.\n");
1877
1878 44 s->tile_attr.xSize = bytestream2_get_le32(gb);
1879 44 s->tile_attr.ySize = bytestream2_get_le32(gb);
1880
1881 44 tileLevel = bytestream2_get_byte(gb);
1882 44 s->tile_attr.level_mode = tileLevel & 0x0f;
1883 44 s->tile_attr.level_round = (tileLevel >> 4) & 0x0f;
1884
1885
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
44 if (s->tile_attr.level_mode >= EXR_TILE_LEVEL_UNKNOWN) {
1886 avpriv_report_missing_feature(s->avctx, "Tile level mode %d",
1887 s->tile_attr.level_mode);
1888 ret = AVERROR_PATCHWELCOME;
1889 goto fail;
1890 }
1891
1892
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
44 if (s->tile_attr.level_round >= EXR_TILE_ROUND_UNKNOWN) {
1893 avpriv_report_missing_feature(s->avctx, "Tile level round %d",
1894 s->tile_attr.level_round);
1895 ret = AVERROR_PATCHWELCOME;
1896 goto fail;
1897 }
1898
1899 44 continue;
1900
2/2
✓ Branch 1 taken 184 times.
✓ Branch 2 taken 2124 times.
2308 } else if ((var_size = check_header_variable(s, "writer",
1901 184 "string", 1)) >= 0) {
1902 184 uint8_t key[256] = { 0 };
1903
1904 184 bytestream2_get_buffer(gb, key, FFMIN(sizeof(key) - 1, var_size));
1905 184 av_dict_set(&metadata, "writer", key, 0);
1906
1907 184 continue;
1908
2/2
✓ Branch 1 taken 184 times.
✓ Branch 2 taken 1940 times.
2124 } else if ((var_size = check_header_variable(s, "framesPerSecond",
1909 "rational", 33)) >= 0) {
1910
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 184 times.
184 if (!var_size) {
1911 ret = AVERROR_INVALIDDATA;
1912 goto fail;
1913 }
1914
1915 184 s->avctx->framerate.num = bytestream2_get_le32(gb);
1916 184 s->avctx->framerate.den = bytestream2_get_le32(gb);
1917
1918 184 continue;
1919
2/2
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 1932 times.
1940 } else if ((var_size = check_header_variable(s, "chunkCount",
1920 "int", 23)) >= 0) {
1921
1922 8 s->chunk_count = bytestream2_get_le32(gb);
1923
1924 8 continue;
1925
2/2
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 1914 times.
1932 } else if ((var_size = check_header_variable(s, "type",
1926 18 "string", 16)) >= 0) {
1927 18 uint8_t key[256] = { 0 };
1928
1929 18 bytestream2_get_buffer(gb, key, FFMIN(sizeof(key) - 1, var_size));
1930
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 10 times.
18 if (strncmp("scanlineimage", key, var_size) &&
1931
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 strncmp("tiledimage", key, var_size)) {
1932 ret = AVERROR_PATCHWELCOME;
1933 goto fail;
1934 }
1935
1936 18 continue;
1937
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1914 times.
1914 } else if ((var_size = check_header_variable(s, "preview",
1938 "preview", 16)) >= 0) {
1939 uint32_t pw = bytestream2_get_le32(gb);
1940 uint32_t ph = bytestream2_get_le32(gb);
1941 uint64_t psize = pw * (uint64_t)ph;
1942 if (psize > INT64_MAX / 4) {
1943 ret = AVERROR_INVALIDDATA;
1944 goto fail;
1945 }
1946 psize *= 4;
1947
1948 if ((int64_t)psize >= bytestream2_get_bytes_left(gb)) {
1949 ret = AVERROR_INVALIDDATA;
1950 goto fail;
1951 }
1952
1953 bytestream2_skip(gb, psize);
1954
1955 continue;
1956 }
1957
1958 // Check if there are enough bytes for a header
1959
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1914 times.
1914 if (bytestream2_get_bytes_left(gb) <= 9) {
1960 av_log(s->avctx, AV_LOG_ERROR, "Incomplete header\n");
1961 ret = AVERROR_INVALIDDATA;
1962 goto fail;
1963 }
1964
1965 // Process unknown variables
1966 {
1967 1914 uint8_t name[256] = { 0 };
1968 1914 uint8_t type[256] = { 0 };
1969 1914 uint8_t value[8192] = { 0 };
1970 1914 int i = 0, size;
1971
1972
2/2
✓ Branch 1 taken 26926 times.
✓ Branch 2 taken 1914 times.
57680 while (bytestream2_get_bytes_left(gb) > 0 &&
1973
2/4
✓ Branch 0 taken 28840 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 26926 times.
✗ Branch 4 not taken.
55766 bytestream2_peek_byte(gb) && i < 255) {
1974 26926 name[i++] = bytestream2_get_byte(gb);
1975 }
1976
1977 1914 bytestream2_skip(gb, 1);
1978 1914 i = 0;
1979
2/2
✓ Branch 1 taken 8774 times.
✓ Branch 2 taken 1914 times.
21376 while (bytestream2_get_bytes_left(gb) > 0 &&
1980
2/4
✓ Branch 0 taken 10688 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 8774 times.
✗ Branch 4 not taken.
19462 bytestream2_peek_byte(gb) && i < 255) {
1981 8774 type[i++] = bytestream2_get_byte(gb);
1982 }
1983 1914 bytestream2_skip(gb, 1);
1984 1914 size = bytestream2_get_le32(gb);
1985
1986 1914 bytestream2_get_buffer(gb, value, FFMIN(sizeof(value) - 1, size));
1987
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1914 times.
1914 if (size > sizeof(value) - 1)
1988 bytestream2_skip(gb, size - (sizeof(value) - 1));
1989
2/2
✓ Branch 0 taken 160 times.
✓ Branch 1 taken 1754 times.
1914 if (!strcmp(type, "string"))
1990 160 av_dict_set(&metadata, name, value, 0);
1991 }
1992 }
1993
1994
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 316 times.
316 if (s->compression == EXR_UNKN) {
1995 av_log(s->avctx, AV_LOG_ERROR, "Missing compression attribute.\n");
1996 ret = AVERROR_INVALIDDATA;
1997 goto fail;
1998 }
1999
2000
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 272 times.
316 if (s->is_tile) {
2001
2/4
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 44 times.
44 if (s->tile_attr.xSize < 1 || s->tile_attr.ySize < 1) {
2002 av_log(s->avctx, AV_LOG_ERROR, "Invalid tile attribute.\n");
2003 ret = AVERROR_INVALIDDATA;
2004 goto fail;
2005 }
2006 }
2007
2008
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 316 times.
316 if (bytestream2_get_bytes_left(gb) <= 0) {
2009 av_log(s->avctx, AV_LOG_ERROR, "Incomplete frame.\n");
2010 ret = AVERROR_INVALIDDATA;
2011 goto fail;
2012 }
2013
2014 316 frame->metadata = metadata;
2015
2016 // aaand we are done
2017 316 bytestream2_skip(gb, 1);
2018 316 return 0;
2019 fail:
2020 av_dict_free(&metadata);
2021 return ret;
2022 }
2023
2024 316 static int decode_frame(AVCodecContext *avctx, AVFrame *picture,
2025 int *got_frame, AVPacket *avpkt)
2026 {
2027 316 EXRContext *s = avctx->priv_data;
2028 316 GetByteContext *gb = &s->gb;
2029 uint8_t *ptr;
2030
2031 int i, y, ret, ymax;
2032 int planes;
2033 int out_line_size;
2034 int nb_blocks; /* nb scanline or nb tile */
2035 uint64_t start_offset_table;
2036 uint64_t start_next_scanline;
2037
2038 316 bytestream2_init(gb, avpkt->data, avpkt->size);
2039
2040
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 316 times.
316 if ((ret = decode_header(s, picture)) < 0)
2041 return ret;
2042
2043
2/4
✓ Branch 0 taken 316 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 316 times.
316 if ((s->compression == EXR_DWAA || s->compression == EXR_DWAB) &&
2044 s->pixel_type == EXR_HALF) {
2045 s->current_channel_offset *= 2;
2046 for (int i = 0; i < 4; i++)
2047 s->channel_offsets[i] *= 2;
2048 }
2049
2050
3/4
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 218 times.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
316 switch (s->pixel_type) {
2051 88 case EXR_HALF:
2052
2/4
✓ Branch 0 taken 88 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 88 times.
✗ Branch 3 not taken.
88 if (!(s->compression == EXR_DWAA || s->compression == EXR_DWAB)) {
2053
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 62 times.
88 if (s->channel_offsets[3] >= 0) {
2054
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 2 times.
26 if (!s->is_luma) {
2055 24 avctx->pix_fmt = AV_PIX_FMT_GBRAPF16;
2056 } else {
2057 /* todo: change this when a floating point pixel format with luma with alpha is implemented */
2058 2 avctx->pix_fmt = AV_PIX_FMT_GBRAPF16;
2059 }
2060 } else {
2061
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 4 times.
62 if (!s->is_luma) {
2062 58 avctx->pix_fmt = AV_PIX_FMT_GBRPF16;
2063 } else {
2064 4 avctx->pix_fmt = AV_PIX_FMT_GRAYF16;
2065 }
2066 }
2067 88 break;
2068 }
2069 case EXR_FLOAT:
2070
2/2
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 130 times.
218 if (s->channel_offsets[3] >= 0) {
2071
1/2
✓ Branch 0 taken 88 times.
✗ Branch 1 not taken.
88 if (!s->is_luma) {
2072 88 avctx->pix_fmt = AV_PIX_FMT_GBRAPF32;
2073 } else {
2074 /* todo: change this when a floating point pixel format with luma with alpha is implemented */
2075 avctx->pix_fmt = AV_PIX_FMT_GBRAPF32;
2076 }
2077 } else {
2078
2/2
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 56 times.
130 if (!s->is_luma) {
2079 74 avctx->pix_fmt = AV_PIX_FMT_GBRPF32;
2080 } else {
2081 56 avctx->pix_fmt = AV_PIX_FMT_GRAYF32;
2082 }
2083 }
2084 218 break;
2085 10 case EXR_UINT:
2086
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (s->channel_offsets[3] >= 0) {
2087 if (!s->is_luma) {
2088 avctx->pix_fmt = AV_PIX_FMT_RGBA64;
2089 } else {
2090 avctx->pix_fmt = AV_PIX_FMT_YA16;
2091 }
2092 } else {
2093
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 if (!s->is_luma) {
2094 10 avctx->pix_fmt = AV_PIX_FMT_RGB48;
2095 } else {
2096 avctx->pix_fmt = AV_PIX_FMT_GRAY16;
2097 }
2098 }
2099 10 break;
2100 default:
2101 av_log(avctx, AV_LOG_ERROR, "Missing channel list.\n");
2102 return AVERROR_INVALIDDATA;
2103 }
2104
2105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 316 times.
316 if (s->apply_trc_type != AVCOL_TRC_UNSPECIFIED)
2106 avctx->color_trc = s->apply_trc_type;
2107
2/4
✓ Branch 0 taken 316 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 316 times.
✗ Branch 3 not taken.
316 else if (s->gamma > 0.9999f && s->gamma < 1.0001f)
2108 316 avctx->color_trc = AVCOL_TRC_LINEAR;
2109
2110
3/5
✓ Branch 0 taken 174 times.
✓ Branch 1 taken 92 times.
✓ Branch 2 taken 50 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
316 switch (s->compression) {
2111 174 case EXR_RAW:
2112 case EXR_RLE:
2113 case EXR_ZIP1:
2114 174 s->scan_lines_per_block = 1;
2115 174 break;
2116 92 case EXR_PXR24:
2117 case EXR_ZIP16:
2118 92 s->scan_lines_per_block = 16;
2119 92 break;
2120 50 case EXR_PIZ:
2121 case EXR_B44:
2122 case EXR_B44A:
2123 case EXR_DWAA:
2124 50 s->scan_lines_per_block = 32;
2125 50 break;
2126 case EXR_DWAB:
2127 s->scan_lines_per_block = 256;
2128 break;
2129 default:
2130 avpriv_report_missing_feature(avctx, "Compression %d", s->compression);
2131 return AVERROR_PATCHWELCOME;
2132 }
2133
2134 /* Verify the xmin, xmax, ymin and ymax before setting the actual image size.
2135 * It's possible for the data window can larger or outside the display window */
2136
2/4
✓ Branch 0 taken 316 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 316 times.
✗ Branch 3 not taken.
316 if (s->xmin > s->xmax || s->ymin > s->ymax ||
2137
2/4
✓ Branch 0 taken 316 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 316 times.
316 s->ydelta == 0xFFFFFFFF || s->xdelta == 0xFFFFFFFF) {
2138 av_log(avctx, AV_LOG_ERROR, "Wrong or missing size information.\n");
2139 return AVERROR_INVALIDDATA;
2140 }
2141
2142
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 316 times.
316 if ((ret = ff_set_dimensions(avctx, s->w, s->h)) < 0)
2143 return ret;
2144
2145 316 ff_set_sar(s->avctx, av_d2q(av_int2float(s->sar), 255));
2146
2147
2/2
✓ Branch 0 taken 86 times.
✓ Branch 1 taken 230 times.
316 if (avctx->skip_frame >= AVDISCARD_ALL)
2148 86 return avpkt->size;
2149
2150 230 s->desc = av_pix_fmt_desc_get(avctx->pix_fmt);
2151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 230 times.
230 if (!s->desc)
2152 return AVERROR_INVALIDDATA;
2153
2154
2/2
✓ Branch 0 taken 171 times.
✓ Branch 1 taken 59 times.
230 if (s->desc->flags & AV_PIX_FMT_FLAG_PLANAR) {
2155 171 planes = s->desc->nb_components;
2156 } else {
2157 59 planes = 1;
2158 }
2159 230 out_line_size = avctx->width * s->desc->comp[0].step;
2160
2161
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 208 times.
230 if (s->is_tile) {
2162 22 nb_blocks = ((s->xdelta + s->tile_attr.xSize - 1) / s->tile_attr.xSize) *
2163 22 ((s->ydelta + s->tile_attr.ySize - 1) / s->tile_attr.ySize);
2164 } else { /* scanline */
2165 208 nb_blocks = (s->ydelta + s->scan_lines_per_block - 1) /
2166 208 s->scan_lines_per_block;
2167 }
2168
2169
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 230 times.
230 if ((ret = ff_thread_get_buffer(avctx, picture, 0)) < 0)
2170 return ret;
2171
2172
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 230 times.
230 if (bytestream2_get_bytes_left(gb)/8 < nb_blocks)
2173 return AVERROR_INVALIDDATA;
2174
2175 // check offset table and recreate it if need
2176
4/4
✓ Branch 0 taken 208 times.
✓ Branch 1 taken 22 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 207 times.
230 if (!s->is_tile && bytestream2_peek_le64(gb) == 0) {
2177 PutByteContext offset_table_writer;
2178
2179 1 av_log(s->avctx, AV_LOG_DEBUG, "recreating invalid scanline offset table\n");
2180
2181 1 s->offset_table = av_realloc_f(s->offset_table, nb_blocks, 8);
2182
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!s->offset_table)
2183 return AVERROR(ENOMEM);
2184
2185 1 start_offset_table = bytestream2_tell(gb);
2186 1 start_next_scanline = start_offset_table + nb_blocks * 8;
2187 1 bytestream2_init_writer(&offset_table_writer, s->offset_table, nb_blocks * 8);
2188
2189
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 times.
9 for (y = 0; y < nb_blocks; y++) {
2190 /* write offset of prev scanline in offset table */
2191 8 bytestream2_put_le64(&offset_table_writer, start_next_scanline);
2192
2193 /* get len of next scanline */
2194 8 bytestream2_seek(gb, start_next_scanline + 4, SEEK_SET);/* skip line number */
2195 8 start_next_scanline += (bytestream2_get_le32(gb) + 8);
2196 }
2197 1 bytestream2_init(gb, s->offset_table, nb_blocks * 8);
2198 }
2199
2200 // save pointer we are going to use in decode_block
2201 230 s->buf = avpkt->data;
2202 230 s->buf_size = avpkt->size;
2203
2204 // Zero out the start if ymin is not 0
2205
2/2
✓ Branch 0 taken 653 times.
✓ Branch 1 taken 230 times.
883 for (i = 0; i < planes; i++) {
2206 653 ptr = picture->data[i];
2207
2/2
✓ Branch 0 taken 240 times.
✓ Branch 1 taken 653 times.
893 for (y = 0; y < FFMIN(s->ymin, s->h); y++) {
2208 240 memset(ptr, 0, out_line_size);
2209 240 ptr += picture->linesize[i];
2210 }
2211 }
2212
2213 230 s->picture = picture;
2214
2215 230 avctx->execute2(avctx, decode_block, s->thread_data, NULL, nb_blocks);
2216
2217 230 ymax = FFMAX(0, s->ymax + 1);
2218 // Zero out the end if ymax+1 is not h
2219
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 226 times.
230 if (ymax < avctx->height)
2220
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 4 times.
16 for (i = 0; i < planes; i++) {
2221 12 ptr = picture->data[i] + (ymax * picture->linesize[i]);
2222
2/2
✓ Branch 0 taken 2772 times.
✓ Branch 1 taken 12 times.
2784 for (y = ymax; y < avctx->height; y++) {
2223 2772 memset(ptr, 0, out_line_size);
2224 2772 ptr += picture->linesize[i];
2225 }
2226 }
2227
2228 230 picture->pict_type = AV_PICTURE_TYPE_I;
2229 230 *got_frame = 1;
2230
2231 230 return avpkt->size;
2232 }
2233
2234 172 static av_cold int decode_init(AVCodecContext *avctx)
2235 {
2236 172 EXRContext *s = avctx->priv_data;
2237
2238 172 ff_init_half2float_tables(&s->h2f_tables);
2239
2240 172 s->avctx = avctx;
2241
2242 172 ff_exrdsp_init(&s->dsp);
2243
2244 #if HAVE_BIGENDIAN
2245 ff_bswapdsp_init(&s->bbdsp);
2246 #endif
2247
2248 // allocate thread data, used for non EXR_RAW compression types
2249 172 s->thread_data = av_calloc(avctx->thread_count, sizeof(*s->thread_data));
2250
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 172 times.
172 if (!s->thread_data)
2251 return AVERROR(ENOMEM);
2252
2253 172 return 0;
2254 }
2255
2256 172 static av_cold int decode_end(AVCodecContext *avctx)
2257 {
2258 172 EXRContext *s = avctx->priv_data;
2259 int i;
2260
2/2
✓ Branch 0 taken 172 times.
✓ Branch 1 taken 172 times.
344 for (i = 0; i < avctx->thread_count; i++) {
2261 172 EXRThreadData *td = &s->thread_data[i];
2262 172 av_freep(&td->uncompressed_data);
2263 172 av_freep(&td->tmp);
2264 172 av_freep(&td->bitmap);
2265 172 av_freep(&td->lut);
2266 172 av_freep(&td->he);
2267 172 av_freep(&td->freq);
2268 172 av_freep(&td->ac_data);
2269 172 av_freep(&td->dc_data);
2270 172 av_freep(&td->rle_data);
2271 172 av_freep(&td->rle_raw_data);
2272 172 ff_vlc_free(&td->vlc);
2273 }
2274
2275 172 av_freep(&s->thread_data);
2276 172 av_freep(&s->channels);
2277 172 av_freep(&s->offset_table);
2278
2279 172 return 0;
2280 }
2281
2282 #define OFFSET(x) offsetof(EXRContext, x)
2283 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
2284 static const AVOption options[] = {
2285 { "layer", "Set the decoding layer", OFFSET(layer),
2286 AV_OPT_TYPE_STRING, { .str = "" }, 0, 0, VD },
2287 { "part", "Set the decoding part", OFFSET(selected_part),
2288 AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VD },
2289 { "gamma", "Set the float gamma value when decoding", OFFSET(gamma),
2290 AV_OPT_TYPE_FLOAT, { .dbl = 1.0f }, 0.001, FLT_MAX, VD },
2291
2292 // XXX: Note the abuse of the enum using AVCOL_TRC_UNSPECIFIED to subsume the existing gamma option
2293 { "apply_trc", "color transfer characteristics to apply to EXR linear input", OFFSET(apply_trc_type),
2294 AV_OPT_TYPE_INT, {.i64 = AVCOL_TRC_UNSPECIFIED }, 1, AVCOL_TRC_NB-1, VD, .unit = "apply_trc_type"},
2295 { "bt709", "BT.709", 0,
2296 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT709 }, INT_MIN, INT_MAX, VD, .unit = "apply_trc_type"},
2297 { "gamma", "gamma", 0,
2298 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_UNSPECIFIED }, INT_MIN, INT_MAX, VD, .unit = "apply_trc_type"},
2299 { "gamma22", "BT.470 M", 0,
2300 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_GAMMA22 }, INT_MIN, INT_MAX, VD, .unit = "apply_trc_type"},
2301 { "gamma28", "BT.470 BG", 0,
2302 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_GAMMA28 }, INT_MIN, INT_MAX, VD, .unit = "apply_trc_type"},
2303 { "smpte170m", "SMPTE 170 M", 0,
2304 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTE170M }, INT_MIN, INT_MAX, VD, .unit = "apply_trc_type"},
2305 { "smpte240m", "SMPTE 240 M", 0,
2306 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTE240M }, INT_MIN, INT_MAX, VD, .unit = "apply_trc_type"},
2307 { "linear", "Linear", 0,
2308 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LINEAR }, INT_MIN, INT_MAX, VD, .unit = "apply_trc_type"},
2309 { "log", "Log", 0,
2310 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LOG }, INT_MIN, INT_MAX, VD, .unit = "apply_trc_type"},
2311 { "log_sqrt", "Log square root", 0,
2312 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LOG_SQRT }, INT_MIN, INT_MAX, VD, .unit = "apply_trc_type"},
2313 { "iec61966_2_4", "IEC 61966-2-4", 0,
2314 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_IEC61966_2_4 }, INT_MIN, INT_MAX, VD, .unit = "apply_trc_type"},
2315 { "bt1361", "BT.1361", 0,
2316 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT1361_ECG }, INT_MIN, INT_MAX, VD, .unit = "apply_trc_type"},
2317 { "iec61966_2_1", "IEC 61966-2-1", 0,
2318 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_IEC61966_2_1 }, INT_MIN, INT_MAX, VD, .unit = "apply_trc_type"},
2319 { "bt2020_10bit", "BT.2020 - 10 bit", 0,
2320 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT2020_10 }, INT_MIN, INT_MAX, VD, .unit = "apply_trc_type"},
2321 { "bt2020_12bit", "BT.2020 - 12 bit", 0,
2322 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT2020_12 }, INT_MIN, INT_MAX, VD, .unit = "apply_trc_type"},
2323 { "smpte2084", "SMPTE ST 2084", 0,
2324 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTEST2084 }, INT_MIN, INT_MAX, VD, .unit = "apply_trc_type"},
2325 { "smpte428_1", "SMPTE ST 428-1", 0,
2326 AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTEST428_1 }, INT_MIN, INT_MAX, VD, .unit = "apply_trc_type"},
2327
2328 { NULL },
2329 };
2330
2331 static const AVClass exr_class = {
2332 .class_name = "EXR",
2333 .item_name = av_default_item_name,
2334 .option = options,
2335 .version = LIBAVUTIL_VERSION_INT,
2336 };
2337
2338 const FFCodec ff_exr_decoder = {
2339 .p.name = "exr",
2340 CODEC_LONG_NAME("OpenEXR image"),
2341 .p.type = AVMEDIA_TYPE_VIDEO,
2342 .p.id = AV_CODEC_ID_EXR,
2343 .priv_data_size = sizeof(EXRContext),
2344 .init = decode_init,
2345 .close = decode_end,
2346 FF_CODEC_DECODE_CB(decode_frame),
2347 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS |
2348 AV_CODEC_CAP_SLICE_THREADS,
2349 .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
2350 .p.priv_class = &exr_class,
2351 };
2352