FFmpeg  4.0
exr.c
Go to the documentation of this file.
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  * exr_flt2uint() and exr_halflt2uint() is credited to Reimar Döffinger.
34  * exr_half2float() is credited to Aaftab Munshi, Dan Ginsburg, Dave Shreiner.
35  */
36 
37 #include <float.h>
38 #include <zlib.h>
39 
40 #include "libavutil/avassert.h"
41 #include "libavutil/common.h"
42 #include "libavutil/imgutils.h"
43 #include "libavutil/intfloat.h"
44 #include "libavutil/opt.h"
45 #include "libavutil/color_utils.h"
46 
47 #include "avcodec.h"
48 #include "bytestream.h"
49 
50 #if HAVE_BIGENDIAN
51 #include "bswapdsp.h"
52 #endif
53 
54 #include "exrdsp.h"
55 #include "get_bits.h"
56 #include "internal.h"
57 #include "mathops.h"
58 #include "thread.h"
59 
60 enum ExrCompr {
72 };
73 
79 };
80 
86 };
87 
92 };
93 
94 typedef struct EXRChannel {
95  int xsub, ysub;
97 } EXRChannel;
98 
99 typedef struct EXRTileAttribute {
102  enum ExrTileLevelMode level_mode;
103  enum ExrTileLevelRound level_round;
105 
106 typedef struct EXRThreadData {
109 
111  int tmp_size;
112 
114  uint16_t *lut;
115 
116  int ysize, xsize;
117 
119 } EXRThreadData;
120 
121 typedef struct EXRContext {
122  AVClass *class;
126 
127 #if HAVE_BIGENDIAN
128  BswapDSPContext bbdsp;
129 #endif
130 
131  enum ExrCompr compression;
133  int channel_offsets[4]; // 0 = red, 1 = green, 2 = blue and 3 = alpha
135 
136  int w, h;
137  uint32_t xmax, xmin;
138  uint32_t ymax, ymin;
139  uint32_t xdelta, ydelta;
140 
142 
143  EXRTileAttribute tile_attr; /* header data attribute of tile */
144  int is_tile; /* 0 if scanline, 1 if tile */
145 
146  int is_luma;/* 1 if there is an Y plane */
147 
149  const uint8_t *buf;
150  int buf_size;
151 
155 
157 
158  const char *layer;
159 
160  enum AVColorTransferCharacteristic apply_trc_type;
161  float gamma;
162  uint16_t gamma_table[65536];
163 } EXRContext;
164 
165 /* -15 stored using a single precision bias of 127 */
166 #define HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP 0x38000000
167 
168 /* max exponent value in single precision that will be converted
169  * to Inf or Nan when stored as a half-float */
170 #define HALF_FLOAT_MAX_BIASED_EXP_AS_SINGLE_FP_EXP 0x47800000
171 
172 /* 255 is the max exponent biased value */
173 #define FLOAT_MAX_BIASED_EXP (0xFF << 23)
174 
175 #define HALF_FLOAT_MAX_BIASED_EXP (0x1F << 10)
176 
177 /**
178  * Convert a half float as a uint16_t into a full float.
179  *
180  * @param hf half float as uint16_t
181  *
182  * @return float value
183  */
184 static union av_intfloat32 exr_half2float(uint16_t hf)
185 {
186  unsigned int sign = (unsigned int) (hf >> 15);
187  unsigned int mantissa = (unsigned int) (hf & ((1 << 10) - 1));
188  unsigned int exp = (unsigned int) (hf & HALF_FLOAT_MAX_BIASED_EXP);
189  union av_intfloat32 f;
190 
191  if (exp == HALF_FLOAT_MAX_BIASED_EXP) {
192  // we have a half-float NaN or Inf
193  // half-float NaNs will be converted to a single precision NaN
194  // half-float Infs will be converted to a single precision Inf
195  exp = FLOAT_MAX_BIASED_EXP;
196  if (mantissa)
197  mantissa = (1 << 23) - 1; // set all bits to indicate a NaN
198  } else if (exp == 0x0) {
199  // convert half-float zero/denorm to single precision value
200  if (mantissa) {
201  mantissa <<= 1;
203  // check for leading 1 in denorm mantissa
204  while ((mantissa & (1 << 10))) {
205  // for every leading 0, decrement single precision exponent by 1
206  // and shift half-float mantissa value to the left
207  mantissa <<= 1;
208  exp -= (1 << 23);
209  }
210  // clamp the mantissa to 10 bits
211  mantissa &= ((1 << 10) - 1);
212  // shift left to generate single-precision mantissa of 23 bits
213  mantissa <<= 13;
214  }
215  } else {
216  // shift left to generate single-precision mantissa of 23 bits
217  mantissa <<= 13;
218  // generate single precision biased exponent value
219  exp = (exp << 13) + HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP;
220  }
221 
222  f.i = (sign << 31) | exp | mantissa;
223 
224  return f;
225 }
226 
227 
228 /**
229  * Convert from 32-bit float as uint32_t to uint16_t.
230  *
231  * @param v 32-bit float
232  *
233  * @return normalized 16-bit unsigned int
234  */
235 static inline uint16_t exr_flt2uint(int32_t v)
236 {
237  int32_t exp = v >> 23;
238  // "HACK": negative values result in exp< 0, so clipping them to 0
239  // is also handled by this condition, avoids explicit check for sign bit.
240  if (exp <= 127 + 7 - 24) // we would shift out all bits anyway
241  return 0;
242  if (exp >= 127)
243  return 0xffff;
244  v &= 0x007fffff;
245  return (v + (1 << 23)) >> (127 + 7 - exp);
246 }
247 
248 /**
249  * Convert from 16-bit float as uint16_t to uint16_t.
250  *
251  * @param v 16-bit float
252  *
253  * @return normalized 16-bit unsigned int
254  */
255 static inline uint16_t exr_halflt2uint(uint16_t v)
256 {
257  unsigned exp = 14 - (v >> 10);
258  if (exp >= 14) {
259  if (exp == 14)
260  return (v >> 9) & 1;
261  else
262  return (v & 0x8000) ? 0 : 0xffff;
263  }
264  v <<= 6;
265  return (v + (1 << 16)) >> (exp + 1);
266 }
267 
268 static int zip_uncompress(EXRContext *s, const uint8_t *src, int compressed_size,
269  int uncompressed_size, EXRThreadData *td)
270 {
271  unsigned long dest_len = uncompressed_size;
272 
273  if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK ||
274  dest_len != uncompressed_size)
275  return AVERROR_INVALIDDATA;
276 
277  av_assert1(uncompressed_size % 2 == 0);
278 
279  s->dsp.predictor(td->tmp, uncompressed_size);
280  s->dsp.reorder_pixels(td->uncompressed_data, td->tmp, uncompressed_size);
281 
282  return 0;
283 }
284 
285 static int rle_uncompress(EXRContext *ctx, const uint8_t *src, int compressed_size,
286  int uncompressed_size, EXRThreadData *td)
287 {
288  uint8_t *d = td->tmp;
289  const int8_t *s = src;
290  int ssize = compressed_size;
291  int dsize = uncompressed_size;
292  uint8_t *dend = d + dsize;
293  int count;
294 
295  while (ssize > 0) {
296  count = *s++;
297 
298  if (count < 0) {
299  count = -count;
300 
301  if ((dsize -= count) < 0 ||
302  (ssize -= count + 1) < 0)
303  return AVERROR_INVALIDDATA;
304 
305  while (count--)
306  *d++ = *s++;
307  } else {
308  count++;
309 
310  if ((dsize -= count) < 0 ||
311  (ssize -= 2) < 0)
312  return AVERROR_INVALIDDATA;
313 
314  while (count--)
315  *d++ = *s;
316 
317  s++;
318  }
319  }
320 
321  if (dend != d)
322  return AVERROR_INVALIDDATA;
323 
324  av_assert1(uncompressed_size % 2 == 0);
325 
326  ctx->dsp.predictor(td->tmp, uncompressed_size);
327  ctx->dsp.reorder_pixels(td->uncompressed_data, td->tmp, uncompressed_size);
328 
329  return 0;
330 }
331 
332 #define USHORT_RANGE (1 << 16)
333 #define BITMAP_SIZE (1 << 13)
334 
335 static uint16_t reverse_lut(const uint8_t *bitmap, uint16_t *lut)
336 {
337  int i, k = 0;
338 
339  for (i = 0; i < USHORT_RANGE; i++)
340  if ((i == 0) || (bitmap[i >> 3] & (1 << (i & 7))))
341  lut[k++] = i;
342 
343  i = k - 1;
344 
345  memset(lut + k, 0, (USHORT_RANGE - k) * 2);
346 
347  return i;
348 }
349 
350 static void apply_lut(const uint16_t *lut, uint16_t *dst, int dsize)
351 {
352  int i;
353 
354  for (i = 0; i < dsize; ++i)
355  dst[i] = lut[dst[i]];
356 }
357 
358 #define HUF_ENCBITS 16 // literal (value) bit length
359 #define HUF_DECBITS 14 // decoding bit size (>= 8)
360 
361 #define HUF_ENCSIZE ((1 << HUF_ENCBITS) + 1) // encoding table size
362 #define HUF_DECSIZE (1 << HUF_DECBITS) // decoding table size
363 #define HUF_DECMASK (HUF_DECSIZE - 1)
364 
365 typedef struct HufDec {
366  int len;
367  int lit;
368  int *p;
369 } HufDec;
370 
371 static void huf_canonical_code_table(uint64_t *hcode)
372 {
373  uint64_t c, n[59] = { 0 };
374  int i;
375 
376  for (i = 0; i < HUF_ENCSIZE; ++i)
377  n[hcode[i]] += 1;
378 
379  c = 0;
380  for (i = 58; i > 0; --i) {
381  uint64_t nc = ((c + n[i]) >> 1);
382  n[i] = c;
383  c = nc;
384  }
385 
386  for (i = 0; i < HUF_ENCSIZE; ++i) {
387  int l = hcode[i];
388 
389  if (l > 0)
390  hcode[i] = l | (n[l]++ << 6);
391  }
392 }
393 
394 #define SHORT_ZEROCODE_RUN 59
395 #define LONG_ZEROCODE_RUN 63
396 #define SHORTEST_LONG_RUN (2 + LONG_ZEROCODE_RUN - SHORT_ZEROCODE_RUN)
397 #define LONGEST_LONG_RUN (255 + SHORTEST_LONG_RUN)
398 
400  int32_t im, int32_t iM, uint64_t *hcode)
401 {
402  GetBitContext gbit;
403  int ret = init_get_bits8(&gbit, gb->buffer, bytestream2_get_bytes_left(gb));
404  if (ret < 0)
405  return ret;
406 
407  for (; im <= iM; im++) {
408  uint64_t l = hcode[im] = get_bits(&gbit, 6);
409 
410  if (l == LONG_ZEROCODE_RUN) {
411  int zerun = get_bits(&gbit, 8) + SHORTEST_LONG_RUN;
412 
413  if (im + zerun > iM + 1)
414  return AVERROR_INVALIDDATA;
415 
416  while (zerun--)
417  hcode[im++] = 0;
418 
419  im--;
420  } else if (l >= SHORT_ZEROCODE_RUN) {
421  int zerun = l - SHORT_ZEROCODE_RUN + 2;
422 
423  if (im + zerun > iM + 1)
424  return AVERROR_INVALIDDATA;
425 
426  while (zerun--)
427  hcode[im++] = 0;
428 
429  im--;
430  }
431  }
432 
433  bytestream2_skip(gb, (get_bits_count(&gbit) + 7) / 8);
435 
436  return 0;
437 }
438 
439 static int huf_build_dec_table(const uint64_t *hcode, int im,
440  int iM, HufDec *hdecod)
441 {
442  for (; im <= iM; im++) {
443  uint64_t c = hcode[im] >> 6;
444  int i, l = hcode[im] & 63;
445 
446  if (c >> l)
447  return AVERROR_INVALIDDATA;
448 
449  if (l > HUF_DECBITS) {
450  HufDec *pl = hdecod + (c >> (l - HUF_DECBITS));
451  if (pl->len)
452  return AVERROR_INVALIDDATA;
453 
454  pl->lit++;
455 
456  pl->p = av_realloc(pl->p, pl->lit * sizeof(int));
457  if (!pl->p)
458  return AVERROR(ENOMEM);
459 
460  pl->p[pl->lit - 1] = im;
461  } else if (l) {
462  HufDec *pl = hdecod + (c << (HUF_DECBITS - l));
463 
464  for (i = 1 << (HUF_DECBITS - l); i > 0; i--, pl++) {
465  if (pl->len || pl->p)
466  return AVERROR_INVALIDDATA;
467  pl->len = l;
468  pl->lit = im;
469  }
470  }
471  }
472 
473  return 0;
474 }
475 
476 #define get_char(c, lc, gb) \
477 { \
478  c = (c << 8) | bytestream2_get_byte(gb); \
479  lc += 8; \
480 }
481 
482 #define get_code(po, rlc, c, lc, gb, out, oe, outb) \
483 { \
484  if (po == rlc) { \
485  if (lc < 8) \
486  get_char(c, lc, gb); \
487  lc -= 8; \
488  \
489  cs = c >> lc; \
490  \
491  if (out + cs > oe || out == outb) \
492  return AVERROR_INVALIDDATA; \
493  \
494  s = out[-1]; \
495  \
496  while (cs-- > 0) \
497  *out++ = s; \
498  } else if (out < oe) { \
499  *out++ = po; \
500  } else { \
501  return AVERROR_INVALIDDATA; \
502  } \
503 }
504 
505 static int huf_decode(const uint64_t *hcode, const HufDec *hdecod,
506  GetByteContext *gb, int nbits,
507  int rlc, int no, uint16_t *out)
508 {
509  uint64_t c = 0;
510  uint16_t *outb = out;
511  uint16_t *oe = out + no;
512  const uint8_t *ie = gb->buffer + (nbits + 7) / 8; // input byte size
513  uint8_t cs;
514  uint16_t s;
515  int i, lc = 0;
516 
517  while (gb->buffer < ie) {
518  get_char(c, lc, gb);
519 
520  while (lc >= HUF_DECBITS) {
521  const HufDec pl = hdecod[(c >> (lc - HUF_DECBITS)) & HUF_DECMASK];
522 
523  if (pl.len) {
524  lc -= pl.len;
525  get_code(pl.lit, rlc, c, lc, gb, out, oe, outb);
526  } else {
527  int j;
528 
529  if (!pl.p)
530  return AVERROR_INVALIDDATA;
531 
532  for (j = 0; j < pl.lit; j++) {
533  int l = hcode[pl.p[j]] & 63;
534 
535  while (lc < l && bytestream2_get_bytes_left(gb) > 0)
536  get_char(c, lc, gb);
537 
538  if (lc >= l) {
539  if ((hcode[pl.p[j]] >> 6) ==
540  ((c >> (lc - l)) & ((1LL << l) - 1))) {
541  lc -= l;
542  get_code(pl.p[j], rlc, c, lc, gb, out, oe, outb);
543  break;
544  }
545  }
546  }
547 
548  if (j == pl.lit)
549  return AVERROR_INVALIDDATA;
550  }
551  }
552  }
553 
554  i = (8 - nbits) & 7;
555  c >>= i;
556  lc -= i;
557 
558  while (lc > 0) {
559  const HufDec pl = hdecod[(c << (HUF_DECBITS - lc)) & HUF_DECMASK];
560 
561  if (pl.len && lc >= pl.len) {
562  lc -= pl.len;
563  get_code(pl.lit, rlc, c, lc, gb, out, oe, outb);
564  } else {
565  return AVERROR_INVALIDDATA;
566  }
567  }
568 
569  if (out - outb != no)
570  return AVERROR_INVALIDDATA;
571  return 0;
572 }
573 
575  uint16_t *dst, int dst_size)
576 {
577  int32_t src_size, im, iM;
578  uint32_t nBits;
579  uint64_t *freq;
580  HufDec *hdec;
581  int ret, i;
582 
583  src_size = bytestream2_get_le32(gb);
584  im = bytestream2_get_le32(gb);
585  iM = bytestream2_get_le32(gb);
586  bytestream2_skip(gb, 4);
587  nBits = bytestream2_get_le32(gb);
588  if (im < 0 || im >= HUF_ENCSIZE ||
589  iM < 0 || iM >= HUF_ENCSIZE ||
590  src_size < 0)
591  return AVERROR_INVALIDDATA;
592 
593  bytestream2_skip(gb, 4);
594 
595  freq = av_mallocz_array(HUF_ENCSIZE, sizeof(*freq));
596  hdec = av_mallocz_array(HUF_DECSIZE, sizeof(*hdec));
597  if (!freq || !hdec) {
598  ret = AVERROR(ENOMEM);
599  goto fail;
600  }
601 
602  if ((ret = huf_unpack_enc_table(gb, im, iM, freq)) < 0)
603  goto fail;
604 
605  if (nBits > 8 * bytestream2_get_bytes_left(gb)) {
606  ret = AVERROR_INVALIDDATA;
607  goto fail;
608  }
609 
610  if ((ret = huf_build_dec_table(freq, im, iM, hdec)) < 0)
611  goto fail;
612  ret = huf_decode(freq, hdec, gb, nBits, iM, dst_size, dst);
613 
614 fail:
615  for (i = 0; i < HUF_DECSIZE; i++)
616  if (hdec)
617  av_freep(&hdec[i].p);
618 
619  av_free(freq);
620  av_free(hdec);
621 
622  return ret;
623 }
624 
625 static inline void wdec14(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
626 {
627  int16_t ls = l;
628  int16_t hs = h;
629  int hi = hs;
630  int ai = ls + (hi & 1) + (hi >> 1);
631  int16_t as = ai;
632  int16_t bs = ai - hi;
633 
634  *a = as;
635  *b = bs;
636 }
637 
638 #define NBITS 16
639 #define A_OFFSET (1 << (NBITS - 1))
640 #define MOD_MASK ((1 << NBITS) - 1)
641 
642 static inline void wdec16(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
643 {
644  int m = l;
645  int d = h;
646  int bb = (m - (d >> 1)) & MOD_MASK;
647  int aa = (d + bb - A_OFFSET) & MOD_MASK;
648  *b = bb;
649  *a = aa;
650 }
651 
652 static void wav_decode(uint16_t *in, int nx, int ox,
653  int ny, int oy, uint16_t mx)
654 {
655  int w14 = (mx < (1 << 14));
656  int n = (nx > ny) ? ny : nx;
657  int p = 1;
658  int p2;
659 
660  while (p <= n)
661  p <<= 1;
662 
663  p >>= 1;
664  p2 = p;
665  p >>= 1;
666 
667  while (p >= 1) {
668  uint16_t *py = in;
669  uint16_t *ey = in + oy * (ny - p2);
670  uint16_t i00, i01, i10, i11;
671  int oy1 = oy * p;
672  int oy2 = oy * p2;
673  int ox1 = ox * p;
674  int ox2 = ox * p2;
675 
676  for (; py <= ey; py += oy2) {
677  uint16_t *px = py;
678  uint16_t *ex = py + ox * (nx - p2);
679 
680  for (; px <= ex; px += ox2) {
681  uint16_t *p01 = px + ox1;
682  uint16_t *p10 = px + oy1;
683  uint16_t *p11 = p10 + ox1;
684 
685  if (w14) {
686  wdec14(*px, *p10, &i00, &i10);
687  wdec14(*p01, *p11, &i01, &i11);
688  wdec14(i00, i01, px, p01);
689  wdec14(i10, i11, p10, p11);
690  } else {
691  wdec16(*px, *p10, &i00, &i10);
692  wdec16(*p01, *p11, &i01, &i11);
693  wdec16(i00, i01, px, p01);
694  wdec16(i10, i11, p10, p11);
695  }
696  }
697 
698  if (nx & p) {
699  uint16_t *p10 = px + oy1;
700 
701  if (w14)
702  wdec14(*px, *p10, &i00, p10);
703  else
704  wdec16(*px, *p10, &i00, p10);
705 
706  *px = i00;
707  }
708  }
709 
710  if (ny & p) {
711  uint16_t *px = py;
712  uint16_t *ex = py + ox * (nx - p2);
713 
714  for (; px <= ex; px += ox2) {
715  uint16_t *p01 = px + ox1;
716 
717  if (w14)
718  wdec14(*px, *p01, &i00, p01);
719  else
720  wdec16(*px, *p01, &i00, p01);
721 
722  *px = i00;
723  }
724  }
725 
726  p2 = p;
727  p >>= 1;
728  }
729 }
730 
731 static int piz_uncompress(EXRContext *s, const uint8_t *src, int ssize,
732  int dsize, EXRThreadData *td)
733 {
734  GetByteContext gb;
735  uint16_t maxval, min_non_zero, max_non_zero;
736  uint16_t *ptr;
737  uint16_t *tmp = (uint16_t *)td->tmp;
738  uint16_t *out;
739  uint16_t *in;
740  int ret, i, j;
741  int pixel_half_size;/* 1 for half, 2 for float and uint32 */
743  int tmp_offset;
744 
745  if (!td->bitmap)
747  if (!td->lut)
748  td->lut = av_malloc(1 << 17);
749  if (!td->bitmap || !td->lut) {
750  av_freep(&td->bitmap);
751  av_freep(&td->lut);
752  return AVERROR(ENOMEM);
753  }
754 
755  bytestream2_init(&gb, src, ssize);
756  min_non_zero = bytestream2_get_le16(&gb);
757  max_non_zero = bytestream2_get_le16(&gb);
758 
759  if (max_non_zero >= BITMAP_SIZE)
760  return AVERROR_INVALIDDATA;
761 
762  memset(td->bitmap, 0, FFMIN(min_non_zero, BITMAP_SIZE));
763  if (min_non_zero <= max_non_zero)
764  bytestream2_get_buffer(&gb, td->bitmap + min_non_zero,
765  max_non_zero - min_non_zero + 1);
766  memset(td->bitmap + max_non_zero + 1, 0, BITMAP_SIZE - max_non_zero - 1);
767 
768  maxval = reverse_lut(td->bitmap, td->lut);
769 
770  ret = huf_uncompress(&gb, tmp, dsize / sizeof(uint16_t));
771  if (ret)
772  return ret;
773 
774  ptr = tmp;
775  for (i = 0; i < s->nb_channels; i++) {
776  channel = &s->channels[i];
777 
778  if (channel->pixel_type == EXR_HALF)
779  pixel_half_size = 1;
780  else
781  pixel_half_size = 2;
782 
783  for (j = 0; j < pixel_half_size; j++)
784  wav_decode(ptr + j, td->xsize, pixel_half_size, td->ysize,
785  td->xsize * pixel_half_size, maxval);
786  ptr += td->xsize * td->ysize * pixel_half_size;
787  }
788 
789  apply_lut(td->lut, tmp, dsize / sizeof(uint16_t));
790 
791  out = (uint16_t *)td->uncompressed_data;
792  for (i = 0; i < td->ysize; i++) {
793  tmp_offset = 0;
794  for (j = 0; j < s->nb_channels; j++) {
795  channel = &s->channels[j];
796  if (channel->pixel_type == EXR_HALF)
797  pixel_half_size = 1;
798  else
799  pixel_half_size = 2;
800 
801  in = tmp + tmp_offset * td->xsize * td->ysize + i * td->xsize * pixel_half_size;
802  tmp_offset += pixel_half_size;
803 
804 #if HAVE_BIGENDIAN
805  s->bbdsp.bswap16_buf(out, in, td->xsize * pixel_half_size);
806 #else
807  memcpy(out, in, td->xsize * 2 * pixel_half_size);
808 #endif
809  out += td->xsize * pixel_half_size;
810  }
811  }
812 
813  return 0;
814 }
815 
817  int compressed_size, int uncompressed_size,
818  EXRThreadData *td)
819 {
820  unsigned long dest_len, expected_len = 0;
821  const uint8_t *in = td->tmp;
822  uint8_t *out;
823  int c, i, j;
824 
825  for (i = 0; i < s->nb_channels; i++) {
826  if (s->channels[i].pixel_type == EXR_FLOAT) {
827  expected_len += (td->xsize * td->ysize * 3);/* PRX 24 store float in 24 bit instead of 32 */
828  } else if (s->channels[i].pixel_type == EXR_HALF) {
829  expected_len += (td->xsize * td->ysize * 2);
830  } else {//UINT 32
831  expected_len += (td->xsize * td->ysize * 4);
832  }
833  }
834 
835  dest_len = expected_len;
836 
837  if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK) {
838  return AVERROR_INVALIDDATA;
839  } else if (dest_len != expected_len) {
840  return AVERROR_INVALIDDATA;
841  }
842 
843  out = td->uncompressed_data;
844  for (i = 0; i < td->ysize; i++)
845  for (c = 0; c < s->nb_channels; c++) {
846  EXRChannel *channel = &s->channels[c];
847  const uint8_t *ptr[4];
848  uint32_t pixel = 0;
849 
850  switch (channel->pixel_type) {
851  case EXR_FLOAT:
852  ptr[0] = in;
853  ptr[1] = ptr[0] + td->xsize;
854  ptr[2] = ptr[1] + td->xsize;
855  in = ptr[2] + td->xsize;
856 
857  for (j = 0; j < td->xsize; ++j) {
858  uint32_t diff = ((unsigned)*(ptr[0]++) << 24) |
859  (*(ptr[1]++) << 16) |
860  (*(ptr[2]++) << 8);
861  pixel += diff;
862  bytestream_put_le32(&out, pixel);
863  }
864  break;
865  case EXR_HALF:
866  ptr[0] = in;
867  ptr[1] = ptr[0] + td->xsize;
868  in = ptr[1] + td->xsize;
869  for (j = 0; j < td->xsize; j++) {
870  uint32_t diff = (*(ptr[0]++) << 8) | *(ptr[1]++);
871 
872  pixel += diff;
873  bytestream_put_le16(&out, pixel);
874  }
875  break;
876  case EXR_UINT:
877  ptr[0] = in;
878  ptr[1] = ptr[0] + s->xdelta;
879  ptr[2] = ptr[1] + s->xdelta;
880  ptr[3] = ptr[2] + s->xdelta;
881  in = ptr[3] + s->xdelta;
882 
883  for (j = 0; j < s->xdelta; ++j) {
884  uint32_t diff = (*(ptr[0]++) << 24) |
885  (*(ptr[1]++) << 16) |
886  (*(ptr[2]++) << 8 ) |
887  (*(ptr[3]++));
888  pixel += diff;
889  bytestream_put_le32(&out, pixel);
890  }
891  break;
892  default:
893  return AVERROR_INVALIDDATA;
894  }
895  }
896 
897  return 0;
898 }
899 
900 static void unpack_14(const uint8_t b[14], uint16_t s[16])
901 {
902  unsigned short shift = (b[ 2] >> 2) & 15;
903  unsigned short bias = (0x20 << shift);
904  int i;
905 
906  s[ 0] = (b[0] << 8) | b[1];
907 
908  s[ 4] = s[ 0] + ((((b[ 2] << 4) | (b[ 3] >> 4)) & 0x3f) << shift) - bias;
909  s[ 8] = s[ 4] + ((((b[ 3] << 2) | (b[ 4] >> 6)) & 0x3f) << shift) - bias;
910  s[12] = s[ 8] + ((b[ 4] & 0x3f) << shift) - bias;
911 
912  s[ 1] = s[ 0] + ((b[ 5] >> 2) << shift) - bias;
913  s[ 5] = s[ 4] + ((((b[ 5] << 4) | (b[ 6] >> 4)) & 0x3f) << shift) - bias;
914  s[ 9] = s[ 8] + ((((b[ 6] << 2) | (b[ 7] >> 6)) & 0x3f) << shift) - bias;
915  s[13] = s[12] + ((b[ 7] & 0x3f) << shift) - bias;
916 
917  s[ 2] = s[ 1] + ((b[ 8] >> 2) << shift) - bias;
918  s[ 6] = s[ 5] + ((((b[ 8] << 4) | (b[ 9] >> 4)) & 0x3f) << shift) - bias;
919  s[10] = s[ 9] + ((((b[ 9] << 2) | (b[10] >> 6)) & 0x3f) << shift) - bias;
920  s[14] = s[13] + ((b[10] & 0x3f) << shift) - bias;
921 
922  s[ 3] = s[ 2] + ((b[11] >> 2) << shift) - bias;
923  s[ 7] = s[ 6] + ((((b[11] << 4) | (b[12] >> 4)) & 0x3f) << shift) - bias;
924  s[11] = s[10] + ((((b[12] << 2) | (b[13] >> 6)) & 0x3f) << shift) - bias;
925  s[15] = s[14] + ((b[13] & 0x3f) << shift) - bias;
926 
927  for (i = 0; i < 16; ++i) {
928  if (s[i] & 0x8000)
929  s[i] &= 0x7fff;
930  else
931  s[i] = ~s[i];
932  }
933 }
934 
935 static void unpack_3(const uint8_t b[3], uint16_t s[16])
936 {
937  int i;
938 
939  s[0] = (b[0] << 8) | b[1];
940 
941  if (s[0] & 0x8000)
942  s[0] &= 0x7fff;
943  else
944  s[0] = ~s[0];
945 
946  for (i = 1; i < 16; i++)
947  s[i] = s[0];
948 }
949 
950 
951 static int b44_uncompress(EXRContext *s, const uint8_t *src, int compressed_size,
952  int uncompressed_size, EXRThreadData *td) {
953  const int8_t *sr = src;
954  int stay_to_uncompress = compressed_size;
955  int nb_b44_block_w, nb_b44_block_h;
956  int index_tl_x, index_tl_y, index_out, index_tmp;
957  uint16_t tmp_buffer[16]; /* B44 use 4x4 half float pixel */
958  int c, iY, iX, y, x;
959  int target_channel_offset = 0;
960 
961  /* calc B44 block count */
962  nb_b44_block_w = td->xsize / 4;
963  if ((td->xsize % 4) != 0)
964  nb_b44_block_w++;
965 
966  nb_b44_block_h = td->ysize / 4;
967  if ((td->ysize % 4) != 0)
968  nb_b44_block_h++;
969 
970  for (c = 0; c < s->nb_channels; c++) {
971  if (s->channels[c].pixel_type == EXR_HALF) {/* B44 only compress half float data */
972  for (iY = 0; iY < nb_b44_block_h; iY++) {
973  for (iX = 0; iX < nb_b44_block_w; iX++) {/* For each B44 block */
974  if (stay_to_uncompress < 3) {
975  av_log(s, AV_LOG_ERROR, "Not enough data for B44A block: %d", stay_to_uncompress);
976  return AVERROR_INVALIDDATA;
977  }
978 
979  if (src[compressed_size - stay_to_uncompress + 2] == 0xfc) { /* B44A block */
980  unpack_3(sr, tmp_buffer);
981  sr += 3;
982  stay_to_uncompress -= 3;
983  } else {/* B44 Block */
984  if (stay_to_uncompress < 14) {
985  av_log(s, AV_LOG_ERROR, "Not enough data for B44 block: %d", stay_to_uncompress);
986  return AVERROR_INVALIDDATA;
987  }
988  unpack_14(sr, tmp_buffer);
989  sr += 14;
990  stay_to_uncompress -= 14;
991  }
992 
993  /* copy data to uncompress buffer (B44 block can exceed target resolution)*/
994  index_tl_x = iX * 4;
995  index_tl_y = iY * 4;
996 
997  for (y = index_tl_y; y < FFMIN(index_tl_y + 4, td->ysize); y++) {
998  for (x = index_tl_x; x < FFMIN(index_tl_x + 4, td->xsize); x++) {
999  index_out = target_channel_offset * td->xsize + y * td->channel_line_size + 2 * x;
1000  index_tmp = (y-index_tl_y) * 4 + (x-index_tl_x);
1001  td->uncompressed_data[index_out] = tmp_buffer[index_tmp] & 0xff;
1002  td->uncompressed_data[index_out + 1] = tmp_buffer[index_tmp] >> 8;
1003  }
1004  }
1005  }
1006  }
1007  target_channel_offset += 2;
1008  } else {/* Float or UINT 32 channel */
1009  if (stay_to_uncompress < td->ysize * td->xsize * 4) {
1010  av_log(s, AV_LOG_ERROR, "Not enough data for uncompress channel: %d", stay_to_uncompress);
1011  return AVERROR_INVALIDDATA;
1012  }
1013 
1014  for (y = 0; y < td->ysize; y++) {
1015  index_out = target_channel_offset * td->xsize + y * td->channel_line_size;
1016  memcpy(&td->uncompressed_data[index_out], sr, td->xsize * 4);
1017  sr += td->xsize * 4;
1018  }
1019  target_channel_offset += 4;
1020 
1021  stay_to_uncompress -= td->ysize * td->xsize * 4;
1022  }
1023  }
1024 
1025  return 0;
1026 }
1027 
1028 static int decode_block(AVCodecContext *avctx, void *tdata,
1029  int jobnr, int threadnr)
1030 {
1031  EXRContext *s = avctx->priv_data;
1032  AVFrame *const p = s->picture;
1033  EXRThreadData *td = &s->thread_data[threadnr];
1034  const uint8_t *channel_buffer[4] = { 0 };
1035  const uint8_t *buf = s->buf;
1036  uint64_t line_offset, uncompressed_size;
1037  uint16_t *ptr_x;
1038  uint8_t *ptr;
1039  uint32_t data_size;
1040  uint64_t line, col = 0;
1041  uint64_t tile_x, tile_y, tile_level_x, tile_level_y;
1042  const uint8_t *src;
1043  int axmax = (avctx->width - (s->xmax + 1)) * 2 * s->desc->nb_components; /* nb pixel to add at the right of the datawindow */
1044  int bxmin = s->xmin * 2 * s->desc->nb_components; /* nb pixel to add at the left of the datawindow */
1045  int i, x, buf_size = s->buf_size;
1046  int c, rgb_channel_count;
1047  float one_gamma = 1.0f / s->gamma;
1049  int ret;
1050 
1051  line_offset = AV_RL64(s->gb.buffer + jobnr * 8);
1052 
1053  if (s->is_tile) {
1054  if (buf_size < 20 || line_offset > buf_size - 20)
1055  return AVERROR_INVALIDDATA;
1056 
1057  src = buf + line_offset + 20;
1058 
1059  tile_x = AV_RL32(src - 20);
1060  tile_y = AV_RL32(src - 16);
1061  tile_level_x = AV_RL32(src - 12);
1062  tile_level_y = AV_RL32(src - 8);
1063 
1064  data_size = AV_RL32(src - 4);
1065  if (data_size <= 0 || data_size > buf_size - line_offset - 20)
1066  return AVERROR_INVALIDDATA;
1067 
1068  if (tile_level_x || tile_level_y) { /* tile level, is not the full res level */
1069  avpriv_report_missing_feature(s->avctx, "Subres tile before full res tile");
1070  return AVERROR_PATCHWELCOME;
1071  }
1072 
1073  if (s->xmin || s->ymin) {
1074  avpriv_report_missing_feature(s->avctx, "Tiles with xmin/ymin");
1075  return AVERROR_PATCHWELCOME;
1076  }
1077 
1078  line = s->tile_attr.ySize * tile_y;
1079  col = s->tile_attr.xSize * tile_x;
1080 
1081  if (line < s->ymin || line > s->ymax ||
1082  col < s->xmin || col > s->xmax)
1083  return AVERROR_INVALIDDATA;
1084 
1085  td->ysize = FFMIN(s->tile_attr.ySize, s->ydelta - tile_y * s->tile_attr.ySize);
1086  td->xsize = FFMIN(s->tile_attr.xSize, s->xdelta - tile_x * s->tile_attr.xSize);
1087 
1088  if (col) { /* not the first tile of the line */
1089  bxmin = 0; /* doesn't add pixel at the left of the datawindow */
1090  }
1091 
1092  if ((col + td->xsize) != s->xdelta)/* not the last tile of the line */
1093  axmax = 0; /* doesn't add pixel at the right of the datawindow */
1094 
1095  td->channel_line_size = td->xsize * s->current_channel_offset;/* uncompress size of one line */
1096  uncompressed_size = td->channel_line_size * (uint64_t)td->ysize;/* uncompress size of the block */
1097  } else {
1098  if (buf_size < 8 || line_offset > buf_size - 8)
1099  return AVERROR_INVALIDDATA;
1100 
1101  src = buf + line_offset + 8;
1102  line = AV_RL32(src - 8);
1103 
1104  if (line < s->ymin || line > s->ymax)
1105  return AVERROR_INVALIDDATA;
1106 
1107  data_size = AV_RL32(src - 4);
1108  if (data_size <= 0 || data_size > buf_size - line_offset - 8)
1109  return AVERROR_INVALIDDATA;
1110 
1111  td->ysize = FFMIN(s->scan_lines_per_block, s->ymax - line + 1); /* s->ydelta - line ?? */
1112  td->xsize = s->xdelta;
1113 
1114  td->channel_line_size = td->xsize * s->current_channel_offset;/* uncompress size of one line */
1115  uncompressed_size = td->channel_line_size * (uint64_t)td->ysize;/* uncompress size of the block */
1116 
1117  if ((s->compression == EXR_RAW && (data_size != uncompressed_size ||
1118  line_offset > buf_size - uncompressed_size)) ||
1119  (s->compression != EXR_RAW && (data_size > uncompressed_size ||
1120  line_offset > buf_size - data_size))) {
1121  return AVERROR_INVALIDDATA;
1122  }
1123  }
1124 
1125  if (data_size < uncompressed_size || s->is_tile) { /* td->tmp is use for tile reorganization */
1126  av_fast_padded_malloc(&td->tmp, &td->tmp_size, uncompressed_size);
1127  if (!td->tmp)
1128  return AVERROR(ENOMEM);
1129  }
1130 
1131  if (data_size < uncompressed_size) {
1133  &td->uncompressed_size, uncompressed_size + 64);/* Force 64 padding for AVX2 reorder_pixels dst */
1134 
1135  if (!td->uncompressed_data)
1136  return AVERROR(ENOMEM);
1137 
1138  ret = AVERROR_INVALIDDATA;
1139  switch (s->compression) {
1140  case EXR_ZIP1:
1141  case EXR_ZIP16:
1142  ret = zip_uncompress(s, src, data_size, uncompressed_size, td);
1143  break;
1144  case EXR_PIZ:
1145  ret = piz_uncompress(s, src, data_size, uncompressed_size, td);
1146  break;
1147  case EXR_PXR24:
1148  ret = pxr24_uncompress(s, src, data_size, uncompressed_size, td);
1149  break;
1150  case EXR_RLE:
1151  ret = rle_uncompress(s, src, data_size, uncompressed_size, td);
1152  break;
1153  case EXR_B44:
1154  case EXR_B44A:
1155  ret = b44_uncompress(s, src, data_size, uncompressed_size, td);
1156  break;
1157  }
1158  if (ret < 0) {
1159  av_log(avctx, AV_LOG_ERROR, "decode_block() failed.\n");
1160  return ret;
1161  }
1162  src = td->uncompressed_data;
1163  }
1164 
1165  if (!s->is_luma) {
1166  channel_buffer[0] = src + td->xsize * s->channel_offsets[0];
1167  channel_buffer[1] = src + td->xsize * s->channel_offsets[1];
1168  channel_buffer[2] = src + td->xsize * s->channel_offsets[2];
1169  rgb_channel_count = 3;
1170  } else { /* put y data in the first channel_buffer */
1171  channel_buffer[0] = src + td->xsize * s->channel_offsets[1];
1172  rgb_channel_count = 1;
1173  }
1174  if (s->channel_offsets[3] >= 0)
1175  channel_buffer[3] = src + td->xsize * s->channel_offsets[3];
1176 
1177  ptr = p->data[0] + line * p->linesize[0] + (col * s->desc->nb_components * 2);
1178 
1179  for (i = 0;
1180  i < td->ysize; i++, ptr += p->linesize[0]) {
1181 
1182  const uint8_t * a;
1183  const uint8_t *rgb[3];
1184 
1185  for (c = 0; c < rgb_channel_count; c++){
1186  rgb[c] = channel_buffer[c];
1187  }
1188 
1189  if (channel_buffer[3])
1190  a = channel_buffer[3];
1191 
1192  ptr_x = (uint16_t *) ptr;
1193 
1194  // Zero out the start if xmin is not 0
1195  memset(ptr_x, 0, bxmin);
1196  ptr_x += s->xmin * s->desc->nb_components;
1197 
1198  if (s->pixel_type == EXR_FLOAT) {
1199  // 32-bit
1200  if (trc_func) {
1201  for (x = 0; x < td->xsize; x++) {
1202  union av_intfloat32 t;
1203 
1204  for (c = 0; c < rgb_channel_count; c++) {
1205  t.i = bytestream_get_le32(&rgb[c]);
1206  t.f = trc_func(t.f);
1207  *ptr_x++ = exr_flt2uint(t.i);
1208  }
1209  if (channel_buffer[3])
1210  *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a));
1211  }
1212  } else {
1213  for (x = 0; x < td->xsize; x++) {
1214  union av_intfloat32 t;
1215  int c;
1216 
1217  for (c = 0; c < rgb_channel_count; c++) {
1218  t.i = bytestream_get_le32(&rgb[c]);
1219  if (t.f > 0.0f) /* avoid negative values */
1220  t.f = powf(t.f, one_gamma);
1221  *ptr_x++ = exr_flt2uint(t.i);
1222  }
1223 
1224  if (channel_buffer[3])
1225  *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a));
1226  }
1227  }
1228  } else if (s->pixel_type == EXR_HALF) {
1229  // 16-bit
1230  for (x = 0; x < td->xsize; x++) {
1231  int c;
1232  for (c = 0; c < rgb_channel_count; c++) {
1233  *ptr_x++ = s->gamma_table[bytestream_get_le16(&rgb[c])];
1234  }
1235 
1236  if (channel_buffer[3])
1237  *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&a));
1238  }
1239  } else if (s->pixel_type == EXR_UINT) {
1240  for (x = 0; x < td->xsize; x++) {
1241  for (c = 0; c < rgb_channel_count; c++) {
1242  *ptr_x++ = bytestream_get_le32(&rgb[c]) >> 16;
1243  }
1244 
1245  if (channel_buffer[3])
1246  *ptr_x++ = bytestream_get_le32(&a) >> 16;
1247  }
1248  }
1249 
1250  // Zero out the end if xmax+1 is not w
1251  memset(ptr_x, 0, axmax);
1252 
1253  channel_buffer[0] += td->channel_line_size;
1254  channel_buffer[1] += td->channel_line_size;
1255  channel_buffer[2] += td->channel_line_size;
1256  if (channel_buffer[3])
1257  channel_buffer[3] += td->channel_line_size;
1258  }
1259 
1260  return 0;
1261 }
1262 
1263 /**
1264  * Check if the variable name corresponds to its data type.
1265  *
1266  * @param s the EXRContext
1267  * @param value_name name of the variable to check
1268  * @param value_type type of the variable to check
1269  * @param minimum_length minimum length of the variable data
1270  *
1271  * @return bytes to read containing variable data
1272  * -1 if variable is not found
1273  * 0 if buffer ended prematurely
1274  */
1276  const char *value_name,
1277  const char *value_type,
1278  unsigned int minimum_length)
1279 {
1280  int var_size = -1;
1281 
1282  if (bytestream2_get_bytes_left(&s->gb) >= minimum_length &&
1283  !strcmp(s->gb.buffer, value_name)) {
1284  // found value_name, jump to value_type (null terminated strings)
1285  s->gb.buffer += strlen(value_name) + 1;
1286  if (!strcmp(s->gb.buffer, value_type)) {
1287  s->gb.buffer += strlen(value_type) + 1;
1288  var_size = bytestream2_get_le32(&s->gb);
1289  // don't go read past boundaries
1290  if (var_size > bytestream2_get_bytes_left(&s->gb))
1291  var_size = 0;
1292  } else {
1293  // value_type not found, reset the buffer
1294  s->gb.buffer -= strlen(value_name) + 1;
1296  "Unknown data type %s for header variable %s.\n",
1297  value_type, value_name);
1298  }
1299  }
1300 
1301  return var_size;
1302 }
1303 
1305 {
1306  AVDictionary *metadata = NULL;
1307  int magic_number, version, i, flags, sar = 0;
1308  int layer_match = 0;
1309  int ret;
1310 
1311  s->current_channel_offset = 0;
1312  s->xmin = ~0;
1313  s->xmax = ~0;
1314  s->ymin = ~0;
1315  s->ymax = ~0;
1316  s->xdelta = ~0;
1317  s->ydelta = ~0;
1318  s->channel_offsets[0] = -1;
1319  s->channel_offsets[1] = -1;
1320  s->channel_offsets[2] = -1;
1321  s->channel_offsets[3] = -1;
1322  s->pixel_type = EXR_UNKNOWN;
1323  s->compression = EXR_UNKN;
1324  s->nb_channels = 0;
1325  s->w = 0;
1326  s->h = 0;
1327  s->tile_attr.xSize = -1;
1328  s->tile_attr.ySize = -1;
1329  s->is_tile = 0;
1330  s->is_luma = 0;
1331 
1332  if (bytestream2_get_bytes_left(&s->gb) < 10) {
1333  av_log(s->avctx, AV_LOG_ERROR, "Header too short to parse.\n");
1334  return AVERROR_INVALIDDATA;
1335  }
1336 
1337  magic_number = bytestream2_get_le32(&s->gb);
1338  if (magic_number != 20000630) {
1339  /* As per documentation of OpenEXR, it is supposed to be
1340  * int 20000630 little-endian */
1341  av_log(s->avctx, AV_LOG_ERROR, "Wrong magic number %d.\n", magic_number);
1342  return AVERROR_INVALIDDATA;
1343  }
1344 
1345  version = bytestream2_get_byte(&s->gb);
1346  if (version != 2) {
1347  avpriv_report_missing_feature(s->avctx, "Version %d", version);
1348  return AVERROR_PATCHWELCOME;
1349  }
1350 
1351  flags = bytestream2_get_le24(&s->gb);
1352 
1353  if (flags & 0x02)
1354  s->is_tile = 1;
1355  if (flags & 0x08) {
1356  avpriv_report_missing_feature(s->avctx, "deep data");
1357  return AVERROR_PATCHWELCOME;
1358  }
1359  if (flags & 0x10) {
1360  avpriv_report_missing_feature(s->avctx, "multipart");
1361  return AVERROR_PATCHWELCOME;
1362  }
1363 
1364  // Parse the header
1365  while (bytestream2_get_bytes_left(&s->gb) > 0 && *s->gb.buffer) {
1366  int var_size;
1367  if ((var_size = check_header_variable(s, "channels",
1368  "chlist", 38)) >= 0) {
1369  GetByteContext ch_gb;
1370  if (!var_size) {
1371  ret = AVERROR_INVALIDDATA;
1372  goto fail;
1373  }
1374 
1375  bytestream2_init(&ch_gb, s->gb.buffer, var_size);
1376 
1377  while (bytestream2_get_bytes_left(&ch_gb) >= 19) {
1379  enum ExrPixelType current_pixel_type;
1380  int channel_index = -1;
1381  int xsub, ysub;
1382 
1383  if (strcmp(s->layer, "") != 0) {
1384  if (strncmp(ch_gb.buffer, s->layer, strlen(s->layer)) == 0) {
1385  layer_match = 1;
1386  av_log(s->avctx, AV_LOG_INFO,
1387  "Channel match layer : %s.\n", ch_gb.buffer);
1388  ch_gb.buffer += strlen(s->layer);
1389  if (*ch_gb.buffer == '.')
1390  ch_gb.buffer++; /* skip dot if not given */
1391  } else {
1392  av_log(s->avctx, AV_LOG_INFO,
1393  "Channel doesn't match layer : %s.\n", ch_gb.buffer);
1394  }
1395  } else {
1396  layer_match = 1;
1397  }
1398 
1399  if (layer_match) { /* only search channel if the layer match is valid */
1400  if (!strcmp(ch_gb.buffer, "R") ||
1401  !strcmp(ch_gb.buffer, "X") ||
1402  !strcmp(ch_gb.buffer, "U")) {
1403  channel_index = 0;
1404  s->is_luma = 0;
1405  } else if (!strcmp(ch_gb.buffer, "G") ||
1406  !strcmp(ch_gb.buffer, "V")) {
1407  channel_index = 1;
1408  s->is_luma = 0;
1409  } else if (!strcmp(ch_gb.buffer, "Y")) {
1410  channel_index = 1;
1411  s->is_luma = 1;
1412  } else if (!strcmp(ch_gb.buffer, "B") ||
1413  !strcmp(ch_gb.buffer, "Z") ||
1414  !strcmp(ch_gb.buffer, "W")){
1415  channel_index = 2;
1416  s->is_luma = 0;
1417  } else if (!strcmp(ch_gb.buffer, "A")) {
1418  channel_index = 3;
1419  } else {
1421  "Unsupported channel %.256s.\n", ch_gb.buffer);
1422  }
1423  }
1424 
1425  /* skip until you get a 0 */
1426  while (bytestream2_get_bytes_left(&ch_gb) > 0 &&
1427  bytestream2_get_byte(&ch_gb))
1428  continue;
1429 
1430  if (bytestream2_get_bytes_left(&ch_gb) < 4) {
1431  av_log(s->avctx, AV_LOG_ERROR, "Incomplete header.\n");
1432  ret = AVERROR_INVALIDDATA;
1433  goto fail;
1434  }
1435 
1436  current_pixel_type = bytestream2_get_le32(&ch_gb);
1437  if (current_pixel_type >= EXR_UNKNOWN) {
1438  avpriv_report_missing_feature(s->avctx, "Pixel type %d",
1439  current_pixel_type);
1440  ret = AVERROR_PATCHWELCOME;
1441  goto fail;
1442  }
1443 
1444  bytestream2_skip(&ch_gb, 4);
1445  xsub = bytestream2_get_le32(&ch_gb);
1446  ysub = bytestream2_get_le32(&ch_gb);
1447 
1448  if (xsub != 1 || ysub != 1) {
1450  "Subsampling %dx%d",
1451  xsub, ysub);
1452  ret = AVERROR_PATCHWELCOME;
1453  goto fail;
1454  }
1455 
1456  if (channel_index >= 0 && s->channel_offsets[channel_index] == -1) { /* channel has not been previously assigned */
1457  if (s->pixel_type != EXR_UNKNOWN &&
1458  s->pixel_type != current_pixel_type) {
1460  "RGB channels not of the same depth.\n");
1461  ret = AVERROR_INVALIDDATA;
1462  goto fail;
1463  }
1464  s->pixel_type = current_pixel_type;
1465  s->channel_offsets[channel_index] = s->current_channel_offset;
1466  }
1467 
1468  s->channels = av_realloc(s->channels,
1469  ++s->nb_channels * sizeof(EXRChannel));
1470  if (!s->channels) {
1471  ret = AVERROR(ENOMEM);
1472  goto fail;
1473  }
1474  channel = &s->channels[s->nb_channels - 1];
1475  channel->pixel_type = current_pixel_type;
1476  channel->xsub = xsub;
1477  channel->ysub = ysub;
1478 
1479  if (current_pixel_type == EXR_HALF) {
1480  s->current_channel_offset += 2;
1481  } else {/* Float or UINT32 */
1482  s->current_channel_offset += 4;
1483  }
1484  }
1485 
1486  /* Check if all channels are set with an offset or if the channels
1487  * are causing an overflow */
1488  if (!s->is_luma){/* if we expected to have at least 3 channels */
1489  if (FFMIN3(s->channel_offsets[0],
1490  s->channel_offsets[1],
1491  s->channel_offsets[2]) < 0) {
1492  if (s->channel_offsets[0] < 0)
1493  av_log(s->avctx, AV_LOG_ERROR, "Missing red channel.\n");
1494  if (s->channel_offsets[1] < 0)
1495  av_log(s->avctx, AV_LOG_ERROR, "Missing green channel.\n");
1496  if (s->channel_offsets[2] < 0)
1497  av_log(s->avctx, AV_LOG_ERROR, "Missing blue channel.\n");
1498  ret = AVERROR_INVALIDDATA;
1499  goto fail;
1500  }
1501  }
1502 
1503  // skip one last byte and update main gb
1504  s->gb.buffer = ch_gb.buffer + 1;
1505  continue;
1506  } else if ((var_size = check_header_variable(s, "dataWindow", "box2i",
1507  31)) >= 0) {
1508  if (!var_size) {
1509  ret = AVERROR_INVALIDDATA;
1510  goto fail;
1511  }
1512 
1513  s->xmin = bytestream2_get_le32(&s->gb);
1514  s->ymin = bytestream2_get_le32(&s->gb);
1515  s->xmax = bytestream2_get_le32(&s->gb);
1516  s->ymax = bytestream2_get_le32(&s->gb);
1517  s->xdelta = (s->xmax - s->xmin) + 1;
1518  s->ydelta = (s->ymax - s->ymin) + 1;
1519 
1520  continue;
1521  } else if ((var_size = check_header_variable(s, "displayWindow",
1522  "box2i", 34)) >= 0) {
1523  if (!var_size) {
1524  ret = AVERROR_INVALIDDATA;
1525  goto fail;
1526  }
1527 
1528  bytestream2_skip(&s->gb, 8);
1529  s->w = bytestream2_get_le32(&s->gb) + 1;
1530  s->h = bytestream2_get_le32(&s->gb) + 1;
1531 
1532  continue;
1533  } else if ((var_size = check_header_variable(s, "lineOrder",
1534  "lineOrder", 25)) >= 0) {
1535  int line_order;
1536  if (!var_size) {
1537  ret = AVERROR_INVALIDDATA;
1538  goto fail;
1539  }
1540 
1541  line_order = bytestream2_get_byte(&s->gb);
1542  av_log(s->avctx, AV_LOG_DEBUG, "line order: %d.\n", line_order);
1543  if (line_order > 2) {
1544  av_log(s->avctx, AV_LOG_ERROR, "Unknown line order.\n");
1545  ret = AVERROR_INVALIDDATA;
1546  goto fail;
1547  }
1548 
1549  continue;
1550  } else if ((var_size = check_header_variable(s, "pixelAspectRatio",
1551  "float", 31)) >= 0) {
1552  if (!var_size) {
1553  ret = AVERROR_INVALIDDATA;
1554  goto fail;
1555  }
1556 
1557  sar = bytestream2_get_le32(&s->gb);
1558 
1559  continue;
1560  } else if ((var_size = check_header_variable(s, "compression",
1561  "compression", 29)) >= 0) {
1562  if (!var_size) {
1563  ret = AVERROR_INVALIDDATA;
1564  goto fail;
1565  }
1566 
1567  if (s->compression == EXR_UNKN)
1568  s->compression = bytestream2_get_byte(&s->gb);
1569  else
1571  "Found more than one compression attribute.\n");
1572 
1573  continue;
1574  } else if ((var_size = check_header_variable(s, "tiles",
1575  "tiledesc", 22)) >= 0) {
1576  char tileLevel;
1577 
1578  if (!s->is_tile)
1580  "Found tile attribute and scanline flags. Exr will be interpreted as scanline.\n");
1581 
1582  s->tile_attr.xSize = bytestream2_get_le32(&s->gb);
1583  s->tile_attr.ySize = bytestream2_get_le32(&s->gb);
1584 
1585  tileLevel = bytestream2_get_byte(&s->gb);
1586  s->tile_attr.level_mode = tileLevel & 0x0f;
1587  s->tile_attr.level_round = (tileLevel >> 4) & 0x0f;
1588 
1590  avpriv_report_missing_feature(s->avctx, "Tile level mode %d",
1591  s->tile_attr.level_mode);
1592  ret = AVERROR_PATCHWELCOME;
1593  goto fail;
1594  }
1595 
1597  avpriv_report_missing_feature(s->avctx, "Tile level round %d",
1598  s->tile_attr.level_round);
1599  ret = AVERROR_PATCHWELCOME;
1600  goto fail;
1601  }
1602 
1603  continue;
1604  } else if ((var_size = check_header_variable(s, "writer",
1605  "string", 1)) >= 0) {
1606  uint8_t key[256] = { 0 };
1607 
1608  bytestream2_get_buffer(&s->gb, key, FFMIN(sizeof(key) - 1, var_size));
1609  av_dict_set(&metadata, "writer", key, 0);
1610 
1611  continue;
1612  }
1613 
1614  // Check if there are enough bytes for a header
1615  if (bytestream2_get_bytes_left(&s->gb) <= 9) {
1616  av_log(s->avctx, AV_LOG_ERROR, "Incomplete header\n");
1617  ret = AVERROR_INVALIDDATA;
1618  goto fail;
1619  }
1620 
1621  // Process unknown variables
1622  for (i = 0; i < 2; i++) // value_name and value_type
1623  while (bytestream2_get_byte(&s->gb) != 0);
1624 
1625  // Skip variable length
1626  bytestream2_skip(&s->gb, bytestream2_get_le32(&s->gb));
1627  }
1628 
1629  ff_set_sar(s->avctx, av_d2q(av_int2float(sar), 255));
1630 
1631  if (s->compression == EXR_UNKN) {
1632  av_log(s->avctx, AV_LOG_ERROR, "Missing compression attribute.\n");
1633  ret = AVERROR_INVALIDDATA;
1634  goto fail;
1635  }
1636 
1637  if (s->is_tile) {
1638  if (s->tile_attr.xSize < 1 || s->tile_attr.ySize < 1) {
1639  av_log(s->avctx, AV_LOG_ERROR, "Invalid tile attribute.\n");
1640  ret = AVERROR_INVALIDDATA;
1641  goto fail;
1642  }
1643  }
1644 
1645  if (bytestream2_get_bytes_left(&s->gb) <= 0) {
1646  av_log(s->avctx, AV_LOG_ERROR, "Incomplete frame.\n");
1647  ret = AVERROR_INVALIDDATA;
1648  goto fail;
1649  }
1650 
1651  frame->metadata = metadata;
1652 
1653  // aaand we are done
1654  bytestream2_skip(&s->gb, 1);
1655  return 0;
1656 fail:
1657  av_dict_free(&metadata);
1658  return ret;
1659 }
1660 
1661 static int decode_frame(AVCodecContext *avctx, void *data,
1662  int *got_frame, AVPacket *avpkt)
1663 {
1664  EXRContext *s = avctx->priv_data;
1665  ThreadFrame frame = { .f = data };
1666  AVFrame *picture = data;
1667  uint8_t *ptr;
1668 
1669  int y, ret;
1670  int out_line_size;
1671  int nb_blocks; /* nb scanline or nb tile */
1672  uint64_t start_offset_table;
1673  uint64_t start_next_scanline;
1674  PutByteContext offset_table_writer;
1675 
1676  bytestream2_init(&s->gb, avpkt->data, avpkt->size);
1677 
1678  if ((ret = decode_header(s, picture)) < 0)
1679  return ret;
1680 
1681  switch (s->pixel_type) {
1682  case EXR_FLOAT:
1683  case EXR_HALF:
1684  case EXR_UINT:
1685  if (s->channel_offsets[3] >= 0) {
1686  if (!s->is_luma) {
1687  avctx->pix_fmt = AV_PIX_FMT_RGBA64;
1688  } else {
1689  avctx->pix_fmt = AV_PIX_FMT_YA16;
1690  }
1691  } else {
1692  if (!s->is_luma) {
1693  avctx->pix_fmt = AV_PIX_FMT_RGB48;
1694  } else {
1695  avctx->pix_fmt = AV_PIX_FMT_GRAY16;
1696  }
1697  }
1698  break;
1699  default:
1700  av_log(avctx, AV_LOG_ERROR, "Missing channel list.\n");
1701  return AVERROR_INVALIDDATA;
1702  }
1703 
1705  avctx->color_trc = s->apply_trc_type;
1706 
1707  switch (s->compression) {
1708  case EXR_RAW:
1709  case EXR_RLE:
1710  case EXR_ZIP1:
1711  s->scan_lines_per_block = 1;
1712  break;
1713  case EXR_PXR24:
1714  case EXR_ZIP16:
1715  s->scan_lines_per_block = 16;
1716  break;
1717  case EXR_PIZ:
1718  case EXR_B44:
1719  case EXR_B44A:
1720  s->scan_lines_per_block = 32;
1721  break;
1722  default:
1723  avpriv_report_missing_feature(avctx, "Compression %d", s->compression);
1724  return AVERROR_PATCHWELCOME;
1725  }
1726 
1727  /* Verify the xmin, xmax, ymin, ymax and xdelta before setting
1728  * the actual image size. */
1729  if (s->xmin > s->xmax ||
1730  s->ymin > s->ymax ||
1731  s->xdelta != s->xmax - s->xmin + 1 ||
1732  s->xmax >= s->w ||
1733  s->ymax >= s->h) {
1734  av_log(avctx, AV_LOG_ERROR, "Wrong or missing size information.\n");
1735  return AVERROR_INVALIDDATA;
1736  }
1737 
1738  if ((ret = ff_set_dimensions(avctx, s->w, s->h)) < 0)
1739  return ret;
1740 
1741  s->desc = av_pix_fmt_desc_get(avctx->pix_fmt);
1742  if (!s->desc)
1743  return AVERROR_INVALIDDATA;
1744  out_line_size = avctx->width * 2 * s->desc->nb_components;
1745 
1746  if (s->is_tile) {
1747  nb_blocks = ((s->xdelta + s->tile_attr.xSize - 1) / s->tile_attr.xSize) *
1748  ((s->ydelta + s->tile_attr.ySize - 1) / s->tile_attr.ySize);
1749  } else { /* scanline */
1750  nb_blocks = (s->ydelta + s->scan_lines_per_block - 1) /
1752  }
1753 
1754  if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
1755  return ret;
1756 
1757  if (bytestream2_get_bytes_left(&s->gb) < nb_blocks * 8)
1758  return AVERROR_INVALIDDATA;
1759 
1760  // check offset table and recreate it if need
1761  if (!s->is_tile && bytestream2_peek_le64(&s->gb) == 0) {
1762  av_log(s->avctx, AV_LOG_DEBUG, "recreating invalid scanline offset table\n");
1763 
1764  start_offset_table = bytestream2_tell(&s->gb);
1765  start_next_scanline = start_offset_table + nb_blocks * 8;
1766  bytestream2_init_writer(&offset_table_writer, &avpkt->data[start_offset_table], nb_blocks * 8);
1767 
1768  for (y = 0; y < nb_blocks; y++) {
1769  /* write offset of prev scanline in offset table */
1770  bytestream2_put_le64(&offset_table_writer, start_next_scanline);
1771 
1772  /* get len of next scanline */
1773  bytestream2_seek(&s->gb, start_next_scanline + 4, SEEK_SET);/* skip line number */
1774  start_next_scanline += (bytestream2_get_le32(&s->gb) + 8);
1775  }
1776  bytestream2_seek(&s->gb, start_offset_table, SEEK_SET);
1777  }
1778 
1779  // save pointer we are going to use in decode_block
1780  s->buf = avpkt->data;
1781  s->buf_size = avpkt->size;
1782  ptr = picture->data[0];
1783 
1784  // Zero out the start if ymin is not 0
1785  for (y = 0; y < s->ymin; y++) {
1786  memset(ptr, 0, out_line_size);
1787  ptr += picture->linesize[0];
1788  }
1789 
1790  s->picture = picture;
1791 
1792  avctx->execute2(avctx, decode_block, s->thread_data, NULL, nb_blocks);
1793 
1794  // Zero out the end if ymax+1 is not h
1795  ptr = picture->data[0] + ((s->ymax+1) * picture->linesize[0]);
1796  for (y = s->ymax + 1; y < avctx->height; y++) {
1797  memset(ptr, 0, out_line_size);
1798  ptr += picture->linesize[0];
1799  }
1800 
1801  picture->pict_type = AV_PICTURE_TYPE_I;
1802  *got_frame = 1;
1803 
1804  return avpkt->size;
1805 }
1806 
1808 {
1809  EXRContext *s = avctx->priv_data;
1810  uint32_t i;
1811  union av_intfloat32 t;
1812  float one_gamma = 1.0f / s->gamma;
1813  avpriv_trc_function trc_func = NULL;
1814 
1815  s->avctx = avctx;
1816 
1817  ff_exrdsp_init(&s->dsp);
1818 
1819 #if HAVE_BIGENDIAN
1820  ff_bswapdsp_init(&s->bbdsp);
1821 #endif
1822 
1824  if (trc_func) {
1825  for (i = 0; i < 65536; ++i) {
1826  t = exr_half2float(i);
1827  t.f = trc_func(t.f);
1828  s->gamma_table[i] = exr_flt2uint(t.i);
1829  }
1830  } else {
1831  if (one_gamma > 0.9999f && one_gamma < 1.0001f) {
1832  for (i = 0; i < 65536; ++i)
1833  s->gamma_table[i] = exr_halflt2uint(i);
1834  } else {
1835  for (i = 0; i < 65536; ++i) {
1836  t = exr_half2float(i);
1837  /* If negative value we reuse half value */
1838  if (t.f <= 0.0f) {
1839  s->gamma_table[i] = exr_halflt2uint(i);
1840  } else {
1841  t.f = powf(t.f, one_gamma);
1842  s->gamma_table[i] = exr_flt2uint(t.i);
1843  }
1844  }
1845  }
1846  }
1847 
1848  // allocate thread data, used for non EXR_RAW compression types
1850  if (!s->thread_data)
1851  return AVERROR_INVALIDDATA;
1852 
1853  return 0;
1854 }
1855 
1856 #if HAVE_THREADS
1857 static int decode_init_thread_copy(AVCodecContext *avctx)
1858 { EXRContext *s = avctx->priv_data;
1859 
1860  // allocate thread data, used for non EXR_RAW compression types
1862  if (!s->thread_data)
1863  return AVERROR_INVALIDDATA;
1864 
1865  return 0;
1866 }
1867 #endif
1868 
1870 {
1871  EXRContext *s = avctx->priv_data;
1872  int i;
1873  for (i = 0; i < avctx->thread_count; i++) {
1874  EXRThreadData *td = &s->thread_data[i];
1876  av_freep(&td->tmp);
1877  av_freep(&td->bitmap);
1878  av_freep(&td->lut);
1879  }
1880 
1881  av_freep(&s->thread_data);
1882  av_freep(&s->channels);
1883 
1884  return 0;
1885 }
1886 
1887 #define OFFSET(x) offsetof(EXRContext, x)
1888 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1889 static const AVOption options[] = {
1890  { "layer", "Set the decoding layer", OFFSET(layer),
1891  AV_OPT_TYPE_STRING, { .str = "" }, 0, 0, VD },
1892  { "gamma", "Set the float gamma value when decoding", OFFSET(gamma),
1893  AV_OPT_TYPE_FLOAT, { .dbl = 1.0f }, 0.001, FLT_MAX, VD },
1894 
1895  // XXX: Note the abuse of the enum using AVCOL_TRC_UNSPECIFIED to subsume the existing gamma option
1896  { "apply_trc", "color transfer characteristics to apply to EXR linear input", OFFSET(apply_trc_type),
1897  AV_OPT_TYPE_INT, {.i64 = AVCOL_TRC_UNSPECIFIED }, 1, AVCOL_TRC_NB-1, VD, "apply_trc_type"},
1898  { "bt709", "BT.709", 0,
1899  AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT709 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1900  { "gamma", "gamma", 0,
1901  AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_UNSPECIFIED }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1902  { "gamma22", "BT.470 M", 0,
1903  AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_GAMMA22 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1904  { "gamma28", "BT.470 BG", 0,
1905  AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_GAMMA28 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1906  { "smpte170m", "SMPTE 170 M", 0,
1907  AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTE170M }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1908  { "smpte240m", "SMPTE 240 M", 0,
1909  AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTE240M }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1910  { "linear", "Linear", 0,
1911  AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LINEAR }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1912  { "log", "Log", 0,
1913  AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LOG }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1914  { "log_sqrt", "Log square root", 0,
1915  AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LOG_SQRT }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1916  { "iec61966_2_4", "IEC 61966-2-4", 0,
1917  AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_IEC61966_2_4 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1918  { "bt1361", "BT.1361", 0,
1919  AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT1361_ECG }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1920  { "iec61966_2_1", "IEC 61966-2-1", 0,
1921  AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_IEC61966_2_1 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1922  { "bt2020_10bit", "BT.2020 - 10 bit", 0,
1923  AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT2020_10 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1924  { "bt2020_12bit", "BT.2020 - 12 bit", 0,
1925  AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT2020_12 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1926  { "smpte2084", "SMPTE ST 2084", 0,
1927  AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTEST2084 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1928  { "smpte428_1", "SMPTE ST 428-1", 0,
1929  AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTEST428_1 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
1930 
1931  { NULL },
1932 };
1933 
1934 static const AVClass exr_class = {
1935  .class_name = "EXR",
1936  .item_name = av_default_item_name,
1937  .option = options,
1938  .version = LIBAVUTIL_VERSION_INT,
1939 };
1940 
1942  .name = "exr",
1943  .long_name = NULL_IF_CONFIG_SMALL("OpenEXR image"),
1944  .type = AVMEDIA_TYPE_VIDEO,
1945  .id = AV_CODEC_ID_EXR,
1946  .priv_data_size = sizeof(EXRContext),
1947  .init = decode_init,
1948  .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
1949  .close = decode_end,
1950  .decode = decode_frame,
1951  .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS |
1953  .priv_class = &exr_class,
1954 };
ITU-R BT2020 for 12-bit system.
Definition: pixfmt.h:460
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
void * av_realloc(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory.
Definition: mem.c:135
static int shift(int a, int b)
Definition: sonic.c:82
IEC 61966-2-4.
Definition: pixfmt.h:456
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2363
This structure describes decoded (raw) audio or video data.
Definition: frame.h:218
static int decode_header(EXRContext *s, AVFrame *frame)
Definition: exr.c:1304
AVOption.
Definition: opt.h:246
"Linear transfer characteristics"
Definition: pixfmt.h:453
Definition: exr.c:66
static uint16_t exr_flt2uint(int32_t v)
Convert from 32-bit float as uint32_t to uint16_t.
Definition: exr.c:235
static uint16_t reverse_lut(const uint8_t *bitmap, uint16_t *lut)
Definition: exr.c:335
misc image utilities
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:269
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
static int init_thread_copy(AVCodecContext *avctx)
Definition: tta.c:392
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:104
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
Definition: exr.c:61
static av_always_inline float av_int2float(uint32_t i)
Reinterpret a 32-bit integer as a float.
Definition: intfloat.h:40
int channel_offsets[4]
Definition: exr.c:133
#define AV_PIX_FMT_RGBA64
Definition: pixfmt.h:358
int buf_size
Definition: exr.c:150
int * p
Definition: exr.c:368
uint32_t ymax
Definition: exr.c:138
static int rle_uncompress(EXRContext *ctx, const uint8_t *src, int compressed_size, int uncompressed_size, EXRThreadData *td)
Definition: exr.c:285
static int pxr24_uncompress(EXRContext *s, const uint8_t *src, int compressed_size, int uncompressed_size, EXRThreadData *td)
Definition: exr.c:816
const char * layer
Definition: exr.c:158
int size
Definition: avcodec.h:1431
const char * b
Definition: vf_curves.c:113
static av_always_inline void bytestream2_init_writer(PutByteContext *p, uint8_t *buf, int buf_size)
Definition: bytestream.h:143
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1727
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
Same behaviour av_fast_malloc but the buffer has additional AV_INPUT_BUFFER_PADDING_SIZE at the end w...
Definition: utils.c:70
enum ExrPixelType pixel_type
Definition: exr.c:132
const char * key
int version
Definition: avisynth_c.h:766
static int decode_block(AVCodecContext *avctx, void *tdata, int jobnr, int threadnr)
Definition: exr.c:1028
#define HALF_FLOAT_MAX_BIASED_EXP
Definition: exr.c:175
#define src
Definition: vp8dsp.c:254
uint8_t * bitmap
Definition: exr.c:113
AVCodec.
Definition: avcodec.h:3408
uint8_t * tmp
Definition: exr.c:110
int w
Definition: exr.c:136
ExrDSPContext dsp
Definition: exr.c:125
AVColorTransferCharacteristic
Color Transfer Characteristic.
Definition: pixfmt.h:444
int lit
Definition: exr.c:367
void(* predictor)(uint8_t *src, ptrdiff_t size)
Definition: exrdsp.h:27
#define VD
Definition: exr.c:1888
#define AV_RL64
Definition: intreadwrite.h:173
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
const uint8_t * buf
Definition: exr.c:149
Definition: exr.c:365
float gamma
Definition: exr.c:161
uint8_t
#define av_cold
Definition: attributes.h:82
#define av_malloc(s)
static void wav_decode(uint16_t *in, int nx, int ox, int ny, int oy, uint16_t mx)
Definition: exr.c:652
AVOptions.
#define HUF_ENCSIZE
Definition: exr.c:361
#define get_code(po, rlc, c, lc, gb, out, oe, outb)
Definition: exr.c:482
Definition: exr.c:76
Multithreading support functions.
#define OFFSET(x)
Definition: exr.c:1887
also ITU-R BT470M / ITU-R BT1700 625 PAL & SECAM
Definition: pixfmt.h:449
static int huf_uncompress(GetByteContext *gb, uint16_t *dst, int dst_size)
Definition: exr.c:574
uint32_t xdelta
Definition: exr.c:139
static int huf_build_dec_table(const uint64_t *hcode, int im, int iM, HufDec *hdecod)
Definition: exr.c:439
static AVFrame * frame
#define get_char(c, lc, gb)
Definition: exr.c:476
const char data[16]
Definition: mxf.c:90
Definition: exr.c:94
#define height
Definition: exr.c:63
uint8_t * data
Definition: avcodec.h:1430
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:200
const uint8_t * buffer
Definition: bytestream.h:34
static int flags
Definition: log.c:55
#define FFMIN3(a, b, c)
Definition: common.h:97
static const AVOption options[]
Definition: exr.c:1889
int ff_set_sar(AVCodecContext *avctx, AVRational sar)
Check that the provided sample aspect ratio is valid and set it on the codec context.
Definition: utils.c:119
AVFrame * picture
Definition: exr.c:123
bitstream reader API header.
uint32_t ymin
Definition: exr.c:138
AVDictionary * metadata
metadata.
Definition: frame.h:505
GetByteContext gb
Definition: exr.c:148
uint32_t ydelta
Definition: exr.c:139
#define av_log(a,...)
uint8_t * uncompressed_data
Definition: exr.c:107
Definition: exr.c:68
#define A_OFFSET
Definition: exr.c:639
static int huf_decode(const uint64_t *hcode, const HufDec *hdecod, GetByteContext *gb, int nbits, int rlc, int no, uint16_t *out)
Definition: exr.c:505
Definition: exr.c:70
#define FLOAT_MAX_BIASED_EXP
Definition: exr.c:173
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define td
Definition: regdef.h:70
ITU-R BT1361 Extended Colour Gamut.
Definition: pixfmt.h:457
int h
Definition: exr.c:136
#define AVERROR(e)
Definition: error.h:43
static av_cold int decode_init(AVCodecContext *avctx)
Definition: exr.c:1807
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:164
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:263
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
AVCodecContext * avctx
Definition: exr.c:124
uint16_t gamma_table[65536]
Definition: exr.c:162
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:203
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:154
#define AV_PIX_FMT_YA16
Definition: pixfmt.h:353
Definition: graph2dot.c:48
#define AV_PIX_FMT_RGB48
Definition: pixfmt.h:354
enum AVColorTransferCharacteristic apply_trc_type
Definition: exr.c:160
simple assert() macros that are a bit more flexible than ISO C assert().
enum ExrPixelType pixel_type
Definition: exr.c:96
int nb_channels
Definition: exr.c:153
const char * name
Name of the codec implementation.
Definition: avcodec.h:3415
#define LONG_ZEROCODE_RUN
Definition: exr.c:395
#define fail()
Definition: checkasm.h:116
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: avcodec.h:1015
int8_t exp
Definition: eval.c:72
AVCodec ff_exr_decoder
Definition: exr.c:1941
int current_channel_offset
Definition: exr.c:154
#define powf(x, y)
Definition: libm.h:50
#define ONLY_IF_THREADS_ENABLED(x)
Define a function with only the non-default version specified.
Definition: internal.h:225
EXRThreadData * thread_data
Definition: exr.c:156
Definition: exr.c:67
static void unpack_3(const uint8_t b[3], uint16_t s[16])
Definition: exr.c:935
static int zip_uncompress(EXRContext *s, const uint8_t *src, int compressed_size, int uncompressed_size, EXRThreadData *td)
Definition: exr.c:268
int is_luma
Definition: exr.c:146
uint8_t nb_components
The number of components each pixel has, (1-4)
Definition: pixdesc.h:83
Definition: exr.c:69
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:301
#define HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP
Definition: exr.c:166
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:352
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
Definition: exr.c:65
int xsub
Definition: exr.c:95
#define FFMIN(a, b)
Definition: common.h:96
int len
Definition: exr.c:366
int32_t xSize
Definition: exr.c:100
uint32_t xmin
Definition: exr.c:137
#define HUF_DECSIZE
Definition: exr.c:362
int width
picture width / height.
Definition: avcodec.h:1690
enum ExrCompr compression
Definition: exr.c:131
static uint16_t exr_halflt2uint(uint16_t v)
Convert from 16-bit float as uint16_t to uint16_t.
Definition: exr.c:255
EXRTileAttribute tile_attr
Definition: exr.c:143
int tmp_size
Definition: exr.c:111
int32_t
AVFormatContext * ctx
Definition: movenc.c:48
static void unpack_14(const uint8_t b[14], uint16_t s[16])
Definition: exr.c:900
uint16_t * lut
Definition: exr.c:114
uint32_t i
Definition: intfloat.h:28
avpriv_trc_function avpriv_get_trc_function_from_trc(enum AVColorTransferCharacteristic trc)
Determine the function needed to apply the given AVColorTransferCharacteristic to linear input...
Definition: color_utils.c:170
#define AV_RL32
Definition: intreadwrite.h:146
Definition: exr.c:77
int n
Definition: avisynth_c.h:684
EXRChannel * channels
Definition: exr.c:152
if(ret< 0)
Definition: vf_mcdeint.c:279
int uncompressed_size
Definition: exr.c:108
#define HUF_DECBITS
Definition: exr.c:359
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:2769
enum ExrTileLevelMode level_mode
Definition: exr.c:102
#define SHORTEST_LONG_RUN
Definition: exr.c:396
static int check_header_variable(EXRContext *s, const char *value_name, const char *value_type, unsigned int minimum_length)
Check if the variable name corresponds to its data type.
Definition: exr.c:1275
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition: avcodec.h:1019
int ysub
Definition: exr.c:95
static int b44_uncompress(EXRContext *s, const uint8_t *src, int compressed_size, int uncompressed_size, EXRThreadData *td)
Definition: exr.c:951
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:188
#define HUF_DECMASK
Definition: exr.c:363
int ysize
Definition: exr.c:116
int(* execute2)(struct AVCodecContext *c, int(*func)(struct AVCodecContext *c2, void *arg, int jobnr, int threadnr), void *arg2, int *ret, int count)
The codec may call this to execute several independent things.
Definition: avcodec.h:2829
also ITU-R BT1361
Definition: pixfmt.h:446
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
also ITU-R BT601-6 525 or 625 / ITU-R BT1358 525 or 625 / ITU-R BT1700 NTSC
Definition: pixfmt.h:451
Libavcodec external API header.
ExrCompr
Definition: exr.c:60
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:249
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:464
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
main external API structure.
Definition: avcodec.h:1518
void * buf
Definition: avisynth_c.h:690
#define BITMAP_SIZE
Definition: exr.c:333
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:70
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
Describe the class of an AVClass context structure.
Definition: log.h:67
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:2134
Definition: exr.c:64
int is_tile
Definition: exr.c:144
float im
Definition: fft.c:82
Not part of ABI.
Definition: pixfmt.h:466
"Logarithmic transfer characteristic (100 * Sqrt(10) : 1 range)"
Definition: pixfmt.h:455
ExrPixelType
Definition: exr.c:74
Definition: exr.c:62
static av_cold int decode_end(AVCodecContext *avctx)
Definition: exr.c:1869
uint8_t pixel
Definition: tiny_ssim.c:42
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:232
AVRational av_d2q(double d, int max)
Convert a double precision floating point number to a rational.
Definition: rational.c:106
#define SHORT_ZEROCODE_RUN
Definition: exr.c:394
int scan_lines_per_block
Definition: exr.c:141
static union av_intfloat32 exr_half2float(uint16_t hf)
Convert a half float as a uint16_t into a full float.
Definition: exr.c:184
int
uint32_t xmax
Definition: exr.c:137
IEC 61966-2-1 (sRGB or sYCC)
Definition: pixfmt.h:458
common internal api header.
common internal and external API header
int channel_line_size
Definition: exr.c:118
static double c[64]
channel
Use these values when setting the channel map with ebur128_set_channel().
Definition: ebur128.h:39
also ITU-R BT470BG
Definition: pixfmt.h:450
Definition: exr.c:75
#define MOD_MASK
Definition: exr.c:640
av_cold void ff_bswapdsp_init(BswapDSPContext *c)
Definition: bswapdsp.c:49
void * priv_data
Definition: avcodec.h:1545
static av_always_inline int diff(const uint32_t a, const uint32_t b)
#define av_free(p)
ExrTileLevelRound
Definition: exr.c:88
static int piz_uncompress(EXRContext *s, const uint8_t *src, int ssize, int dsize, EXRThreadData *td)
Definition: exr.c:731
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
Definition: bytestream.h:208
static void huf_canonical_code_table(uint64_t *hcode)
Definition: exr.c:371
ITU-R BT2020 for 10-bit system.
Definition: pixfmt.h:459
static void apply_lut(const uint16_t *lut, uint16_t *dst, int dsize)
Definition: exr.c:350
FILE * out
Definition: movenc.c:54
#define av_freep(p)
void INT64 INT64 count
Definition: avisynth_c.h:690
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: exr.c:1661
static void wdec14(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
Definition: exr.c:625
av_cold void ff_exrdsp_init(ExrDSPContext *c)
Definition: exrdsp.c:49
int xsize
Definition: exr.c:116
static void wdec16(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
Definition: exr.c:642
static int huf_unpack_enc_table(GetByteContext *gb, int32_t im, int32_t iM, uint64_t *hcode)
Definition: exr.c:399
const AVPixFmtDescriptor * desc
Definition: exr.c:134
void(* reorder_pixels)(uint8_t *dst, const uint8_t *src, ptrdiff_t size)
Definition: exrdsp.h:26
This structure stores compressed data.
Definition: avcodec.h:1407
ExrTileLevelMode
Definition: exr.c:81
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:959
for(j=16;j >0;--j)
Definition: exr.c:71
static const AVClass exr_class
Definition: exr.c:1934
#define USHORT_RANGE
Definition: exr.c:332
enum ExrTileLevelRound level_round
Definition: exr.c:103
void * av_mallocz_array(size_t nmemb, size_t size)
Allocate a memory block for an array with av_mallocz().
Definition: mem.c:191
"Logarithmic transfer characteristic (100:1 range)"
Definition: pixfmt.h:454
double(* avpriv_trc_function)(double)
Definition: color_utils.h:40
int32_t ySize
Definition: exr.c:101
static uint8_t tmp[11]
Definition: aes_ctr.c:26