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