FFmpeg coverage


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