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