FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/exr.c
Date: 2024-07-14 13:34:57
Exec Total Coverage
Lines: 854 1291 66.2%
Functions: 23 31 74.2%
Branches: 460 778 59.1%

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 union av_intfloat32 gamma_table[65536];
194
195 uint8_t *offset_table;
196
197 Half2FloatTables h2f_tables;
198 } EXRContext;
199
200 12853 static int zip_uncompress(const EXRContext *s, const uint8_t *src, int compressed_size,
201 int uncompressed_size, EXRThreadData *td)
202 {
203 12853 unsigned long dest_len = uncompressed_size;
204
205
1/2
✓ Branch 1 taken 12853 times.
✗ Branch 2 not taken.
12853 if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK ||
206
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12853 times.
12853 dest_len != uncompressed_size)
207 return AVERROR_INVALIDDATA;
208
209 av_assert1(uncompressed_size % 2 == 0);
210
211 12853 s->dsp.predictor(td->tmp, uncompressed_size);
212 12853 s->dsp.reorder_pixels(td->uncompressed_data, td->tmp, uncompressed_size);
213
214 12853 return 0;
215 }
216
217 4848 static int rle(uint8_t *dst, const uint8_t *src,
218 int compressed_size, int uncompressed_size)
219 {
220 4848 uint8_t *d = dst;
221 4848 const int8_t *s = src;
222 4848 int ssize = compressed_size;
223 4848 int dsize = uncompressed_size;
224 4848 uint8_t *dend = d + dsize;
225 int count;
226
227
2/2
✓ Branch 0 taken 671923 times.
✓ Branch 1 taken 4848 times.
676771 while (ssize > 0) {
228 671923 count = *s++;
229
230
2/2
✓ Branch 0 taken 501696 times.
✓ Branch 1 taken 170227 times.
671923 if (count < 0) {
231 501696 count = -count;
232
233
1/2
✓ Branch 0 taken 501696 times.
✗ Branch 1 not taken.
501696 if ((dsize -= count) < 0 ||
234
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 501696 times.
501696 (ssize -= count + 1) < 0)
235 return AVERROR_INVALIDDATA;
236
237
2/2
✓ Branch 0 taken 21206629 times.
✓ Branch 1 taken 501696 times.
21708325 while (count--)
238 21206629 *d++ = *s++;
239 } else {
240 170227 count++;
241
242
1/2
✓ Branch 0 taken 170227 times.
✗ Branch 1 not taken.
170227 if ((dsize -= count) < 0 ||
243
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 170227 times.
170227 (ssize -= 2) < 0)
244 return AVERROR_INVALIDDATA;
245
246
2/2
✓ Branch 0 taken 4743203 times.
✓ Branch 1 taken 170227 times.
4913430 while (count--)
247 4743203 *d++ = *s;
248
249 170227 s++;
250 }
251 }
252
253
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4848 times.
4848 if (dend != d)
254 return AVERROR_INVALIDDATA;
255
256 4848 return 0;
257 }
258
259 4848 static int rle_uncompress(const EXRContext *ctx, const uint8_t *src, int compressed_size,
260 int uncompressed_size, EXRThreadData *td)
261 {
262 4848 rle(td->tmp, src, compressed_size, uncompressed_size);
263
264 av_assert1(uncompressed_size % 2 == 0);
265
266 4848 ctx->dsp.predictor(td->tmp, uncompressed_size);
267 4848 ctx->dsp.reorder_pixels(td->uncompressed_data, td->tmp, uncompressed_size);
268
269 4848 return 0;
270 }
271
272 #define USHORT_RANGE (1 << 16)
273 #define BITMAP_SIZE (1 << 13)
274
275 30 static uint16_t reverse_lut(const uint8_t *bitmap, uint16_t *lut)
276 {
277 30 int i, k = 0;
278
279
2/2
✓ Branch 0 taken 1966080 times.
✓ Branch 1 taken 30 times.
1966110 for (i = 0; i < USHORT_RANGE; i++)
280
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))))
281 15385 lut[k++] = i;
282
283 30 i = k - 1;
284
285 30 memset(lut + k, 0, (USHORT_RANGE - k) * 2);
286
287 30 return i;
288 }
289
290 30 static void apply_lut(const uint16_t *lut, uint16_t *dst, int dsize)
291 {
292 int i;
293
294
2/2
✓ Branch 0 taken 820722 times.
✓ Branch 1 taken 30 times.
820752 for (i = 0; i < dsize; ++i)
295 820722 dst[i] = lut[dst[i]];
296 30 }
297
298 #define HUF_ENCBITS 16 // literal (value) bit length
299 #define HUF_ENCSIZE ((1 << HUF_ENCBITS) + 1) // encoding table size
300
301 30 static void huf_canonical_code_table(uint64_t *freq)
302 {
303 30 uint64_t c, n[59] = { 0 };
304 int i;
305
306
2/2
✓ Branch 0 taken 1966110 times.
✓ Branch 1 taken 30 times.
1966140 for (i = 0; i < HUF_ENCSIZE; i++)
307 1966110 n[freq[i]] += 1;
308
309 30 c = 0;
310
2/2
✓ Branch 0 taken 1740 times.
✓ Branch 1 taken 30 times.
1770 for (i = 58; i > 0; --i) {
311 1740 uint64_t nc = ((c + n[i]) >> 1);
312 1740 n[i] = c;
313 1740 c = nc;
314 }
315
316
2/2
✓ Branch 0 taken 1966110 times.
✓ Branch 1 taken 30 times.
1966140 for (i = 0; i < HUF_ENCSIZE; ++i) {
317 1966110 int l = freq[i];
318
319
2/2
✓ Branch 0 taken 6417 times.
✓ Branch 1 taken 1959693 times.
1966110 if (l > 0)
320 6417 freq[i] = l | (n[l]++ << 6);
321 }
322 30 }
323
324 #define SHORT_ZEROCODE_RUN 59
325 #define LONG_ZEROCODE_RUN 63
326 #define SHORTEST_LONG_RUN (2 + LONG_ZEROCODE_RUN - SHORT_ZEROCODE_RUN)
327 #define LONGEST_LONG_RUN (255 + SHORTEST_LONG_RUN)
328
329 30 static int huf_unpack_enc_table(GetByteContext *gb,
330 int32_t im, int32_t iM, uint64_t *freq)
331 {
332 GetBitContext gbit;
333 30 int ret = init_get_bits8(&gbit, gb->buffer, bytestream2_get_bytes_left(gb));
334
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (ret < 0)
335 return ret;
336
337
2/2
✓ Branch 0 taken 16606 times.
✓ Branch 1 taken 30 times.
16636 for (; im <= iM; im++) {
338 int l;
339
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16606 times.
16606 if (get_bits_left(&gbit) < 6)
340 return AVERROR_INVALIDDATA;
341 16606 l = freq[im] = get_bits(&gbit, 6);
342
343
2/2
✓ Branch 0 taken 8339 times.
✓ Branch 1 taken 8267 times.
16606 if (l == LONG_ZEROCODE_RUN) {
344 8339 int zerun = get_bits(&gbit, 8) + SHORTEST_LONG_RUN;
345
346
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8339 times.
8339 if (im + zerun > iM + 1)
347 return AVERROR_INVALIDDATA;
348
349
2/2
✓ Branch 0 taken 1955626 times.
✓ Branch 1 taken 8339 times.
1963965 while (zerun--)
350 1955626 freq[im++] = 0;
351
352 8339 im--;
353
2/2
✓ Branch 0 taken 1053 times.
✓ Branch 1 taken 7214 times.
8267 } else if (l >= SHORT_ZEROCODE_RUN) {
354 1053 int zerun = l - SHORT_ZEROCODE_RUN + 2;
355
356
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1053 times.
1053 if (im + zerun > iM + 1)
357 return AVERROR_INVALIDDATA;
358
359
2/2
✓ Branch 0 taken 3270 times.
✓ Branch 1 taken 1053 times.
4323 while (zerun--)
360 3270 freq[im++] = 0;
361
362 1053 im--;
363 }
364 }
365
366 30 bytestream2_skip(gb, (get_bits_count(&gbit) + 7) / 8);
367 30 huf_canonical_code_table(freq);
368
369 30 return 0;
370 }
371
372 30 static int huf_build_dec_table(const EXRContext *s,
373 EXRThreadData *td, int im, int iM)
374 {
375 30 int j = 0;
376
377 30 td->run_sym = -1;
378
2/2
✓ Branch 0 taken 1966080 times.
✓ Branch 1 taken 30 times.
1966110 for (int i = im; i < iM; i++) {
379 1966080 td->he[j].sym = i;
380 1966080 td->he[j].len = td->freq[i] & 63;
381 1966080 td->he[j].code = td->freq[i] >> 6;
382
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1966080 times.
1966080 if (td->he[j].len > 32) {
383 avpriv_request_sample(s->avctx, "Too big code length");
384 return AVERROR_PATCHWELCOME;
385 }
386
2/2
✓ Branch 0 taken 6387 times.
✓ Branch 1 taken 1959693 times.
1966080 if (td->he[j].len > 0)
387 6387 j++;
388 else
389 1959693 td->run_sym = i;
390 }
391
392
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (im > 0)
393 td->run_sym = 0;
394
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 else if (iM < 65535)
395 td->run_sym = 65535;
396
397
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (td->run_sym == -1) {
398 avpriv_request_sample(s->avctx, "No place for run symbol");
399 return AVERROR_PATCHWELCOME;
400 }
401
402 30 td->he[j].sym = td->run_sym;
403 30 td->he[j].len = td->freq[iM] & 63;
404
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (td->he[j].len > 32) {
405 avpriv_request_sample(s->avctx, "Too big code length");
406 return AVERROR_PATCHWELCOME;
407 }
408 30 td->he[j].code = td->freq[iM] >> 6;
409 30 j++;
410
411 30 ff_vlc_free(&td->vlc);
412 30 return ff_vlc_init_sparse(&td->vlc, 12, j,
413 30 &td->he[0].len, sizeof(td->he[0]), sizeof(td->he[0].len),
414 30 &td->he[0].code, sizeof(td->he[0]), sizeof(td->he[0].code),
415 30 &td->he[0].sym, sizeof(td->he[0]), sizeof(td->he[0].sym), 0);
416 }
417
418 30 static int huf_decode(VLC *vlc, GetByteContext *gb, int nbits, int run_sym,
419 int no, uint16_t *out)
420 {
421 GetBitContext gbit;
422 30 int oe = 0;
423
424 30 init_get_bits(&gbit, gb->buffer, nbits);
425
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) {
426 213481 uint16_t x = get_vlc2(&gbit, vlc->table, 12, 3);
427
428
2/2
✓ Branch 0 taken 3387 times.
✓ Branch 1 taken 210094 times.
213481 if (x == run_sym) {
429 3387 int run = get_bits(&gbit, 8);
430 uint16_t fill;
431
432
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)
433 return AVERROR_INVALIDDATA;
434
435 3387 fill = out[oe - 1];
436
437
2/2
✓ Branch 0 taken 610628 times.
✓ Branch 1 taken 3387 times.
614015 while (run-- > 0)
438 610628 out[oe++] = fill;
439 } else {
440 210094 out[oe++] = x;
441 }
442 }
443
444 30 return 0;
445 }
446
447 30 static int huf_uncompress(const EXRContext *s,
448 EXRThreadData *td,
449 GetByteContext *gb,
450 uint16_t *dst, int dst_size)
451 {
452 int32_t im, iM;
453 uint32_t nBits;
454 int ret;
455
456 30 im = bytestream2_get_le32(gb);
457 30 iM = bytestream2_get_le32(gb);
458 30 bytestream2_skip(gb, 4);
459 30 nBits = bytestream2_get_le32(gb);
460
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 ||
461
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 iM < 0 || iM >= HUF_ENCSIZE)
462 return AVERROR_INVALIDDATA;
463
464 30 bytestream2_skip(gb, 4);
465
466
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 23 times.
30 if (!td->freq)
467 7 td->freq = av_malloc_array(HUF_ENCSIZE, sizeof(*td->freq));
468
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 23 times.
30 if (!td->he)
469 7 td->he = av_calloc(HUF_ENCSIZE, sizeof(*td->he));
470
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) {
471 ret = AVERROR(ENOMEM);
472 return ret;
473 }
474
475 30 memset(td->freq, 0, sizeof(*td->freq) * HUF_ENCSIZE);
476
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
30 if ((ret = huf_unpack_enc_table(gb, im, iM, td->freq)) < 0)
477 return ret;
478
479
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
30 if (nBits > 8 * bytestream2_get_bytes_left(gb)) {
480 ret = AVERROR_INVALIDDATA;
481 return ret;
482 }
483
484
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
30 if ((ret = huf_build_dec_table(s, td, im, iM)) < 0)
485 return ret;
486 30 return huf_decode(&td->vlc, gb, nBits, td->run_sym, dst_size, dst);
487 }
488
489 1091709 static inline void wdec14(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
490 {
491 1091709 int16_t ls = l;
492 1091709 int16_t hs = h;
493 1091709 int hi = hs;
494 1091709 int ai = ls + (hi & 1) + (hi >> 1);
495 1091709 int16_t as = ai;
496 1091709 int16_t bs = ai - hi;
497
498 1091709 *a = as;
499 1091709 *b = bs;
500 1091709 }
501
502 #define NBITS 16
503 #define A_OFFSET (1 << (NBITS - 1))
504 #define MOD_MASK ((1 << NBITS) - 1)
505
506 static inline void wdec16(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
507 {
508 int m = l;
509 int d = h;
510 int bb = (m - (d >> 1)) & MOD_MASK;
511 int aa = (d + bb - A_OFFSET) & MOD_MASK;
512 *b = bb;
513 *a = aa;
514 }
515
516 102 static void wav_decode(uint16_t *in, int nx, int ox,
517 int ny, int oy, uint16_t mx)
518 {
519 102 int w14 = (mx < (1 << 14));
520 102 int n = (nx > ny) ? ny : nx;
521 102 int p = 1;
522 int p2;
523
524
2/2
✓ Branch 0 taken 591 times.
✓ Branch 1 taken 102 times.
693 while (p <= n)
525 591 p <<= 1;
526
527 102 p >>= 1;
528 102 p2 = p;
529 102 p >>= 1;
530
531
2/2
✓ Branch 0 taken 489 times.
✓ Branch 1 taken 102 times.
591 while (p >= 1) {
532 489 uint16_t *py = in;
533 489 uint16_t *ey = in + oy * (ny - p2);
534 uint16_t i00, i01, i10, i11;
535 489 int oy1 = oy * p;
536 489 int oy2 = oy * p2;
537 489 int ox1 = ox * p;
538 489 int ox2 = ox * p2;
539
540
2/2
✓ Branch 0 taken 2940 times.
✓ Branch 1 taken 489 times.
3429 for (; py <= ey; py += oy2) {
541 2940 uint16_t *px = py;
542 2940 uint16_t *ex = py + ox * (nx - p2);
543
544
2/2
✓ Branch 0 taken 272751 times.
✓ Branch 1 taken 2940 times.
275691 for (; px <= ex; px += ox2) {
545 272751 uint16_t *p01 = px + ox1;
546 272751 uint16_t *p10 = px + oy1;
547 272751 uint16_t *p11 = p10 + ox1;
548
549
1/2
✓ Branch 0 taken 272751 times.
✗ Branch 1 not taken.
272751 if (w14) {
550 272751 wdec14(*px, *p10, &i00, &i10);
551 272751 wdec14(*p01, *p11, &i01, &i11);
552 272751 wdec14(i00, i01, px, p01);
553 272751 wdec14(i10, i11, p10, p11);
554 } else {
555 wdec16(*px, *p10, &i00, &i10);
556 wdec16(*p01, *p11, &i01, &i11);
557 wdec16(i00, i01, px, p01);
558 wdec16(i10, i11, p10, p11);
559 }
560 }
561
562
2/2
✓ Branch 0 taken 324 times.
✓ Branch 1 taken 2616 times.
2940 if (nx & p) {
563 324 uint16_t *p10 = px + oy1;
564
565
1/2
✓ Branch 0 taken 324 times.
✗ Branch 1 not taken.
324 if (w14)
566 324 wdec14(*px, *p10, &i00, p10);
567 else
568 wdec16(*px, *p10, &i00, p10);
569
570 324 *px = i00;
571 }
572 }
573
574
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 474 times.
489 if (ny & p) {
575 15 uint16_t *px = py;
576 15 uint16_t *ex = py + ox * (nx - p2);
577
578
2/2
✓ Branch 0 taken 381 times.
✓ Branch 1 taken 15 times.
396 for (; px <= ex; px += ox2) {
579 381 uint16_t *p01 = px + ox1;
580
581
1/2
✓ Branch 0 taken 381 times.
✗ Branch 1 not taken.
381 if (w14)
582 381 wdec14(*px, *p01, &i00, p01);
583 else
584 wdec16(*px, *p01, &i00, p01);
585
586 381 *px = i00;
587 }
588 }
589
590 489 p2 = p;
591 489 p >>= 1;
592 }
593 102 }
594
595 30 static int piz_uncompress(const EXRContext *s, const uint8_t *src, int ssize,
596 int dsize, EXRThreadData *td)
597 {
598 GetByteContext gb;
599 uint16_t maxval, min_non_zero, max_non_zero;
600 uint16_t *ptr;
601 30 uint16_t *tmp = (uint16_t *)td->tmp;
602 uint16_t *out;
603 uint16_t *in;
604 int ret, i, j;
605 int pixel_half_size;/* 1 for half, 2 for float and uint32 */
606 EXRChannel *channel;
607 int tmp_offset;
608
609
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 23 times.
30 if (!td->bitmap)
610 7 td->bitmap = av_malloc(BITMAP_SIZE);
611
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 23 times.
30 if (!td->lut)
612 7 td->lut = av_malloc(1 << 17);
613
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) {
614 av_freep(&td->bitmap);
615 av_freep(&td->lut);
616 return AVERROR(ENOMEM);
617 }
618
619 30 bytestream2_init(&gb, src, ssize);
620 30 min_non_zero = bytestream2_get_le16(&gb);
621 30 max_non_zero = bytestream2_get_le16(&gb);
622
623
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (max_non_zero >= BITMAP_SIZE)
624 return AVERROR_INVALIDDATA;
625
626 30 memset(td->bitmap, 0, FFMIN(min_non_zero, BITMAP_SIZE));
627
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 if (min_non_zero <= max_non_zero)
628 30 bytestream2_get_buffer(&gb, td->bitmap + min_non_zero,
629 30 max_non_zero - min_non_zero + 1);
630 30 memset(td->bitmap + max_non_zero + 1, 0, BITMAP_SIZE - max_non_zero - 1);
631
632 30 maxval = reverse_lut(td->bitmap, td->lut);
633
634 30 bytestream2_skip(&gb, 4);
635 30 ret = huf_uncompress(s, td, &gb, tmp, dsize / sizeof(uint16_t));
636
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (ret)
637 return ret;
638
639 30 ptr = tmp;
640
2/2
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 30 times.
120 for (i = 0; i < s->nb_channels; i++) {
641 90 channel = &s->channels[i];
642
643
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 12 times.
90 if (channel->pixel_type == EXR_HALF)
644 78 pixel_half_size = 1;
645 else
646 12 pixel_half_size = 2;
647
648
2/2
✓ Branch 0 taken 102 times.
✓ Branch 1 taken 90 times.
192 for (j = 0; j < pixel_half_size; j++)
649 102 wav_decode(ptr + j, td->xsize, pixel_half_size, td->ysize,
650 102 td->xsize * pixel_half_size, maxval);
651 90 ptr += td->xsize * td->ysize * pixel_half_size;
652 }
653
654 30 apply_lut(td->lut, tmp, dsize / sizeof(uint16_t));
655
656 30 out = (uint16_t *)td->uncompressed_data;
657
2/2
✓ Branch 0 taken 891 times.
✓ Branch 1 taken 30 times.
921 for (i = 0; i < td->ysize; i++) {
658 891 tmp_offset = 0;
659
2/2
✓ Branch 0 taken 2673 times.
✓ Branch 1 taken 891 times.
3564 for (j = 0; j < s->nb_channels; j++) {
660 2673 channel = &s->channels[j];
661
2/2
✓ Branch 0 taken 2289 times.
✓ Branch 1 taken 384 times.
2673 if (channel->pixel_type == EXR_HALF)
662 2289 pixel_half_size = 1;
663 else
664 384 pixel_half_size = 2;
665
666 2673 in = tmp + tmp_offset * td->xsize * td->ysize + i * td->xsize * pixel_half_size;
667 2673 tmp_offset += pixel_half_size;
668
669 #if HAVE_BIGENDIAN
670 s->bbdsp.bswap16_buf(out, in, td->xsize * pixel_half_size);
671 #else
672 2673 memcpy(out, in, td->xsize * 2 * pixel_half_size);
673 #endif
674 2673 out += td->xsize * pixel_half_size;
675 }
676 }
677
678 30 return 0;
679 }
680
681 61 static int pxr24_uncompress(const EXRContext *s, const uint8_t *src,
682 int compressed_size, int uncompressed_size,
683 EXRThreadData *td)
684 {
685 61 unsigned long dest_len, expected_len = 0;
686 61 const uint8_t *in = td->tmp;
687 uint8_t *out;
688 int c, i, j;
689
690
2/2
✓ Branch 0 taken 218 times.
✓ Branch 1 taken 61 times.
279 for (i = 0; i < s->nb_channels; i++) {
691
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 187 times.
218 if (s->channels[i].pixel_type == EXR_FLOAT) {
692 31 expected_len += (td->xsize * td->ysize * 3);/* PRX 24 store float in 24 bit instead of 32 */
693
2/2
✓ Branch 0 taken 181 times.
✓ Branch 1 taken 6 times.
187 } else if (s->channels[i].pixel_type == EXR_HALF) {
694 181 expected_len += (td->xsize * td->ysize * 2);
695 } else {//UINT 32
696 6 expected_len += (td->xsize * td->ysize * 4);
697 }
698 }
699
700 61 dest_len = expected_len;
701
702
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 61 times.
61 if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK) {
703 return AVERROR_INVALIDDATA;
704
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
61 } else if (dest_len != expected_len) {
705 return AVERROR_INVALIDDATA;
706 }
707
708 61 out = td->uncompressed_data;
709
2/2
✓ Branch 0 taken 889 times.
✓ Branch 1 taken 61 times.
950 for (i = 0; i < td->ysize; i++)
710
2/2
✓ Branch 0 taken 2950 times.
✓ Branch 1 taken 889 times.
3839 for (c = 0; c < s->nb_channels; c++) {
711 2950 EXRChannel *channel = &s->channels[c];
712 const uint8_t *ptr[4];
713 2950 uint32_t pixel = 0;
714
715
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) {
716 248 case EXR_FLOAT:
717 248 ptr[0] = in;
718 248 ptr[1] = ptr[0] + td->xsize;
719 248 ptr[2] = ptr[1] + td->xsize;
720 248 in = ptr[2] + td->xsize;
721
722
2/2
✓ Branch 0 taken 2976 times.
✓ Branch 1 taken 248 times.
3224 for (j = 0; j < td->xsize; ++j) {
723 2976 uint32_t diff = ((unsigned)*(ptr[0]++) << 24) |
724 2976 (*(ptr[1]++) << 16) |
725 2976 (*(ptr[2]++) << 8);
726 2976 pixel += diff;
727 2976 bytestream_put_le32(&out, pixel);
728 }
729 248 break;
730 2651 case EXR_HALF:
731 2651 ptr[0] = in;
732 2651 ptr[1] = ptr[0] + td->xsize;
733 2651 in = ptr[1] + td->xsize;
734
2/2
✓ Branch 0 taken 1923039 times.
✓ Branch 1 taken 2651 times.
1925690 for (j = 0; j < td->xsize; j++) {
735 1923039 uint32_t diff = (*(ptr[0]++) << 8) | *(ptr[1]++);
736
737 1923039 pixel += diff;
738 1923039 bytestream_put_le16(&out, pixel);
739 }
740 2651 break;
741 51 case EXR_UINT:
742 51 ptr[0] = in;
743 51 ptr[1] = ptr[0] + s->xdelta;
744 51 ptr[2] = ptr[1] + s->xdelta;
745 51 ptr[3] = ptr[2] + s->xdelta;
746 51 in = ptr[3] + s->xdelta;
747
748
2/2
✓ Branch 0 taken 639 times.
✓ Branch 1 taken 51 times.
690 for (j = 0; j < s->xdelta; ++j) {
749 639 uint32_t diff = ((uint32_t)*(ptr[0]++) << 24) |
750 639 (*(ptr[1]++) << 16) |
751 639 (*(ptr[2]++) << 8 ) |
752 639 (*(ptr[3]++));
753 639 pixel += diff;
754 639 bytestream_put_le32(&out, pixel);
755 }
756 51 break;
757 default:
758 return AVERROR_INVALIDDATA;
759 }
760 }
761
762 61 return 0;
763 }
764
765 59279 static void unpack_14(const uint8_t b[14], uint16_t s[16])
766 {
767 59279 uint16_t shift = (b[ 2] >> 2) & 15;
768 59279 uint16_t bias = (0x20 << shift);
769 int i;
770
771 59279 s[ 0] = (b[0] << 8) | b[1];
772
773 59279 s[ 4] = s[ 0] + ((((b[ 2] << 4) | (b[ 3] >> 4)) & 0x3f) << shift) - bias;
774 59279 s[ 8] = s[ 4] + ((((b[ 3] << 2) | (b[ 4] >> 6)) & 0x3f) << shift) - bias;
775 59279 s[12] = s[ 8] + ((b[ 4] & 0x3f) << shift) - bias;
776
777 59279 s[ 1] = s[ 0] + ((b[ 5] >> 2) << shift) - bias;
778 59279 s[ 5] = s[ 4] + ((((b[ 5] << 4) | (b[ 6] >> 4)) & 0x3f) << shift) - bias;
779 59279 s[ 9] = s[ 8] + ((((b[ 6] << 2) | (b[ 7] >> 6)) & 0x3f) << shift) - bias;
780 59279 s[13] = s[12] + ((b[ 7] & 0x3f) << shift) - bias;
781
782 59279 s[ 2] = s[ 1] + ((b[ 8] >> 2) << shift) - bias;
783 59279 s[ 6] = s[ 5] + ((((b[ 8] << 4) | (b[ 9] >> 4)) & 0x3f) << shift) - bias;
784 59279 s[10] = s[ 9] + ((((b[ 9] << 2) | (b[10] >> 6)) & 0x3f) << shift) - bias;
785 59279 s[14] = s[13] + ((b[10] & 0x3f) << shift) - bias;
786
787 59279 s[ 3] = s[ 2] + ((b[11] >> 2) << shift) - bias;
788 59279 s[ 7] = s[ 6] + ((((b[11] << 4) | (b[12] >> 4)) & 0x3f) << shift) - bias;
789 59279 s[11] = s[10] + ((((b[12] << 2) | (b[13] >> 6)) & 0x3f) << shift) - bias;
790 59279 s[15] = s[14] + ((b[13] & 0x3f) << shift) - bias;
791
792
2/2
✓ Branch 0 taken 948464 times.
✓ Branch 1 taken 59279 times.
1007743 for (i = 0; i < 16; ++i) {
793
2/2
✓ Branch 0 taken 942347 times.
✓ Branch 1 taken 6117 times.
948464 if (s[i] & 0x8000)
794 942347 s[i] &= 0x7fff;
795 else
796 6117 s[i] = ~s[i];
797 }
798 59279 }
799
800 106 static void unpack_3(const uint8_t b[3], uint16_t s[16])
801 {
802 int i;
803
804 106 s[0] = (b[0] << 8) | b[1];
805
806
2/2
✓ Branch 0 taken 105 times.
✓ Branch 1 taken 1 times.
106 if (s[0] & 0x8000)
807 105 s[0] &= 0x7fff;
808 else
809 1 s[0] = ~s[0];
810
811
2/2
✓ Branch 0 taken 1590 times.
✓ Branch 1 taken 106 times.
1696 for (i = 1; i < 16; i++)
812 1590 s[i] = s[0];
813 106 }
814
815
816 23 static int b44_uncompress(const EXRContext *s, const uint8_t *src, int compressed_size,
817 int uncompressed_size, EXRThreadData *td) {
818 23 const int8_t *sr = src;
819 23 int stay_to_uncompress = compressed_size;
820 int nb_b44_block_w, nb_b44_block_h;
821 int index_tl_x, index_tl_y, index_out, index_tmp;
822 uint16_t tmp_buffer[16]; /* B44 use 4x4 half float pixel */
823 int c, iY, iX, y, x;
824 23 int target_channel_offset = 0;
825
826 /* calc B44 block count */
827 23 nb_b44_block_w = td->xsize / 4;
828
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 10 times.
23 if ((td->xsize % 4) != 0)
829 13 nb_b44_block_w++;
830
831 23 nb_b44_block_h = td->ysize / 4;
832
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 17 times.
23 if ((td->ysize % 4) != 0)
833 6 nb_b44_block_h++;
834
835
2/2
✓ Branch 0 taken 133 times.
✓ Branch 1 taken 23 times.
156 for (c = 0; c < s->nb_channels; c++) {
836
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 */
837
2/2
✓ Branch 0 taken 374 times.
✓ Branch 1 taken 85 times.
459 for (iY = 0; iY < nb_b44_block_h; iY++) {
838
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 */
839
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59385 times.
59385 if (stay_to_uncompress < 3)
840 return AVERROR_INVALIDDATA;
841
842
2/2
✓ Branch 0 taken 106 times.
✓ Branch 1 taken 59279 times.
59385 if (src[compressed_size - stay_to_uncompress + 2] == 0xfc) { /* B44A block */
843 106 unpack_3(sr, tmp_buffer);
844 106 sr += 3;
845 106 stay_to_uncompress -= 3;
846 } else {/* B44 Block */
847
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59279 times.
59279 if (stay_to_uncompress < 14)
848 return AVERROR_INVALIDDATA;
849 59279 unpack_14(sr, tmp_buffer);
850 59279 sr += 14;
851 59279 stay_to_uncompress -= 14;
852 }
853
854 /* copy data to uncompress buffer (B44 block can exceed target resolution)*/
855 59385 index_tl_x = iX * 4;
856 59385 index_tl_y = iY * 4;
857
858
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++) {
859
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++) {
860 943987 index_out = target_channel_offset * td->xsize + y * td->channel_line_size + 2 * x;
861 943987 index_tmp = (y-index_tl_y) * 4 + (x-index_tl_x);
862 943987 td->uncompressed_data[index_out] = tmp_buffer[index_tmp] & 0xff;
863 943987 td->uncompressed_data[index_out + 1] = tmp_buffer[index_tmp] >> 8;
864 }
865 }
866 }
867 }
868 85 target_channel_offset += 2;
869 } else {/* Float or UINT 32 channel */
870
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
48 if (stay_to_uncompress < td->ysize * td->xsize * 4)
871 return AVERROR_INVALIDDATA;
872
873
2/2
✓ Branch 0 taken 400 times.
✓ Branch 1 taken 48 times.
448 for (y = 0; y < td->ysize; y++) {
874 400 index_out = target_channel_offset * td->xsize + y * td->channel_line_size;
875 400 memcpy(&td->uncompressed_data[index_out], sr, td->xsize * 4);
876 400 sr += td->xsize * 4;
877 }
878 48 target_channel_offset += 4;
879
880 48 stay_to_uncompress -= td->ysize * td->xsize * 4;
881 }
882 }
883
884 23 return 0;
885 }
886
887 static int ac_uncompress(const EXRContext *s, GetByteContext *gb, float *block)
888 {
889 int ret = 0, n = 1;
890
891 while (n < 64) {
892 uint16_t val = bytestream2_get_ne16(gb);
893
894 if (val == 0xff00) {
895 n = 64;
896 } else if ((val >> 8) == 0xff) {
897 n += val & 0xff;
898 } else {
899 ret = n;
900 block[ff_zigzag_direct[n]] = av_int2float(half2float(val, &s->h2f_tables));
901 n++;
902 }
903 }
904
905 return ret;
906 }
907
908 static void idct_1d(float *blk, int step)
909 {
910 const float a = .5f * cosf( M_PI / 4.f);
911 const float b = .5f * cosf( M_PI / 16.f);
912 const float c = .5f * cosf( M_PI / 8.f);
913 const float d = .5f * cosf(3.f*M_PI / 16.f);
914 const float e = .5f * cosf(5.f*M_PI / 16.f);
915 const float f = .5f * cosf(3.f*M_PI / 8.f);
916 const float g = .5f * cosf(7.f*M_PI / 16.f);
917
918 float alpha[4], beta[4], theta[4], gamma[4];
919
920 alpha[0] = c * blk[2 * step];
921 alpha[1] = f * blk[2 * step];
922 alpha[2] = c * blk[6 * step];
923 alpha[3] = f * blk[6 * step];
924
925 beta[0] = b * blk[1 * step] + d * blk[3 * step] + e * blk[5 * step] + g * blk[7 * step];
926 beta[1] = d * blk[1 * step] - g * blk[3 * step] - b * blk[5 * step] - e * blk[7 * step];
927 beta[2] = e * blk[1 * step] - b * blk[3 * step] + g * blk[5 * step] + d * blk[7 * step];
928 beta[3] = g * blk[1 * step] - e * blk[3 * step] + d * blk[5 * step] - b * blk[7 * step];
929
930 theta[0] = a * (blk[0 * step] + blk[4 * step]);
931 theta[3] = a * (blk[0 * step] - blk[4 * step]);
932
933 theta[1] = alpha[0] + alpha[3];
934 theta[2] = alpha[1] - alpha[2];
935
936 gamma[0] = theta[0] + theta[1];
937 gamma[1] = theta[3] + theta[2];
938 gamma[2] = theta[3] - theta[2];
939 gamma[3] = theta[0] - theta[1];
940
941 blk[0 * step] = gamma[0] + beta[0];
942 blk[1 * step] = gamma[1] + beta[1];
943 blk[2 * step] = gamma[2] + beta[2];
944 blk[3 * step] = gamma[3] + beta[3];
945
946 blk[4 * step] = gamma[3] - beta[3];
947 blk[5 * step] = gamma[2] - beta[2];
948 blk[6 * step] = gamma[1] - beta[1];
949 blk[7 * step] = gamma[0] - beta[0];
950 }
951
952 static void dct_inverse(float *block)
953 {
954 for (int i = 0; i < 8; i++)
955 idct_1d(block + i, 8);
956
957 for (int i = 0; i < 8; i++) {
958 idct_1d(block, 1);
959 block += 8;
960 }
961 }
962
963 static void convert(float y, float u, float v,
964 float *b, float *g, float *r)
965 {
966 *r = y + 1.5747f * v;
967 *g = y - 0.1873f * u - 0.4682f * v;
968 *b = y + 1.8556f * u;
969 }
970
971 static float to_linear(float x, float scale)
972 {
973 float ax = fabsf(x);
974
975 if (ax <= 1.f) {
976 return FFSIGN(x) * powf(ax, 2.2f * scale);
977 } else {
978 const float log_base = expf(2.2f * scale);
979
980 return FFSIGN(x) * powf(log_base, ax - 1.f);
981 }
982 }
983
984 static int dwa_uncompress(const EXRContext *s, const uint8_t *src, int compressed_size,
985 int uncompressed_size, EXRThreadData *td)
986 {
987 int64_t version, lo_usize, lo_size;
988 int64_t ac_size, dc_size, rle_usize, rle_csize, rle_raw_size;
989 int64_t ac_count, dc_count, ac_compression;
990 const int dc_w = td->xsize >> 3;
991 const int dc_h = td->ysize >> 3;
992 GetByteContext gb, agb;
993 int skip, ret;
994
995 if (compressed_size <= 88)
996 return AVERROR_INVALIDDATA;
997
998 version = AV_RL64(src + 0);
999 if (version != 2)
1000 return AVERROR_INVALIDDATA;
1001
1002 lo_usize = AV_RL64(src + 8);
1003 lo_size = AV_RL64(src + 16);
1004 ac_size = AV_RL64(src + 24);
1005 dc_size = AV_RL64(src + 32);
1006 rle_csize = AV_RL64(src + 40);
1007 rle_usize = AV_RL64(src + 48);
1008 rle_raw_size = AV_RL64(src + 56);
1009 ac_count = AV_RL64(src + 64);
1010 dc_count = AV_RL64(src + 72);
1011 ac_compression = AV_RL64(src + 80);
1012
1013 if ( compressed_size < (uint64_t)(lo_size | ac_size | dc_size | rle_csize) || compressed_size < 88LL + lo_size + ac_size + dc_size + rle_csize
1014 || ac_count > (uint64_t)INT_MAX/2
1015 )
1016 return AVERROR_INVALIDDATA;
1017
1018 bytestream2_init(&gb, src + 88, compressed_size - 88);
1019 skip = bytestream2_get_le16(&gb);
1020 if (skip < 2)
1021 return AVERROR_INVALIDDATA;
1022
1023 bytestream2_skip(&gb, skip - 2);
1024
1025 if (lo_size > 0) {
1026 if (lo_usize > uncompressed_size)
1027 return AVERROR_INVALIDDATA;
1028 bytestream2_skip(&gb, lo_size);
1029 }
1030
1031 if (ac_size > 0) {
1032 unsigned long dest_len;
1033 GetByteContext agb = gb;
1034
1035 if (ac_count > 3LL * td->xsize * s->scan_lines_per_block)
1036 return AVERROR_INVALIDDATA;
1037
1038 dest_len = ac_count * 2LL;
1039
1040 av_fast_padded_malloc(&td->ac_data, &td->ac_size, dest_len);
1041 if (!td->ac_data)
1042 return AVERROR(ENOMEM);
1043
1044 switch (ac_compression) {
1045 case 0:
1046 ret = huf_uncompress(s, td, &agb, (int16_t *)td->ac_data, ac_count);
1047 if (ret < 0)
1048 return ret;
1049 break;
1050 case 1:
1051 if (uncompress(td->ac_data, &dest_len, agb.buffer, ac_size) != Z_OK ||
1052 dest_len != ac_count * 2LL)
1053 return AVERROR_INVALIDDATA;
1054 break;
1055 default:
1056 return AVERROR_INVALIDDATA;
1057 }
1058
1059 bytestream2_skip(&gb, ac_size);
1060 }
1061
1062 {
1063 unsigned long dest_len;
1064 GetByteContext agb = gb;
1065
1066 if (dc_count != dc_w * dc_h * 3)
1067 return AVERROR_INVALIDDATA;
1068
1069 dest_len = dc_count * 2LL;
1070
1071 av_fast_padded_malloc(&td->dc_data, &td->dc_size, FFALIGN(dest_len, 64) * 2);
1072 if (!td->dc_data)
1073 return AVERROR(ENOMEM);
1074
1075 if (uncompress(td->dc_data + FFALIGN(dest_len, 64), &dest_len, agb.buffer, dc_size) != Z_OK ||
1076 (dest_len != dc_count * 2LL))
1077 return AVERROR_INVALIDDATA;
1078
1079 s->dsp.predictor(td->dc_data + FFALIGN(dest_len, 64), dest_len);
1080 s->dsp.reorder_pixels(td->dc_data, td->dc_data + FFALIGN(dest_len, 64), dest_len);
1081
1082 bytestream2_skip(&gb, dc_size);
1083 }
1084
1085 if (rle_raw_size > 0 && rle_csize > 0 && rle_usize > 0) {
1086 unsigned long dest_len = rle_usize;
1087
1088 av_fast_padded_malloc(&td->rle_data, &td->rle_size, rle_usize);
1089 if (!td->rle_data)
1090 return AVERROR(ENOMEM);
1091
1092 av_fast_padded_malloc(&td->rle_raw_data, &td->rle_raw_size, rle_raw_size);
1093 if (!td->rle_raw_data)
1094 return AVERROR(ENOMEM);
1095
1096 if (uncompress(td->rle_data, &dest_len, gb.buffer, rle_csize) != Z_OK ||
1097 (dest_len != rle_usize))
1098 return AVERROR_INVALIDDATA;
1099
1100 ret = rle(td->rle_raw_data, td->rle_data, rle_usize, rle_raw_size);
1101 if (ret < 0)
1102 return ret;
1103 bytestream2_skip(&gb, rle_csize);
1104 }
1105
1106 bytestream2_init(&agb, td->ac_data, ac_count * 2);
1107
1108 for (int y = 0; y < td->ysize; y += 8) {
1109 for (int x = 0; x < td->xsize; x += 8) {
1110 memset(td->block, 0, sizeof(td->block));
1111
1112 for (int j = 0; j < 3; j++) {
1113 float *block = td->block[j];
1114 const int idx = (x >> 3) + (y >> 3) * dc_w + dc_w * dc_h * j;
1115 uint16_t *dc = (uint16_t *)td->dc_data;
1116 union av_intfloat32 dc_val;
1117
1118 dc_val.i = half2float(dc[idx], &s->h2f_tables);
1119
1120 block[0] = dc_val.f;
1121 ac_uncompress(s, &agb, block);
1122 dct_inverse(block);
1123 }
1124
1125 {
1126 const int o = s->nb_channels == 4;
1127 float *bo = ((float *)td->uncompressed_data) +
1128 y * td->xsize * s->nb_channels + td->xsize * (o + 0) + x;
1129 float *go = ((float *)td->uncompressed_data) +
1130 y * td->xsize * s->nb_channels + td->xsize * (o + 1) + x;
1131 float *ro = ((float *)td->uncompressed_data) +
1132 y * td->xsize * s->nb_channels + td->xsize * (o + 2) + x;
1133 float *yb = td->block[0];
1134 float *ub = td->block[1];
1135 float *vb = td->block[2];
1136
1137 for (int yy = 0; yy < 8; yy++) {
1138 for (int xx = 0; xx < 8; xx++) {
1139 const int idx = xx + yy * 8;
1140
1141 convert(yb[idx], ub[idx], vb[idx], &bo[xx], &go[xx], &ro[xx]);
1142
1143 bo[xx] = to_linear(bo[xx], 1.f);
1144 go[xx] = to_linear(go[xx], 1.f);
1145 ro[xx] = to_linear(ro[xx], 1.f);
1146 }
1147
1148 bo += td->xsize * s->nb_channels;
1149 go += td->xsize * s->nb_channels;
1150 ro += td->xsize * s->nb_channels;
1151 }
1152 }
1153 }
1154 }
1155
1156 if (s->nb_channels < 4)
1157 return 0;
1158
1159 for (int y = 0; y < td->ysize && td->rle_raw_data; y++) {
1160 uint32_t *ao = ((uint32_t *)td->uncompressed_data) + y * td->xsize * s->nb_channels;
1161 uint8_t *ai0 = td->rle_raw_data + y * td->xsize;
1162 uint8_t *ai1 = td->rle_raw_data + y * td->xsize + rle_raw_size / 2;
1163
1164 for (int x = 0; x < td->xsize; x++) {
1165 uint16_t ha = ai0[x] | (ai1[x] << 8);
1166
1167 ao[x] = half2float(ha, &s->h2f_tables);
1168 }
1169 }
1170
1171 return 0;
1172 }
1173
1174 37267 static int decode_block(AVCodecContext *avctx, void *tdata,
1175 int jobnr, int threadnr)
1176 {
1177 37267 const EXRContext *s = avctx->priv_data;
1178 37267 AVFrame *const p = s->picture;
1179 37267 EXRThreadData *td = &s->thread_data[threadnr];
1180 37267 const uint8_t *channel_buffer[4] = { 0 };
1181 37267 const uint8_t *buf = s->buf;
1182 uint64_t line_offset, uncompressed_size;
1183 uint8_t *ptr;
1184 uint32_t data_size;
1185 37267 int line, col = 0;
1186 uint64_t tile_x, tile_y, tile_level_x, tile_level_y;
1187 const uint8_t *src;
1188
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 37256 times.
37267 int step = s->desc->flags & AV_PIX_FMT_FLAG_FLOAT ? 4 : 2 * s->desc->nb_components;
1189 37267 int bxmin = 0, axmax = 0, window_xoffset = 0;
1190 int window_xmin, window_xmax, window_ymin, window_ymax;
1191 int data_xoffset, data_yoffset, data_window_offset, xsize, ysize;
1192 37267 int i, x, buf_size = s->buf_size;
1193 int c, rgb_channel_count;
1194 37267 float one_gamma = 1.0f / s->gamma;
1195 37267 av_csp_trc_function trc_func = av_csp_trc_func_from_id(s->apply_trc_type);
1196 int ret;
1197
1198 37267 line_offset = AV_RL64(s->gb.buffer + jobnr * 8);
1199
1200
2/2
✓ Branch 0 taken 104 times.
✓ Branch 1 taken 37163 times.
37267 if (s->is_tile) {
1201
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)
1202 return AVERROR_INVALIDDATA;
1203
1204 104 src = buf + line_offset + 20;
1205
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 104 times.
104 if (s->is_multipart)
1206 src += 4;
1207
1208 104 tile_x = AV_RL32(src - 20);
1209 104 tile_y = AV_RL32(src - 16);
1210 104 tile_level_x = AV_RL32(src - 12);
1211 104 tile_level_y = AV_RL32(src - 8);
1212
1213 104 data_size = AV_RL32(src - 4);
1214
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)
1215 return AVERROR_INVALIDDATA;
1216
1217
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 */
1218 avpriv_report_missing_feature(s->avctx, "Subres tile before full res tile");
1219 return AVERROR_PATCHWELCOME;
1220 }
1221
1222
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 )
1223 return AVERROR_INVALIDDATA;
1224
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 )
1225 return AVERROR_INVALIDDATA;
1226
1227 104 line = s->ymin + s->tile_attr.ySize * tile_y;
1228 104 col = s->tile_attr.xSize * tile_x;
1229
1230
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 ||
1231
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 104 times.
104 s->xmin + col < s->xmin || s->xmin + col > s->xmax)
1232 return AVERROR_INVALIDDATA;
1233
1234 104 td->ysize = FFMIN(s->tile_attr.ySize, s->ydelta - tile_y * s->tile_attr.ySize);
1235 104 td->xsize = FFMIN(s->tile_attr.xSize, s->xdelta - tile_x * s->tile_attr.xSize);
1236
1237
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 ||
1238 104 av_image_check_size2(td->xsize, td->ysize, s->avctx->max_pixels, AV_PIX_FMT_NONE, 0, s->avctx) < 0)
1239 return AVERROR_INVALIDDATA;
1240
1241 104 td->channel_line_size = td->xsize * s->current_channel_offset;/* uncompress size of one line */
1242 104 uncompressed_size = td->channel_line_size * (uint64_t)td->ysize;/* uncompress size of the block */
1243 } else {
1244
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)
1245 return AVERROR_INVALIDDATA;
1246
1247 37163 src = buf + line_offset + 8;
1248
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37163 times.
37163 if (s->is_multipart)
1249 src += 4;
1250 37163 line = AV_RL32(src - 8);
1251
1252
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)
1253 return AVERROR_INVALIDDATA;
1254
1255 37163 data_size = AV_RL32(src - 4);
1256
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)
1257 return AVERROR_INVALIDDATA;
1258
1259 37163 td->ysize = FFMIN(s->scan_lines_per_block, s->ymax - line + 1); /* s->ydelta - line ?? */
1260 37163 td->xsize = s->xdelta;
1261
1262
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 ||
1263 37163 av_image_check_size2(td->xsize, td->ysize, s->avctx->max_pixels, AV_PIX_FMT_NONE, 0, s->avctx) < 0)
1264 return AVERROR_INVALIDDATA;
1265
1266 37163 td->channel_line_size = td->xsize * s->current_channel_offset;/* uncompress size of one line */
1267 37163 uncompressed_size = td->channel_line_size * (uint64_t)td->ysize;/* uncompress size of the block */
1268
1269
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 ||
1270
1/2
✓ Branch 0 taken 11927 times.
✗ Branch 1 not taken.
11927 line_offset > buf_size - uncompressed_size)) ||
1271
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 ||
1272
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25236 times.
25236 line_offset > buf_size - data_size))) {
1273 return AVERROR_INVALIDDATA;
1274 }
1275 }
1276
1277 37267 window_xmin = FFMIN(avctx->width, FFMAX(0, s->xmin + col));
1278 37267 window_xmax = FFMIN(avctx->width, FFMAX(0, s->xmin + col + td->xsize));
1279 37267 window_ymin = FFMIN(avctx->height, FFMAX(0, line ));
1280 37267 window_ymax = FFMIN(avctx->height, FFMAX(0, line + td->ysize));
1281 37267 xsize = window_xmax - window_xmin;
1282 37267 ysize = window_ymax - window_ymin;
1283
1284 /* tile or scanline not visible skip decoding */
1285
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)
1286 433 return 0;
1287
1288 /* is the first tile or is a scanline */
1289
2/2
✓ Branch 0 taken 36794 times.
✓ Branch 1 taken 40 times.
36834 if(col == 0) {
1290 36794 window_xmin = 0;
1291 /* pixels to add at the left of the display window */
1292 36794 window_xoffset = FFMAX(0, s->xmin);
1293 /* bytes to add at the left of the display window */
1294 36794 bxmin = window_xoffset * step;
1295 }
1296
1297 /* is the last tile or is a scanline */
1298
2/2
✓ Branch 0 taken 36794 times.
✓ Branch 1 taken 40 times.
36834 if(col + td->xsize == s->xdelta) {
1299 36794 window_xmax = avctx->width;
1300 /* bytes to add at the right of the display window */
1301 36794 axmax = FFMAX(0, (avctx->width - (s->xmax + 1))) * step;
1302 }
1303
1304
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)
1305 return AVERROR_INVALIDDATA;
1306
1307
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 */
1308 17828 av_fast_padded_malloc(&td->tmp, &td->tmp_size, uncompressed_size);
1309
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17828 times.
17828 if (!td->tmp)
1310 return AVERROR(ENOMEM);
1311 }
1312
1313
2/2
✓ Branch 0 taken 17815 times.
✓ Branch 1 taken 19019 times.
36834 if (data_size < uncompressed_size) {
1314 17815 av_fast_padded_malloc(&td->uncompressed_data,
1315 17815 &td->uncompressed_size, uncompressed_size + 64);/* Force 64 padding for AVX2 reorder_pixels dst */
1316
1317
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17815 times.
17815 if (!td->uncompressed_data)
1318 return AVERROR(ENOMEM);
1319
1320 17815 ret = AVERROR_INVALIDDATA;
1321
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) {
1322 12853 case EXR_ZIP1:
1323 case EXR_ZIP16:
1324 12853 ret = zip_uncompress(s, src, data_size, uncompressed_size, td);
1325 12853 break;
1326 30 case EXR_PIZ:
1327 30 ret = piz_uncompress(s, src, data_size, uncompressed_size, td);
1328 30 break;
1329 61 case EXR_PXR24:
1330 61 ret = pxr24_uncompress(s, src, data_size, uncompressed_size, td);
1331 61 break;
1332 4848 case EXR_RLE:
1333 4848 ret = rle_uncompress(s, src, data_size, uncompressed_size, td);
1334 4848 break;
1335 23 case EXR_B44:
1336 case EXR_B44A:
1337 23 ret = b44_uncompress(s, src, data_size, uncompressed_size, td);
1338 23 break;
1339 case EXR_DWAA:
1340 case EXR_DWAB:
1341 ret = dwa_uncompress(s, src, data_size, uncompressed_size, td);
1342 break;
1343 }
1344
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17815 times.
17815 if (ret < 0) {
1345 av_log(avctx, AV_LOG_ERROR, "decode_block() failed.\n");
1346 return ret;
1347 }
1348 17815 src = td->uncompressed_data;
1349 }
1350
1351 /* offsets to crop data outside display window */
1352
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);
1353
2/2
✓ Branch 0 taken 36828 times.
✓ Branch 1 taken 6 times.
36834 data_yoffset = FFABS(FFMIN(0, line));
1354 36834 data_window_offset = (data_yoffset * td->channel_line_size) + data_xoffset;
1355
1356
2/2
✓ Branch 0 taken 25358 times.
✓ Branch 1 taken 11476 times.
36834 if (!s->is_luma) {
1357 25358 channel_buffer[0] = src + (td->xsize * s->channel_offsets[0]) + data_window_offset;
1358 25358 channel_buffer[1] = src + (td->xsize * s->channel_offsets[1]) + data_window_offset;
1359 25358 channel_buffer[2] = src + (td->xsize * s->channel_offsets[2]) + data_window_offset;
1360 25358 rgb_channel_count = 3;
1361 } else { /* put y data in the first channel_buffer */
1362 11476 channel_buffer[0] = src + (td->xsize * s->channel_offsets[1]) + data_window_offset;
1363 11476 rgb_channel_count = 1;
1364 }
1365
2/2
✓ Branch 0 taken 13595 times.
✓ Branch 1 taken 23239 times.
36834 if (s->channel_offsets[3] >= 0)
1366 13595 channel_buffer[3] = src + (td->xsize * s->channel_offsets[3]) + data_window_offset;
1367
1368
2/2
✓ Branch 0 taken 36824 times.
✓ Branch 1 taken 10 times.
36834 if (s->desc->flags & AV_PIX_FMT_FLAG_FLOAT) {
1369 /* todo: change this when a floating point pixel format with luma with alpha is implemented */
1370
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;
1371
2/2
✓ Branch 0 taken 11476 times.
✓ Branch 1 taken 25348 times.
36824 if (s->is_luma) {
1372 11476 channel_buffer[1] = channel_buffer[0];
1373 11476 channel_buffer[2] = channel_buffer[0];
1374 }
1375
1376
2/2
✓ Branch 0 taken 101131 times.
✓ Branch 1 taken 36824 times.
137955 for (c = 0; c < channel_count; c++) {
1377 101131 int plane = s->desc->comp[c].plane;
1378 101131 ptr = p->data[plane] + window_ymin * p->linesize[plane] + (window_xmin * 4);
1379
1380
2/2
✓ Branch 0 taken 146524 times.
✓ Branch 1 taken 101131 times.
247655 for (i = 0; i < ysize; i++, ptr += p->linesize[plane]) {
1381 const uint8_t *src;
1382 union av_intfloat32 *ptr_x;
1383
1384 146524 src = channel_buffer[c];
1385 146524 ptr_x = (union av_intfloat32 *)ptr;
1386
1387 // Zero out the start if xmin is not 0
1388 146524 memset(ptr_x, 0, bxmin);
1389 146524 ptr_x += window_xoffset;
1390
1391
2/2
✓ Branch 0 taken 24648 times.
✓ Branch 1 taken 121876 times.
146524 if (s->pixel_type == EXR_FLOAT ||
1392
1/2
✓ Branch 0 taken 24648 times.
✗ Branch 1 not taken.
24648 s->compression == EXR_DWAA ||
1393
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24648 times.
146524 s->compression == EXR_DWAB) {
1394 // 32-bit
1395 union av_intfloat32 t;
1396
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) {
1397 for (x = 0; x < xsize; x++) {
1398 t.i = bytestream_get_le32(&src);
1399 t.f = trc_func(t.f);
1400 *ptr_x++ = t;
1401 }
1402
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 121876 times.
121876 } else if (one_gamma != 1.f) {
1403 for (x = 0; x < xsize; x++) {
1404 t.i = bytestream_get_le32(&src);
1405 if (t.f > 0.0f && c < 3) /* avoid negative values */
1406 t.f = powf(t.f, one_gamma);
1407 *ptr_x++ = t;
1408 }
1409 } else {
1410
2/2
✓ Branch 0 taken 42250824 times.
✓ Branch 1 taken 121876 times.
42372700 for (x = 0; x < xsize; x++) {
1411 42250824 t.i = bytestream_get_le32(&src);
1412 42250824 *ptr_x++ = t;
1413 }
1414 }
1415
1/2
✓ Branch 0 taken 24648 times.
✗ Branch 1 not taken.
24648 } else if (s->pixel_type == EXR_HALF) {
1416 // 16-bit
1417
3/4
✓ Branch 0 taken 3041 times.
✓ Branch 1 taken 21607 times.
✓ Branch 2 taken 3041 times.
✗ Branch 3 not taken.
24648 if (c < 3 || !trc_func) {
1418
2/2
✓ Branch 0 taken 10514097 times.
✓ Branch 1 taken 24648 times.
10538745 for (x = 0; x < xsize; x++) {
1419 10514097 *ptr_x++ = s->gamma_table[bytestream_get_le16(&src)];
1420 }
1421 } else {
1422 for (x = 0; x < xsize; x++) {
1423 ptr_x[0].i = half2float(bytestream_get_le16(&src), &s->h2f_tables);
1424 ptr_x++;
1425 }
1426 }
1427 }
1428
1429 // Zero out the end if xmax+1 is not w
1430 146524 memset(ptr_x, 0, axmax);
1431 146524 channel_buffer[c] += td->channel_line_size;
1432 }
1433 }
1434 } else {
1435
1436 av_assert1(s->pixel_type == EXR_UINT);
1437 10 ptr = p->data[0] + window_ymin * p->linesize[0] + (window_xmin * s->desc->nb_components * 2);
1438
1439
2/2
✓ Branch 0 taken 175 times.
✓ Branch 1 taken 10 times.
185 for (i = 0; i < ysize; i++, ptr += p->linesize[0]) {
1440
1441 const uint8_t * a;
1442 const uint8_t *rgb[3];
1443 uint16_t *ptr_x;
1444
1445
2/2
✓ Branch 0 taken 525 times.
✓ Branch 1 taken 175 times.
700 for (c = 0; c < rgb_channel_count; c++) {
1446 525 rgb[c] = channel_buffer[c];
1447 }
1448
1449
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 175 times.
175 if (channel_buffer[3])
1450 a = channel_buffer[3];
1451
1452 175 ptr_x = (uint16_t *) ptr;
1453
1454 // Zero out the start if xmin is not 0
1455 175 memset(ptr_x, 0, bxmin);
1456 175 ptr_x += window_xoffset * s->desc->nb_components;
1457
1458
2/2
✓ Branch 0 taken 5309 times.
✓ Branch 1 taken 175 times.
5484 for (x = 0; x < xsize; x++) {
1459
2/2
✓ Branch 0 taken 15927 times.
✓ Branch 1 taken 5309 times.
21236 for (c = 0; c < rgb_channel_count; c++) {
1460 15927 *ptr_x++ = bytestream_get_le32(&rgb[c]) >> 16;
1461 }
1462
1463
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5309 times.
5309 if (channel_buffer[3])
1464 *ptr_x++ = bytestream_get_le32(&a) >> 16;
1465 }
1466
1467 // Zero out the end if xmax+1 is not w
1468 175 memset(ptr_x, 0, axmax);
1469
1470 175 channel_buffer[0] += td->channel_line_size;
1471 175 channel_buffer[1] += td->channel_line_size;
1472 175 channel_buffer[2] += td->channel_line_size;
1473
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 175 times.
175 if (channel_buffer[3])
1474 channel_buffer[3] += td->channel_line_size;
1475 }
1476 }
1477
1478 36834 return 0;
1479 }
1480
1481 static void skip_header_chunk(EXRContext *s)
1482 {
1483 GetByteContext *gb = &s->gb;
1484
1485 while (bytestream2_get_bytes_left(gb) > 0) {
1486 if (!bytestream2_peek_byte(gb))
1487 break;
1488
1489 // Process unknown variables
1490 for (int i = 0; i < 2; i++) // value_name and value_type
1491 while (bytestream2_get_byte(gb) != 0);
1492
1493 // Skip variable length
1494 bytestream2_skip(gb, bytestream2_get_le32(gb));
1495 }
1496 }
1497
1498 /**
1499 * Check if the variable name corresponds to its data type.
1500 *
1501 * @param s the EXRContext
1502 * @param value_name name of the variable to check
1503 * @param value_type type of the variable to check
1504 * @param minimum_length minimum length of the variable data
1505 *
1506 * @return bytes to read containing variable data
1507 * -1 if variable is not found
1508 * 0 if buffer ended prematurely
1509 */
1510 32478 static int check_header_variable(EXRContext *s,
1511 const char *value_name,
1512 const char *value_type,
1513 unsigned int minimum_length)
1514 {
1515 32478 GetByteContext *gb = &s->gb;
1516 32478 int var_size = -1;
1517
1518
1/2
✓ Branch 1 taken 32478 times.
✗ Branch 2 not taken.
32478 if (bytestream2_get_bytes_left(gb) >= minimum_length &&
1519
2/2
✓ Branch 0 taken 2166 times.
✓ Branch 1 taken 30312 times.
32478 !strcmp(gb->buffer, value_name)) {
1520 // found value_name, jump to value_type (null terminated strings)
1521 2166 gb->buffer += strlen(value_name) + 1;
1522
1/2
✓ Branch 0 taken 2166 times.
✗ Branch 1 not taken.
2166 if (!strcmp(gb->buffer, value_type)) {
1523 2166 gb->buffer += strlen(value_type) + 1;
1524 2166 var_size = bytestream2_get_le32(gb);
1525 // don't go read past boundaries
1526
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2166 times.
2166 if (var_size > bytestream2_get_bytes_left(gb))
1527 var_size = 0;
1528 } else {
1529 // value_type not found, reset the buffer
1530 gb->buffer -= strlen(value_name) + 1;
1531 av_log(s->avctx, AV_LOG_WARNING,
1532 "Unknown data type %s for header variable %s.\n",
1533 value_type, value_name);
1534 }
1535 }
1536
1537 32478 return var_size;
1538 }
1539
1540 316 static int decode_header(EXRContext *s, AVFrame *frame)
1541 {
1542 316 AVDictionary *metadata = NULL;
1543 316 GetByteContext *gb = &s->gb;
1544 int magic_number, version, flags;
1545 316 int layer_match = 0;
1546 int ret;
1547 316 int dup_channels = 0;
1548
1549 316 s->current_channel_offset = 0;
1550 316 s->xmin = ~0;
1551 316 s->xmax = ~0;
1552 316 s->ymin = ~0;
1553 316 s->ymax = ~0;
1554 316 s->xdelta = ~0;
1555 316 s->ydelta = ~0;
1556 316 s->channel_offsets[0] = -1;
1557 316 s->channel_offsets[1] = -1;
1558 316 s->channel_offsets[2] = -1;
1559 316 s->channel_offsets[3] = -1;
1560 316 s->pixel_type = EXR_UNKNOWN;
1561 316 s->compression = EXR_UNKN;
1562 316 s->nb_channels = 0;
1563 316 s->w = 0;
1564 316 s->h = 0;
1565 316 s->tile_attr.xSize = -1;
1566 316 s->tile_attr.ySize = -1;
1567 316 s->is_tile = 0;
1568 316 s->is_multipart = 0;
1569 316 s->is_luma = 0;
1570 316 s->current_part = 0;
1571
1572
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 316 times.
316 if (bytestream2_get_bytes_left(gb) < 10) {
1573 av_log(s->avctx, AV_LOG_ERROR, "Header too short to parse.\n");
1574 return AVERROR_INVALIDDATA;
1575 }
1576
1577 316 magic_number = bytestream2_get_le32(gb);
1578
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 316 times.
316 if (magic_number != 20000630) {
1579 /* As per documentation of OpenEXR, it is supposed to be
1580 * int 20000630 little-endian */
1581 av_log(s->avctx, AV_LOG_ERROR, "Wrong magic number %d.\n", magic_number);
1582 return AVERROR_INVALIDDATA;
1583 }
1584
1585 316 version = bytestream2_get_byte(gb);
1586
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 316 times.
316 if (version != 2) {
1587 avpriv_report_missing_feature(s->avctx, "Version %d", version);
1588 return AVERROR_PATCHWELCOME;
1589 }
1590
1591 316 flags = bytestream2_get_le24(gb);
1592
1593
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 272 times.
316 if (flags & 0x02)
1594 44 s->is_tile = 1;
1595
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 316 times.
316 if (flags & 0x10)
1596 s->is_multipart = 1;
1597
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 316 times.
316 if (flags & 0x08) {
1598 avpriv_report_missing_feature(s->avctx, "deep data");
1599 return AVERROR_PATCHWELCOME;
1600 }
1601
1602 // Parse the header
1603
1/2
✓ Branch 1 taken 4396 times.
✗ Branch 2 not taken.
4396 while (bytestream2_get_bytes_left(gb) > 0) {
1604 int var_size;
1605
1606
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 &&
1607 bytestream2_get_bytes_left(gb) > 0) {
1608 if (bytestream2_peek_byte(gb)) {
1609 skip_header_chunk(s);
1610 } else {
1611 bytestream2_skip(gb, 1);
1612 if (!bytestream2_peek_byte(gb))
1613 break;
1614 }
1615 bytestream2_skip(gb, 1);
1616 s->current_part++;
1617 }
1618
1619
2/2
✓ Branch 1 taken 316 times.
✓ Branch 2 taken 4080 times.
4396 if (!bytestream2_peek_byte(gb)) {
1620
1/2
✓ Branch 0 taken 316 times.
✗ Branch 1 not taken.
316 if (!s->is_multipart)
1621 316 break;
1622 bytestream2_skip(gb, 1);
1623 if (s->current_part == s->selected_part) {
1624 while (bytestream2_get_bytes_left(gb) > 0) {
1625 if (bytestream2_peek_byte(gb)) {
1626 skip_header_chunk(s);
1627 } else {
1628 bytestream2_skip(gb, 1);
1629 if (!bytestream2_peek_byte(gb))
1630 break;
1631 }
1632 }
1633 }
1634 if (!bytestream2_peek_byte(gb))
1635 break;
1636 s->current_part++;
1637 }
1638
1639
2/2
✓ Branch 1 taken 316 times.
✓ Branch 2 taken 3764 times.
4080 if ((var_size = check_header_variable(s, "channels",
1640 316 "chlist", 38)) >= 0) {
1641 GetByteContext ch_gb;
1642
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 316 times.
316 if (!var_size) {
1643 ret = AVERROR_INVALIDDATA;
1644 goto fail;
1645 }
1646
1647 316 bytestream2_init(&ch_gb, gb->buffer, var_size);
1648
1649
2/2
✓ Branch 1 taken 1212 times.
✓ Branch 2 taken 316 times.
1528 while (bytestream2_get_bytes_left(&ch_gb) >= 19) {
1650 EXRChannel *channel;
1651 enum ExrPixelType current_pixel_type;
1652 1212 int channel_index = -1;
1653 int xsub, ysub;
1654
1655
2/2
✓ Branch 0 taken 272 times.
✓ Branch 1 taken 940 times.
1212 if (strcmp(s->layer, "") != 0) {
1656
2/2
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 122 times.
272 if (strncmp(ch_gb.buffer, s->layer, strlen(s->layer)) == 0) {
1657 150 layer_match = 1;
1658 150 av_log(s->avctx, AV_LOG_INFO,
1659 "Channel match layer : %s.\n", ch_gb.buffer);
1660 150 ch_gb.buffer += strlen(s->layer);
1661
1/2
✓ Branch 0 taken 150 times.
✗ Branch 1 not taken.
150 if (*ch_gb.buffer == '.')
1662 150 ch_gb.buffer++; /* skip dot if not given */
1663 } else {
1664 122 layer_match = 0;
1665 122 av_log(s->avctx, AV_LOG_INFO,
1666 "Channel doesn't match layer : %s.\n", ch_gb.buffer);
1667 }
1668 } else {
1669 940 layer_match = 1;
1670 }
1671
1672
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 */
1673
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") ||
1674
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 836 times.
1672 !av_strcasecmp(ch_gb.buffer, "X") ||
1675 836 !av_strcasecmp(ch_gb.buffer, "U")) {
1676 254 channel_index = 0;
1677 254 s->is_luma = 0;
1678
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") ||
1679 582 !av_strcasecmp(ch_gb.buffer, "V")) {
1680 254 channel_index = 1;
1681 254 s->is_luma = 0;
1682
2/2
✓ Branch 1 taken 62 times.
✓ Branch 2 taken 520 times.
582 } else if (!av_strcasecmp(ch_gb.buffer, "Y")) {
1683 62 channel_index = 1;
1684 62 s->is_luma = 1;
1685
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") ||
1686
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 266 times.
532 !av_strcasecmp(ch_gb.buffer, "Z") ||
1687 266 !av_strcasecmp(ch_gb.buffer, "W")) {
1688 254 channel_index = 2;
1689 254 s->is_luma = 0;
1690
2/2
✓ Branch 1 taken 114 times.
✓ Branch 2 taken 152 times.
266 } else if (!av_strcasecmp(ch_gb.buffer, "A")) {
1691 114 channel_index = 3;
1692 } else {
1693 152 av_log(s->avctx, AV_LOG_WARNING,
1694 "Unsupported channel %.256s.\n", ch_gb.buffer);
1695 }
1696 }
1697
1698 /* skip until you get a 0 */
1699
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 &&
1700 4856 bytestream2_get_byte(&ch_gb))
1701 3644 continue;
1702
1703
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1212 times.
1212 if (bytestream2_get_bytes_left(&ch_gb) < 4) {
1704 av_log(s->avctx, AV_LOG_ERROR, "Incomplete header.\n");
1705 ret = AVERROR_INVALIDDATA;
1706 goto fail;
1707 }
1708
1709 1212 current_pixel_type = bytestream2_get_le32(&ch_gb);
1710
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1212 times.
1212 if (current_pixel_type >= EXR_UNKNOWN) {
1711 avpriv_report_missing_feature(s->avctx, "Pixel type %d",
1712 current_pixel_type);
1713 ret = AVERROR_PATCHWELCOME;
1714 goto fail;
1715 }
1716
1717 1212 bytestream2_skip(&ch_gb, 4);
1718 1212 xsub = bytestream2_get_le32(&ch_gb);
1719 1212 ysub = bytestream2_get_le32(&ch_gb);
1720
1721
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) {
1722 avpriv_report_missing_feature(s->avctx,
1723 "Subsampling %dx%d",
1724 xsub, ysub);
1725 ret = AVERROR_PATCHWELCOME;
1726 goto fail;
1727 }
1728
1729
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 */
1730
2/2
✓ Branch 0 taken 622 times.
✓ Branch 1 taken 316 times.
938 if (s->pixel_type != EXR_UNKNOWN &&
1731
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 622 times.
622 s->pixel_type != current_pixel_type) {
1732 av_log(s->avctx, AV_LOG_ERROR,
1733 "RGB channels not of the same depth.\n");
1734 ret = AVERROR_INVALIDDATA;
1735 goto fail;
1736 }
1737 938 s->pixel_type = current_pixel_type;
1738 938 s->channel_offsets[channel_index] = s->current_channel_offset;
1739
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 274 times.
274 } else if (channel_index >= 0) {
1740 av_log(s->avctx, AV_LOG_WARNING,
1741 "Multiple channels with index %d.\n", channel_index);
1742 if (++dup_channels > 10) {
1743 ret = AVERROR_INVALIDDATA;
1744 goto fail;
1745 }
1746 }
1747
1748 2424 s->channels = av_realloc(s->channels,
1749 1212 ++s->nb_channels * sizeof(EXRChannel));
1750
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1212 times.
1212 if (!s->channels) {
1751 ret = AVERROR(ENOMEM);
1752 goto fail;
1753 }
1754 1212 channel = &s->channels[s->nb_channels - 1];
1755 1212 channel->pixel_type = current_pixel_type;
1756 1212 channel->xsub = xsub;
1757 1212 channel->ysub = ysub;
1758
1759
2/2
✓ Branch 0 taken 404 times.
✓ Branch 1 taken 808 times.
1212 if (current_pixel_type == EXR_HALF) {
1760 404 s->current_channel_offset += 2;
1761 } else {/* Float or UINT32 */
1762 808 s->current_channel_offset += 4;
1763 }
1764 }
1765
1766 /* Check if all channels are set with an offset or if the channels
1767 * are causing an overflow */
1768
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 */
1769
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 254 times.
254 if (FFMIN3(s->channel_offsets[0],
1770 s->channel_offsets[1],
1771 s->channel_offsets[2]) < 0) {
1772 if (s->channel_offsets[0] < 0)
1773 av_log(s->avctx, AV_LOG_ERROR, "Missing red channel.\n");
1774 if (s->channel_offsets[1] < 0)
1775 av_log(s->avctx, AV_LOG_ERROR, "Missing green channel.\n");
1776 if (s->channel_offsets[2] < 0)
1777 av_log(s->avctx, AV_LOG_ERROR, "Missing blue channel.\n");
1778 ret = AVERROR_INVALIDDATA;
1779 goto fail;
1780 }
1781 }
1782
1783 // skip one last byte and update main gb
1784 316 gb->buffer = ch_gb.buffer + 1;
1785 316 continue;
1786
2/2
✓ Branch 1 taken 316 times.
✓ Branch 2 taken 3448 times.
3764 } else if ((var_size = check_header_variable(s, "dataWindow", "box2i",
1787 316 31)) >= 0) {
1788 int xmin, ymin, xmax, ymax;
1789
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 316 times.
316 if (!var_size) {
1790 ret = AVERROR_INVALIDDATA;
1791 goto fail;
1792 }
1793
1794 316 xmin = bytestream2_get_le32(gb);
1795 316 ymin = bytestream2_get_le32(gb);
1796 316 xmax = bytestream2_get_le32(gb);
1797 316 ymax = bytestream2_get_le32(gb);
1798
1799
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 ||
1800
1/2
✓ Branch 0 taken 316 times.
✗ Branch 1 not taken.
316 ymax == INT_MAX || xmax == INT_MAX ||
1801
1/2
✓ Branch 0 taken 316 times.
✗ Branch 1 not taken.
316 (unsigned)xmax - xmin >= INT_MAX ||
1802
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 316 times.
316 (unsigned)ymax - ymin >= INT_MAX) {
1803 ret = AVERROR_INVALIDDATA;
1804 goto fail;
1805 }
1806 316 s->xmin = xmin;
1807 316 s->xmax = xmax;
1808 316 s->ymin = ymin;
1809 316 s->ymax = ymax;
1810 316 s->xdelta = (s->xmax - s->xmin) + 1;
1811 316 s->ydelta = (s->ymax - s->ymin) + 1;
1812
1813 316 continue;
1814
2/2
✓ Branch 1 taken 316 times.
✓ Branch 2 taken 3132 times.
3448 } else if ((var_size = check_header_variable(s, "displayWindow",
1815 316 "box2i", 34)) >= 0) {
1816 int32_t sx, sy, dx, dy;
1817
1818
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 316 times.
316 if (!var_size) {
1819 ret = AVERROR_INVALIDDATA;
1820 goto fail;
1821 }
1822
1823 316 sx = bytestream2_get_le32(gb);
1824 316 sy = bytestream2_get_le32(gb);
1825 316 dx = bytestream2_get_le32(gb);
1826 316 dy = bytestream2_get_le32(gb);
1827
1828 316 s->w = (unsigned)dx - sx + 1;
1829 316 s->h = (unsigned)dy - sy + 1;
1830
1831 316 continue;
1832
2/2
✓ Branch 1 taken 316 times.
✓ Branch 2 taken 2816 times.
3132 } else if ((var_size = check_header_variable(s, "lineOrder",
1833 316 "lineOrder", 25)) >= 0) {
1834 int line_order;
1835
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 316 times.
316 if (!var_size) {
1836 ret = AVERROR_INVALIDDATA;
1837 goto fail;
1838 }
1839
1840 316 line_order = bytestream2_get_byte(gb);
1841 316 av_log(s->avctx, AV_LOG_DEBUG, "line order: %d.\n", line_order);
1842
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 316 times.
316 if (line_order > 2) {
1843 av_log(s->avctx, AV_LOG_ERROR, "Unknown line order.\n");
1844 ret = AVERROR_INVALIDDATA;
1845 goto fail;
1846 }
1847
1848 316 continue;
1849
2/2
✓ Branch 1 taken 148 times.
✓ Branch 2 taken 2668 times.
2816 } else if ((var_size = check_header_variable(s, "pixelAspectRatio",
1850 "float", 31)) >= 0) {
1851
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 148 times.
148 if (!var_size) {
1852 ret = AVERROR_INVALIDDATA;
1853 goto fail;
1854 }
1855
1856 148 s->sar = bytestream2_get_le32(gb);
1857
1858 148 continue;
1859
2/2
✓ Branch 1 taken 316 times.
✓ Branch 2 taken 2352 times.
2668 } else if ((var_size = check_header_variable(s, "compression",
1860 "compression", 29)) >= 0) {
1861
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 316 times.
316 if (!var_size) {
1862 ret = AVERROR_INVALIDDATA;
1863 goto fail;
1864 }
1865
1866
1/2
✓ Branch 0 taken 316 times.
✗ Branch 1 not taken.
316 if (s->compression == EXR_UNKN)
1867 316 s->compression = bytestream2_get_byte(gb);
1868 else {
1869 bytestream2_skip(gb, 1);
1870 av_log(s->avctx, AV_LOG_WARNING,
1871 "Found more than one compression attribute.\n");
1872 }
1873
1874 316 continue;
1875
2/2
✓ Branch 1 taken 44 times.
✓ Branch 2 taken 2308 times.
2352 } else if ((var_size = check_header_variable(s, "tiles",
1876 44 "tiledesc", 22)) >= 0) {
1877 uint8_t tileLevel;
1878
1879
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
44 if (!s->is_tile)
1880 av_log(s->avctx, AV_LOG_WARNING,
1881 "Found tile attribute and scanline flags. Exr will be interpreted as scanline.\n");
1882
1883 44 s->tile_attr.xSize = bytestream2_get_le32(gb);
1884 44 s->tile_attr.ySize = bytestream2_get_le32(gb);
1885
1886 44 tileLevel = bytestream2_get_byte(gb);
1887 44 s->tile_attr.level_mode = tileLevel & 0x0f;
1888 44 s->tile_attr.level_round = (tileLevel >> 4) & 0x0f;
1889
1890
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
44 if (s->tile_attr.level_mode >= EXR_TILE_LEVEL_UNKNOWN) {
1891 avpriv_report_missing_feature(s->avctx, "Tile level mode %d",
1892 s->tile_attr.level_mode);
1893 ret = AVERROR_PATCHWELCOME;
1894 goto fail;
1895 }
1896
1897
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
44 if (s->tile_attr.level_round >= EXR_TILE_ROUND_UNKNOWN) {
1898 avpriv_report_missing_feature(s->avctx, "Tile level round %d",
1899 s->tile_attr.level_round);
1900 ret = AVERROR_PATCHWELCOME;
1901 goto fail;
1902 }
1903
1904 44 continue;
1905
2/2
✓ Branch 1 taken 184 times.
✓ Branch 2 taken 2124 times.
2308 } else if ((var_size = check_header_variable(s, "writer",
1906 184 "string", 1)) >= 0) {
1907 184 uint8_t key[256] = { 0 };
1908
1909 184 bytestream2_get_buffer(gb, key, FFMIN(sizeof(key) - 1, var_size));
1910 184 av_dict_set(&metadata, "writer", key, 0);
1911
1912 184 continue;
1913
2/2
✓ Branch 1 taken 184 times.
✓ Branch 2 taken 1940 times.
2124 } else if ((var_size = check_header_variable(s, "framesPerSecond",
1914 "rational", 33)) >= 0) {
1915
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 184 times.
184 if (!var_size) {
1916 ret = AVERROR_INVALIDDATA;
1917 goto fail;
1918 }
1919
1920 184 s->avctx->framerate.num = bytestream2_get_le32(gb);
1921 184 s->avctx->framerate.den = bytestream2_get_le32(gb);
1922
1923 184 continue;
1924
2/2
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 1932 times.
1940 } else if ((var_size = check_header_variable(s, "chunkCount",
1925 "int", 23)) >= 0) {
1926
1927 8 s->chunk_count = bytestream2_get_le32(gb);
1928
1929 8 continue;
1930
2/2
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 1914 times.
1932 } else if ((var_size = check_header_variable(s, "type",
1931 18 "string", 16)) >= 0) {
1932 18 uint8_t key[256] = { 0 };
1933
1934 18 bytestream2_get_buffer(gb, key, FFMIN(sizeof(key) - 1, var_size));
1935
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 10 times.
18 if (strncmp("scanlineimage", key, var_size) &&
1936
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 strncmp("tiledimage", key, var_size)) {
1937 ret = AVERROR_PATCHWELCOME;
1938 goto fail;
1939 }
1940
1941 18 continue;
1942
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1914 times.
1914 } else if ((var_size = check_header_variable(s, "preview",
1943 "preview", 16)) >= 0) {
1944 uint32_t pw = bytestream2_get_le32(gb);
1945 uint32_t ph = bytestream2_get_le32(gb);
1946 uint64_t psize = pw * (uint64_t)ph;
1947 if (psize > INT64_MAX / 4) {
1948 ret = AVERROR_INVALIDDATA;
1949 goto fail;
1950 }
1951 psize *= 4;
1952
1953 if ((int64_t)psize >= bytestream2_get_bytes_left(gb)) {
1954 ret = AVERROR_INVALIDDATA;
1955 goto fail;
1956 }
1957
1958 bytestream2_skip(gb, psize);
1959
1960 continue;
1961 }
1962
1963 // Check if there are enough bytes for a header
1964
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1914 times.
1914 if (bytestream2_get_bytes_left(gb) <= 9) {
1965 av_log(s->avctx, AV_LOG_ERROR, "Incomplete header\n");
1966 ret = AVERROR_INVALIDDATA;
1967 goto fail;
1968 }
1969
1970 // Process unknown variables
1971 {
1972 1914 uint8_t name[256] = { 0 };
1973 1914 uint8_t type[256] = { 0 };
1974 1914 uint8_t value[8192] = { 0 };
1975 1914 int i = 0, size;
1976
1977
2/2
✓ Branch 1 taken 26926 times.
✓ Branch 2 taken 1914 times.
57680 while (bytestream2_get_bytes_left(gb) > 0 &&
1978
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) {
1979 26926 name[i++] = bytestream2_get_byte(gb);
1980 }
1981
1982 1914 bytestream2_skip(gb, 1);
1983 1914 i = 0;
1984
2/2
✓ Branch 1 taken 8774 times.
✓ Branch 2 taken 1914 times.
21376 while (bytestream2_get_bytes_left(gb) > 0 &&
1985
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) {
1986 8774 type[i++] = bytestream2_get_byte(gb);
1987 }
1988 1914 bytestream2_skip(gb, 1);
1989 1914 size = bytestream2_get_le32(gb);
1990
1991 1914 bytestream2_get_buffer(gb, value, FFMIN(sizeof(value) - 1, size));
1992
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1914 times.
1914 if (size > sizeof(value) - 1)
1993 bytestream2_skip(gb, size - (sizeof(value) - 1));
1994
2/2
✓ Branch 0 taken 160 times.
✓ Branch 1 taken 1754 times.
1914 if (!strcmp(type, "string"))
1995 160 av_dict_set(&metadata, name, value, 0);
1996 }
1997 }
1998
1999
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 316 times.
316 if (s->compression == EXR_UNKN) {
2000 av_log(s->avctx, AV_LOG_ERROR, "Missing compression attribute.\n");
2001 ret = AVERROR_INVALIDDATA;
2002 goto fail;
2003 }
2004
2005
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 272 times.
316 if (s->is_tile) {
2006
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) {
2007 av_log(s->avctx, AV_LOG_ERROR, "Invalid tile attribute.\n");
2008 ret = AVERROR_INVALIDDATA;
2009 goto fail;
2010 }
2011 }
2012
2013
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 316 times.
316 if (bytestream2_get_bytes_left(gb) <= 0) {
2014 av_log(s->avctx, AV_LOG_ERROR, "Incomplete frame.\n");
2015 ret = AVERROR_INVALIDDATA;
2016 goto fail;
2017 }
2018
2019 316 frame->metadata = metadata;
2020
2021 // aaand we are done
2022 316 bytestream2_skip(gb, 1);
2023 316 return 0;
2024 fail:
2025 av_dict_free(&metadata);
2026 return ret;
2027 }
2028
2029 316 static int decode_frame(AVCodecContext *avctx, AVFrame *picture,
2030 int *got_frame, AVPacket *avpkt)
2031 {
2032 316 EXRContext *s = avctx->priv_data;
2033 316 GetByteContext *gb = &s->gb;
2034 uint8_t *ptr;
2035
2036 int i, y, ret, ymax;
2037 int planes;
2038 int out_line_size;
2039 int nb_blocks; /* nb scanline or nb tile */
2040 uint64_t start_offset_table;
2041 uint64_t start_next_scanline;
2042
2043 316 bytestream2_init(gb, avpkt->data, avpkt->size);
2044
2045
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 316 times.
316 if ((ret = decode_header(s, picture)) < 0)
2046 return ret;
2047
2048
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) &&
2049 s->pixel_type == EXR_HALF) {
2050 s->current_channel_offset *= 2;
2051 for (int i = 0; i < 4; i++)
2052 s->channel_offsets[i] *= 2;