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