FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/exr.c
Date: 2023-03-22 23:59:29
Exec Total Coverage
Lines: 851 1280 66.5%
Functions: 23 31 74.2%
Branches: 457 774 59.0%

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_free_vlc(&td->vlc);
408 30 return ff_init_vlc_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 unsigned short shift = (b[ 2] >> 2) & 15;
764 59279 unsigned short 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 float scale = s->pixel_type == EXR_FLOAT ? 2.f : 1.f;
1123 const int o = s->nb_channels == 4;
1124 float *bo = ((float *)td->uncompressed_data) +
1125 y * td->xsize * s->nb_channels + td->xsize * (o + 0) + x;
1126 float *go = ((float *)td->uncompressed_data) +
1127 y * td->xsize * s->nb_channels + td->xsize * (o + 1) + x;
1128 float *ro = ((float *)td->uncompressed_data) +
1129 y * td->xsize * s->nb_channels + td->xsize * (o + 2) + x;
1130 float *yb = td->block[0];
1131 float *ub = td->block[1];
1132 float *vb = td->block[2];
1133
1134 for (int yy = 0; yy < 8; yy++) {
1135 for (int xx = 0; xx < 8; xx++) {
1136 const int idx = xx + yy * 8;
1137
1138 convert(yb[idx], ub[idx], vb[idx], &bo[xx], &go[xx], &ro[xx]);
1139
1140 bo[xx] = to_linear(bo[xx], scale);
1141 go[xx] = to_linear(go[xx], scale);
1142 ro[xx] = to_linear(ro[xx], scale);
1143 }
1144
1145 bo += td->xsize * s->nb_channels;
1146 go += td->xsize * s->nb_channels;
1147 ro += td->xsize * s->nb_channels;
1148 }
1149 }
1150 }
1151 }
1152
1153 if (s->nb_channels < 4)
1154 return 0;
1155
1156 for (int y = 0; y < td->ysize && td->rle_raw_data; y++) {
1157 uint32_t *ao = ((uint32_t *)td->uncompressed_data) + y * td->xsize * s->nb_channels;
1158 uint8_t *ai0 = td->rle_raw_data + y * td->xsize;
1159 uint8_t *ai1 = td->rle_raw_data + y * td->xsize + rle_raw_size / 2;
1160
1161 for (int x = 0; x < td->xsize; x++) {
1162 uint16_t ha = ai0[x] | (ai1[x] << 8);
1163
1164 ao[x] = half2float(ha, &s->h2f_tables);
1165 }
1166 }
1167
1168 return 0;
1169 }
1170
1171 37267 static int decode_block(AVCodecContext *avctx, void *tdata,
1172 int jobnr, int threadnr)
1173 {
1174 37267 const EXRContext *s = avctx->priv_data;
1175 37267 AVFrame *const p = s->picture;
1176 37267 EXRThreadData *td = &s->thread_data[threadnr];
1177 37267 const uint8_t *channel_buffer[4] = { 0 };
1178 37267 const uint8_t *buf = s->buf;
1179 uint64_t line_offset, uncompressed_size;
1180 uint8_t *ptr;
1181 uint32_t data_size;
1182 37267 int line, col = 0;
1183 uint64_t tile_x, tile_y, tile_level_x, tile_level_y;
1184 const uint8_t *src;
1185
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;
1186 37267 int bxmin = 0, axmax = 0, window_xoffset = 0;
1187 int window_xmin, window_xmax, window_ymin, window_ymax;
1188 int data_xoffset, data_yoffset, data_window_offset, xsize, ysize;
1189 37267 int i, x, buf_size = s->buf_size;
1190 int c, rgb_channel_count;
1191 37267 float one_gamma = 1.0f / s->gamma;
1192 37267 av_csp_trc_function trc_func = av_csp_trc_func_from_id(s->apply_trc_type);
1193 int ret;
1194
1195 37267 line_offset = AV_RL64(s->gb.buffer + jobnr * 8);
1196
1197
2/2
✓ Branch 0 taken 104 times.
✓ Branch 1 taken 37163 times.
37267 if (s->is_tile) {
1198
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)
1199 return AVERROR_INVALIDDATA;
1200
1201 104 src = buf + line_offset + 20;
1202
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 104 times.
104 if (s->is_multipart)
1203 src += 4;
1204
1205 104 tile_x = AV_RL32(src - 20);
1206 104 tile_y = AV_RL32(src - 16);
1207 104 tile_level_x = AV_RL32(src - 12);
1208 104 tile_level_y = AV_RL32(src - 8);
1209
1210 104 data_size = AV_RL32(src - 4);
1211
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)
1212 return AVERROR_INVALIDDATA;
1213
1214
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 */
1215 avpriv_report_missing_feature(s->avctx, "Subres tile before full res tile");
1216 return AVERROR_PATCHWELCOME;
1217 }
1218
1219
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 )
1220 return AVERROR_INVALIDDATA;
1221
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 )
1222 return AVERROR_INVALIDDATA;
1223
1224 104 line = s->ymin + s->tile_attr.ySize * tile_y;
1225 104 col = s->tile_attr.xSize * tile_x;
1226
1227
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 ||
1228
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 104 times.
104 s->xmin + col < s->xmin || s->xmin + col > s->xmax)
1229 return AVERROR_INVALIDDATA;
1230
1231 104 td->ysize = FFMIN(s->tile_attr.ySize, s->ydelta - tile_y * s->tile_attr.ySize);
1232 104 td->xsize = FFMIN(s->tile_attr.xSize, s->xdelta - tile_x * s->tile_attr.xSize);
1233
1234
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 ||
1235 104 av_image_check_size2(td->xsize, td->ysize, s->avctx->max_pixels, AV_PIX_FMT_NONE, 0, s->avctx) < 0)
1236 return AVERROR_INVALIDDATA;
1237
1238 104 td->channel_line_size = td->xsize * s->current_channel_offset;/* uncompress size of one line */
1239 104 uncompressed_size = td->channel_line_size * (uint64_t)td->ysize;/* uncompress size of the block */
1240 } else {
1241
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)
1242 return AVERROR_INVALIDDATA;
1243
1244 37163 src = buf + line_offset + 8;
1245
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37163 times.
37163 if (s->is_multipart)
1246 src += 4;
1247 37163 line = AV_RL32(src - 8);
1248
1249
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)
1250 return AVERROR_INVALIDDATA;
1251
1252 37163 data_size = AV_RL32(src - 4);
1253
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)
1254 return AVERROR_INVALIDDATA;
1255
1256 37163 td->ysize = FFMIN(s->scan_lines_per_block, s->ymax - line + 1); /* s->ydelta - line ?? */
1257 37163 td->xsize = s->xdelta;
1258
1259
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 ||
1260 37163 av_image_check_size2(td->xsize, td->ysize, s->avctx->max_pixels, AV_PIX_FMT_NONE, 0, s->avctx) < 0)
1261 return AVERROR_INVALIDDATA;
1262
1263 37163 td->channel_line_size = td->xsize * s->current_channel_offset;/* uncompress size of one line */
1264 37163 uncompressed_size = td->channel_line_size * (uint64_t)td->ysize;/* uncompress size of the block */
1265
1266
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 ||
1267
1/2
✓ Branch 0 taken 11927 times.
✗ Branch 1 not taken.
11927 line_offset > buf_size - uncompressed_size)) ||
1268
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 ||
1269
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25236 times.
25236 line_offset > buf_size - data_size))) {
1270 return AVERROR_INVALIDDATA;
1271 }
1272 }
1273
1274 37267 window_xmin = FFMIN(avctx->width, FFMAX(0, s->xmin + col));
1275 37267 window_xmax = FFMIN(avctx->width, FFMAX(0, s->xmin + col + td->xsize));
1276 37267 window_ymin = FFMIN(avctx->height, FFMAX(0, line ));
1277 37267 window_ymax = FFMIN(avctx->height, FFMAX(0, line + td->ysize));
1278 37267 xsize = window_xmax - window_xmin;
1279 37267 ysize = window_ymax - window_ymin;
1280
1281 /* tile or scanline not visible skip decoding */
1282
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)
1283 433 return 0;
1284
1285 /* is the first tile or is a scanline */
1286
2/2
✓ Branch 0 taken 36794 times.
✓ Branch 1 taken 40 times.
36834 if(col == 0) {
1287 36794 window_xmin = 0;
1288 /* pixels to add at the left of the display window */
1289 36794 window_xoffset = FFMAX(0, s->xmin);
1290 /* bytes to add at the left of the display window */
1291 36794 bxmin = window_xoffset * step;
1292 }
1293
1294 /* is the last tile or is a scanline */
1295
2/2
✓ Branch 0 taken 36794 times.
✓ Branch 1 taken 40 times.
36834 if(col + td->xsize == s->xdelta) {
1296 36794 window_xmax = avctx->width;
1297 /* bytes to add at the right of the display window */
1298 36794 axmax = FFMAX(0, (avctx->width - (s->xmax + 1))) * step;
1299 }
1300
1301
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)
1302 return AVERROR_INVALIDDATA;
1303
1304
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 */
1305 17828 av_fast_padded_malloc(&td->tmp, &td->tmp_size, uncompressed_size);
1306
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17828 times.
17828 if (!td->tmp)
1307 return AVERROR(ENOMEM);
1308 }
1309
1310
2/2
✓ Branch 0 taken 17815 times.
✓ Branch 1 taken 19019 times.
36834 if (data_size < uncompressed_size) {
1311 17815 av_fast_padded_malloc(&td->uncompressed_data,
1312 17815 &td->uncompressed_size, uncompressed_size + 64);/* Force 64 padding for AVX2 reorder_pixels dst */
1313
1314
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17815 times.
17815 if (!td->uncompressed_data)
1315 return AVERROR(ENOMEM);
1316
1317 17815 ret = AVERROR_INVALIDDATA;
1318
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) {
1319 12853 case EXR_ZIP1:
1320 case EXR_ZIP16:
1321 12853 ret = zip_uncompress(s, src, data_size, uncompressed_size, td);
1322 12853 break;
1323 30 case EXR_PIZ:
1324 30 ret = piz_uncompress(s, src, data_size, uncompressed_size, td);
1325 30 break;
1326 61 case EXR_PXR24:
1327 61 ret = pxr24_uncompress(s, src, data_size, uncompressed_size, td);
1328 61 break;
1329 4848 case EXR_RLE:
1330 4848 ret = rle_uncompress(s, src, data_size, uncompressed_size, td);
1331 4848 break;
1332 23 case EXR_B44:
1333 case EXR_B44A:
1334 23 ret = b44_uncompress(s, src, data_size, uncompressed_size, td);
1335 23 break;
1336 case EXR_DWAA:
1337 case EXR_DWAB:
1338 ret = dwa_uncompress(s, src, data_size, uncompressed_size, td);
1339 break;
1340 }
1341
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17815 times.
17815 if (ret < 0) {
1342 av_log(avctx, AV_LOG_ERROR, "decode_block() failed.\n");
1343 return ret;
1344 }
1345 17815 src = td->uncompressed_data;
1346 }
1347
1348 /* offsets to crop data outside display window */
1349
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);
1350
2/2
✓ Branch 0 taken 36828 times.
✓ Branch 1 taken 6 times.
36834 data_yoffset = FFABS(FFMIN(0, line));
1351 36834 data_window_offset = (data_yoffset * td->channel_line_size) + data_xoffset;
1352
1353
2/2
✓ Branch 0 taken 25358 times.
✓ Branch 1 taken 11476 times.
36834 if (!s->is_luma) {
1354 25358 channel_buffer[0] = src + (td->xsize * s->channel_offsets[0]) + data_window_offset;
1355 25358 channel_buffer[1] = src + (td->xsize * s->channel_offsets[1]) + data_window_offset;
1356 25358 channel_buffer[2] = src + (td->xsize * s->channel_offsets[2]) + data_window_offset;
1357 25358 rgb_channel_count = 3;
1358 } else { /* put y data in the first channel_buffer */
1359 11476 channel_buffer[0] = src + (td->xsize * s->channel_offsets[1]) + data_window_offset;
1360 11476 rgb_channel_count = 1;
1361 }
1362
2/2
✓ Branch 0 taken 13595 times.
✓ Branch 1 taken 23239 times.
36834 if (s->channel_offsets[3] >= 0)
1363 13595 channel_buffer[3] = src + (td->xsize * s->channel_offsets[3]) + data_window_offset;
1364
1365
2/2
✓ Branch 0 taken 36824 times.
✓ Branch 1 taken 10 times.
36834 if (s->desc->flags & AV_PIX_FMT_FLAG_FLOAT) {
1366 /* todo: change this when a floating point pixel format with luma with alpha is implemented */
1367
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;
1368
2/2
✓ Branch 0 taken 11476 times.
✓ Branch 1 taken 25348 times.
36824 if (s->is_luma) {
1369 11476 channel_buffer[1] = channel_buffer[0];
1370 11476 channel_buffer[2] = channel_buffer[0];
1371 }
1372
1373
2/2
✓ Branch 0 taken 101131 times.
✓ Branch 1 taken 36824 times.
137955 for (c = 0; c < channel_count; c++) {
1374 101131 int plane = s->desc->comp[c].plane;
1375 101131 ptr = p->data[plane] + window_ymin * p->linesize[plane] + (window_xmin * 4);
1376
1377
2/2
✓ Branch 0 taken 146524 times.
✓ Branch 1 taken 101131 times.
247655 for (i = 0; i < ysize; i++, ptr += p->linesize[plane]) {
1378 const uint8_t *src;
1379 union av_intfloat32 *ptr_x;
1380
1381 146524 src = channel_buffer[c];
1382 146524 ptr_x = (union av_intfloat32 *)ptr;
1383
1384 // Zero out the start if xmin is not 0
1385 146524 memset(ptr_x, 0, bxmin);
1386 146524 ptr_x += window_xoffset;
1387
1388
2/2
✓ Branch 0 taken 24648 times.
✓ Branch 1 taken 121876 times.
146524 if (s->pixel_type == EXR_FLOAT ||
1389
1/2
✓ Branch 0 taken 24648 times.
✗ Branch 1 not taken.
24648 s->compression == EXR_DWAA ||
1390
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24648 times.
146524 s->compression == EXR_DWAB) {
1391 // 32-bit
1392 union av_intfloat32 t;
1393
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) {
1394 for (x = 0; x < xsize; x++) {
1395 t.i = bytestream_get_le32(&src);
1396 t.f = trc_func(t.f);
1397 *ptr_x++ = t;
1398 }
1399
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 121876 times.
121876 } else if (one_gamma != 1.f) {
1400 for (x = 0; x < xsize; x++) {
1401 t.i = bytestream_get_le32(&src);
1402 if (t.f > 0.0f && c < 3) /* avoid negative values */
1403 t.f = powf(t.f, one_gamma);
1404 *ptr_x++ = t;
1405 }
1406 } else {
1407
2/2
✓ Branch 0 taken 42250824 times.
✓ Branch 1 taken 121876 times.
42372700 for (x = 0; x < xsize; x++) {
1408 42250824 t.i = bytestream_get_le32(&src);
1409 42250824 *ptr_x++ = t;
1410 }
1411 }
1412
1/2
✓ Branch 0 taken 24648 times.
✗ Branch 1 not taken.
24648 } else if (s->pixel_type == EXR_HALF) {
1413 // 16-bit
1414
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) {
1415
2/2
✓ Branch 0 taken 10514097 times.
✓ Branch 1 taken 24648 times.
10538745 for (x = 0; x < xsize; x++) {
1416 10514097 *ptr_x++ = s->gamma_table[bytestream_get_le16(&src)];
1417 }
1418 } else {
1419 for (x = 0; x < xsize; x++) {
1420 ptr_x[0].i = half2float(bytestream_get_le16(&src), &s->h2f_tables);
1421 ptr_x++;
1422 }
1423 }
1424 }
1425
1426 // Zero out the end if xmax+1 is not w
1427 146524 memset(ptr_x, 0, axmax);
1428 146524 channel_buffer[c] += td->channel_line_size;
1429 }
1430 }
1431 } else {
1432
1433 av_assert1(s->pixel_type == EXR_UINT);
1434 10 ptr = p->data[0] + window_ymin * p->linesize[0] + (window_xmin * s->desc->nb_components * 2);
1435
1436
2/2
✓ Branch 0 taken 175 times.
✓ Branch 1 taken 10 times.
185 for (i = 0; i < ysize; i++, ptr += p->linesize[0]) {
1437
1438 const uint8_t * a;
1439 const uint8_t *rgb[3];
1440 uint16_t *ptr_x;
1441
1442
2/2
✓ Branch 0 taken 525 times.
✓ Branch 1 taken 175 times.
700 for (c = 0; c < rgb_channel_count; c++) {
1443 525 rgb[c] = channel_buffer[c];
1444 }
1445
1446
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 175 times.
175 if (channel_buffer[3])
1447 a = channel_buffer[3];
1448
1449 175 ptr_x = (uint16_t *) ptr;
1450
1451 // Zero out the start if xmin is not 0
1452 175 memset(ptr_x, 0, bxmin);
1453 175 ptr_x += window_xoffset * s->desc->nb_components;
1454
1455
2/2
✓ Branch 0 taken 5309 times.
✓ Branch 1 taken 175 times.
5484 for (x = 0; x < xsize; x++) {
1456
2/2
✓ Branch 0 taken 15927 times.
✓ Branch 1 taken 5309 times.
21236 for (c = 0; c < rgb_channel_count; c++) {
1457 15927 *ptr_x++ = bytestream_get_le32(&rgb[c]) >> 16;
1458 }
1459
1460
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5309 times.
5309 if (channel_buffer[3])
1461 *ptr_x++ = bytestream_get_le32(&a) >> 16;
1462 }
1463
1464 // Zero out the end if xmax+1 is not w
1465 175 memset(ptr_x, 0, axmax);
1466
1467 175 channel_buffer[0] += td->channel_line_size;
1468 175 channel_buffer[1] += td->channel_line_size;
1469 175 channel_buffer[2] += td->channel_line_size;
1470
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 175 times.
175 if (channel_buffer[3])
1471 channel_buffer[3] += td->channel_line_size;
1472 }
1473 }
1474
1475 36834 return 0;
1476 }
1477
1478 static void skip_header_chunk(EXRContext *s)
1479 {
1480 GetByteContext *gb = &s->gb;
1481
1482 while (bytestream2_get_bytes_left(gb) > 0) {
1483 if (!bytestream2_peek_byte(gb))
1484 break;
1485
1486 // Process unknown variables
1487 for (int i = 0; i < 2; i++) // value_name and value_type
1488 while (bytestream2_get_byte(gb) != 0);
1489
1490 // Skip variable length
1491 bytestream2_skip(gb, bytestream2_get_le32(gb));
1492 }
1493 }
1494
1495 /**
1496 * Check if the variable name corresponds to its data type.
1497 *
1498 * @param s the EXRContext
1499 * @param value_name name of the variable to check
1500 * @param value_type type of the variable to check
1501 * @param minimum_length minimum length of the variable data
1502 *
1503 * @return bytes to read containing variable data
1504 * -1 if variable is not found
1505 * 0 if buffer ended prematurely
1506 */
1507 32478 static int check_header_variable(EXRContext *s,
1508 const char *value_name,
1509 const char *value_type,
1510 unsigned int minimum_length)
1511 {
1512 32478 GetByteContext *gb = &s->gb;
1513 32478 int var_size = -1;
1514
1515
1/2
✓ Branch 1 taken 32478 times.
✗ Branch 2 not taken.
32478 if (bytestream2_get_bytes_left(gb) >= minimum_length &&
1516
2/2
✓ Branch 0 taken 2166 times.
✓ Branch 1 taken 30312 times.
32478 !strcmp(gb->buffer, value_name)) {
1517 // found value_name, jump to value_type (null terminated strings)
1518 2166 gb->buffer += strlen(value_name) + 1;
1519
1/2
✓ Branch 0 taken 2166 times.
✗ Branch 1 not taken.
2166 if (!strcmp(gb->buffer, value_type)) {
1520 2166 gb->buffer += strlen(value_type) + 1;
1521 2166 var_size = bytestream2_get_le32(gb);
1522 // don't go read past boundaries
1523
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2166 times.
2166 if (var_size > bytestream2_get_bytes_left(gb))
1524 var_size = 0;
1525 } else {
1526 // value_type not found, reset the buffer
1527 gb->buffer -= strlen(value_name) + 1;
1528 av_log(s->avctx, AV_LOG_WARNING,
1529 "Unknown data type %s for header variable %s.\n",
1530 value_type, value_name);
1531 }
1532 }
1533
1534 32478 return var_size;
1535 }
1536
1537 316 static int decode_header(EXRContext *s, AVFrame *frame)
1538 {
1539 316 AVDictionary *metadata = NULL;
1540 316 GetByteContext *gb = &s->gb;
1541 int magic_number, version, flags;
1542 316 int layer_match = 0;
1543 int ret;
1544 316 int dup_channels = 0;
1545
1546 316 s->current_channel_offset = 0;
1547 316 s->xmin = ~0;
1548 316 s->xmax = ~0;
1549 316 s->ymin = ~0;
1550 316 s->ymax = ~0;
1551 316 s->xdelta = ~0;
1552 316 s->ydelta = ~0;
1553 316 s->channel_offsets[0] = -1;
1554 316 s->channel_offsets[1] = -1;
1555 316 s->channel_offsets[2] = -1;
1556 316 s->channel_offsets[3] = -1;
1557 316 s->pixel_type = EXR_UNKNOWN;
1558 316 s->compression = EXR_UNKN;
1559 316 s->nb_channels = 0;
1560 316 s->w = 0;
1561 316 s->h = 0;
1562 316 s->tile_attr.xSize = -1;
1563 316 s->tile_attr.ySize = -1;
1564 316 s->is_tile = 0;
1565 316 s->is_multipart = 0;
1566 316 s->is_luma = 0;
1567 316 s->current_part = 0;
1568
1569
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 316 times.
316 if (bytestream2_get_bytes_left(gb) < 10) {
1570 av_log(s->avctx, AV_LOG_ERROR, "Header too short to parse.\n");
1571 return AVERROR_INVALIDDATA;
1572 }
1573
1574 316 magic_number = bytestream2_get_le32(gb);
1575
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 316 times.
316 if (magic_number != 20000630) {
1576 /* As per documentation of OpenEXR, it is supposed to be
1577 * int 20000630 little-endian */
1578 av_log(s->avctx, AV_LOG_ERROR, "Wrong magic number %d.\n", magic_number);
1579 return AVERROR_INVALIDDATA;
1580 }
1581
1582 316 version = bytestream2_get_byte(gb);
1583
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 316 times.
316 if (version != 2) {
1584 avpriv_report_missing_feature(s->avctx, "Version %d", version);
1585 return AVERROR_PATCHWELCOME;
1586 }
1587
1588 316 flags = bytestream2_get_le24(gb);
1589
1590
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 272 times.
316 if (flags & 0x02)
1591 44 s->is_tile = 1;
1592
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 316 times.
316 if (flags & 0x10)
1593 s->is_multipart = 1;
1594
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 316 times.
316 if (flags & 0x08) {
1595 avpriv_report_missing_feature(s->avctx, "deep data");
1596 return AVERROR_PATCHWELCOME;
1597 }
1598
1599 // Parse the header
1600
1/2
✓ Branch 1 taken 4396 times.
✗ Branch 2 not taken.
4396 while (bytestream2_get_bytes_left(gb) > 0) {
1601 int var_size;
1602
1603
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 &&
1604 bytestream2_get_bytes_left(gb) > 0) {
1605 if (bytestream2_peek_byte(gb)) {
1606 skip_header_chunk(s);
1607 } else {
1608 bytestream2_skip(gb, 1);
1609 if (!bytestream2_peek_byte(gb))
1610 break;
1611 }
1612 bytestream2_skip(gb, 1);
1613 s->current_part++;
1614 }
1615
1616
2/2
✓ Branch 1 taken 316 times.
✓ Branch 2 taken 4080 times.
4396 if (!bytestream2_peek_byte(gb)) {
1617
1/2
✓ Branch 0 taken 316 times.
✗ Branch 1 not taken.
316 if (!s->is_multipart)
1618 316 break;
1619 bytestream2_skip(gb, 1);
1620 if (s->current_part == s->selected_part) {
1621 while (bytestream2_get_bytes_left(gb) > 0) {
1622 if (bytestream2_peek_byte(gb)) {
1623 skip_header_chunk(s);
1624 } else {
1625 bytestream2_skip(gb, 1);
1626 if (!bytestream2_peek_byte(gb))
1627 break;
1628 }
1629 }
1630 }
1631 if (!bytestream2_peek_byte(gb))
1632 break;
1633 s->current_part++;
1634 }
1635
1636
2/2
✓ Branch 1 taken 316 times.
✓ Branch 2 taken 3764 times.
4080 if ((var_size = check_header_variable(s, "channels",
1637 316 "chlist", 38)) >= 0) {
1638 GetByteContext ch_gb;
1639
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 316 times.
316 if (!var_size) {
1640 ret = AVERROR_INVALIDDATA;
1641 goto fail;
1642 }
1643
1644 316 bytestream2_init(&ch_gb, gb->buffer, var_size);
1645
1646
2/2
✓ Branch 1 taken 1212 times.
✓ Branch 2 taken 316 times.
1528 while (bytestream2_get_bytes_left(&ch_gb) >= 19) {
1647 EXRChannel *channel;
1648 enum ExrPixelType current_pixel_type;
1649 1212 int channel_index = -1;
1650 int xsub, ysub;
1651
1652
2/2
✓ Branch 0 taken 272 times.
✓ Branch 1 taken 940 times.
1212 if (strcmp(s->layer, "") != 0) {
1653
2/2
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 122 times.
272 if (strncmp(ch_gb.buffer, s->layer, strlen(s->layer)) == 0) {
1654 150 layer_match = 1;
1655 150 av_log(s->avctx, AV_LOG_INFO,
1656 "Channel match layer : %s.\n", ch_gb.buffer);
1657 150 ch_gb.buffer += strlen(s->layer);
1658
1/2
✓ Branch 0 taken 150 times.
✗ Branch 1 not taken.
150 if (*ch_gb.buffer == '.')
1659 150 ch_gb.buffer++; /* skip dot if not given */
1660 } else {
1661 122 layer_match = 0;
1662 122 av_log(s->avctx, AV_LOG_INFO,
1663 "Channel doesn't match layer : %s.\n", ch_gb.buffer);
1664 }
1665 } else {
1666 940 layer_match = 1;
1667 }
1668
1669
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 */
1670
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") ||
1671
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 836 times.
1672 !av_strcasecmp(ch_gb.buffer, "X") ||
1672 836 !av_strcasecmp(ch_gb.buffer, "U")) {
1673 254 channel_index = 0;
1674 254 s->is_luma = 0;
1675
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") ||
1676 582 !av_strcasecmp(ch_gb.buffer, "V")) {
1677 254 channel_index = 1;
1678 254 s->is_luma = 0;
1679
2/2
✓ Branch 1 taken 62 times.
✓ Branch 2 taken 520 times.
582 } else if (!av_strcasecmp(ch_gb.buffer, "Y")) {
1680 62 channel_index = 1;
1681 62 s->is_luma = 1;
1682
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") ||
1683
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 266 times.
532 !av_strcasecmp(ch_gb.buffer, "Z") ||
1684 266 !av_strcasecmp(ch_gb.buffer, "W")) {
1685 254 channel_index = 2;
1686 254 s->is_luma = 0;
1687
2/2
✓ Branch 1 taken 114 times.
✓ Branch 2 taken 152 times.
266 } else if (!av_strcasecmp(ch_gb.buffer, "A")) {
1688 114 channel_index = 3;
1689 } else {
1690 152 av_log(s->avctx, AV_LOG_WARNING,
1691 "Unsupported channel %.256s.\n", ch_gb.buffer);
1692 }
1693 }
1694
1695 /* skip until you get a 0 */
1696
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 &&
1697 4856 bytestream2_get_byte(&ch_gb))
1698 3644 continue;
1699
1700
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1212 times.
1212 if (bytestream2_get_bytes_left(&ch_gb) < 4) {
1701 av_log(s->avctx, AV_LOG_ERROR, "Incomplete header.\n");
1702 ret = AVERROR_INVALIDDATA;
1703 goto fail;
1704 }
1705
1706 1212 current_pixel_type = bytestream2_get_le32(&ch_gb);
1707
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1212 times.
1212 if (current_pixel_type >= EXR_UNKNOWN) {
1708 avpriv_report_missing_feature(s->avctx, "Pixel type %d",
1709 current_pixel_type);
1710 ret = AVERROR_PATCHWELCOME;
1711 goto fail;
1712 }
1713
1714 1212 bytestream2_skip(&ch_gb, 4);
1715 1212 xsub = bytestream2_get_le32(&ch_gb);
1716 1212 ysub = bytestream2_get_le32(&ch_gb);
1717
1718
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) {
1719 avpriv_report_missing_feature(s->avctx,
1720 "Subsampling %dx%d",
1721 xsub, ysub);
1722 ret = AVERROR_PATCHWELCOME;
1723 goto fail;
1724 }
1725
1726
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 */
1727
2/2
✓ Branch 0 taken 622 times.
✓ Branch 1 taken 316 times.
938 if (s->pixel_type != EXR_UNKNOWN &&
1728
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 622 times.
622 s->pixel_type != current_pixel_type) {
1729 av_log(s->avctx, AV_LOG_ERROR,
1730 "RGB channels not of the same depth.\n");
1731 ret = AVERROR_INVALIDDATA;
1732 goto fail;
1733 }
1734 938 s->pixel_type = current_pixel_type;
1735 938 s->channel_offsets[channel_index] = s->current_channel_offset;
1736
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 274 times.
274 } else if (channel_index >= 0) {
1737 av_log(s->avctx, AV_LOG_WARNING,
1738 "Multiple channels with index %d.\n", channel_index);
1739 if (++dup_channels > 10) {
1740 ret = AVERROR_INVALIDDATA;
1741 goto fail;
1742 }
1743 }
1744
1745 2424 s->channels = av_realloc(s->channels,
1746 1212 ++s->nb_channels * sizeof(EXRChannel));
1747
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1212 times.
1212 if (!s->channels) {
1748 ret = AVERROR(ENOMEM);
1749 goto fail;
1750 }
1751 1212 channel = &s->channels[s->nb_channels - 1];
1752 1212 channel->pixel_type = current_pixel_type;
1753 1212 channel->xsub = xsub;
1754 1212 channel->ysub = ysub;
1755
1756
2/2
✓ Branch 0 taken 404 times.
✓ Branch 1 taken 808 times.
1212 if (current_pixel_type == EXR_HALF) {
1757 404 s->current_channel_offset += 2;
1758 } else {/* Float or UINT32 */
1759 808 s->current_channel_offset += 4;
1760 }
1761 }
1762
1763 /* Check if all channels are set with an offset or if the channels
1764 * are causing an overflow */
1765
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 */
1766
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 254 times.
254 if (FFMIN3(s->channel_offsets[0],
1767 s->channel_offsets[1],
1768 s->channel_offsets[2]) < 0) {
1769 if (s->channel_offsets[0] < 0)
1770 av_log(s->avctx, AV_LOG_ERROR, "Missing red channel.\n");
1771 if (s->channel_offsets[1] < 0)
1772 av_log(s->avctx, AV_LOG_ERROR, "Missing green channel.\n");
1773 if (s->channel_offsets[2] < 0)
1774 av_log(s->avctx, AV_LOG_ERROR, "Missing blue channel.\n");
1775 ret = AVERROR_INVALIDDATA;
1776 goto fail;
1777 }
1778 }
1779
1780 // skip one last byte and update main gb
1781 316 gb->buffer = ch_gb.buffer + 1;
1782 316 continue;
1783
2/2
✓ Branch 1 taken 316 times.
✓ Branch 2 taken 3448 times.
3764 } else if ((var_size = check_header_variable(s, "dataWindow", "box2i",
1784 316 31)) >= 0) {
1785 int xmin, ymin, xmax, ymax;
1786
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 316 times.
316 if (!var_size) {
1787 ret = AVERROR_INVALIDDATA;
1788 goto fail;
1789 }
1790
1791 316 xmin = bytestream2_get_le32(gb);
1792 316 ymin = bytestream2_get_le32(gb);
1793 316 xmax = bytestream2_get_le32(gb);
1794 316 ymax = bytestream2_get_le32(gb);
1795
1796
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 ||
1797
1/2
✓ Branch 0 taken 316 times.
✗ Branch 1 not taken.
316 ymax == INT_MAX || xmax == INT_MAX ||
1798
1/2
✓ Branch 0 taken 316 times.
✗ Branch 1 not taken.
316 (unsigned)xmax - xmin >= INT_MAX ||
1799
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 316 times.
316 (unsigned)ymax - ymin >= INT_MAX) {
1800 ret = AVERROR_INVALIDDATA;
1801 goto fail;
1802 }
1803 316 s->xmin = xmin;
1804 316 s->xmax = xmax;
1805 316 s->ymin = ymin;
1806 316 s->ymax = ymax;
1807 316 s->xdelta = (s->xmax - s->xmin) + 1;
1808 316 s->ydelta = (s->ymax - s->ymin) + 1;
1809
1810 316 continue;
1811
2/2
✓ Branch 1 taken 316 times.
✓ Branch 2 taken 3132 times.
3448 } else if ((var_size = check_header_variable(s, "displayWindow",
1812 316 "box2i", 34)) >= 0) {
1813 int32_t sx, sy, dx, dy;
1814
1815
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 316 times.
316 if (!var_size) {
1816 ret = AVERROR_INVALIDDATA;
1817 goto fail;
1818 }
1819
1820 316 sx = bytestream2_get_le32(gb);
1821 316 sy = bytestream2_get_le32(gb);
1822 316 dx = bytestream2_get_le32(gb);
1823 316 dy = bytestream2_get_le32(gb);
1824
1825 316 s->w = (unsigned)dx - sx + 1;
1826 316 s->h = (unsigned)dy - sy + 1;
1827
1828 316 continue;
1829
2/2
✓ Branch 1 taken 316 times.
✓ Branch 2 taken 2816 times.
3132 } else if ((var_size = check_header_variable(s, "lineOrder",
1830 316 "lineOrder", 25)) >= 0) {
1831 int line_order;
1832
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 316 times.
316 if (!var_size) {
1833 ret = AVERROR_INVALIDDATA;
1834 goto fail;
1835 }
1836
1837 316 line_order = bytestream2_get_byte(gb);
1838 316 av_log(s->avctx, AV_LOG_DEBUG, "line order: %d.\n", line_order);
1839
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 316 times.
316 if (line_order > 2) {
1840 av_log(s->avctx, AV_LOG_ERROR, "Unknown line order.\n");
1841 ret = AVERROR_INVALIDDATA;
1842 goto fail;
1843 }
1844
1845 316 continue;
1846
2/2
✓ Branch 1 taken 148 times.
✓ Branch 2 taken 2668 times.
2816 } else if ((var_size = check_header_variable(s, "pixelAspectRatio",
1847 "float", 31)) >= 0) {
1848
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 148 times.
148 if (!var_size) {
1849 ret = AVERROR_INVALIDDATA;
1850 goto fail;
1851 }
1852
1853 148 s->sar = bytestream2_get_le32(gb);
1854
1855 148 continue;
1856
2/2
✓ Branch 1 taken 316 times.
✓ Branch 2 taken 2352 times.
2668 } else if ((var_size = check_header_variable(s, "compression",
1857 "compression", 29)) >= 0) {
1858
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 316 times.
316 if (!var_size) {
1859 ret = AVERROR_INVALIDDATA;
1860 goto fail;
1861 }
1862
1863
1/2
✓ Branch 0 taken 316 times.
✗ Branch 1 not taken.
316 if (s->compression == EXR_UNKN)
1864 316 s->compression = bytestream2_get_byte(gb);
1865 else {
1866 bytestream2_skip(gb, 1);
1867 av_log(s->avctx, AV_LOG_WARNING,
1868 "Found more than one compression attribute.\n");
1869 }
1870
1871 316 continue;
1872
2/2
✓ Branch 1 taken 44 times.
✓ Branch 2 taken 2308 times.
2352 } else if ((var_size = check_header_variable(s, "tiles",
1873 44 "tiledesc", 22)) >= 0) {
1874 char tileLevel;
1875
1876
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
44 if (!s->is_tile)
1877 av_log(s->avctx, AV_LOG_WARNING,
1878 "Found tile attribute and scanline flags. Exr will be interpreted as scanline.\n");
1879
1880 44 s->tile_attr.xSize = bytestream2_get_le32(gb);
1881 44 s->tile_attr.ySize = bytestream2_get_le32(gb);
1882
1883 44 tileLevel = bytestream2_get_byte(gb);
1884 44 s->tile_attr.level_mode = tileLevel & 0x0f;
1885 44 s->tile_attr.level_round = (tileLevel >> 4) & 0x0f;
1886
1887
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
44 if (s->tile_attr.level_mode >= EXR_TILE_LEVEL_UNKNOWN) {
1888 avpriv_report_missing_feature(s->avctx, "Tile level mode %d",
1889 s->tile_attr.level_mode);
1890 ret = AVERROR_PATCHWELCOME;
1891 goto fail;
1892 }
1893
1894
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
44 if (s->tile_attr.level_round >= EXR_TILE_ROUND_UNKNOWN) {
1895 avpriv_report_missing_feature(s->avctx, "Tile level round %d",
1896 s->tile_attr.level_round);
1897 ret = AVERROR_PATCHWELCOME;
1898 goto fail;
1899 }
1900
1901 44 continue;
1902
2/2
✓ Branch 1 taken 184 times.
✓ Branch 2 taken 2124 times.
2308 } else if ((var_size = check_header_variable(s, "writer",
1903 184 "string", 1)) >= 0) {
1904 184 uint8_t key[256] = { 0 };
1905
1906 184 bytestream2_get_buffer(gb, key, FFMIN(sizeof(key) - 1, var_size));
1907 184 av_dict_set(&metadata, "writer", key, 0);
1908
1909 184 continue;
1910
2/2
✓ Branch 1 taken 184 times.
✓ Branch 2 taken 1940 times.
2124 } else if ((var_size = check_header_variable(s, "framesPerSecond",
1911 "rational", 33)) >= 0) {
1912
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 184 times.
184 if (!var_size) {
1913 ret = AVERROR_INVALIDDATA;
1914 goto fail;
1915 }
1916
1917 184 s->avctx->framerate.num = bytestream2_get_le32(gb);
1918 184 s->avctx->framerate.den = bytestream2_get_le32(gb);
1919
1920 184 continue;
1921
2/2
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 1932 times.
1940 } else if ((var_size = check_header_variable(s, "chunkCount",
1922 "int", 23)) >= 0) {
1923
1924 8 s->chunk_count = bytestream2_get_le32(gb);
1925
1926 8 continue;
1927
2/2
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 1914 times.
1932 } else if ((var_size = check_header_variable(s, "type",
1928 18 "string", 16)) >= 0) {
1929 18 uint8_t key[256] = { 0 };
1930
1931 18 bytestream2_get_buffer(gb, key, FFMIN(sizeof(key) - 1, var_size));
1932
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 10 times.
18 if (strncmp("scanlineimage", key, var_size) &&
1933
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 strncmp("tiledimage", key, var_size))
1934 return AVERROR_PATCHWELCOME;
1935
1936 18 continue;
1937
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1914 times.
1914 } else if ((var_size = check_header_variable(s, "preview",
1938 "preview", 16)) >= 0) {
1939 uint32_t pw = bytestream2_get_le32(gb);
1940 uint32_t ph = bytestream2_get_le32(gb);
1941 uint64_t psize = pw * ph;
1942 if (psize > INT64_MAX / 4)
1943 return AVERROR_INVALIDDATA;
1944 psize *= 4;
1945
1946 if ((int64_t)psize >= bytestream2_get_bytes_left(gb))
1947 return AVERROR_INVALIDDATA;
1948
1949 bytestream2_skip(gb, psize);
1950
1951 continue;
1952 }
1953
1954 // Check if there are enough bytes for a header
1955
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1914 times.
1914 if (bytestream2_get_bytes_left(gb) <= 9) {
1956 av_log(s->avctx, AV_LOG_ERROR, "Incomplete header\n");
1957 ret = AVERROR_INVALIDDATA;
1958 goto fail;
1959 }
1960
1961 // Process unknown variables
1962 {
1963 1914 uint8_t name[256] = { 0 };
1964 1914 uint8_t type[256] = { 0 };
1965 1914 uint8_t value[8192] = { 0 };
1966 1914 int i = 0, size;
1967
1968
2/2
✓ Branch 1 taken 26926 times.
✓ Branch 2 taken 1914 times.
57680 while (bytestream2_get_bytes_left(gb) > 0 &&
1969
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) {
1970 26926 name[i++] = bytestream2_get_byte(gb);
1971 }
1972
1973 1914 bytestream2_skip(gb, 1);
1974 1914 i = 0;
1975
2/2
✓ Branch 1 taken 8774 times.
✓ Branch 2 taken 1914 times.
21376 while (bytestream2_get_bytes_left(gb) > 0 &&
1976
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) {
1977 8774 type[i++] = bytestream2_get_byte(gb);
1978 }
1979 1914 bytestream2_skip(gb, 1);
1980 1914 size = bytestream2_get_le32(gb);
1981
1982 1914 bytestream2_get_buffer(gb, value, FFMIN(sizeof(value) - 1, size));
1983
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1914 times.
1914 if (size > sizeof(value) - 1)
1984 bytestream2_skip(gb, size - (sizeof(value) - 1));
1985
2/2
✓ Branch 0 taken 160 times.
✓ Branch 1 taken 1754 times.
1914 if (!strcmp(type, "string"))
1986 160 av_dict_set(&metadata, name, value, 0);
1987 }
1988 }
1989
1990
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 316 times.
316 if (s->compression == EXR_UNKN) {
1991 av_log(s->avctx, AV_LOG_ERROR, "Missing compression attribute.\n");
1992 ret = AVERROR_INVALIDDATA;
1993 goto fail;
1994 }
1995
1996
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 272 times.
316 if (s->is_tile) {
1997
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) {
1998 av_log(s->avctx, AV_LOG_ERROR, "Invalid tile attribute.\n");
1999 ret = AVERROR_INVALIDDATA;
2000 goto fail;
2001 }
2002 }
2003
2004
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 316 times.
316 if (bytestream2_get_bytes_left(gb) <= 0) {
2005 av_log(s->avctx, AV_LOG_ERROR, "Incomplete frame.\n");
2006 ret = AVERROR_INVALIDDATA;
2007 goto fail;
2008 }
2009
2010 316 frame->metadata = metadata;
2011
2012 // aaand we are done
2013 316 bytestream2_skip(gb, 1);
2014 316 return 0;
2015 fail:
2016 av_dict_free(&metadata);
2017 return ret;
2018 }
2019
2020 316 static int decode_frame(AVCodecContext *avctx, AVFrame *picture,
2021 int *got_frame, AVPacket *avpkt)
2022 {
2023 316 EXRContext *s = avctx->priv_data;
2024 316 GetByteContext *gb = &s->gb;
2025 uint8_t *ptr;
2026
2027 int i, y, ret, ymax;
2028 int planes;
2029 int out_line_size;
2030 int nb_blocks; /* nb scanline or nb tile */
2031 uint64_t start_offset_table;
2032 uint64_t start_next_scanline;
2033
2034 316 bytestream2_init(gb, avpkt->data, avpkt->size);
2035
2036
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 316 times.
316 if ((ret = decode_header(s, picture)) < 0)
2037 return ret;
2038
2039
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) &&
2040 s->pixel_type == EXR_HALF) {
2041 s->current_channel_offset *= 2;
2042 for (int i = 0; i < 4; i++)
2043 s->channel_offsets[i] *= 2;
2044 }
2045