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