FFmpeg  4.4.9
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 
34 #include <float.h>
35 #include <zlib.h>
36 
37 #include "libavutil/avassert.h"
38 #include "libavutil/common.h"
39 #include "libavutil/imgutils.h"
40 #include "libavutil/intfloat.h"
41 #include "libavutil/avstring.h"
42 #include "libavutil/opt.h"
43 #include "libavutil/color_utils.h"
44 
45 #include "avcodec.h"
46 #include "bytestream.h"
47 
48 #if HAVE_BIGENDIAN
49 #include "bswapdsp.h"
50 #endif
51 
52 #include "exrdsp.h"
53 #include "get_bits.h"
54 #include "internal.h"
55 #include "half2float.h"
56 #include "mathops.h"
57 #include "thread.h"
58 
59 enum ExrCompr {
71 };
72 
78 };
79 
85 };
86 
91 };
92 
93 typedef struct HuffEntry {
95  uint16_t sym;
96  uint32_t code;
97 } HuffEntry;
98 
99 typedef struct EXRChannel {
100  int xsub, ysub;
102 } EXRChannel;
103 
104 typedef struct EXRTileAttribute {
110 
111 typedef struct EXRThreadData {
114 
116  int tmp_size;
117 
119  uint16_t *lut;
120 
122  unsigned ac_size;
123 
125  unsigned dc_size;
126 
128  unsigned rle_size;
129 
131  unsigned rle_raw_size;
132 
133  float block[3][64];
134 
135  int ysize, xsize;
136 
138 
139  int run_sym;
141  uint64_t *freq;
143 } EXRThreadData;
144 
145 typedef struct EXRContext {
146  AVClass *class;
150 
151 #if HAVE_BIGENDIAN
152  BswapDSPContext bbdsp;
153 #endif
154 
155  enum ExrCompr compression;
157  int channel_offsets[4]; // 0 = red, 1 = green, 2 = blue and 3 = alpha
159 
160  int w, h;
161  uint32_t sar;
164  uint32_t xdelta, ydelta;
165 
167 
168  EXRTileAttribute tile_attr; /* header data attribute of tile */
169  int is_tile; /* 0 if scanline, 1 if tile */
172 
173  int is_luma;/* 1 if there is an Y plane */
174 
175 #define M(chr) (1<<chr - 'A')
176  int has_channel; ///< combination of flags representing the channel codes A-Z
177 
179  const uint8_t *buf;
180  int buf_size;
181 
185  uint32_t chunk_count;
186 
188 
189  const char *layer;
191 
193  float gamma;
194  union av_intfloat32 gamma_table[65536];
195 
196  uint32_t mantissatable[2048];
197  uint32_t exponenttable[64];
198  uint16_t offsettable[64];
199 } EXRContext;
200 
201 static int zip_uncompress(EXRContext *s, const uint8_t *src, int compressed_size,
202  int uncompressed_size, EXRThreadData *td)
203 {
204  unsigned long dest_len = uncompressed_size;
205 
206  if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK ||
207  dest_len != uncompressed_size)
208  return AVERROR_INVALIDDATA;
209 
210  av_assert1(uncompressed_size % 2 == 0);
211 
212  s->dsp.predictor(td->tmp, uncompressed_size);
213  s->dsp.reorder_pixels(td->uncompressed_data, td->tmp, uncompressed_size);
214 
215  return 0;
216 }
217 
218 static int rle(uint8_t *dst, const uint8_t *src,
219  int compressed_size, int uncompressed_size)
220 {
221  uint8_t *d = dst;
222  const int8_t *s = src;
223  int ssize = compressed_size;
224  int dsize = uncompressed_size;
225  uint8_t *dend = d + dsize;
226  int count;
227 
228  while (ssize > 0) {
229  count = *s++;
230 
231  if (count < 0) {
232  count = -count;
233 
234  if ((dsize -= count) < 0 ||
235  (ssize -= count + 1) < 0)
236  return AVERROR_INVALIDDATA;
237 
238  while (count--)
239  *d++ = *s++;
240  } else {
241  count++;
242 
243  if ((dsize -= count) < 0 ||
244  (ssize -= 2) < 0)
245  return AVERROR_INVALIDDATA;
246 
247  while (count--)
248  *d++ = *s;
249 
250  s++;
251  }
252  }
253 
254  if (dend != d)
255  return AVERROR_INVALIDDATA;
256 
257  return 0;
258 }
259 
260 static int rle_uncompress(EXRContext *ctx, const uint8_t *src, int compressed_size,
261  int uncompressed_size, EXRThreadData *td)
262 {
263  int ret = rle(td->tmp, src, compressed_size, uncompressed_size);
264  if (ret < 0)
265  return ret;
266 
267  av_assert1(uncompressed_size % 2 == 0);
268 
269  ctx->dsp.predictor(td->tmp, uncompressed_size);
270  ctx->dsp.reorder_pixels(td->uncompressed_data, td->tmp, uncompressed_size);
271 
272  return 0;
273 }
274 
275 #define USHORT_RANGE (1 << 16)
276 #define BITMAP_SIZE (1 << 13)
277 
278 static uint16_t reverse_lut(const uint8_t *bitmap, uint16_t *lut)
279 {
280  int i, k = 0;
281 
282  for (i = 0; i < USHORT_RANGE; i++)
283  if ((i == 0) || (bitmap[i >> 3] & (1 << (i & 7))))
284  lut[k++] = i;
285 
286  i = k - 1;
287 
288  memset(lut + k, 0, (USHORT_RANGE - k) * 2);
289 
290  return i;
291 }
292 
293 static void apply_lut(const uint16_t *lut, uint16_t *dst, int dsize)
294 {
295  int i;
296 
297  for (i = 0; i < dsize; ++i)
298  dst[i] = lut[dst[i]];
299 }
300 
301 #define HUF_ENCBITS 16 // literal (value) bit length
302 #define HUF_ENCSIZE ((1 << HUF_ENCBITS) + 1) // encoding table size
303 
304 static void huf_canonical_code_table(uint64_t *freq)
305 {
306  uint64_t c, n[59] = { 0 };
307  int i;
308 
309  for (i = 0; i < HUF_ENCSIZE; i++)
310  n[freq[i]] += 1;
311 
312  c = 0;
313  for (i = 58; i > 0; --i) {
314  uint64_t nc = ((c + n[i]) >> 1);
315  n[i] = c;
316  c = nc;
317  }
318 
319  for (i = 0; i < HUF_ENCSIZE; ++i) {
320  int l = freq[i];
321 
322  if (l > 0)
323  freq[i] = l | (n[l]++ << 6);
324  }
325 }
326 
327 #define SHORT_ZEROCODE_RUN 59
328 #define LONG_ZEROCODE_RUN 63
329 #define SHORTEST_LONG_RUN (2 + LONG_ZEROCODE_RUN - SHORT_ZEROCODE_RUN)
330 #define LONGEST_LONG_RUN (255 + SHORTEST_LONG_RUN)
331 
333  int32_t im, int32_t iM, uint64_t *freq)
334 {
335  GetBitContext gbit;
336  int ret = init_get_bits8(&gbit, gb->buffer, bytestream2_get_bytes_left(gb));
337  if (ret < 0)
338  return ret;
339 
340  for (; im <= iM; im++) {
341  int l;
342  if (get_bits_left(&gbit) < 6)
343  return AVERROR_INVALIDDATA;
344  l = freq[im] = get_bits(&gbit, 6);
345 
346  if (l == LONG_ZEROCODE_RUN) {
347  int zerun = get_bits(&gbit, 8) + SHORTEST_LONG_RUN;
348 
349  if (im + zerun > iM + 1)
350  return AVERROR_INVALIDDATA;
351 
352  while (zerun--)
353  freq[im++] = 0;
354 
355  im--;
356  } else if (l >= SHORT_ZEROCODE_RUN) {
357  int zerun = l - SHORT_ZEROCODE_RUN + 2;
358 
359  if (im + zerun > iM + 1)
360  return AVERROR_INVALIDDATA;
361 
362  while (zerun--)
363  freq[im++] = 0;
364 
365  im--;
366  }
367  }
368 
369  bytestream2_skip(gb, (get_bits_count(&gbit) + 7) / 8);
371 
372  return 0;
373 }
374 
376  EXRThreadData *td, int im, int iM)
377 {
378  int j = 0;
379 
380  td->run_sym = -1;
381  for (int i = im; i < iM; i++) {
382  td->he[j].sym = i;
383  td->he[j].len = td->freq[i] & 63;
384  td->he[j].code = td->freq[i] >> 6;
385  if (td->he[j].len > 32) {
386  avpriv_request_sample(s->avctx, "Too big code length");
387  return AVERROR_PATCHWELCOME;
388  }
389  if (td->he[j].len > 0)
390  j++;
391  else
392  td->run_sym = i;
393  }
394 
395  if (im > 0)
396  td->run_sym = 0;
397  else if (iM < 65535)
398  td->run_sym = 65535;
399 
400  if (td->run_sym == -1) {
401  avpriv_request_sample(s->avctx, "No place for run symbol");
402  return AVERROR_PATCHWELCOME;
403  }
404 
405  td->he[j].sym = td->run_sym;
406  td->he[j].len = td->freq[iM] & 63;
407  if (td->he[j].len > 32) {
408  avpriv_request_sample(s->avctx, "Too big code length");
409  return AVERROR_PATCHWELCOME;
410  }
411  td->he[j].code = td->freq[iM] >> 6;
412  j++;
413 
414  ff_free_vlc(&td->vlc);
415  return ff_init_vlc_sparse(&td->vlc, 12, j,
416  &td->he[0].len, sizeof(td->he[0]), sizeof(td->he[0].len),
417  &td->he[0].code, sizeof(td->he[0]), sizeof(td->he[0].code),
418  &td->he[0].sym, sizeof(td->he[0]), sizeof(td->he[0].sym), 0);
419 }
420 
421 static int huf_decode(VLC *vlc, GetByteContext *gb, int nbits, int run_sym,
422  int no, uint16_t *out)
423 {
424  GetBitContext gbit;
425  int oe = 0;
426 
427  init_get_bits(&gbit, gb->buffer, nbits);
428  while (get_bits_left(&gbit) > 0 && oe < no) {
429  uint16_t x = get_vlc2(&gbit, vlc->table, 12, 3);
430 
431  if (x == run_sym) {
432  int run = get_bits(&gbit, 8);
433  uint16_t fill;
434 
435  if (oe == 0 || oe + run > no)
436  return AVERROR_INVALIDDATA;
437 
438  fill = out[oe - 1];
439 
440  while (run-- > 0)
441  out[oe++] = fill;
442  } else {
443  out[oe++] = x;
444  }
445  }
446 
447  return 0;
448 }
449 
451  EXRThreadData *td,
452  GetByteContext *gb,
453  uint16_t *dst, int dst_size)
454 {
455  int32_t im, iM;
456  uint32_t nBits;
457  int ret;
458 
459  im = bytestream2_get_le32(gb);
460  iM = bytestream2_get_le32(gb);
461  bytestream2_skip(gb, 4);
462  nBits = bytestream2_get_le32(gb);
463  if (im < 0 || im >= HUF_ENCSIZE ||
464  iM < 0 || iM >= HUF_ENCSIZE)
465  return AVERROR_INVALIDDATA;
466 
467  bytestream2_skip(gb, 4);
468 
469  if (!td->freq)
470  td->freq = av_malloc_array(HUF_ENCSIZE, sizeof(*td->freq));
471  if (!td->he)
472  td->he = av_calloc(HUF_ENCSIZE, sizeof(*td->he));
473  if (!td->freq || !td->he) {
474  ret = AVERROR(ENOMEM);
475  return ret;
476  }
477 
478  memset(td->freq, 0, sizeof(*td->freq) * HUF_ENCSIZE);
479  if ((ret = huf_unpack_enc_table(gb, im, iM, td->freq)) < 0)
480  return ret;
481 
482  if (nBits > 8 * bytestream2_get_bytes_left(gb)) {
483  ret = AVERROR_INVALIDDATA;
484  return ret;
485  }
486 
487  if ((ret = huf_build_dec_table(s, td, im, iM)) < 0)
488  return ret;
489  return huf_decode(&td->vlc, gb, nBits, td->run_sym, dst_size, dst);
490 }
491 
492 static inline void wdec14(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
493 {
494  int16_t ls = l;
495  int16_t hs = h;
496  int hi = hs;
497  int ai = ls + (hi & 1) + (hi >> 1);
498  int16_t as = ai;
499  int16_t bs = ai - hi;
500 
501  *a = as;
502  *b = bs;
503 }
504 
505 #define NBITS 16
506 #define A_OFFSET (1 << (NBITS - 1))
507 #define MOD_MASK ((1 << NBITS) - 1)
508 
509 static inline void wdec16(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
510 {
511  int m = l;
512  int d = h;
513  int bb = (m - (d >> 1)) & MOD_MASK;
514  int aa = (d + bb - A_OFFSET) & MOD_MASK;
515  *b = bb;
516  *a = aa;
517 }
518 
519 static void wav_decode(uint16_t *in, int nx, int ox,
520  int ny, int oy, uint16_t mx)
521 {
522  int w14 = (mx < (1 << 14));
523  int n = (nx > ny) ? ny : nx;
524  int p = 1;
525  int p2;
526 
527  while (p <= n)
528  p <<= 1;
529 
530  p >>= 1;
531  p2 = p;
532  p >>= 1;
533 
534  while (p >= 1) {
535  uint16_t *py = in;
536  uint16_t *ey = in + oy * (ny - p2);
537  uint16_t i00, i01, i10, i11;
538  int oy1 = oy * p;
539  int oy2 = oy * p2;
540  int ox1 = ox * p;
541  int ox2 = ox * p2;
542 
543  for (; py <= ey; py += oy2) {
544  uint16_t *px = py;
545  uint16_t *ex = py + ox * (nx - p2);
546 
547  for (; px <= ex; px += ox2) {
548  uint16_t *p01 = px + ox1;
549  uint16_t *p10 = px + oy1;
550  uint16_t *p11 = p10 + ox1;
551 
552  if (w14) {
553  wdec14(*px, *p10, &i00, &i10);
554  wdec14(*p01, *p11, &i01, &i11);
555  wdec14(i00, i01, px, p01);
556  wdec14(i10, i11, p10, p11);
557  } else {
558  wdec16(*px, *p10, &i00, &i10);
559  wdec16(*p01, *p11, &i01, &i11);
560  wdec16(i00, i01, px, p01);
561  wdec16(i10, i11, p10, p11);
562  }
563  }
564 
565  if (nx & p) {
566  uint16_t *p10 = px + oy1;
567 
568  if (w14)
569  wdec14(*px, *p10, &i00, p10);
570  else
571  wdec16(*px, *p10, &i00, p10);
572 
573  *px = i00;
574  }
575  }
576 
577  if (ny & p) {
578  uint16_t *px = py;
579  uint16_t *ex = py + ox * (nx - p2);
580 
581  for (; px <= ex; px += ox2) {
582  uint16_t *p01 = px + ox1;
583 
584  if (w14)
585  wdec14(*px, *p01, &i00, p01);
586  else
587  wdec16(*px, *p01, &i00, p01);
588 
589  *px = i00;
590  }
591  }
592 
593  p2 = p;
594  p >>= 1;
595  }
596 }
597 
598 static int piz_uncompress(EXRContext *s, const uint8_t *src, int ssize,
599  int dsize, EXRThreadData *td)
600 {
601  GetByteContext gb;
602  uint16_t maxval, min_non_zero, max_non_zero;
603  uint16_t *ptr;
604  uint16_t *tmp = (uint16_t *)td->tmp;
605  uint16_t *out;
606  uint16_t *in;
607  int ret, i, j;
608  int pixel_half_size;/* 1 for half, 2 for float and uint32 */
610  int tmp_offset;
611 
612  if (!td->bitmap)
613  td->bitmap = av_malloc(BITMAP_SIZE);
614  if (!td->lut)
615  td->lut = av_malloc(1 << 17);
616  if (!td->bitmap || !td->lut) {
617  av_freep(&td->bitmap);
618  av_freep(&td->lut);
619  return AVERROR(ENOMEM);
620  }
621 
622  bytestream2_init(&gb, src, ssize);
623  min_non_zero = bytestream2_get_le16(&gb);
624  max_non_zero = bytestream2_get_le16(&gb);
625 
626  if (max_non_zero >= BITMAP_SIZE)
627  return AVERROR_INVALIDDATA;
628 
629  memset(td->bitmap, 0, FFMIN(min_non_zero, BITMAP_SIZE));
630  if (min_non_zero <= max_non_zero)
631  bytestream2_get_buffer(&gb, td->bitmap + min_non_zero,
632  max_non_zero - min_non_zero + 1);
633  memset(td->bitmap + max_non_zero + 1, 0, BITMAP_SIZE - max_non_zero - 1);
634 
635  if (bytestream2_get_bytes_left(&gb) < 4)
636  return AVERROR_INVALIDDATA;
637 
638  maxval = reverse_lut(td->bitmap, td->lut);
639 
640  bytestream2_skip(&gb, 4);
641  ret = huf_uncompress(s, td, &gb, tmp, dsize / sizeof(uint16_t));
642  if (ret)
643  return ret;
644 
645  ptr = tmp;
646  for (i = 0; i < s->nb_channels; i++) {
647  channel = &s->channels[i];
648 
649  if (channel->pixel_type == EXR_HALF)
650  pixel_half_size = 1;
651  else
652  pixel_half_size = 2;
653 
654  for (j = 0; j < pixel_half_size; j++)
655  wav_decode(ptr + j, td->xsize, pixel_half_size, td->ysize,
656  td->xsize * pixel_half_size, maxval);
657  ptr += td->xsize * td->ysize * pixel_half_size;
658  }
659 
660  apply_lut(td->lut, tmp, dsize / sizeof(uint16_t));
661 
662  out = (uint16_t *)td->uncompressed_data;
663  for (i = 0; i < td->ysize; i++) {
664  tmp_offset = 0;
665  for (j = 0; j < s->nb_channels; j++) {
666  channel = &s->channels[j];
667  if (channel->pixel_type == EXR_HALF)
668  pixel_half_size = 1;
669  else
670  pixel_half_size = 2;
671 
672  in = tmp + tmp_offset * td->xsize * td->ysize + i * td->xsize * pixel_half_size;
673  tmp_offset += pixel_half_size;
674 
675 #if HAVE_BIGENDIAN
676  s->bbdsp.bswap16_buf(out, in, td->xsize * pixel_half_size);
677 #else
678  memcpy(out, in, td->xsize * 2 * pixel_half_size);
679 #endif
680  out += td->xsize * pixel_half_size;
681  }
682  }
683 
684  return 0;
685 }
686 
688  int compressed_size, int uncompressed_size,
689  EXRThreadData *td)
690 {
691  unsigned long dest_len, expected_len = 0;
692  const uint8_t *in = td->tmp;
693  uint8_t *out;
694  int c, i, j;
695 
696  for (i = 0; i < s->nb_channels; i++) {
697  if (s->channels[i].pixel_type == EXR_FLOAT) {
698  expected_len += (td->xsize * td->ysize * 3);/* PRX 24 store float in 24 bit instead of 32 */
699  } else if (s->channels[i].pixel_type == EXR_HALF) {
700  expected_len += (td->xsize * td->ysize * 2);
701  } else {//UINT 32
702  expected_len += (td->xsize * td->ysize * 4);
703  }
704  }
705 
706  dest_len = expected_len;
707 
708  if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK) {
709  return AVERROR_INVALIDDATA;
710  } else if (dest_len != expected_len) {
711  return AVERROR_INVALIDDATA;
712  }
713 
714  out = td->uncompressed_data;
715  for (i = 0; i < td->ysize; i++)
716  for (c = 0; c < s->nb_channels; c++) {
717  EXRChannel *channel = &s->channels[c];
718  const uint8_t *ptr[4];
719  uint32_t pixel = 0;
720 
721  switch (channel->pixel_type) {
722  case EXR_FLOAT:
723  ptr[0] = in;
724  ptr[1] = ptr[0] + td->xsize;
725  ptr[2] = ptr[1] + td->xsize;
726  in = ptr[2] + td->xsize;
727 
728  for (j = 0; j < td->xsize; ++j) {
729  uint32_t diff = ((unsigned)*(ptr[0]++) << 24) |
730  (*(ptr[1]++) << 16) |
731  (*(ptr[2]++) << 8);
732  pixel += diff;
733  bytestream_put_le32(&out, pixel);
734  }
735  break;
736  case EXR_HALF:
737  ptr[0] = in;
738  ptr[1] = ptr[0] + td->xsize;
739  in = ptr[1] + td->xsize;
740  for (j = 0; j < td->xsize; j++) {
741  uint32_t diff = (*(ptr[0]++) << 8) | *(ptr[1]++);
742 
743  pixel += diff;
744  bytestream_put_le16(&out, pixel);
745  }
746  break;
747  case EXR_UINT:
748  ptr[0] = in;
749  ptr[1] = ptr[0] + td->xsize;
750  ptr[2] = ptr[1] + td->xsize;
751  ptr[3] = ptr[2] + td->xsize;
752  in = ptr[3] + td->xsize;
753 
754  for (j = 0; j < td->xsize; ++j) {
755  uint32_t diff = ((uint32_t)*(ptr[0]++) << 24) |
756  (*(ptr[1]++) << 16) |
757  (*(ptr[2]++) << 8 ) |
758  (*(ptr[3]++));
759  pixel += diff;
760  bytestream_put_le32(&out, pixel);
761  }
762  break;
763  default:
764  return AVERROR_INVALIDDATA;
765  }
766  }
767 
768  return 0;
769 }
770 
771 static void unpack_14(const uint8_t b[14], uint16_t s[16])
772 {
773  unsigned short shift = (b[ 2] >> 2) & 15;
774  unsigned short bias = (0x20 << shift);
775  int i;
776 
777  s[ 0] = (b[0] << 8) | b[1];
778 
779  s[ 4] = s[ 0] + ((((b[ 2] << 4) | (b[ 3] >> 4)) & 0x3f) << shift) - bias;
780  s[ 8] = s[ 4] + ((((b[ 3] << 2) | (b[ 4] >> 6)) & 0x3f) << shift) - bias;
781  s[12] = s[ 8] + ((b[ 4] & 0x3f) << shift) - bias;
782 
783  s[ 1] = s[ 0] + ((b[ 5] >> 2) << shift) - bias;
784  s[ 5] = s[ 4] + ((((b[ 5] << 4) | (b[ 6] >> 4)) & 0x3f) << shift) - bias;
785  s[ 9] = s[ 8] + ((((b[ 6] << 2) | (b[ 7] >> 6)) & 0x3f) << shift) - bias;
786  s[13] = s[12] + ((b[ 7] & 0x3f) << shift) - bias;
787 
788  s[ 2] = s[ 1] + ((b[ 8] >> 2) << shift) - bias;
789  s[ 6] = s[ 5] + ((((b[ 8] << 4) | (b[ 9] >> 4)) & 0x3f) << shift) - bias;
790  s[10] = s[ 9] + ((((b[ 9] << 2) | (b[10] >> 6)) & 0x3f) << shift) - bias;
791  s[14] = s[13] + ((b[10] & 0x3f) << shift) - bias;
792 
793  s[ 3] = s[ 2] + ((b[11] >> 2) << shift) - bias;
794  s[ 7] = s[ 6] + ((((b[11] << 4) | (b[12] >> 4)) & 0x3f) << shift) - bias;
795  s[11] = s[10] + ((((b[12] << 2) | (b[13] >> 6)) & 0x3f) << shift) - bias;
796  s[15] = s[14] + ((b[13] & 0x3f) << shift) - bias;
797 
798  for (i = 0; i < 16; ++i) {
799  if (s[i] & 0x8000)
800  s[i] &= 0x7fff;
801  else
802  s[i] = ~s[i];
803  }
804 }
805 
806 static void unpack_3(const uint8_t b[3], uint16_t s[16])
807 {
808  int i;
809 
810  s[0] = (b[0] << 8) | b[1];
811 
812  if (s[0] & 0x8000)
813  s[0] &= 0x7fff;
814  else
815  s[0] = ~s[0];
816 
817  for (i = 1; i < 16; i++)
818  s[i] = s[0];
819 }
820 
821 
822 static int b44_uncompress(EXRContext *s, const uint8_t *src, int compressed_size,
823  int uncompressed_size, EXRThreadData *td) {
824  const int8_t *sr = src;
825  int stay_to_uncompress = compressed_size;
826  int nb_b44_block_w, nb_b44_block_h;
827  int index_tl_x, index_tl_y, index_out, index_tmp;
828  uint16_t tmp_buffer[16]; /* B44 use 4x4 half float pixel */
829  int c, iY, iX, y, x;
830  int target_channel_offset = 0;
831 
832  /* calc B44 block count */
833  nb_b44_block_w = td->xsize / 4;
834  if ((td->xsize % 4) != 0)
835  nb_b44_block_w++;
836 
837  nb_b44_block_h = td->ysize / 4;
838  if ((td->ysize % 4) != 0)
839  nb_b44_block_h++;
840 
841  for (c = 0; c < s->nb_channels; c++) {
842  if (s->channels[c].pixel_type == EXR_HALF) {/* B44 only compress half float data */
843  for (iY = 0; iY < nb_b44_block_h; iY++) {
844  for (iX = 0; iX < nb_b44_block_w; iX++) {/* For each B44 block */
845  if (stay_to_uncompress < 3) {
846  av_log(s, AV_LOG_ERROR, "Not enough data for B44A block: %d", stay_to_uncompress);
847  return AVERROR_INVALIDDATA;
848  }
849 
850  if (src[compressed_size - stay_to_uncompress + 2] == 0xfc) { /* B44A block */
851  unpack_3(sr, tmp_buffer);
852  sr += 3;
853  stay_to_uncompress -= 3;
854  } else {/* B44 Block */
855  if (stay_to_uncompress < 14) {
856  av_log(s, AV_LOG_ERROR, "Not enough data for B44 block: %d", stay_to_uncompress);
857  return AVERROR_INVALIDDATA;
858  }
859  unpack_14(sr, tmp_buffer);
860  sr += 14;
861  stay_to_uncompress -= 14;
862  }
863 
864  /* copy data to uncompress buffer (B44 block can exceed target resolution)*/
865  index_tl_x = iX * 4;
866  index_tl_y = iY * 4;
867 
868  for (y = index_tl_y; y < FFMIN(index_tl_y + 4, td->ysize); y++) {
869  for (x = index_tl_x; x < FFMIN(index_tl_x + 4, td->xsize); x++) {
870  index_out = target_channel_offset * td->xsize + y * td->channel_line_size + 2 * x;
871  index_tmp = (y-index_tl_y) * 4 + (x-index_tl_x);
872  td->uncompressed_data[index_out] = tmp_buffer[index_tmp] & 0xff;
873  td->uncompressed_data[index_out + 1] = tmp_buffer[index_tmp] >> 8;
874  }
875  }
876  }
877  }
878  target_channel_offset += 2;
879  } else {/* Float or UINT 32 channel */
880  if (stay_to_uncompress < td->ysize * td->xsize * 4) {
881  av_log(s, AV_LOG_ERROR, "Not enough data for uncompress channel: %d", stay_to_uncompress);
882  return AVERROR_INVALIDDATA;
883  }
884 
885  for (y = 0; y < td->ysize; y++) {
886  index_out = target_channel_offset * td->xsize + y * td->channel_line_size;
887  memcpy(&td->uncompressed_data[index_out], sr, td->xsize * 4);
888  sr += td->xsize * 4;
889  }
890  target_channel_offset += 4;
891 
892  stay_to_uncompress -= td->ysize * td->xsize * 4;
893  }
894  }
895 
896  return 0;
897 }
898 
899 static int ac_uncompress(EXRContext *s, GetByteContext *gb, float *block)
900 {
901  int ret = 0, n = 1;
902 
903  while (n < 64) {
904  uint16_t val = bytestream2_get_ne16(gb);
905 
906  if (val == 0xff00) {
907  n = 64;
908  } else if ((val >> 8) == 0xff) {
909  n += val & 0xff;
910  } else {
911  ret = n;
913  s->mantissatable,
914  s->exponenttable,
915  s->offsettable));
916  n++;
917  }
918  }
919 
920  return ret;
921 }
922 
923 static void idct_1d(float *blk, int step)
924 {
925  const float a = .5f * cosf( M_PI / 4.f);
926  const float b = .5f * cosf( M_PI / 16.f);
927  const float c = .5f * cosf( M_PI / 8.f);
928  const float d = .5f * cosf(3.f*M_PI / 16.f);
929  const float e = .5f * cosf(5.f*M_PI / 16.f);
930  const float f = .5f * cosf(3.f*M_PI / 8.f);
931  const float g = .5f * cosf(7.f*M_PI / 16.f);
932 
933  float alpha[4], beta[4], theta[4], gamma[4];
934 
935  alpha[0] = c * blk[2 * step];
936  alpha[1] = f * blk[2 * step];
937  alpha[2] = c * blk[6 * step];
938  alpha[3] = f * blk[6 * step];
939 
940  beta[0] = b * blk[1 * step] + d * blk[3 * step] + e * blk[5 * step] + g * blk[7 * step];
941  beta[1] = d * blk[1 * step] - g * blk[3 * step] - b * blk[5 * step] - e * blk[7 * step];
942  beta[2] = e * blk[1 * step] - b * blk[3 * step] + g * blk[5 * step] + d * blk[7 * step];
943  beta[3] = g * blk[1 * step] - e * blk[3 * step] + d * blk[5 * step] - b * blk[7 * step];
944 
945  theta[0] = a * (blk[0 * step] + blk[4 * step]);
946  theta[3] = a * (blk[0 * step] - blk[4 * step]);
947 
948  theta[1] = alpha[0] + alpha[3];
949  theta[2] = alpha[1] - alpha[2];
950 
951  gamma[0] = theta[0] + theta[1];
952  gamma[1] = theta[3] + theta[2];
953  gamma[2] = theta[3] - theta[2];
954  gamma[3] = theta[0] - theta[1];
955 
956  blk[0 * step] = gamma[0] + beta[0];
957  blk[1 * step] = gamma[1] + beta[1];
958  blk[2 * step] = gamma[2] + beta[2];
959  blk[3 * step] = gamma[3] + beta[3];
960 
961  blk[4 * step] = gamma[3] - beta[3];
962  blk[5 * step] = gamma[2] - beta[2];
963  blk[6 * step] = gamma[1] - beta[1];
964  blk[7 * step] = gamma[0] - beta[0];
965 }
966 
967 static void dct_inverse(float *block)
968 {
969  for (int i = 0; i < 8; i++)
970  idct_1d(block + i, 8);
971 
972  for (int i = 0; i < 8; i++) {
973  idct_1d(block, 1);
974  block += 8;
975  }
976 }
977 
978 static void convert(float y, float u, float v,
979  float *b, float *g, float *r)
980 {
981  *r = y + 1.5747f * v;
982  *g = y - 0.1873f * u - 0.4682f * v;
983  *b = y + 1.8556f * u;
984 }
985 
986 static float to_linear(float x, float scale)
987 {
988  float ax = fabsf(x);
989 
990  if (ax <= 1.f) {
991  return FFSIGN(x) * powf(ax, 2.2f * scale);
992  } else {
993  const float log_base = expf(2.2f * scale);
994 
995  return FFSIGN(x) * powf(log_base, ax - 1.f);
996  }
997 }
998 
999 static int dwa_uncompress(EXRContext *s, const uint8_t *src, int compressed_size,
1000  int uncompressed_size, EXRThreadData *td)
1001 {
1002  int64_t version, lo_usize, lo_size;
1003  int64_t ac_size, dc_size, rle_usize, rle_csize, rle_raw_size;
1004  int64_t ac_count, dc_count, ac_compression;
1005  const int dc_w = (td->xsize + 7) >> 3;
1006  const int dc_h = (td->ysize + 7) >> 3;
1007  GetByteContext gb, agb;
1008  int skip, ret;
1009  int have_rle = 0;
1010 
1011  if (compressed_size <= 88)
1012  return AVERROR_INVALIDDATA;
1013 
1014  version = AV_RL64(src + 0);
1015  if (version != 2)
1016  return AVERROR_INVALIDDATA;
1017 
1018  if (s->nb_channels < 3) {
1019  avpriv_request_sample(s->avctx, "Gray DWA");
1020  return AVERROR_PATCHWELCOME;
1021  }
1022 
1023  lo_usize = AV_RL64(src + 8);
1024  lo_size = AV_RL64(src + 16);
1025  ac_size = AV_RL64(src + 24);
1026  dc_size = AV_RL64(src + 32);
1027  rle_csize = AV_RL64(src + 40);
1028  rle_usize = AV_RL64(src + 48);
1029  rle_raw_size = AV_RL64(src + 56);
1030  ac_count = AV_RL64(src + 64);
1031  dc_count = AV_RL64(src + 72);
1032  ac_compression = AV_RL64(src + 80);
1033 
1034  if ( compressed_size < (uint64_t)(lo_size | ac_size | dc_size | rle_csize)
1035  || ac_count > (uint64_t)INT_MAX/2
1036  )
1037  return AVERROR_INVALIDDATA;
1038 
1039  if (ac_size <= 0) {
1040  avpriv_request_sample(s->avctx, "Zero ac_size");
1041  return AVERROR_INVALIDDATA;
1042  }
1043 
1044  if ((uint64_t)rle_raw_size > INT_MAX) {
1045  avpriv_request_sample(s->avctx, "Too big rle_raw_size");
1046  return AVERROR_INVALIDDATA;
1047  }
1048 
1049  if (td->xsize % 8 || td->ysize % 8) {
1050  avpriv_request_sample(s->avctx, "odd dimensions DWA");
1051  }
1052 
1053  bytestream2_init(&gb, src + 88, compressed_size - 88);
1054  skip = bytestream2_get_le16(&gb);
1055  if (skip < 2 || compressed_size < 88LL + skip + lo_size + ac_size + dc_size + rle_csize)
1056  return AVERROR_INVALIDDATA;
1057 
1058  bytestream2_skip(&gb, skip - 2);
1059 
1060  if (lo_size > 0) {
1061  if (lo_usize > uncompressed_size)
1062  return AVERROR_INVALIDDATA;
1063  bytestream2_skip(&gb, lo_size);
1064  }
1065 
1066  if (ac_size > 0) {
1067  unsigned long dest_len;
1068  GetByteContext agb = gb;
1069 
1070  if (ac_count > 3LL * td->xsize * s->scan_lines_per_block)
1071  return AVERROR_INVALIDDATA;
1072 
1073  dest_len = ac_count * 2LL;
1074 
1075  av_fast_padded_malloc(&td->ac_data, &td->ac_size, dest_len);
1076  if (!td->ac_data)
1077  return AVERROR(ENOMEM);
1078 
1079  switch (ac_compression) {
1080  case 0:
1081  ret = huf_uncompress(s, td, &agb, (int16_t *)td->ac_data, ac_count);
1082  if (ret < 0)
1083  return ret;
1084  break;
1085  case 1:
1086  if (uncompress(td->ac_data, &dest_len, agb.buffer, ac_size) != Z_OK ||
1087  dest_len != ac_count * 2LL)
1088  return AVERROR_INVALIDDATA;
1089  break;
1090  default:
1091  return AVERROR_INVALIDDATA;
1092  }
1093 
1094  bytestream2_skip(&gb, ac_size);
1095  }
1096 
1097  {
1098  unsigned long dest_len;
1099  GetByteContext agb = gb;
1100 
1101  if (dc_count != dc_w * dc_h * 3)
1102  return AVERROR_INVALIDDATA;
1103 
1104  dest_len = dc_count * 2LL;
1105 
1106  av_fast_padded_malloc(&td->dc_data, &td->dc_size, FFALIGN(dest_len, 64) * 2);
1107  if (!td->dc_data)
1108  return AVERROR(ENOMEM);
1109 
1110  if (uncompress(td->dc_data + FFALIGN(dest_len, 64), &dest_len, agb.buffer, dc_size) != Z_OK ||
1111  (dest_len != dc_count * 2LL))
1112  return AVERROR_INVALIDDATA;
1113 
1114  s->dsp.predictor(td->dc_data + FFALIGN(dest_len, 64), dest_len);
1115  s->dsp.reorder_pixels(td->dc_data, td->dc_data + FFALIGN(dest_len, 64), dest_len);
1116 
1117  bytestream2_skip(&gb, dc_size);
1118  }
1119 
1120  if (rle_raw_size > 0 && rle_csize > 0 && rle_usize > 0) {
1121  unsigned long dest_len = rle_usize;
1122 
1123  if (2LL * td->xsize * td->ysize > rle_raw_size)
1124  return AVERROR_INVALIDDATA;
1125 
1126  av_fast_padded_malloc(&td->rle_data, &td->rle_size, rle_usize);
1127  if (!td->rle_data)
1128  return AVERROR(ENOMEM);
1129 
1130  av_fast_padded_malloc(&td->rle_raw_data, &td->rle_raw_size, rle_raw_size);
1131  if (!td->rle_raw_data)
1132  return AVERROR(ENOMEM);
1133 
1134  if (uncompress(td->rle_data, &dest_len, gb.buffer, rle_csize) != Z_OK ||
1135  (dest_len != rle_usize))
1136  return AVERROR_INVALIDDATA;
1137 
1138  ret = rle(td->rle_raw_data, td->rle_data, rle_usize, rle_raw_size);
1139  if (ret < 0)
1140  return ret;
1141  bytestream2_skip(&gb, rle_csize);
1142 
1143  have_rle = 1;
1144  }
1145 
1146  bytestream2_init(&agb, td->ac_data, ac_count * 2);
1147 
1148  for (int y = 0; y < td->ysize; y += 8) {
1149  for (int x = 0; x < td->xsize; x += 8) {
1150  int bw = FFMIN(8, td->xsize - x);
1151  int bh = FFMIN(8, td->ysize - y);
1152 
1153  memset(td->block, 0, sizeof(td->block));
1154 
1155  for (int j = 0; j < 3; j++) {
1156  float *block = td->block[j];
1157  const int idx = (x >> 3) + (y >> 3) * dc_w + dc_w * dc_h * j;
1158  uint16_t *dc = (uint16_t *)td->dc_data;
1159  union av_intfloat32 dc_val;
1160 
1161  dc_val.i = half2float(dc[idx], s->mantissatable,
1162  s->exponenttable, s->offsettable);
1163 
1164  block[0] = dc_val.f;
1165  ac_uncompress(s, &agb, block);
1166  dct_inverse(block);
1167  }
1168 
1169  {
1170  const float scale = s->pixel_type == EXR_FLOAT ? 2.f : 1.f;
1171  const int o = s->nb_channels == 4;
1172  float *bo = ((float *)td->uncompressed_data) +
1173  y * td->xsize * s->nb_channels + td->xsize * (o + 0) + x;
1174  float *go = ((float *)td->uncompressed_data) +
1175  y * td->xsize * s->nb_channels + td->xsize * (o + 1) + x;
1176  float *ro = ((float *)td->uncompressed_data) +
1177  y * td->xsize * s->nb_channels + td->xsize * (o + 2) + x;
1178  float *yb = td->block[0];
1179  float *ub = td->block[1];
1180  float *vb = td->block[2];
1181 
1182  for (int yy = 0; yy < bh; yy++) {
1183  for (int xx = 0; xx < bw; xx++) {
1184  const int idx = xx + yy * 8;
1185 
1186  convert(yb[idx], ub[idx], vb[idx], &bo[xx], &go[xx], &ro[xx]);
1187 
1188  bo[xx] = to_linear(bo[xx], scale);
1189  go[xx] = to_linear(go[xx], scale);
1190  ro[xx] = to_linear(ro[xx], scale);
1191  }
1192 
1193  bo += td->xsize * s->nb_channels;
1194  go += td->xsize * s->nb_channels;
1195  ro += td->xsize * s->nb_channels;
1196  }
1197  }
1198  }
1199  }
1200 
1201  if (s->nb_channels < 4)
1202  return 0;
1203 
1204  for (int y = 0; y < td->ysize && have_rle; y++) {
1205  uint32_t *ao = ((uint32_t *)td->uncompressed_data) + y * td->xsize * s->nb_channels;
1206  uint8_t *ai0 = td->rle_raw_data + y * td->xsize;
1207  uint8_t *ai1 = td->rle_raw_data + y * td->xsize + rle_raw_size / 2;
1208 
1209  for (int x = 0; x < td->xsize; x++) {
1210  uint16_t ha = ai0[x] | (ai1[x] << 8);
1211 
1212  ao[x] = half2float(ha, s->mantissatable, s->exponenttable, s->offsettable);
1213  }
1214  }
1215 
1216  return 0;
1217 }
1218 
1219 static int decode_block(AVCodecContext *avctx, void *tdata,
1220  int jobnr, int threadnr)
1221 {
1222  EXRContext *s = avctx->priv_data;
1223  AVFrame *const p = s->picture;
1224  EXRThreadData *td = &s->thread_data[threadnr];
1225  const uint8_t *channel_buffer[4] = { 0 };
1226  const uint8_t *buf = s->buf;
1227  uint64_t line_offset, uncompressed_size;
1228  uint8_t *ptr;
1229  uint32_t data_size;
1230  int line, col = 0;
1231  uint64_t tile_x, tile_y, tile_level_x, tile_level_y;
1232  const uint8_t *src;
1233  int step = s->desc->flags & AV_PIX_FMT_FLAG_FLOAT ? 4 : 2 * s->desc->nb_components;
1234  int bxmin = 0, axmax = 0, window_xoffset = 0;
1235  int window_xmin, window_xmax, window_ymin, window_ymax;
1236  int data_xoffset, data_yoffset, data_window_offset, xsize, ysize;
1237  int i, x, buf_size = s->buf_size;
1238  int c, rgb_channel_count;
1239  float one_gamma = 1.0f / s->gamma;
1240  avpriv_trc_function trc_func = avpriv_get_trc_function_from_trc(s->apply_trc_type);
1241  int ret;
1242 
1243  line_offset = AV_RL64(s->gb.buffer + jobnr * 8);
1244 
1245  if (s->is_tile) {
1246  if (buf_size < 20 || line_offset > buf_size - 20)
1247  return AVERROR_INVALIDDATA;
1248 
1249  src = buf + line_offset + 20;
1250  if (s->is_multipart)
1251  src += 4;
1252 
1253  tile_x = AV_RL32(src - 20);
1254  tile_y = AV_RL32(src - 16);
1255  tile_level_x = AV_RL32(src - 12);
1256  tile_level_y = AV_RL32(src - 8);
1257 
1258  data_size = AV_RL32(src - 4);
1259  if (data_size <= 0 || data_size > buf_size - line_offset - 20)
1260  return AVERROR_INVALIDDATA;
1261 
1262  if (tile_level_x || tile_level_y) { /* tile level, is not the full res level */
1263  avpriv_report_missing_feature(s->avctx, "Subres tile before full res tile");
1264  return AVERROR_PATCHWELCOME;
1265  }
1266 
1267  if (tile_x && s->tile_attr.xSize + (int64_t)FFMAX(s->xmin, 0) >= INT_MAX / tile_x )
1268  return AVERROR_INVALIDDATA;
1269  if (tile_y && s->tile_attr.ySize + (int64_t)FFMAX(s->ymin, 0) >= INT_MAX / tile_y )
1270  return AVERROR_INVALIDDATA;
1271 
1272  line = s->ymin + s->tile_attr.ySize * tile_y;
1273  col = s->tile_attr.xSize * tile_x;
1274 
1275  if (line < s->ymin || line > s->ymax ||
1276  s->xmin + col < s->xmin || s->xmin + col > s->xmax)
1277  return AVERROR_INVALIDDATA;
1278 
1279  td->ysize = FFMIN(s->tile_attr.ySize, s->ydelta - tile_y * s->tile_attr.ySize);
1280  td->xsize = FFMIN(s->tile_attr.xSize, s->xdelta - tile_x * s->tile_attr.xSize);
1281 
1282  if (td->xsize * (uint64_t)s->current_channel_offset > INT_MAX ||
1283  av_image_check_size2(td->xsize, td->ysize, s->avctx->max_pixels, AV_PIX_FMT_NONE, 0, s->avctx) < 0)
1284  return AVERROR_INVALIDDATA;
1285 
1286  td->channel_line_size = td->xsize * s->current_channel_offset;/* uncompress size of one line */
1287  uncompressed_size = td->channel_line_size * (uint64_t)td->ysize;/* uncompress size of the block */
1288  } else {
1289  if (buf_size < 8 || line_offset > buf_size - 8)
1290  return AVERROR_INVALIDDATA;
1291 
1292  src = buf + line_offset + 8;
1293  if (s->is_multipart)
1294  src += 4;
1295  line = AV_RL32(src - 8);
1296 
1297  if (line < s->ymin || line > s->ymax)
1298  return AVERROR_INVALIDDATA;
1299 
1300  data_size = AV_RL32(src - 4);
1301  if (data_size <= 0 || data_size > buf_size - line_offset - 8)
1302  return AVERROR_INVALIDDATA;
1303 
1304  td->ysize = FFMIN(s->scan_lines_per_block, s->ymax - line + 1); /* s->ydelta - line ?? */
1305  td->xsize = s->xdelta;
1306 
1307  if (td->xsize * (uint64_t)s->current_channel_offset > INT_MAX ||
1308  av_image_check_size2(td->xsize, td->ysize, s->avctx->max_pixels, AV_PIX_FMT_NONE, 0, s->avctx) < 0)
1309  return AVERROR_INVALIDDATA;
1310 
1311  td->channel_line_size = td->xsize * s->current_channel_offset;/* uncompress size of one line */
1312  uncompressed_size = td->channel_line_size * (uint64_t)td->ysize;/* uncompress size of the block */
1313 
1314  if ((s->compression == EXR_RAW && (data_size != uncompressed_size ||
1315  line_offset > buf_size - uncompressed_size)) ||
1316  (s->compression != EXR_RAW && (data_size > uncompressed_size ||
1317  line_offset > buf_size - data_size))) {
1318  return AVERROR_INVALIDDATA;
1319  }
1320  }
1321 
1322  window_xmin = FFMIN(avctx->width, FFMAX(0, s->xmin + col));
1323  window_xmax = FFMIN(avctx->width, FFMAX(0, s->xmin + col + td->xsize));
1324  window_ymin = FFMIN(avctx->height, FFMAX(0, line ));
1325  window_ymax = FFMIN(avctx->height, FFMAX(0, line + td->ysize));
1326  xsize = window_xmax - window_xmin;
1327  ysize = window_ymax - window_ymin;
1328 
1329  /* tile or scanline not visible skip decoding */
1330  if (xsize <= 0 || ysize <= 0)
1331  return 0;
1332 
1333  /* is the first tile or is a scanline */
1334  if(col == 0) {
1335  window_xmin = 0;
1336  /* pixels to add at the left of the display window */
1337  window_xoffset = FFMAX(0, s->xmin);
1338  /* bytes to add at the left of the display window */
1339  bxmin = window_xoffset * step;
1340  }
1341 
1342  /* is the last tile or is a scanline */
1343  if(col + td->xsize == s->xdelta) {
1344  window_xmax = avctx->width;
1345  /* bytes to add at the right of the display window */
1346  axmax = FFMAX(0, (avctx->width - (s->xmax + 1))) * step;
1347  }
1348 
1349  if (avctx->max_pixels && uncompressed_size > avctx->max_pixels * 16LL)
1350  return AVERROR_INVALIDDATA;
1351 
1352  if (data_size < uncompressed_size || s->is_tile) { /* td->tmp is use for tile reorganization */
1353  av_fast_padded_malloc(&td->tmp, &td->tmp_size, uncompressed_size);
1354  if (!td->tmp)
1355  return AVERROR(ENOMEM);
1356  }
1357 
1358  if (data_size < uncompressed_size) {
1359  av_fast_padded_malloc(&td->uncompressed_data,
1360  &td->uncompressed_size, uncompressed_size + 64);/* Force 64 padding for AVX2 reorder_pixels dst */
1361 
1362  if (!td->uncompressed_data)
1363  return AVERROR(ENOMEM);
1364 
1365  ret = AVERROR_INVALIDDATA;
1366  switch (s->compression) {
1367  case EXR_ZIP1:
1368  case EXR_ZIP16:
1369  ret = zip_uncompress(s, src, data_size, uncompressed_size, td);
1370  break;
1371  case EXR_PIZ:
1372  ret = piz_uncompress(s, src, data_size, uncompressed_size, td);
1373  break;
1374  case EXR_PXR24:
1375  ret = pxr24_uncompress(s, src, data_size, uncompressed_size, td);
1376  break;
1377  case EXR_RLE:
1378  ret = rle_uncompress(s, src, data_size, uncompressed_size, td);
1379  break;
1380  case EXR_B44:
1381  case EXR_B44A:
1382  ret = b44_uncompress(s, src, data_size, uncompressed_size, td);
1383  break;
1384  case EXR_DWAA:
1385  case EXR_DWAB:
1386  ret = dwa_uncompress(s, src, data_size, uncompressed_size, td);
1387  break;
1388  }
1389  if (ret < 0) {
1390  av_log(avctx, AV_LOG_ERROR, "decode_block() failed.\n");
1391  return ret;
1392  }
1393  src = td->uncompressed_data;
1394  }
1395 
1396  /* offsets to crop data outside display window */
1397  data_xoffset = FFABS(FFMIN(0, s->xmin + col)) * (s->pixel_type == EXR_HALF ? 2 : 4);
1398  data_yoffset = FFABS(FFMIN(0, line));
1399  data_window_offset = (data_yoffset * td->channel_line_size) + data_xoffset;
1400 
1401  if (!s->is_luma) {
1402  channel_buffer[0] = src + (td->xsize * s->channel_offsets[0]) + data_window_offset;
1403  channel_buffer[1] = src + (td->xsize * s->channel_offsets[1]) + data_window_offset;
1404  channel_buffer[2] = src + (td->xsize * s->channel_offsets[2]) + data_window_offset;
1405  rgb_channel_count = 3;
1406  } else { /* put y data in the first channel_buffer */
1407  channel_buffer[0] = src + (td->xsize * s->channel_offsets[1]) + data_window_offset;
1408  rgb_channel_count = 1;
1409  }
1410  if (s->channel_offsets[3] >= 0)
1411  channel_buffer[3] = src + (td->xsize * s->channel_offsets[3]) + data_window_offset;
1412 
1413  if (s->desc->flags & AV_PIX_FMT_FLAG_FLOAT) {
1414  /* todo: change this when a floating point pixel format with luma with alpha is implemented */
1415  int channel_count = s->channel_offsets[3] >= 0 ? 4 : rgb_channel_count;
1416  if (s->is_luma) {
1417  channel_buffer[1] = channel_buffer[0];
1418  channel_buffer[2] = channel_buffer[0];
1419  }
1420 
1421  for (c = 0; c < channel_count; c++) {
1422  int plane = s->desc->comp[c].plane;
1423  ptr = p->data[plane] + window_ymin * p->linesize[plane] + (window_xmin * 4);
1424 
1425  for (i = 0; i < ysize; i++, ptr += p->linesize[plane]) {
1426  const uint8_t *src;
1427  union av_intfloat32 *ptr_x;
1428 
1429  src = channel_buffer[c];
1430  ptr_x = (union av_intfloat32 *)ptr;
1431 
1432  // Zero out the start if xmin is not 0
1433  memset(ptr_x, 0, bxmin);
1434  ptr_x += window_xoffset;
1435 
1436  if (s->pixel_type == EXR_FLOAT ||
1437  s->compression == EXR_DWAA ||
1438  s->compression == EXR_DWAB) {
1439  // 32-bit
1440  union av_intfloat32 t;
1441  if (trc_func && c < 3) {
1442  for (x = 0; x < xsize; x++) {
1443  t.i = bytestream_get_le32(&src);
1444  t.f = trc_func(t.f);
1445  *ptr_x++ = t;
1446  }
1447  } else if (one_gamma != 1.f) {
1448  for (x = 0; x < xsize; x++) {
1449  t.i = bytestream_get_le32(&src);
1450  if (t.f > 0.0f && c < 3) /* avoid negative values */
1451  t.f = powf(t.f, one_gamma);
1452  *ptr_x++ = t;
1453  }
1454  } else {
1455  for (x = 0; x < xsize; x++) {
1456  t.i = bytestream_get_le32(&src);
1457  *ptr_x++ = t;
1458  }
1459  }
1460  } else if (s->pixel_type == EXR_HALF) {
1461  // 16-bit
1462  if (c < 3 || !trc_func) {
1463  for (x = 0; x < xsize; x++) {
1464  *ptr_x++ = s->gamma_table[bytestream_get_le16(&src)];
1465  }
1466  } else {
1467  for (x = 0; x < xsize; x++) {
1468  ptr_x[0].i = half2float(bytestream_get_le16(&src),
1469  s->mantissatable,
1470  s->exponenttable,
1471  s->offsettable);
1472  ptr_x++;
1473  }
1474  }
1475  }
1476 
1477  // Zero out the end if xmax+1 is not w
1478  if (s->desc->flags & AV_PIX_FMT_FLAG_PLANAR || !c)
1479  memset(ptr_x, 0, axmax);
1480  channel_buffer[c] += td->channel_line_size;
1481  }
1482  }
1483  } else {
1484 
1485  av_assert1(s->pixel_type == EXR_UINT);
1486  ptr = p->data[0] + window_ymin * p->linesize[0] + (window_xmin * s->desc->nb_components * 2);
1487 
1488  for (i = 0; i < ysize; i++, ptr += p->linesize[0]) {
1489 
1490  const uint8_t * a;
1491  const uint8_t *rgb[3];
1492  uint16_t *ptr_x;
1493 
1494  for (c = 0; c < rgb_channel_count; c++) {
1495  rgb[c] = channel_buffer[c];
1496  }
1497 
1498  if (channel_buffer[3])
1499  a = channel_buffer[3];
1500 
1501  ptr_x = (uint16_t *) ptr;
1502 
1503  // Zero out the start if xmin is not 0
1504  memset(ptr_x, 0, bxmin);
1505  ptr_x += window_xoffset * s->desc->nb_components;
1506 
1507  for (x = 0; x < xsize; x++) {
1508  for (c = 0; c < rgb_channel_count; c++) {
1509  *ptr_x++ = bytestream_get_le32(&rgb[c]) >> 16;
1510  }
1511 
1512  if (channel_buffer[3])
1513  *ptr_x++ = bytestream_get_le32(&a) >> 16;
1514  }
1515 
1516  // Zero out the end if xmax+1 is not w
1517  memset(ptr_x, 0, axmax);
1518 
1519  channel_buffer[0] += td->channel_line_size;
1520  channel_buffer[1] += td->channel_line_size;
1521  channel_buffer[2] += td->channel_line_size;
1522  if (channel_buffer[3])
1523  channel_buffer[3] += td->channel_line_size;
1524  }
1525  }
1526 
1527  return 0;
1528 }
1529 
1531 {
1532  GetByteContext *gb = &s->gb;
1533 
1534  while (bytestream2_get_bytes_left(gb) > 0) {
1535  if (!bytestream2_peek_byte(gb))
1536  break;
1537 
1538  // Process unknown variables
1539  for (int i = 0; i < 2; i++) // value_name and value_type
1540  while (bytestream2_get_byte(gb) != 0);
1541 
1542  // Skip variable length
1543  bytestream2_skip(gb, bytestream2_get_le32(gb));
1544  }
1545 }
1546 
1547 /**
1548  * Check if the variable name corresponds to its data type.
1549  *
1550  * @param s the EXRContext
1551  * @param value_name name of the variable to check
1552  * @param value_type type of the variable to check
1553  * @param minimum_length minimum length of the variable data
1554  *
1555  * @return bytes to read containing variable data
1556  * -1 if variable is not found
1557  * 0 if buffer ended prematurely
1558  */
1560  const char *value_name,
1561  const char *value_type,
1562  unsigned int minimum_length)
1563 {
1564  GetByteContext *gb = &s->gb;
1565  int var_size = -1;
1566 
1567  if (bytestream2_get_bytes_left(gb) >= minimum_length &&
1568  !strcmp(gb->buffer, value_name)) {
1569  // found value_name, jump to value_type (null terminated strings)
1570  gb->buffer += strlen(value_name) + 1;
1571  if (!strcmp(gb->buffer, value_type)) {
1572  gb->buffer += strlen(value_type) + 1;
1573  var_size = bytestream2_get_le32(gb);
1574  // don't go read past boundaries
1575  if (var_size > bytestream2_get_bytes_left(gb))
1576  var_size = 0;
1577  } else {
1578  // value_type not found, reset the buffer
1579  gb->buffer -= strlen(value_name) + 1;
1580  av_log(s->avctx, AV_LOG_WARNING,
1581  "Unknown data type %s for header variable %s.\n",
1582  value_type, value_name);
1583  }
1584  }
1585 
1586  return var_size;
1587 }
1588 
1590 {
1591  AVDictionary *metadata = NULL;
1592  GetByteContext *gb = &s->gb;
1593  int magic_number, version, flags;
1594  int layer_match = 0;
1595  int ret;
1596  int dup_channels = 0;
1597 
1598  s->current_channel_offset = 0;
1599  s->xmin = ~0;
1600  s->xmax = ~0;
1601  s->ymin = ~0;
1602  s->ymax = ~0;
1603  s->xdelta = ~0;
1604  s->ydelta = ~0;
1605  s->channel_offsets[0] = -1;
1606  s->channel_offsets[1] = -1;
1607  s->channel_offsets[2] = -1;
1608  s->channel_offsets[3] = -1;
1609  s->pixel_type = EXR_UNKNOWN;
1610  s->compression = EXR_UNKN;
1611  s->nb_channels = 0;
1612  s->w = 0;
1613  s->h = 0;
1614  s->tile_attr.xSize = -1;
1615  s->tile_attr.ySize = -1;
1616  s->is_tile = 0;
1617  s->is_multipart = 0;
1618  s->is_luma = 0;
1619  s->has_channel = 0;
1620  s->current_part = 0;
1621 
1622  if (bytestream2_get_bytes_left(gb) < 10) {
1623  av_log(s->avctx, AV_LOG_ERROR, "Header too short to parse.\n");
1624  return AVERROR_INVALIDDATA;
1625  }
1626 
1627  magic_number = bytestream2_get_le32(gb);
1628  if (magic_number != 20000630) {
1629  /* As per documentation of OpenEXR, it is supposed to be
1630  * int 20000630 little-endian */
1631  av_log(s->avctx, AV_LOG_ERROR, "Wrong magic number %d.\n", magic_number);
1632  return AVERROR_INVALIDDATA;
1633  }
1634 
1635  version = bytestream2_get_byte(gb);
1636  if (version != 2) {
1637  avpriv_report_missing_feature(s->avctx, "Version %d", version);
1638  return AVERROR_PATCHWELCOME;
1639  }
1640 
1641  flags = bytestream2_get_le24(gb);
1642 
1643  if (flags & 0x02)
1644  s->is_tile = 1;
1645  if (flags & 0x10)
1646  s->is_multipart = 1;
1647  if (flags & 0x08) {
1648  avpriv_report_missing_feature(s->avctx, "deep data");
1649  return AVERROR_PATCHWELCOME;
1650  }
1651 
1652  // Parse the header
1653  while (bytestream2_get_bytes_left(gb) > 0) {
1654  int var_size;
1655 
1656  while (s->is_multipart && s->current_part < s->selected_part &&
1657  bytestream2_get_bytes_left(gb) > 0) {
1658  if (bytestream2_peek_byte(gb)) {
1660  } else {
1661  bytestream2_skip(gb, 1);
1662  if (!bytestream2_peek_byte(gb))
1663  break;
1664  }
1665  bytestream2_skip(gb, 1);
1666  s->current_part++;
1667  }
1668 
1669  if (!bytestream2_peek_byte(gb)) {
1670  if (!s->is_multipart)
1671  break;
1672  bytestream2_skip(gb, 1);
1673  if (s->current_part == s->selected_part) {
1674  while (bytestream2_get_bytes_left(gb) > 0) {
1675  if (bytestream2_peek_byte(gb)) {
1677  } else {
1678  bytestream2_skip(gb, 1);
1679  if (!bytestream2_peek_byte(gb))
1680  break;
1681  }
1682  }
1683  }
1684  if (!bytestream2_peek_byte(gb))
1685  break;
1686  s->current_part++;
1687  }
1688 
1689  if ((var_size = check_header_variable(s, "channels",
1690  "chlist", 38)) >= 0) {
1691  GetByteContext ch_gb;
1692  if (!var_size) {
1693  ret = AVERROR_INVALIDDATA;
1694  goto fail;
1695  }
1696 
1697  bytestream2_init(&ch_gb, gb->buffer, var_size);
1698 
1699  while (bytestream2_get_bytes_left(&ch_gb) >= 19) {
1701  enum ExrPixelType current_pixel_type;
1702  int channel_index = -1;
1703  int xsub, ysub;
1704 
1705  if (strcmp(s->layer, "") != 0) {
1706  if (strncmp(ch_gb.buffer, s->layer, strlen(s->layer)) == 0) {
1707  layer_match = 1;
1708  av_log(s->avctx, AV_LOG_INFO,
1709  "Channel match layer : %s.\n", ch_gb.buffer);
1710  ch_gb.buffer += strlen(s->layer);
1711  if (*ch_gb.buffer == '.')
1712  ch_gb.buffer++; /* skip dot if not given */
1713  } else {
1714  layer_match = 0;
1715  av_log(s->avctx, AV_LOG_INFO,
1716  "Channel doesn't match layer : %s.\n", ch_gb.buffer);
1717  }
1718  } else {
1719  layer_match = 1;
1720  }
1721 
1722  if (layer_match) { /* only search channel if the layer match is valid */
1723  if (strlen(ch_gb.buffer) == 1) {
1724  int ch_chr = av_toupper(*ch_gb.buffer);
1725  if (ch_chr >= 'A' && ch_chr <= 'Z')
1726  s->has_channel |= M(ch_chr);
1727  av_log(s->avctx, AV_LOG_DEBUG, "%c\n", ch_chr);
1728  }
1729 
1730  if (!av_strcasecmp(ch_gb.buffer, "R") ||
1731  !av_strcasecmp(ch_gb.buffer, "X") ||
1732  !av_strcasecmp(ch_gb.buffer, "U")) {
1733  channel_index = 0;
1734  } else if (!av_strcasecmp(ch_gb.buffer, "G") ||
1735  !av_strcasecmp(ch_gb.buffer, "V")) {
1736  channel_index = 1;
1737  } else if (!av_strcasecmp(ch_gb.buffer, "Y")) {
1738  channel_index = 1;
1739  } else if (!av_strcasecmp(ch_gb.buffer, "B") ||
1740  !av_strcasecmp(ch_gb.buffer, "Z") ||
1741  !av_strcasecmp(ch_gb.buffer, "W")) {
1742  channel_index = 2;
1743  } else if (!av_strcasecmp(ch_gb.buffer, "A")) {
1744  channel_index = 3;
1745  } else {
1746  av_log(s->avctx, AV_LOG_WARNING,
1747  "Unsupported channel %.256s.\n", ch_gb.buffer);
1748  }
1749  }
1750 
1751  /* skip until you get a 0 */
1752  while (bytestream2_get_bytes_left(&ch_gb) > 0 &&
1753  bytestream2_get_byte(&ch_gb))
1754  continue;
1755 
1756  if (bytestream2_get_bytes_left(&ch_gb) < 4) {
1757  av_log(s->avctx, AV_LOG_ERROR, "Incomplete header.\n");
1758  ret = AVERROR_INVALIDDATA;
1759  goto fail;
1760  }
1761 
1762  current_pixel_type = bytestream2_get_le32(&ch_gb);
1763  if (current_pixel_type >= EXR_UNKNOWN) {
1764  avpriv_report_missing_feature(s->avctx, "Pixel type %d",
1765  current_pixel_type);
1766  ret = AVERROR_PATCHWELCOME;
1767  goto fail;
1768  }
1769 
1770  bytestream2_skip(&ch_gb, 4);
1771  xsub = bytestream2_get_le32(&ch_gb);
1772  ysub = bytestream2_get_le32(&ch_gb);
1773 
1774  if (xsub != 1 || ysub != 1) {
1776  "Subsampling %dx%d",
1777  xsub, ysub);
1778  ret = AVERROR_PATCHWELCOME;
1779  goto fail;
1780  }
1781 
1782  if (channel_index >= 0 && s->channel_offsets[channel_index] == -1) { /* channel has not been previously assigned */
1783  if (s->pixel_type != EXR_UNKNOWN &&
1784  s->pixel_type != current_pixel_type) {
1785  av_log(s->avctx, AV_LOG_ERROR,
1786  "RGB channels not of the same depth.\n");
1787  ret = AVERROR_INVALIDDATA;
1788  goto fail;
1789  }
1790  s->pixel_type = current_pixel_type;
1791  s->channel_offsets[channel_index] = s->current_channel_offset;
1792  } else if (channel_index >= 0) {
1793  av_log(s->avctx, AV_LOG_WARNING,
1794  "Multiple channels with index %d.\n", channel_index);
1795  if (++dup_channels > 10) {
1796  ret = AVERROR_INVALIDDATA;
1797  goto fail;
1798  }
1799  }
1800 
1801  av_assert0(s->nb_channels < INT_MAX); // Impossible due to size of the bitstream
1802  EXRChannel *new_channels = av_realloc_array(s->channels,
1803  s->nb_channels + 1,
1804  sizeof(EXRChannel));
1805  if (!new_channels) {
1806  ret = AVERROR(ENOMEM);
1807  goto fail;
1808  }
1809  s->nb_channels ++;
1810  s->channels = new_channels;
1811 
1812  channel = &s->channels[s->nb_channels - 1];
1813  channel->pixel_type = current_pixel_type;
1814  channel->xsub = xsub;
1815  channel->ysub = ysub;
1816 
1817  if (current_pixel_type == EXR_HALF) {
1818  s->current_channel_offset += 2;
1819  } else {/* Float or UINT32 */
1820  s->current_channel_offset += 4;
1821  }
1822  }
1823  if (!((M('R') + M('G') + M('B')) & ~s->has_channel)) {
1824  s->is_luma = 0;
1825  } else if (!((M('X') + M('Y') + M('Z')) & ~s->has_channel)) {
1826  s->is_luma = 0;
1827  } else if (!((M('Y') + M('U') + M('V')) & ~s->has_channel)) {
1828  s->is_luma = 0;
1829  } else if (!((M('Y') ) & ~s->has_channel) &&
1830  !((M('R') + M('G') + M('B') + M('U') + M('V') + M('X') + M('Z')) & s->has_channel)) {
1831  s->is_luma = 1;
1832  } else {
1833  avpriv_request_sample(s->avctx, "Uncommon channel combination");
1834  ret = AVERROR_PATCHWELCOME;
1835  goto fail;
1836  }
1837 
1838  /* Check if all channels are set with an offset or if the channels
1839  * are causing an overflow */
1840  if (!s->is_luma) {/* if we expected to have at least 3 channels */
1841  if (FFMIN3(s->channel_offsets[0],
1842  s->channel_offsets[1],
1843  s->channel_offsets[2]) < 0) {
1844  if (s->channel_offsets[0] < 0)
1845  av_log(s->avctx, AV_LOG_ERROR, "Missing red channel.\n");
1846  if (s->channel_offsets[1] < 0)
1847  av_log(s->avctx, AV_LOG_ERROR, "Missing green channel.\n");
1848  if (s->channel_offsets[2] < 0)
1849  av_log(s->avctx, AV_LOG_ERROR, "Missing blue channel.\n");
1850  ret = AVERROR_INVALIDDATA;
1851  goto fail;
1852  }
1853  }
1854 
1855  // skip one last byte and update main gb
1856  gb->buffer = ch_gb.buffer + 1;
1857  continue;
1858  } else if ((var_size = check_header_variable(s, "dataWindow", "box2i",
1859  31)) >= 0) {
1860  int xmin, ymin, xmax, ymax;
1861  if (!var_size) {
1862  ret = AVERROR_INVALIDDATA;
1863  goto fail;
1864  }
1865 
1866  xmin = bytestream2_get_le32(gb);
1867  ymin = bytestream2_get_le32(gb);
1868  xmax = bytestream2_get_le32(gb);
1869  ymax = bytestream2_get_le32(gb);
1870 
1871  if (xmin > xmax || ymin > ymax ||
1872  ymax == INT_MAX || xmax == INT_MAX ||
1873  (unsigned)xmax - xmin >= INT_MAX ||
1874  (unsigned)ymax - ymin >= INT_MAX) {
1875  ret = AVERROR_INVALIDDATA;
1876  goto fail;
1877  }
1878  s->xmin = xmin;
1879  s->xmax = xmax;
1880  s->ymin = ymin;
1881  s->ymax = ymax;
1882  s->xdelta = (s->xmax - s->xmin) + 1;
1883  s->ydelta = (s->ymax - s->ymin) + 1;
1884 
1885  continue;
1886  } else if ((var_size = check_header_variable(s, "displayWindow",
1887  "box2i", 34)) >= 0) {
1888  int32_t sx, sy, dx, dy;
1889 
1890  if (!var_size) {
1891  ret = AVERROR_INVALIDDATA;
1892  goto fail;
1893  }
1894 
1895  sx = bytestream2_get_le32(gb);
1896  sy = bytestream2_get_le32(gb);
1897  dx = bytestream2_get_le32(gb);
1898  dy = bytestream2_get_le32(gb);
1899 
1900  s->w = (unsigned)dx - sx + 1;
1901  s->h = (unsigned)dy - sy + 1;
1902 
1903  continue;
1904  } else if ((var_size = check_header_variable(s, "lineOrder",
1905  "lineOrder", 25)) >= 0) {
1906  int line_order;
1907  if (!var_size) {
1908  ret = AVERROR_INVALIDDATA;
1909  goto fail;
1910  }
1911 
1912  line_order = bytestream2_get_byte(gb);
1913  av_log(s->avctx, AV_LOG_DEBUG, "line order: %d.\n", line_order);
1914  if (line_order > 2) {
1915  av_log(s->avctx, AV_LOG_ERROR, "Unknown line order.\n");
1916  ret = AVERROR_INVALIDDATA;
1917  goto fail;
1918  }
1919 
1920  continue;
1921  } else if ((var_size = check_header_variable(s, "pixelAspectRatio",
1922  "float", 31)) >= 0) {
1923  if (!var_size) {
1924  ret = AVERROR_INVALIDDATA;
1925  goto fail;
1926  }
1927 
1928  s->sar = bytestream2_get_le32(gb);
1929 
1930  continue;
1931  } else if ((var_size = check_header_variable(s, "compression",
1932  "compression", 29)) >= 0) {
1933  if (!var_size) {
1934  ret = AVERROR_INVALIDDATA;
1935  goto fail;
1936  }
1937 
1938  if (s->compression == EXR_UNKN)
1939  s->compression = bytestream2_get_byte(gb);
1940  else {
1941  bytestream2_skip(gb, 1);
1942  av_log(s->avctx, AV_LOG_WARNING,
1943  "Found more than one compression attribute.\n");
1944  }
1945 
1946  continue;
1947  } else if ((var_size = check_header_variable(s, "tiles",
1948  "tiledesc", 22)) >= 0) {
1949  char tileLevel;
1950 
1951  if (!s->is_tile)
1952  av_log(s->avctx, AV_LOG_WARNING,
1953  "Found tile attribute and scanline flags. Exr will be interpreted as scanline.\n");
1954 
1955  s->tile_attr.xSize = bytestream2_get_le32(gb);
1956  s->tile_attr.ySize = bytestream2_get_le32(gb);
1957 
1958  tileLevel = bytestream2_get_byte(gb);
1959  s->tile_attr.level_mode = tileLevel & 0x0f;
1960  s->tile_attr.level_round = (tileLevel >> 4) & 0x0f;
1961 
1962  if (s->tile_attr.level_mode >= EXR_TILE_LEVEL_UNKNOWN) {
1963  avpriv_report_missing_feature(s->avctx, "Tile level mode %d",
1964  s->tile_attr.level_mode);
1965  ret = AVERROR_PATCHWELCOME;
1966  goto fail;
1967  }
1968 
1969  if (s->tile_attr.level_round >= EXR_TILE_ROUND_UNKNOWN) {
1970  avpriv_report_missing_feature(s->avctx, "Tile level round %d",
1971  s->tile_attr.level_round);
1972  ret = AVERROR_PATCHWELCOME;
1973  goto fail;
1974  }
1975 
1976  continue;
1977  } else if ((var_size = check_header_variable(s, "writer",
1978  "string", 1)) >= 0) {
1979  uint8_t key[256] = { 0 };
1980 
1981  bytestream2_get_buffer(gb, key, FFMIN(sizeof(key) - 1, var_size));
1982  av_dict_set(&metadata, "writer", key, 0);
1983 
1984  continue;
1985  } else if ((var_size = check_header_variable(s, "framesPerSecond",
1986  "rational", 33)) >= 0) {
1987  if (!var_size) {
1988  ret = AVERROR_INVALIDDATA;
1989  goto fail;
1990  }
1991 
1992  s->avctx->framerate.num = bytestream2_get_le32(gb);
1993  s->avctx->framerate.den = bytestream2_get_le32(gb);
1994 
1995  continue;
1996  } else if ((var_size = check_header_variable(s, "chunkCount",
1997  "int", 23)) >= 0) {
1998 
1999  s->chunk_count = bytestream2_get_le32(gb);
2000 
2001  continue;
2002  } else if ((var_size = check_header_variable(s, "type",
2003  "string", 16)) >= 0) {
2004  uint8_t key[256] = { 0 };
2005 
2006  bytestream2_get_buffer(gb, key, FFMIN(sizeof(key) - 1, var_size));
2007  if (strncmp("scanlineimage", key, var_size) &&
2008  strncmp("tiledimage", key, var_size)) {
2009  ret = AVERROR_PATCHWELCOME;
2010  goto fail;
2011  }
2012 
2013  continue;
2014  } else if ((var_size = check_header_variable(s, "preview",
2015  "preview", 16)) >= 0) {
2016  uint32_t pw = bytestream2_get_le32(gb);
2017  uint32_t ph = bytestream2_get_le32(gb);
2018  uint64_t psize = pw * (uint64_t)ph;
2019  if (psize > INT64_MAX / 4) {
2020  ret = AVERROR_INVALIDDATA;
2021  goto fail;
2022  }
2023  psize *= 4;
2024 
2025  if ((int64_t)psize >= bytestream2_get_bytes_left(gb)) {
2026  ret = AVERROR_INVALIDDATA;
2027  goto fail;
2028  }
2029 
2030  bytestream2_skip(gb, psize);
2031 
2032  continue;
2033  }
2034 
2035  // Check if there are enough bytes for a header
2036  if (bytestream2_get_bytes_left(gb) <= 9) {
2037  av_log(s->avctx, AV_LOG_ERROR, "Incomplete header\n");
2038  ret = AVERROR_INVALIDDATA;
2039  goto fail;
2040  }
2041 
2042  // Process unknown variables
2043  {
2044  uint8_t name[256] = { 0 };
2045  uint8_t type[256] = { 0 };
2046  uint8_t value[8192] = { 0 };
2047  int i = 0, size;
2048 
2049  while (bytestream2_get_bytes_left(gb) > 0 &&
2050  bytestream2_peek_byte(gb) && i < 255) {
2051  name[i++] = bytestream2_get_byte(gb);
2052  }
2053 
2054  bytestream2_skip(gb, 1);
2055  i = 0;
2056  while (bytestream2_get_bytes_left(gb) > 0 &&
2057  bytestream2_peek_byte(gb) && i < 255) {
2058  type[i++] = bytestream2_get_byte(gb);
2059  }
2060  bytestream2_skip(gb, 1);
2061  size = bytestream2_get_le32(gb);
2062 
2063  bytestream2_get_buffer(gb, value, FFMIN(sizeof(value) - 1, size));
2064  if (size > sizeof(value) - 1)
2065  bytestream2_skip(gb, size - (sizeof(value) - 1));
2066  if (!strcmp(type, "string"))
2067  av_dict_set(&metadata, name, value, 0);
2068  }
2069  }
2070 
2071  if (s->compression == EXR_UNKN) {
2072  av_log(s->avctx, AV_LOG_ERROR, "Missing compression attribute.\n");
2073  ret = AVERROR_INVALIDDATA;
2074  goto fail;
2075  }
2076 
2077  if (s->is_tile) {
2078  if (s->tile_attr.xSize < 1 || s->tile_attr.ySize < 1) {
2079  av_log(s->avctx, AV_LOG_ERROR, "Invalid tile attribute.\n");
2080  ret = AVERROR_INVALIDDATA;
2081  goto fail;
2082  }
2083  }
2084 
2085  if (bytestream2_get_bytes_left(gb) <= 0) {
2086  av_log(s->avctx, AV_LOG_ERROR, "Incomplete frame.\n");
2087  ret = AVERROR_INVALIDDATA;
2088  goto fail;
2089  }
2090 
2091  frame->metadata = metadata;
2092 
2093  // aaand we are done
2094  bytestream2_skip(gb, 1);
2095  return 0;
2096 fail:
2097  av_dict_free(&metadata);
2098  return ret;
2099 }
2100 
2101 static int decode_frame(AVCodecContext *avctx, void *data,
2102  int *got_frame, AVPacket *avpkt)
2103 {
2104  EXRContext *s = avctx->priv_data;
2105  GetByteContext *gb = &s->gb;
2106  ThreadFrame frame = { .f = data };
2107  AVFrame *picture = data;
2108  uint8_t *ptr;
2109 
2110  int i, y, ret, ymax;
2111  int planes;
2112  int out_line_size;
2113  int64_t nb_blocks; /* nb scanline or nb tile */
2114  uint64_t start_offset_table;
2115  uint64_t start_next_scanline;
2116  PutByteContext offset_table_writer;
2117 
2118  bytestream2_init(gb, avpkt->data, avpkt->size);
2119 
2120  if ((ret = decode_header(s, picture)) < 0)
2121  return ret;
2122 
2123  if ((s->compression == EXR_DWAA || s->compression == EXR_DWAB) &&
2124  s->pixel_type == EXR_HALF) {
2125  s->current_channel_offset *= 2;
2126  for (int i = 0; i < 4; i++)
2127  s->channel_offsets[i] *= 2;
2128  }
2129  if (s->compression == EXR_DWAA ||
2130  s->compression == EXR_DWAB) {
2131  for (int i = 0; i<s->nb_channels; i++) {
2132  EXRChannel *channel = &s->channels[i];
2133  if (channel->pixel_type != s->pixel_type) {
2134  avpriv_request_sample(s->avctx, "mixed pixel type DWA");
2135  return AVERROR_PATCHWELCOME;
2136  }
2137  }
2138  }
2139 
2140  switch (s->pixel_type) {
2141  case EXR_FLOAT:
2142  case EXR_HALF:
2143  if (s->channel_offsets[3] >= 0) {
2144  if (!s->is_luma) {
2145  avctx->pix_fmt = AV_PIX_FMT_GBRAPF32;
2146  } else {
2147  /* todo: change this when a floating point pixel format with luma with alpha is implemented */
2148  avctx->pix_fmt = AV_PIX_FMT_GBRAPF32;
2149  }
2150  } else {
2151  if (!s->is_luma) {
2152  avctx->pix_fmt = AV_PIX_FMT_GBRPF32;
2153  } else {
2154  avctx->pix_fmt = AV_PIX_FMT_GRAYF32;
2155  }
2156  }
2157  break;
2158  case EXR_UINT:
2159  if (s->channel_offsets[3] >= 0) {
2160  if (!s->is_luma) {
2161  avctx->pix_fmt = AV_PIX_FMT_RGBA64;
2162  } else {
2163  avctx->pix_fmt = AV_PIX_FMT_YA16;
2164  }
2165  } else {
2166  if (!s->is_luma) {
2167  avctx->pix_fmt = AV_PIX_FMT_RGB48;
2168  } else {
2169  avctx->pix_fmt = AV_PIX_FMT_GRAY16;
2170  }
2171  }
2172  break;
2173  default:
2174  av_log(avctx, AV_LOG_ERROR, "Missing channel list.\n");
2175  return AVERROR_INVALIDDATA;
2176  }
2177 
2178  if (s->apply_trc_type != AVCOL_TRC_UNSPECIFIED)
2179  avctx->color_trc = s->apply_trc_type;
2180 
2181  switch (s->compression) {
2182  case EXR_RAW:
2183  case EXR_RLE:
2184  case EXR_ZIP1:
2185  s->scan_lines_per_block = 1;
2186  break;
2187  case EXR_PXR24:
2188  case EXR_ZIP16:
2189  s->scan_lines_per_block = 16;
2190  break;
2191  case EXR_PIZ:
2192  case EXR_B44:
2193  case EXR_B44A:
2194  case EXR_DWAA:
2195  s->scan_lines_per_block = 32;
2196  break;
2197  case EXR_DWAB:
2198  s->scan_lines_per_block = 256;
2199  break;
2200  default:
2201  avpriv_report_missing_feature(avctx, "Compression %d", s->compression);
2202  return AVERROR_PATCHWELCOME;
2203  }
2204 
2205  /* Verify the xmin, xmax, ymin and ymax before setting the actual image size.
2206  * It's possible for the data window can larger or outside the display window */
2207  if (s->xmin > s->xmax || s->ymin > s->ymax ||
2208  s->ydelta == 0xFFFFFFFF || s->xdelta == 0xFFFFFFFF) {
2209  av_log(avctx, AV_LOG_ERROR, "Wrong or missing size information.\n");
2210  return AVERROR_INVALIDDATA;
2211  }
2212 
2213  if ((ret = ff_set_dimensions(avctx, s->w, s->h)) < 0)
2214  return ret;
2215 
2216  ff_set_sar(s->avctx, av_d2q(av_int2float(s->sar), 255));
2217 
2218  s->desc = av_pix_fmt_desc_get(avctx->pix_fmt);
2219  if (!s->desc)
2220  return AVERROR_INVALIDDATA;
2221 
2222  if (s->desc->flags & AV_PIX_FMT_FLAG_FLOAT) {
2223  planes = s->desc->nb_components;
2224  out_line_size = avctx->width * 4;
2225  } else {
2226  planes = 1;
2227  out_line_size = avctx->width * 2 * s->desc->nb_components;
2228  }
2229 
2230  if (s->is_tile) {
2231  if (s->tile_attr.ySize <= 0 || s->tile_attr.xSize <= 0)
2232  return AVERROR_INVALIDDATA;
2233  nb_blocks = ((s->xdelta + s->tile_attr.xSize - 1) / s->tile_attr.xSize) *
2234  (int64_t)((s->ydelta + s->tile_attr.ySize - 1) / s->tile_attr.ySize);
2235  } else { /* scanline */
2236  nb_blocks = (s->ydelta + s->scan_lines_per_block - 1) /
2237  s->scan_lines_per_block;
2238  }
2239 
2240  if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
2241  return ret;
2242 
2243  if (bytestream2_get_bytes_left(gb)/8 < nb_blocks)
2244  return AVERROR_INVALIDDATA;
2245 
2246  if (avctx->max_pixels) {
2247  int64_t block_pixels = s->is_tile
2248  ? (int64_t)FFMIN(s->tile_attr.xSize, s->xdelta) *
2249  FFMIN(s->tile_attr.ySize, s->ydelta)
2250  : (int64_t)s->xdelta *
2251  FFMIN(s->scan_lines_per_block, s->ydelta);
2252  if (nb_blocks > avctx->max_pixels / FFMAX(block_pixels, 1))
2253  return AVERROR_INVALIDDATA;
2254  }
2255 
2256  // check offset table and recreate it if need
2257  if (!s->is_tile && bytestream2_peek_le64(gb) == 0) {
2258  av_log(s->avctx, AV_LOG_DEBUG, "recreating invalid scanline offset table\n");
2259 
2260  start_offset_table = bytestream2_tell(gb);
2261  start_next_scanline = start_offset_table + nb_blocks * 8;
2262  bytestream2_init_writer(&offset_table_writer, &avpkt->data[start_offset_table], nb_blocks * 8);
2263 
2264  for (y = 0; y < nb_blocks; y++) {
2265  /* write offset of prev scanline in offset table */
2266  bytestream2_put_le64(&offset_table_writer, start_next_scanline);
2267 
2268  /* get len of next scanline */
2269  bytestream2_seek(gb, start_next_scanline + 4, SEEK_SET);/* skip line number */
2270  start_next_scanline += (bytestream2_get_le32(gb) + 8);
2271  }
2272  bytestream2_seek(gb, start_offset_table, SEEK_SET);
2273  }
2274 
2275  // save pointer we are going to use in decode_block
2276  s->buf = avpkt->data;
2277  s->buf_size = avpkt->size;
2278 
2279  // Zero out the start if ymin is not 0
2280  for (i = 0; i < planes; i++) {
2281  ptr = picture->data[i];
2282  for (y = 0; y < FFMIN(s->ymin, s->h); y++) {
2283  memset(ptr, 0, out_line_size);
2284  ptr += picture->linesize[i];
2285  }
2286  }
2287 
2288  s->picture = picture;
2289 
2290  avctx->execute2(avctx, decode_block, s->thread_data, NULL, nb_blocks);
2291 
2292  ymax = FFMAX(0, s->ymax + 1);
2293  // Zero out the end if ymax+1 is not h
2294  if (ymax < avctx->height)
2295  for (i = 0; i < planes; i++) {
2296  ptr = picture->data[i] + (ymax * picture->linesize[i]);
2297  for (y = ymax; y < avctx->height; y++) {
2298  memset(ptr, 0, out_line_size);
2299  ptr += picture->linesize[i];
2300  }
2301  }
2302 
2303  picture->pict_type = AV_PICTURE_TYPE_I;
2304  *got_frame = 1;
2305 
2306  return avpkt->size;
2307 }
2308 
2310 {
2311  EXRContext *s = avctx->priv_data;
2312  uint32_t i;
2313  union av_intfloat32 t;
2314  float one_gamma = 1.0f / s->gamma;
2315  avpriv_trc_function trc_func = NULL;
2316 
2317  half2float_table(s->mantissatable, s->exponenttable, s->offsettable);
2318 
2319  s->avctx = avctx;
2320 
2321  ff_exrdsp_init(&s->dsp);
2322 
2323 #if HAVE_BIGENDIAN
2324  ff_bswapdsp_init(&s->bbdsp);
2325 #endif
2326 
2327  trc_func = avpriv_get_trc_function_from_trc(s->apply_trc_type);
2328  if (trc_func) {
2329  for (i = 0; i < 65536; ++i) {
2330  t.i = half2float(i, s->mantissatable, s->exponenttable, s->offsettable);
2331  t.f = trc_func(t.f);
2332  s->gamma_table[i] = t;
2333  }
2334  } else {
2335  if (one_gamma > 0.9999f && one_gamma < 1.0001f) {
2336  for (i = 0; i < 65536; ++i) {
2337  s->gamma_table[i].i = half2float(i, s->mantissatable, s->exponenttable, s->offsettable);
2338  }
2339  } else {
2340  for (i = 0; i < 65536; ++i) {
2341  t.i = half2float(i, s->mantissatable, s->exponenttable, s->offsettable);
2342  /* If negative value we reuse half value */
2343  if (t.f <= 0.0f) {
2344  s->gamma_table[i] = t;
2345  } else {
2346  t.f = powf(t.f, one_gamma);
2347  s->gamma_table[i] = t;
2348  }
2349  }
2350  }
2351  }
2352 
2353  // allocate thread data, used for non EXR_RAW compression types
2354  s->thread_data = av_mallocz_array(avctx->thread_count, sizeof(EXRThreadData));
2355  if (!s->thread_data)
2356  return AVERROR(ENOMEM);
2357 
2358  return 0;
2359 }
2360 
2362 {
2363  EXRContext *s = avctx->priv_data;
2364  int i;
2365  for (i = 0; i < avctx->thread_count; i++) {
2366  EXRThreadData *td = &s->thread_data[i];
2367  av_freep(&td->uncompressed_data);
2368  av_freep(&td->tmp);
2369  av_freep(&td->bitmap);
2370  av_freep(&td->lut);
2371  av_freep(&td->he);
2372  av_freep(&td->freq);
2373  av_freep(&td->ac_data);
2374  av_freep(&td->dc_data);
2375  av_freep(&td->rle_data);
2376  av_freep(&td->rle_raw_data);
2377  ff_free_vlc(&td->vlc);
2378  }
2379 
2380  av_freep(&s->thread_data);
2381  av_freep(&s->channels);
2382 
2383  return 0;
2384 }
2385 
2386 #define OFFSET(x) offsetof(EXRContext, x)
2387 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
2388 static const AVOption options[] = {
2389  { "layer", "Set the decoding layer", OFFSET(layer),
2390  AV_OPT_TYPE_STRING, { .str = "" }, 0, 0, VD },
2391  { "part", "Set the decoding part", OFFSET(selected_part),
2392  AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VD },
2393  { "gamma", "Set the float gamma value when decoding", OFFSET(gamma),
2394  AV_OPT_TYPE_FLOAT, { .dbl = 1.0f }, 0.001, FLT_MAX, VD },
2395 
2396  // XXX: Note the abuse of the enum using AVCOL_TRC_UNSPECIFIED to subsume the existing gamma option
2397  { "apply_trc", "color transfer characteristics to apply to EXR linear input", OFFSET(apply_trc_type),
2398  AV_OPT_TYPE_INT, {.i64 = AVCOL_TRC_UNSPECIFIED }, 1, AVCOL_TRC_NB-1, VD, "apply_trc_type"},
2399  { "bt709", "BT.709", 0,
2400  AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT709 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
2401  { "gamma", "gamma", 0,
2402  AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_UNSPECIFIED }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
2403  { "gamma22", "BT.470 M", 0,
2404  AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_GAMMA22 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
2405  { "gamma28", "BT.470 BG", 0,
2406  AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_GAMMA28 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
2407  { "smpte170m", "SMPTE 170 M", 0,
2408  AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTE170M }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
2409  { "smpte240m", "SMPTE 240 M", 0,
2410  AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTE240M }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
2411  { "linear", "Linear", 0,
2412  AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LINEAR }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
2413  { "log", "Log", 0,
2414  AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LOG }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
2415  { "log_sqrt", "Log square root", 0,
2416  AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LOG_SQRT }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
2417  { "iec61966_2_4", "IEC 61966-2-4", 0,
2418  AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_IEC61966_2_4 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
2419  { "bt1361", "BT.1361", 0,
2420  AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT1361_ECG }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
2421  { "iec61966_2_1", "IEC 61966-2-1", 0,
2422  AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_IEC61966_2_1 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
2423  { "bt2020_10bit", "BT.2020 - 10 bit", 0,
2424  AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT2020_10 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
2425  { "bt2020_12bit", "BT.2020 - 12 bit", 0,
2426  AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT2020_12 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
2427  { "smpte2084", "SMPTE ST 2084", 0,
2428  AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTEST2084 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
2429  { "smpte428_1", "SMPTE ST 428-1", 0,
2430  AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTEST428_1 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
2431 
2432  { NULL },
2433 };
2434 
2435 static const AVClass exr_class = {
2436  .class_name = "EXR",
2437  .item_name = av_default_item_name,
2438  .option = options,
2439  .version = LIBAVUTIL_VERSION_INT,
2440 };
2441 
2443  .name = "exr",
2444  .long_name = NULL_IF_CONFIG_SMALL("OpenEXR image"),
2445  .type = AVMEDIA_TYPE_VIDEO,
2446  .id = AV_CODEC_ID_EXR,
2447  .priv_data_size = sizeof(EXRContext),
2448  .init = decode_init,
2449  .close = decode_end,
2450  .decode = decode_frame,
2451  .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS |
2453  .priv_class = &exr_class,
2454 };
static double val(void *priv, double ch)
Definition: aeval.c:76
#define av_cold
Definition: attributes.h:88
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> dc
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
uint8_t
int32_t
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
Libavcodec external API header.
#define AV_RL64
Definition: intreadwrite.h:173
#define AV_RL32
Definition: intreadwrite.h:146
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
int ff_init_vlc_sparse(VLC *vlc_arg, int nb_bits, int nb_codes, const void *bits, int bits_wrap, int bits_size, const void *codes, int codes_wrap, int codes_size, const void *symbols, int symbols_wrap, int symbols_size, int flags)
Definition: bitstream.c:323
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:431
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:267
static av_always_inline void bytestream2_init_writer(PutByteContext *p, uint8_t *buf, int buf_size)
Definition: bytestream.h:147
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:158
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:168
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
Definition: bytestream.h:212
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:192
#define bytestream2_get_ne16
Definition: bytestream.h:119
#define flags(name, subs,...)
Definition: cbs_av1.c:572
#define u(width, name, range_min, range_max)
Definition: cbs_h2645.c:274
#define ub(width, name)
Definition: cbs_h2645.c:276
#define s(width, name)
Definition: cbs_vp9.c:257
#define f(width, name)
Definition: cbs_vp9.c:255
#define fail()
Definition: checkasm.h:133
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
double(* avpriv_trc_function)(double)
Definition: color_utils.h:40
common internal and external API header
#define FFMIN(a, b)
Definition: common.h:105
#define FFMAX(a, b)
Definition: common.h:103
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
#define FFSIGN(a)
Definition: common.h:73
#define FFMIN3(a, b, c)
Definition: common.h:106
#define NULL
Definition: coverity.c:32
long long int64_t
Definition: coverity.c:34
static __device__ float fabsf(float a)
Definition: cuda_runtime.h:181
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
static AVFrame * frame
channel
Use these values when setting the channel map with ebur128_set_channel().
Definition: ebur128.h:39
double value
Definition: eval.c:100
ExrCompr
Definition: exr.c:59
@ EXR_UNKN
Definition: exr.c:70
@ EXR_B44A
Definition: exr.c:67
@ EXR_DWAB
Definition: exr.c:69
@ EXR_ZIP16
Definition: exr.c:63
@ EXR_DWAA
Definition: exr.c:68
@ EXR_PIZ
Definition: exr.c:64
@ EXR_RLE
Definition: exr.c:61
@ EXR_B44
Definition: exr.c:66
@ EXR_PXR24
Definition: exr.c:65
@ EXR_ZIP1
Definition: exr.c:62
@ EXR_RAW
Definition: exr.c:60
static void idct_1d(float *blk, int step)
Definition: exr.c:923
static int huf_unpack_enc_table(GetByteContext *gb, int32_t im, int32_t iM, uint64_t *freq)
Definition: exr.c:332
ExrTileLevelMode
Definition: exr.c:80
@ EXR_TILE_LEVEL_UNKNOWN
Definition: exr.c:84
@ EXR_TILE_LEVEL_ONE
Definition: exr.c:81
@ EXR_TILE_LEVEL_MIPMAP
Definition: exr.c:82
@ EXR_TILE_LEVEL_RIPMAP
Definition: exr.c:83
static int huf_build_dec_table(EXRContext *s, EXRThreadData *td, int im, int iM)
Definition: exr.c:375
static void wdec16(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
Definition: exr.c:509
#define USHORT_RANGE
Definition: exr.c:275
static const AVOption options[]
Definition: exr.c:2388
#define MOD_MASK
Definition: exr.c:507
AVCodec ff_exr_decoder
Definition: exr.c:2442
static uint16_t reverse_lut(const uint8_t *bitmap, uint16_t *lut)
Definition: exr.c:278
#define LONG_ZEROCODE_RUN
Definition: exr.c:328
static float to_linear(float x, float scale)
Definition: exr.c:986
#define SHORT_ZEROCODE_RUN
Definition: exr.c:327
static av_cold int decode_init(AVCodecContext *avctx)
Definition: exr.c:2309
static const AVClass exr_class
Definition: exr.c:2435
static int decode_header(EXRContext *s, AVFrame *frame)
Definition: exr.c:1589
#define M(chr)
Definition: exr.c:175
static int dwa_uncompress(EXRContext *s, const uint8_t *src, int compressed_size, int uncompressed_size, EXRThreadData *td)
Definition: exr.c:999
#define HUF_ENCSIZE
Definition: exr.c:302
static int b44_uncompress(EXRContext *s, const uint8_t *src, int compressed_size, int uncompressed_size, EXRThreadData *td)
Definition: exr.c:822
static void unpack_14(const uint8_t b[14], uint16_t s[16])
Definition: exr.c:771
static av_cold int decode_end(AVCodecContext *avctx)
Definition: exr.c:2361
static int piz_uncompress(EXRContext *s, const uint8_t *src, int ssize, int dsize, EXRThreadData *td)
Definition: exr.c:598
static int zip_uncompress(EXRContext *s, const uint8_t *src, int compressed_size, int uncompressed_size, EXRThreadData *td)
Definition: exr.c:201
ExrTileLevelRound
Definition: exr.c:87
@ EXR_TILE_ROUND_DOWN
Definition: exr.c:89
@ EXR_TILE_ROUND_UP
Definition: exr.c:88
@ EXR_TILE_ROUND_UNKNOWN
Definition: exr.c:90
static void skip_header_chunk(EXRContext *s)
Definition: exr.c:1530
static void dct_inverse(float *block)
Definition: exr.c:967
ExrPixelType
Definition: exr.c:73
@ EXR_UINT
Definition: exr.c:74
@ EXR_HALF
Definition: exr.c:75
@ EXR_UNKNOWN
Definition: exr.c:77
@ EXR_FLOAT
Definition: exr.c:76
static void apply_lut(const uint16_t *lut, uint16_t *dst, int dsize)
Definition: exr.c:293
static int huf_decode(VLC *vlc, GetByteContext *gb, int nbits, int run_sym, int no, uint16_t *out)
Definition: exr.c:421
static void convert(float y, float u, float v, float *b, float *g, float *r)
Definition: exr.c:978
#define BITMAP_SIZE
Definition: exr.c:276
static void unpack_3(const uint8_t b[3], uint16_t s[16])
Definition: exr.c:806
#define VD
Definition: exr.c:2387
static void huf_canonical_code_table(uint64_t *freq)
Definition: exr.c:304
#define SHORTEST_LONG_RUN
Definition: exr.c:329
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: exr.c:2101
static int huf_uncompress(EXRContext *s, EXRThreadData *td, GetByteContext *gb, uint16_t *dst, int dst_size)
Definition: exr.c:450
static int ac_uncompress(EXRContext *s, GetByteContext *gb, float *block)
Definition: exr.c:899
#define OFFSET(x)
Definition: exr.c:2386
static void wav_decode(uint16_t *in, int nx, int ox, int ny, int oy, uint16_t mx)
Definition: exr.c:519
static int rle_uncompress(EXRContext *ctx, const uint8_t *src, int compressed_size, int uncompressed_size, EXRThreadData *td)
Definition: exr.c:260
#define A_OFFSET
Definition: exr.c:506
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:1559
static void wdec14(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
Definition: exr.c:492
static int decode_block(AVCodecContext *avctx, void *tdata, int jobnr, int threadnr)
Definition: exr.c:1219
static int pxr24_uncompress(EXRContext *s, const uint8_t *src, int compressed_size, int uncompressed_size, EXRThreadData *td)
Definition: exr.c:687
static int rle(uint8_t *dst, const uint8_t *src, int compressed_size, int uncompressed_size)
Definition: exr.c:218
float im
Definition: fft.c:82
bitstream reader API header.
static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE(*table)[2], int bits, int max_depth)
Parse a vlc code.
Definition: get_bits.h:797
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:849
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:677
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:219
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:659
@ AV_OPT_TYPE_CONST
Definition: opt.h:234
@ AV_OPT_TYPE_INT
Definition: opt.h:225
@ AV_OPT_TYPE_FLOAT
Definition: opt.h:228
@ AV_OPT_TYPE_STRING
Definition: opt.h:229
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition: codec.h:112
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: codec.h:108
@ AV_CODEC_ID_EXR
Definition: codec_id.h:229
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:50
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition: dict.c:199
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
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:215
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
#define AV_LOG_INFO
Standard information.
Definition: log.h:205
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
AVRational av_d2q(double d, int max)
Convert a double precision floating point number to a rational.
Definition: rational.c:110
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Allocate, reallocate, or free an array.
Definition: mem.c:198
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:245
void * av_mallocz_array(size_t nmemb, size_t size)
Allocate a memory block for an array with av_mallocz().
Definition: mem.c:190
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
int av_image_check_size2(unsigned int w, unsigned int h, int64_t max_pixels, enum AVPixelFormat pix_fmt, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of a plane of an image with...
Definition: imgutils.c:291
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
Definition: avstring.c:215
static av_const int av_toupper(int c)
Locale-independent conversion of ASCII characters to uppercase.
Definition: avstring.h:236
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
for(j=16;j >0;--j)
static void half2float_table(uint32_t *mantissatable, uint32_t *exponenttable, uint16_t *offsettable)
Definition: half2float.h:40
static uint32_t half2float(uint16_t h, uint32_t *mantissatable, uint32_t *exponenttable, uint16_t *offsettable)
Definition: half2float.h:64
cl_device_type type
const char * key
static const int16_t alpha[]
Definition: ilbcdata.h:55
misc image utilities
int i
Definition: input.c:407
static av_always_inline float av_int2float(uint32_t i)
Reinterpret a 32-bit integer as a float.
Definition: intfloat.h:40
av_cold void ff_bswapdsp_init(BswapDSPContext *c)
Definition: bswapdsp.c:49
av_cold void ff_exrdsp_init(ExrDSPContext *c)
Definition: exrdsp.c:49
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:99
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:84
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
version
Definition: libkvazaar.c:326
#define cosf(x)
Definition: libm.h:78
#define expf(x)
Definition: libm.h:283
#define powf(x, y)
Definition: libm.h:50
static const struct @323 planes[]
#define FFALIGN(x, a)
Definition: macros.h:48
#define M_PI
Definition: mathematics.h:52
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:98
const char data[16]
Definition: mxf.c:142
AVOptions.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2573
#define AV_PIX_FMT_FLAG_FLOAT
The pixel format contains IEEE-754 floating point values.
Definition: pixdesc.h:190
#define AV_PIX_FMT_FLAG_PLANAR
At least one pixel component is not in the first data plane.
Definition: pixdesc.h:144
#define AV_PIX_FMT_GBRPF32
Definition: pixfmt.h:428
#define AV_PIX_FMT_YA16
Definition: pixfmt.h:384
#define AV_PIX_FMT_RGBA64
Definition: pixfmt.h:389
#define AV_PIX_FMT_RGB48
Definition: pixfmt.h:385
#define AV_PIX_FMT_GRAYF32
Definition: pixfmt.h:431
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:383
AVColorTransferCharacteristic
Color Transfer Characteristic.
Definition: pixfmt.h:483
@ AVCOL_TRC_SMPTE170M
also ITU-R BT601-6 525 or 625 / ITU-R BT1358 525 or 625 / ITU-R BT1700 NTSC
Definition: pixfmt.h:490
@ AVCOL_TRC_SMPTEST428_1
Definition: pixfmt.h:503
@ AVCOL_TRC_SMPTEST2084
Definition: pixfmt.h:501
@ AVCOL_TRC_GAMMA22
also ITU-R BT470M / ITU-R BT1700 625 PAL & SECAM
Definition: pixfmt.h:488
@ AVCOL_TRC_BT1361_ECG
ITU-R BT1361 Extended Colour Gamut.
Definition: pixfmt.h:496
@ AVCOL_TRC_SMPTE240M
Definition: pixfmt.h:491
@ AVCOL_TRC_LOG
"Logarithmic transfer characteristic (100:1 range)"
Definition: pixfmt.h:493
@ AVCOL_TRC_IEC61966_2_4
IEC 61966-2-4.
Definition: pixfmt.h:495
@ AVCOL_TRC_LINEAR
"Linear transfer characteristics"
Definition: pixfmt.h:492
@ AVCOL_TRC_GAMMA28
also ITU-R BT470BG
Definition: pixfmt.h:489
@ AVCOL_TRC_LOG_SQRT
"Logarithmic transfer characteristic (100 * Sqrt(10) : 1 range)"
Definition: pixfmt.h:494
@ AVCOL_TRC_BT2020_12
ITU-R BT2020 for 12-bit system.
Definition: pixfmt.h:499
@ AVCOL_TRC_IEC61966_2_1
IEC 61966-2-1 (sRGB or sYCC)
Definition: pixfmt.h:497
@ AVCOL_TRC_BT2020_10
ITU-R BT2020 for 10-bit system.
Definition: pixfmt.h:498
@ AVCOL_TRC_UNSPECIFIED
Definition: pixfmt.h:486
@ AVCOL_TRC_BT709
also ITU-R BT1361
Definition: pixfmt.h:485
@ AVCOL_TRC_NB
Not part of ABI.
Definition: pixfmt.h:505
#define AV_PIX_FMT_GBRAPF32
Definition: pixfmt.h:429
FF_ENABLE_DEPRECATION_WARNINGS int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
const char * name
Definition: qsvenc.c:46
#define td
Definition: regdef.h:70
#define blk(i)
Definition: sha.c:185
static int shift(int a, int b)
Definition: sonic.c:82
Describe the class of an AVClass context structure.
Definition: log.h:67
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
main external API structure.
Definition: avcodec.h:536
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:746
int width
picture width / height.
Definition: avcodec.h:709
int64_t max_pixels
The number of pixels per image to maximally accept.
Definition: avcodec.h:2252
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
Definition: avcodec.h:1777
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:1157
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:1848
void * priv_data
Definition: avcodec.h:563
AVCodec.
Definition: codec.h:197
const char * name
Name of the codec implementation.
Definition: codec.h:204
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
AVDictionary * metadata
metadata.
Definition: frame.h:604
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:349
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:401
AVOption.
Definition: opt.h:248
This structure stores compressed data.
Definition: packet.h:346
int size
Definition: packet.h:370
uint8_t * data
Definition: packet.h:369
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
Definition: exr.c:99
int xsub
Definition: exr.c:100
int ysub
Definition: exr.c:100
enum ExrPixelType pixel_type
Definition: exr.c:101
int h
Definition: exr.c:160
int32_t xmin
Definition: exr.c:162
GetByteContext gb
Definition: exr.c:178
enum AVColorTransferCharacteristic apply_trc_type
Definition: exr.c:192
uint32_t xdelta
Definition: exr.c:164
int32_t ymin
Definition: exr.c:163
uint32_t sar
Definition: exr.c:161
const char * layer
Definition: exr.c:189
int is_luma
Definition: exr.c:173
float gamma
Definition: exr.c:193
int scan_lines_per_block
Definition: exr.c:166
EXRTileAttribute tile_attr
Definition: exr.c:168
int current_channel_offset
Definition: exr.c:184
uint32_t ydelta
Definition: exr.c:164
EXRThreadData * thread_data
Definition: exr.c:187
int selected_part
Definition: exr.c:190
uint32_t exponenttable[64]
Definition: exr.c:197
ExrDSPContext dsp
Definition: exr.c:149
int32_t ymax
Definition: exr.c:163
int nb_channels
Definition: exr.c:183
AVFrame * picture
Definition: exr.c:147
int current_part
Definition: exr.c:171
int w
Definition: exr.c:160
AVCodecContext * avctx
Definition: exr.c:148
uint16_t offsettable[64]
Definition: exr.c:198
int channel_offsets[4]
Definition: exr.c:157
int has_channel
combination of flags representing the channel codes A-Z
Definition: exr.c:176
int is_tile
Definition: exr.c:169
enum ExrCompr compression
Definition: exr.c:155
int is_multipart
Definition: exr.c:170
union av_intfloat32 gamma_table[65536]
Definition: exr.c:194
uint32_t chunk_count
Definition: exr.c:185
int32_t xmax
Definition: exr.c:162
int buf_size
Definition: exr.c:180
enum ExrPixelType pixel_type
Definition: exr.c:156
EXRChannel * channels
Definition: exr.c:182
uint32_t mantissatable[2048]
Definition: exr.c:196
const AVPixFmtDescriptor * desc
Definition: exr.c:158
const uint8_t * buf
Definition: exr.c:179
int tmp_size
Definition: exr.c:116
uint8_t * dc_data
Definition: exr.c:124
unsigned rle_size
Definition: exr.c:128
int run_sym
Definition: exr.c:139
uint8_t * ac_data
Definition: exr.c:121
HuffEntry * he
Definition: exr.c:140
uint16_t * lut
Definition: exr.c:119
uint8_t * rle_data
Definition: exr.c:127
uint8_t * rle_raw_data
Definition: exr.c:130
unsigned dc_size
Definition: exr.c:125
uint64_t * freq
Definition: exr.c:141
int channel_line_size
Definition: exr.c:137
uint8_t * bitmap
Definition: exr.c:118
unsigned ac_size
Definition: exr.c:122
unsigned rle_raw_size
Definition: exr.c:131
int uncompressed_size
Definition: exr.c:113
int xsize
Definition: exr.c:135
float block[3][64]
Definition: exr.c:133
uint8_t * uncompressed_data
Definition: exr.c:112
int ysize
Definition: exr.c:135
uint8_t * tmp
Definition: exr.c:115
VLC vlc
Definition: exr.c:142
int32_t ySize
Definition: exr.c:106
enum ExrTileLevelRound level_round
Definition: exr.c:108
enum ExrTileLevelMode level_mode
Definition: exr.c:107
int32_t xSize
Definition: exr.c:105
const uint8_t * buffer
Definition: bytestream.h:34
Definition: exr.c:93
uint8_t len
Definition: exr.c:94
uint32_t code
Definition: exr.c:96
uint16_t sym
Definition: exr.c:95
Definition: vlc.h:26
VLC_TYPE(* table)[2]
code, bits
Definition: vlc.h:28
Definition: graph2dot.c:48
Definition: rpzaenc.c:58
uint8_t run
Definition: svq3.c:205
#define av_malloc_array(a, b)
#define avpriv_request_sample(...)
#define av_freep(p)
#define av_malloc(s)
#define av_log(a,...)
static uint8_t tmp[11]
Definition: aes_ctr.c:27
#define src
Definition: vp8dsp.c:255
static int16_t block[64]
Definition: dct.c:116
FILE * out
Definition: movenc.c:54
AVFormatContext * ctx
Definition: movenc.c:48
#define height
uint8_t pixel
Definition: tiny_ssim.c:42
int size
uint32_t i
Definition: intfloat.h:28
const char * b
Definition: vf_curves.c:119
const char * g
Definition: vf_curves.c:118
const char * r
Definition: vf_curves.c:117
if(ret< 0)
Definition: vf_mcdeint.c:282
static av_always_inline int diff(const uint32_t a, const uint32_t b)
static double c[64]