You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1869 lines
60KB

  1. /*
  2. * OpenEXR (.exr) image decoder
  3. * Copyright (c) 2006 Industrial Light & Magic, a division of Lucas Digital Ltd. LLC
  4. * Copyright (c) 2009 Jimmy Christensen
  5. *
  6. * B44/B44A, Tile added by Jokyo Images support by CNC - French National Center for Cinema
  7. *
  8. * This file is part of FFmpeg.
  9. *
  10. * FFmpeg is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * FFmpeg is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with FFmpeg; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. /**
  25. * @file
  26. * OpenEXR decoder
  27. * @author Jimmy Christensen
  28. *
  29. * For more information on the OpenEXR format, visit:
  30. * http://openexr.com/
  31. *
  32. * exr_flt2uint() and exr_halflt2uint() is credited to Reimar Döffinger.
  33. * exr_half2float() is credited to Aaftab Munshi, Dan Ginsburg, Dave Shreiner.
  34. */
  35. #include <float.h>
  36. #include <zlib.h>
  37. #include "libavutil/common.h"
  38. #include "libavutil/imgutils.h"
  39. #include "libavutil/intfloat.h"
  40. #include "libavutil/opt.h"
  41. #include "libavutil/color_utils.h"
  42. #include "avcodec.h"
  43. #include "bytestream.h"
  44. #include "get_bits.h"
  45. #include "internal.h"
  46. #include "mathops.h"
  47. #include "thread.h"
  48. enum ExrCompr {
  49. EXR_RAW,
  50. EXR_RLE,
  51. EXR_ZIP1,
  52. EXR_ZIP16,
  53. EXR_PIZ,
  54. EXR_PXR24,
  55. EXR_B44,
  56. EXR_B44A,
  57. EXR_UNKN,
  58. };
  59. enum ExrPixelType {
  60. EXR_UINT,
  61. EXR_HALF,
  62. EXR_FLOAT,
  63. EXR_UNKNOWN,
  64. };
  65. enum ExrTileLevelMode {
  66. EXR_TILE_LEVEL_ONE,
  67. EXR_TILE_LEVEL_MIPMAP,
  68. EXR_TILE_LEVEL_RIPMAP,
  69. EXR_TILE_LEVEL_UNKNOWN,
  70. };
  71. enum ExrTileLevelRound {
  72. EXR_TILE_ROUND_UP,
  73. EXR_TILE_ROUND_DOWN,
  74. EXR_TILE_ROUND_UNKNOWN,
  75. };
  76. typedef struct EXRChannel {
  77. int xsub, ysub;
  78. enum ExrPixelType pixel_type;
  79. } EXRChannel;
  80. typedef struct EXRTileAttribute {
  81. int32_t xSize;
  82. int32_t ySize;
  83. enum ExrTileLevelMode level_mode;
  84. enum ExrTileLevelRound level_round;
  85. } EXRTileAttribute;
  86. typedef struct EXRThreadData {
  87. uint8_t *uncompressed_data;
  88. int uncompressed_size;
  89. uint8_t *tmp;
  90. int tmp_size;
  91. uint8_t *bitmap;
  92. uint16_t *lut;
  93. int ysize, xsize;
  94. int channel_line_size;
  95. } EXRThreadData;
  96. typedef struct EXRContext {
  97. AVClass *class;
  98. AVFrame *picture;
  99. AVCodecContext *avctx;
  100. enum ExrCompr compression;
  101. enum ExrPixelType pixel_type;
  102. int channel_offsets[4]; // 0 = red, 1 = green, 2 = blue and 3 = alpha
  103. const AVPixFmtDescriptor *desc;
  104. int w, h;
  105. uint32_t xmax, xmin;
  106. uint32_t ymax, ymin;
  107. uint32_t xdelta, ydelta;
  108. int scan_lines_per_block;
  109. EXRTileAttribute tile_attr; /* header data attribute of tile */
  110. int is_tile; /* 0 if scanline, 1 if tile */
  111. int is_luma;/* 1 if there is an Y plane */
  112. GetByteContext gb;
  113. const uint8_t *buf;
  114. int buf_size;
  115. EXRChannel *channels;
  116. int nb_channels;
  117. int current_channel_offset;
  118. EXRThreadData *thread_data;
  119. const char *layer;
  120. enum AVColorTransferCharacteristic apply_trc_type;
  121. float gamma;
  122. uint16_t gamma_table[65536];
  123. } EXRContext;
  124. /* -15 stored using a single precision bias of 127 */
  125. #define HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP 0x38000000
  126. /* max exponent value in single precision that will be converted
  127. * to Inf or Nan when stored as a half-float */
  128. #define HALF_FLOAT_MAX_BIASED_EXP_AS_SINGLE_FP_EXP 0x47800000
  129. /* 255 is the max exponent biased value */
  130. #define FLOAT_MAX_BIASED_EXP (0xFF << 23)
  131. #define HALF_FLOAT_MAX_BIASED_EXP (0x1F << 10)
  132. /**
  133. * Convert a half float as a uint16_t into a full float.
  134. *
  135. * @param hf half float as uint16_t
  136. *
  137. * @return float value
  138. */
  139. static union av_intfloat32 exr_half2float(uint16_t hf)
  140. {
  141. unsigned int sign = (unsigned int) (hf >> 15);
  142. unsigned int mantissa = (unsigned int) (hf & ((1 << 10) - 1));
  143. unsigned int exp = (unsigned int) (hf & HALF_FLOAT_MAX_BIASED_EXP);
  144. union av_intfloat32 f;
  145. if (exp == HALF_FLOAT_MAX_BIASED_EXP) {
  146. // we have a half-float NaN or Inf
  147. // half-float NaNs will be converted to a single precision NaN
  148. // half-float Infs will be converted to a single precision Inf
  149. exp = FLOAT_MAX_BIASED_EXP;
  150. if (mantissa)
  151. mantissa = (1 << 23) - 1; // set all bits to indicate a NaN
  152. } else if (exp == 0x0) {
  153. // convert half-float zero/denorm to single precision value
  154. if (mantissa) {
  155. mantissa <<= 1;
  156. exp = HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP;
  157. // check for leading 1 in denorm mantissa
  158. while ((mantissa & (1 << 10))) {
  159. // for every leading 0, decrement single precision exponent by 1
  160. // and shift half-float mantissa value to the left
  161. mantissa <<= 1;
  162. exp -= (1 << 23);
  163. }
  164. // clamp the mantissa to 10 bits
  165. mantissa &= ((1 << 10) - 1);
  166. // shift left to generate single-precision mantissa of 23 bits
  167. mantissa <<= 13;
  168. }
  169. } else {
  170. // shift left to generate single-precision mantissa of 23 bits
  171. mantissa <<= 13;
  172. // generate single precision biased exponent value
  173. exp = (exp << 13) + HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP;
  174. }
  175. f.i = (sign << 31) | exp | mantissa;
  176. return f;
  177. }
  178. /**
  179. * Convert from 32-bit float as uint32_t to uint16_t.
  180. *
  181. * @param v 32-bit float
  182. *
  183. * @return normalized 16-bit unsigned int
  184. */
  185. static inline uint16_t exr_flt2uint(int32_t v)
  186. {
  187. int32_t exp = v >> 23;
  188. // "HACK": negative values result in exp< 0, so clipping them to 0
  189. // is also handled by this condition, avoids explicit check for sign bit.
  190. if (exp <= 127 + 7 - 24) // we would shift out all bits anyway
  191. return 0;
  192. if (exp >= 127)
  193. return 0xffff;
  194. v &= 0x007fffff;
  195. return (v + (1 << 23)) >> (127 + 7 - exp);
  196. }
  197. /**
  198. * Convert from 16-bit float as uint16_t to uint16_t.
  199. *
  200. * @param v 16-bit float
  201. *
  202. * @return normalized 16-bit unsigned int
  203. */
  204. static inline uint16_t exr_halflt2uint(uint16_t v)
  205. {
  206. unsigned exp = 14 - (v >> 10);
  207. if (exp >= 14) {
  208. if (exp == 14)
  209. return (v >> 9) & 1;
  210. else
  211. return (v & 0x8000) ? 0 : 0xffff;
  212. }
  213. v <<= 6;
  214. return (v + (1 << 16)) >> (exp + 1);
  215. }
  216. static void predictor(uint8_t *src, int size)
  217. {
  218. uint8_t *t = src + 1;
  219. uint8_t *stop = src + size;
  220. while (t < stop) {
  221. int d = (int) t[-1] + (int) t[0] - 128;
  222. t[0] = d;
  223. ++t;
  224. }
  225. }
  226. static void reorder_pixels(uint8_t *src, uint8_t *dst, int size)
  227. {
  228. const int8_t *t1 = src;
  229. const int8_t *t2 = src + (size + 1) / 2;
  230. int8_t *s = dst;
  231. int8_t *stop = s + size;
  232. while (1) {
  233. if (s < stop)
  234. *(s++) = *(t1++);
  235. else
  236. break;
  237. if (s < stop)
  238. *(s++) = *(t2++);
  239. else
  240. break;
  241. }
  242. }
  243. static int zip_uncompress(const uint8_t *src, int compressed_size,
  244. int uncompressed_size, EXRThreadData *td)
  245. {
  246. unsigned long dest_len = uncompressed_size;
  247. if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK ||
  248. dest_len != uncompressed_size)
  249. return AVERROR_INVALIDDATA;
  250. predictor(td->tmp, uncompressed_size);
  251. reorder_pixels(td->tmp, td->uncompressed_data, uncompressed_size);
  252. return 0;
  253. }
  254. static int rle_uncompress(const uint8_t *src, int compressed_size,
  255. int uncompressed_size, EXRThreadData *td)
  256. {
  257. uint8_t *d = td->tmp;
  258. const int8_t *s = src;
  259. int ssize = compressed_size;
  260. int dsize = uncompressed_size;
  261. uint8_t *dend = d + dsize;
  262. int count;
  263. while (ssize > 0) {
  264. count = *s++;
  265. if (count < 0) {
  266. count = -count;
  267. if ((dsize -= count) < 0 ||
  268. (ssize -= count + 1) < 0)
  269. return AVERROR_INVALIDDATA;
  270. while (count--)
  271. *d++ = *s++;
  272. } else {
  273. count++;
  274. if ((dsize -= count) < 0 ||
  275. (ssize -= 2) < 0)
  276. return AVERROR_INVALIDDATA;
  277. while (count--)
  278. *d++ = *s;
  279. s++;
  280. }
  281. }
  282. if (dend != d)
  283. return AVERROR_INVALIDDATA;
  284. predictor(td->tmp, uncompressed_size);
  285. reorder_pixels(td->tmp, td->uncompressed_data, uncompressed_size);
  286. return 0;
  287. }
  288. #define USHORT_RANGE (1 << 16)
  289. #define BITMAP_SIZE (1 << 13)
  290. static uint16_t reverse_lut(const uint8_t *bitmap, uint16_t *lut)
  291. {
  292. int i, k = 0;
  293. for (i = 0; i < USHORT_RANGE; i++)
  294. if ((i == 0) || (bitmap[i >> 3] & (1 << (i & 7))))
  295. lut[k++] = i;
  296. i = k - 1;
  297. memset(lut + k, 0, (USHORT_RANGE - k) * 2);
  298. return i;
  299. }
  300. static void apply_lut(const uint16_t *lut, uint16_t *dst, int dsize)
  301. {
  302. int i;
  303. for (i = 0; i < dsize; ++i)
  304. dst[i] = lut[dst[i]];
  305. }
  306. #define HUF_ENCBITS 16 // literal (value) bit length
  307. #define HUF_DECBITS 14 // decoding bit size (>= 8)
  308. #define HUF_ENCSIZE ((1 << HUF_ENCBITS) + 1) // encoding table size
  309. #define HUF_DECSIZE (1 << HUF_DECBITS) // decoding table size
  310. #define HUF_DECMASK (HUF_DECSIZE - 1)
  311. typedef struct HufDec {
  312. int len;
  313. int lit;
  314. int *p;
  315. } HufDec;
  316. static void huf_canonical_code_table(uint64_t *hcode)
  317. {
  318. uint64_t c, n[59] = { 0 };
  319. int i;
  320. for (i = 0; i < HUF_ENCSIZE; ++i)
  321. n[hcode[i]] += 1;
  322. c = 0;
  323. for (i = 58; i > 0; --i) {
  324. uint64_t nc = ((c + n[i]) >> 1);
  325. n[i] = c;
  326. c = nc;
  327. }
  328. for (i = 0; i < HUF_ENCSIZE; ++i) {
  329. int l = hcode[i];
  330. if (l > 0)
  331. hcode[i] = l | (n[l]++ << 6);
  332. }
  333. }
  334. #define SHORT_ZEROCODE_RUN 59
  335. #define LONG_ZEROCODE_RUN 63
  336. #define SHORTEST_LONG_RUN (2 + LONG_ZEROCODE_RUN - SHORT_ZEROCODE_RUN)
  337. #define LONGEST_LONG_RUN (255 + SHORTEST_LONG_RUN)
  338. static int huf_unpack_enc_table(GetByteContext *gb,
  339. int32_t im, int32_t iM, uint64_t *hcode)
  340. {
  341. GetBitContext gbit;
  342. int ret = init_get_bits8(&gbit, gb->buffer, bytestream2_get_bytes_left(gb));
  343. if (ret < 0)
  344. return ret;
  345. for (; im <= iM; im++) {
  346. uint64_t l = hcode[im] = get_bits(&gbit, 6);
  347. if (l == LONG_ZEROCODE_RUN) {
  348. int zerun = get_bits(&gbit, 8) + SHORTEST_LONG_RUN;
  349. if (im + zerun > iM + 1)
  350. return AVERROR_INVALIDDATA;
  351. while (zerun--)
  352. hcode[im++] = 0;
  353. im--;
  354. } else if (l >= SHORT_ZEROCODE_RUN) {
  355. int zerun = l - SHORT_ZEROCODE_RUN + 2;
  356. if (im + zerun > iM + 1)
  357. return AVERROR_INVALIDDATA;
  358. while (zerun--)
  359. hcode[im++] = 0;
  360. im--;
  361. }
  362. }
  363. bytestream2_skip(gb, (get_bits_count(&gbit) + 7) / 8);
  364. huf_canonical_code_table(hcode);
  365. return 0;
  366. }
  367. static int huf_build_dec_table(const uint64_t *hcode, int im,
  368. int iM, HufDec *hdecod)
  369. {
  370. for (; im <= iM; im++) {
  371. uint64_t c = hcode[im] >> 6;
  372. int i, l = hcode[im] & 63;
  373. if (c >> l)
  374. return AVERROR_INVALIDDATA;
  375. if (l > HUF_DECBITS) {
  376. HufDec *pl = hdecod + (c >> (l - HUF_DECBITS));
  377. if (pl->len)
  378. return AVERROR_INVALIDDATA;
  379. pl->lit++;
  380. pl->p = av_realloc(pl->p, pl->lit * sizeof(int));
  381. if (!pl->p)
  382. return AVERROR(ENOMEM);
  383. pl->p[pl->lit - 1] = im;
  384. } else if (l) {
  385. HufDec *pl = hdecod + (c << (HUF_DECBITS - l));
  386. for (i = 1 << (HUF_DECBITS - l); i > 0; i--, pl++) {
  387. if (pl->len || pl->p)
  388. return AVERROR_INVALIDDATA;
  389. pl->len = l;
  390. pl->lit = im;
  391. }
  392. }
  393. }
  394. return 0;
  395. }
  396. #define get_char(c, lc, gb) \
  397. { \
  398. c = (c << 8) | bytestream2_get_byte(gb); \
  399. lc += 8; \
  400. }
  401. #define get_code(po, rlc, c, lc, gb, out, oe, outb) \
  402. { \
  403. if (po == rlc) { \
  404. if (lc < 8) \
  405. get_char(c, lc, gb); \
  406. lc -= 8; \
  407. \
  408. cs = c >> lc; \
  409. \
  410. if (out + cs > oe || out == outb) \
  411. return AVERROR_INVALIDDATA; \
  412. \
  413. s = out[-1]; \
  414. \
  415. while (cs-- > 0) \
  416. *out++ = s; \
  417. } else if (out < oe) { \
  418. *out++ = po; \
  419. } else { \
  420. return AVERROR_INVALIDDATA; \
  421. } \
  422. }
  423. static int huf_decode(const uint64_t *hcode, const HufDec *hdecod,
  424. GetByteContext *gb, int nbits,
  425. int rlc, int no, uint16_t *out)
  426. {
  427. uint64_t c = 0;
  428. uint16_t *outb = out;
  429. uint16_t *oe = out + no;
  430. const uint8_t *ie = gb->buffer + (nbits + 7) / 8; // input byte size
  431. uint8_t cs;
  432. uint16_t s;
  433. int i, lc = 0;
  434. while (gb->buffer < ie) {
  435. get_char(c, lc, gb);
  436. while (lc >= HUF_DECBITS) {
  437. const HufDec pl = hdecod[(c >> (lc - HUF_DECBITS)) & HUF_DECMASK];
  438. if (pl.len) {
  439. lc -= pl.len;
  440. get_code(pl.lit, rlc, c, lc, gb, out, oe, outb);
  441. } else {
  442. int j;
  443. if (!pl.p)
  444. return AVERROR_INVALIDDATA;
  445. for (j = 0; j < pl.lit; j++) {
  446. int l = hcode[pl.p[j]] & 63;
  447. while (lc < l && bytestream2_get_bytes_left(gb) > 0)
  448. get_char(c, lc, gb);
  449. if (lc >= l) {
  450. if ((hcode[pl.p[j]] >> 6) ==
  451. ((c >> (lc - l)) & ((1LL << l) - 1))) {
  452. lc -= l;
  453. get_code(pl.p[j], rlc, c, lc, gb, out, oe, outb);
  454. break;
  455. }
  456. }
  457. }
  458. if (j == pl.lit)
  459. return AVERROR_INVALIDDATA;
  460. }
  461. }
  462. }
  463. i = (8 - nbits) & 7;
  464. c >>= i;
  465. lc -= i;
  466. while (lc > 0) {
  467. const HufDec pl = hdecod[(c << (HUF_DECBITS - lc)) & HUF_DECMASK];
  468. if (pl.len) {
  469. lc -= pl.len;
  470. get_code(pl.lit, rlc, c, lc, gb, out, oe, outb);
  471. } else {
  472. return AVERROR_INVALIDDATA;
  473. }
  474. }
  475. if (out - outb != no)
  476. return AVERROR_INVALIDDATA;
  477. return 0;
  478. }
  479. static int huf_uncompress(GetByteContext *gb,
  480. uint16_t *dst, int dst_size)
  481. {
  482. int32_t src_size, im, iM;
  483. uint32_t nBits;
  484. uint64_t *freq;
  485. HufDec *hdec;
  486. int ret, i;
  487. src_size = bytestream2_get_le32(gb);
  488. im = bytestream2_get_le32(gb);
  489. iM = bytestream2_get_le32(gb);
  490. bytestream2_skip(gb, 4);
  491. nBits = bytestream2_get_le32(gb);
  492. if (im < 0 || im >= HUF_ENCSIZE ||
  493. iM < 0 || iM >= HUF_ENCSIZE ||
  494. src_size < 0)
  495. return AVERROR_INVALIDDATA;
  496. bytestream2_skip(gb, 4);
  497. freq = av_mallocz_array(HUF_ENCSIZE, sizeof(*freq));
  498. hdec = av_mallocz_array(HUF_DECSIZE, sizeof(*hdec));
  499. if (!freq || !hdec) {
  500. ret = AVERROR(ENOMEM);
  501. goto fail;
  502. }
  503. if ((ret = huf_unpack_enc_table(gb, im, iM, freq)) < 0)
  504. goto fail;
  505. if (nBits > 8 * bytestream2_get_bytes_left(gb)) {
  506. ret = AVERROR_INVALIDDATA;
  507. goto fail;
  508. }
  509. if ((ret = huf_build_dec_table(freq, im, iM, hdec)) < 0)
  510. goto fail;
  511. ret = huf_decode(freq, hdec, gb, nBits, iM, dst_size, dst);
  512. fail:
  513. for (i = 0; i < HUF_DECSIZE; i++)
  514. if (hdec)
  515. av_freep(&hdec[i].p);
  516. av_free(freq);
  517. av_free(hdec);
  518. return ret;
  519. }
  520. static inline void wdec14(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
  521. {
  522. int16_t ls = l;
  523. int16_t hs = h;
  524. int hi = hs;
  525. int ai = ls + (hi & 1) + (hi >> 1);
  526. int16_t as = ai;
  527. int16_t bs = ai - hi;
  528. *a = as;
  529. *b = bs;
  530. }
  531. #define NBITS 16
  532. #define A_OFFSET (1 << (NBITS - 1))
  533. #define MOD_MASK ((1 << NBITS) - 1)
  534. static inline void wdec16(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
  535. {
  536. int m = l;
  537. int d = h;
  538. int bb = (m - (d >> 1)) & MOD_MASK;
  539. int aa = (d + bb - A_OFFSET) & MOD_MASK;
  540. *b = bb;
  541. *a = aa;
  542. }
  543. static void wav_decode(uint16_t *in, int nx, int ox,
  544. int ny, int oy, uint16_t mx)
  545. {
  546. int w14 = (mx < (1 << 14));
  547. int n = (nx > ny) ? ny : nx;
  548. int p = 1;
  549. int p2;
  550. while (p <= n)
  551. p <<= 1;
  552. p >>= 1;
  553. p2 = p;
  554. p >>= 1;
  555. while (p >= 1) {
  556. uint16_t *py = in;
  557. uint16_t *ey = in + oy * (ny - p2);
  558. uint16_t i00, i01, i10, i11;
  559. int oy1 = oy * p;
  560. int oy2 = oy * p2;
  561. int ox1 = ox * p;
  562. int ox2 = ox * p2;
  563. for (; py <= ey; py += oy2) {
  564. uint16_t *px = py;
  565. uint16_t *ex = py + ox * (nx - p2);
  566. for (; px <= ex; px += ox2) {
  567. uint16_t *p01 = px + ox1;
  568. uint16_t *p10 = px + oy1;
  569. uint16_t *p11 = p10 + ox1;
  570. if (w14) {
  571. wdec14(*px, *p10, &i00, &i10);
  572. wdec14(*p01, *p11, &i01, &i11);
  573. wdec14(i00, i01, px, p01);
  574. wdec14(i10, i11, p10, p11);
  575. } else {
  576. wdec16(*px, *p10, &i00, &i10);
  577. wdec16(*p01, *p11, &i01, &i11);
  578. wdec16(i00, i01, px, p01);
  579. wdec16(i10, i11, p10, p11);
  580. }
  581. }
  582. if (nx & p) {
  583. uint16_t *p10 = px + oy1;
  584. if (w14)
  585. wdec14(*px, *p10, &i00, p10);
  586. else
  587. wdec16(*px, *p10, &i00, p10);
  588. *px = i00;
  589. }
  590. }
  591. if (ny & p) {
  592. uint16_t *px = py;
  593. uint16_t *ex = py + ox * (nx - p2);
  594. for (; px <= ex; px += ox2) {
  595. uint16_t *p01 = px + ox1;
  596. if (w14)
  597. wdec14(*px, *p01, &i00, p01);
  598. else
  599. wdec16(*px, *p01, &i00, p01);
  600. *px = i00;
  601. }
  602. }
  603. p2 = p;
  604. p >>= 1;
  605. }
  606. }
  607. static int piz_uncompress(EXRContext *s, const uint8_t *src, int ssize,
  608. int dsize, EXRThreadData *td)
  609. {
  610. GetByteContext gb;
  611. uint16_t maxval, min_non_zero, max_non_zero;
  612. uint16_t *ptr;
  613. uint16_t *tmp = (uint16_t *)td->tmp;
  614. uint8_t *out;
  615. int ret, i, j;
  616. int pixel_half_size;/* 1 for half, 2 for float and uint32 */
  617. EXRChannel *channel;
  618. int tmp_offset;
  619. if (!td->bitmap)
  620. td->bitmap = av_malloc(BITMAP_SIZE);
  621. if (!td->lut)
  622. td->lut = av_malloc(1 << 17);
  623. if (!td->bitmap || !td->lut) {
  624. av_freep(&td->bitmap);
  625. av_freep(&td->lut);
  626. return AVERROR(ENOMEM);
  627. }
  628. bytestream2_init(&gb, src, ssize);
  629. min_non_zero = bytestream2_get_le16(&gb);
  630. max_non_zero = bytestream2_get_le16(&gb);
  631. if (max_non_zero >= BITMAP_SIZE)
  632. return AVERROR_INVALIDDATA;
  633. memset(td->bitmap, 0, FFMIN(min_non_zero, BITMAP_SIZE));
  634. if (min_non_zero <= max_non_zero)
  635. bytestream2_get_buffer(&gb, td->bitmap + min_non_zero,
  636. max_non_zero - min_non_zero + 1);
  637. memset(td->bitmap + max_non_zero + 1, 0, BITMAP_SIZE - max_non_zero - 1);
  638. maxval = reverse_lut(td->bitmap, td->lut);
  639. ret = huf_uncompress(&gb, tmp, dsize / sizeof(uint16_t));
  640. if (ret)
  641. return ret;
  642. ptr = tmp;
  643. for (i = 0; i < s->nb_channels; i++) {
  644. channel = &s->channels[i];
  645. if (channel->pixel_type == EXR_HALF)
  646. pixel_half_size = 1;
  647. else
  648. pixel_half_size = 2;
  649. for (j = 0; j < pixel_half_size; j++)
  650. wav_decode(ptr + j, td->xsize, pixel_half_size, td->ysize,
  651. td->xsize * pixel_half_size, maxval);
  652. ptr += td->xsize * td->ysize * pixel_half_size;
  653. }
  654. apply_lut(td->lut, tmp, dsize / sizeof(uint16_t));
  655. out = td->uncompressed_data;
  656. for (i = 0; i < td->ysize; i++) {
  657. tmp_offset = 0;
  658. for (j = 0; j < s->nb_channels; j++) {
  659. uint16_t *in;
  660. EXRChannel *channel = &s->channels[j];
  661. if (channel->pixel_type == EXR_HALF)
  662. pixel_half_size = 1;
  663. else
  664. pixel_half_size = 2;
  665. in = tmp + tmp_offset * td->xsize * td->ysize + i * td->xsize * pixel_half_size;
  666. tmp_offset += pixel_half_size;
  667. memcpy(out, in, td->xsize * 2 * pixel_half_size);
  668. out += td->xsize * 2 * pixel_half_size;
  669. }
  670. }
  671. return 0;
  672. }
  673. static int pxr24_uncompress(EXRContext *s, const uint8_t *src,
  674. int compressed_size, int uncompressed_size,
  675. EXRThreadData *td)
  676. {
  677. unsigned long dest_len, expected_len = 0;
  678. const uint8_t *in = td->tmp;
  679. uint8_t *out;
  680. int c, i, j;
  681. for (i = 0; i < s->nb_channels; i++) {
  682. if (s->channels[i].pixel_type == EXR_FLOAT) {
  683. expected_len += (td->xsize * td->ysize * 3);/* PRX 24 store float in 24 bit instead of 32 */
  684. } else if (s->channels[i].pixel_type == EXR_HALF) {
  685. expected_len += (td->xsize * td->ysize * 2);
  686. } else {//UINT 32
  687. expected_len += (td->xsize * td->ysize * 4);
  688. }
  689. }
  690. dest_len = expected_len;
  691. if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK) {
  692. return AVERROR_INVALIDDATA;
  693. } else if (dest_len != expected_len) {
  694. return AVERROR_INVALIDDATA;
  695. }
  696. out = td->uncompressed_data;
  697. for (i = 0; i < td->ysize; i++)
  698. for (c = 0; c < s->nb_channels; c++) {
  699. EXRChannel *channel = &s->channels[c];
  700. const uint8_t *ptr[4];
  701. uint32_t pixel = 0;
  702. switch (channel->pixel_type) {
  703. case EXR_FLOAT:
  704. ptr[0] = in;
  705. ptr[1] = ptr[0] + td->xsize;
  706. ptr[2] = ptr[1] + td->xsize;
  707. in = ptr[2] + td->xsize;
  708. for (j = 0; j < td->xsize; ++j) {
  709. uint32_t diff = ((unsigned)*(ptr[0]++) << 24) |
  710. (*(ptr[1]++) << 16) |
  711. (*(ptr[2]++) << 8);
  712. pixel += diff;
  713. bytestream_put_le32(&out, pixel);
  714. }
  715. break;
  716. case EXR_HALF:
  717. ptr[0] = in;
  718. ptr[1] = ptr[0] + td->xsize;
  719. in = ptr[1] + td->xsize;
  720. for (j = 0; j < td->xsize; j++) {
  721. uint32_t diff = (*(ptr[0]++) << 8) | *(ptr[1]++);
  722. pixel += diff;
  723. bytestream_put_le16(&out, pixel);
  724. }
  725. break;
  726. default:
  727. return AVERROR_INVALIDDATA;
  728. }
  729. }
  730. return 0;
  731. }
  732. static void unpack_14(const uint8_t b[14], uint16_t s[16])
  733. {
  734. unsigned short shift = (b[ 2] >> 2);
  735. unsigned short bias = (0x20 << shift);
  736. int i;
  737. s[ 0] = (b[0] << 8) | b[1];
  738. s[ 4] = s[ 0] + ((((b[ 2] << 4) | (b[ 3] >> 4)) & 0x3f) << shift) - bias;
  739. s[ 8] = s[ 4] + ((((b[ 3] << 2) | (b[ 4] >> 6)) & 0x3f) << shift) - bias;
  740. s[12] = s[ 8] + ((b[ 4] & 0x3f) << shift) - bias;
  741. s[ 1] = s[ 0] + ((b[ 5] >> 2) << shift) - bias;
  742. s[ 5] = s[ 4] + ((((b[ 5] << 4) | (b[ 6] >> 4)) & 0x3f) << shift) - bias;
  743. s[ 9] = s[ 8] + ((((b[ 6] << 2) | (b[ 7] >> 6)) & 0x3f) << shift) - bias;
  744. s[13] = s[12] + ((b[ 7] & 0x3f) << shift) - bias;
  745. s[ 2] = s[ 1] + ((b[ 8] >> 2) << shift) - bias;
  746. s[ 6] = s[ 5] + ((((b[ 8] << 4) | (b[ 9] >> 4)) & 0x3f) << shift) - bias;
  747. s[10] = s[ 9] + ((((b[ 9] << 2) | (b[10] >> 6)) & 0x3f) << shift) - bias;
  748. s[14] = s[13] + ((b[10] & 0x3f) << shift) - bias;
  749. s[ 3] = s[ 2] + ((b[11] >> 2) << shift) - bias;
  750. s[ 7] = s[ 6] + ((((b[11] << 4) | (b[12] >> 4)) & 0x3f) << shift) - bias;
  751. s[11] = s[10] + ((((b[12] << 2) | (b[13] >> 6)) & 0x3f) << shift) - bias;
  752. s[15] = s[14] + ((b[13] & 0x3f) << shift) - bias;
  753. for (i = 0; i < 16; ++i) {
  754. if (s[i] & 0x8000)
  755. s[i] &= 0x7fff;
  756. else
  757. s[i] = ~s[i];
  758. }
  759. }
  760. static void unpack_3(const uint8_t b[3], uint16_t s[16])
  761. {
  762. int i;
  763. s[0] = (b[0] << 8) | b[1];
  764. if (s[0] & 0x8000)
  765. s[0] &= 0x7fff;
  766. else
  767. s[0] = ~s[0];
  768. for (i = 1; i < 16; i++)
  769. s[i] = s[0];
  770. }
  771. static int b44_uncompress(EXRContext *s, const uint8_t *src, int compressed_size,
  772. int uncompressed_size, EXRThreadData *td) {
  773. const int8_t *sr = src;
  774. int stay_to_uncompress = compressed_size;
  775. int nb_b44_block_w, nb_b44_block_h;
  776. int index_tl_x, index_tl_y, index_out, index_tmp;
  777. uint16_t tmp_buffer[16]; /* B44 use 4x4 half float pixel */
  778. int c, iY, iX, y, x;
  779. int target_channel_offset = 0;
  780. /* calc B44 block count */
  781. nb_b44_block_w = td->xsize / 4;
  782. if ((td->xsize % 4) != 0)
  783. nb_b44_block_w++;
  784. nb_b44_block_h = td->ysize / 4;
  785. if ((td->ysize % 4) != 0)
  786. nb_b44_block_h++;
  787. for (c = 0; c < s->nb_channels; c++) {
  788. if (s->channels[c].pixel_type == EXR_HALF) {/* B44 only compress half float data */
  789. for (iY = 0; iY < nb_b44_block_h; iY++) {
  790. for (iX = 0; iX < nb_b44_block_w; iX++) {/* For each B44 block */
  791. if (stay_to_uncompress < 3) {
  792. av_log(s, AV_LOG_ERROR, "Not enough data for B44A block: %d", stay_to_uncompress);
  793. return AVERROR_INVALIDDATA;
  794. }
  795. if (src[compressed_size - stay_to_uncompress + 2] == 0xfc) { /* B44A block */
  796. unpack_3(sr, tmp_buffer);
  797. sr += 3;
  798. stay_to_uncompress -= 3;
  799. } else {/* B44 Block */
  800. if (stay_to_uncompress < 14) {
  801. av_log(s, AV_LOG_ERROR, "Not enough data for B44 block: %d", stay_to_uncompress);
  802. return AVERROR_INVALIDDATA;
  803. }
  804. unpack_14(sr, tmp_buffer);
  805. sr += 14;
  806. stay_to_uncompress -= 14;
  807. }
  808. /* copy data to uncompress buffer (B44 block can exceed target resolution)*/
  809. index_tl_x = iX * 4;
  810. index_tl_y = iY * 4;
  811. for (y = index_tl_y; y < FFMIN(index_tl_y + 4, td->ysize); y++) {
  812. for (x = index_tl_x; x < FFMIN(index_tl_x + 4, td->xsize); x++) {
  813. index_out = target_channel_offset * td->xsize + y * td->channel_line_size + 2 * x;
  814. index_tmp = (y-index_tl_y) * 4 + (x-index_tl_x);
  815. td->uncompressed_data[index_out] = tmp_buffer[index_tmp] & 0xff;
  816. td->uncompressed_data[index_out + 1] = tmp_buffer[index_tmp] >> 8;
  817. }
  818. }
  819. }
  820. }
  821. target_channel_offset += 2;
  822. } else {/* Float or UINT 32 channel */
  823. if (stay_to_uncompress < td->ysize * td->xsize * 4) {
  824. av_log(s, AV_LOG_ERROR, "Not enough data for uncompress channel: %d", stay_to_uncompress);
  825. return AVERROR_INVALIDDATA;
  826. }
  827. for (y = 0; y < td->ysize; y++) {
  828. index_out = target_channel_offset * td->xsize + y * td->channel_line_size;
  829. memcpy(&td->uncompressed_data[index_out], sr, td->xsize * 4);
  830. sr += td->xsize * 4;
  831. }
  832. target_channel_offset += 4;
  833. stay_to_uncompress -= td->ysize * td->xsize * 4;
  834. }
  835. }
  836. return 0;
  837. }
  838. static int decode_block(AVCodecContext *avctx, void *tdata,
  839. int jobnr, int threadnr)
  840. {
  841. EXRContext *s = avctx->priv_data;
  842. AVFrame *const p = s->picture;
  843. EXRThreadData *td = &s->thread_data[threadnr];
  844. const uint8_t *channel_buffer[4] = { 0 };
  845. const uint8_t *buf = s->buf;
  846. uint64_t line_offset, uncompressed_size;
  847. uint16_t *ptr_x;
  848. uint8_t *ptr;
  849. uint32_t data_size;
  850. uint64_t line, col = 0;
  851. uint64_t tileX, tileY, tileLevelX, tileLevelY;
  852. const uint8_t *src;
  853. int axmax = (avctx->width - (s->xmax + 1)) * 2 * s->desc->nb_components; /* nb pixel to add at the right of the datawindow */
  854. int bxmin = s->xmin * 2 * s->desc->nb_components; /* nb pixel to add at the left of the datawindow */
  855. int i, x, buf_size = s->buf_size;
  856. int c, rgb_channel_count;
  857. float one_gamma = 1.0f / s->gamma;
  858. avpriv_trc_function trc_func = avpriv_get_trc_function_from_trc(s->apply_trc_type);
  859. int ret;
  860. line_offset = AV_RL64(s->gb.buffer + jobnr * 8);
  861. if (s->is_tile) {
  862. if (line_offset > buf_size - 20)
  863. return AVERROR_INVALIDDATA;
  864. src = buf + line_offset + 20;
  865. tileX = AV_RL32(src - 20);
  866. tileY = AV_RL32(src - 16);
  867. tileLevelX = AV_RL32(src - 12);
  868. tileLevelY = AV_RL32(src - 8);
  869. data_size = AV_RL32(src - 4);
  870. if (data_size <= 0 || data_size > buf_size)
  871. return AVERROR_INVALIDDATA;
  872. if (tileLevelX || tileLevelY) { /* tile level, is not the full res level */
  873. avpriv_report_missing_feature(s->avctx, "Subres tile before full res tile");
  874. return AVERROR_PATCHWELCOME;
  875. }
  876. if (s->xmin || s->ymin) {
  877. avpriv_report_missing_feature(s->avctx, "Tiles with xmin/ymin");
  878. return AVERROR_PATCHWELCOME;
  879. }
  880. line = s->tile_attr.ySize * tileY;
  881. col = s->tile_attr.xSize * tileX;
  882. if (line < s->ymin || line > s->ymax ||
  883. col < s->xmin || col > s->xmax)
  884. return AVERROR_INVALIDDATA;
  885. td->ysize = FFMIN(s->tile_attr.ySize, s->ydelta - tileY * s->tile_attr.ySize);
  886. td->xsize = FFMIN(s->tile_attr.xSize, s->xdelta - tileX * s->tile_attr.xSize);
  887. if (col) { /* not the first tile of the line */
  888. bxmin = 0; /* doesn't add pixel at the left of the datawindow */
  889. }
  890. if ((col + td->xsize) != s->xdelta)/* not the last tile of the line */
  891. axmax = 0; /* doesn't add pixel at the right of the datawindow */
  892. td->channel_line_size = td->xsize * s->current_channel_offset;/* uncompress size of one line */
  893. uncompressed_size = td->channel_line_size * (uint64_t)td->ysize;/* uncompress size of the block */
  894. } else {
  895. if (line_offset > buf_size - 8)
  896. return AVERROR_INVALIDDATA;
  897. src = buf + line_offset + 8;
  898. line = AV_RL32(src - 8);
  899. if (line < s->ymin || line > s->ymax)
  900. return AVERROR_INVALIDDATA;
  901. data_size = AV_RL32(src - 4);
  902. if (data_size <= 0 || data_size > buf_size)
  903. return AVERROR_INVALIDDATA;
  904. td->ysize = FFMIN(s->scan_lines_per_block, s->ymax - line + 1); /* s->ydelta - line ?? */
  905. td->xsize = s->xdelta;
  906. td->channel_line_size = td->xsize * s->current_channel_offset;/* uncompress size of one line */
  907. uncompressed_size = td->channel_line_size * (uint64_t)td->ysize;/* uncompress size of the block */
  908. if ((s->compression == EXR_RAW && (data_size != uncompressed_size ||
  909. line_offset > buf_size - uncompressed_size)) ||
  910. (s->compression != EXR_RAW && (data_size > uncompressed_size ||
  911. line_offset > buf_size - data_size))) {
  912. return AVERROR_INVALIDDATA;
  913. }
  914. }
  915. if (data_size < uncompressed_size || s->is_tile) { /* td->tmp is use for tile reorganization */
  916. av_fast_padded_malloc(&td->tmp, &td->tmp_size, uncompressed_size);
  917. if (!td->tmp)
  918. return AVERROR(ENOMEM);
  919. }
  920. if (data_size < uncompressed_size) {
  921. av_fast_padded_malloc(&td->uncompressed_data,
  922. &td->uncompressed_size, uncompressed_size);
  923. if (!td->uncompressed_data)
  924. return AVERROR(ENOMEM);
  925. ret = AVERROR_INVALIDDATA;
  926. switch (s->compression) {
  927. case EXR_ZIP1:
  928. case EXR_ZIP16:
  929. ret = zip_uncompress(src, data_size, uncompressed_size, td);
  930. break;
  931. case EXR_PIZ:
  932. ret = piz_uncompress(s, src, data_size, uncompressed_size, td);
  933. break;
  934. case EXR_PXR24:
  935. ret = pxr24_uncompress(s, src, data_size, uncompressed_size, td);
  936. break;
  937. case EXR_RLE:
  938. ret = rle_uncompress(src, data_size, uncompressed_size, td);
  939. break;
  940. case EXR_B44:
  941. case EXR_B44A:
  942. ret = b44_uncompress(s, src, data_size, uncompressed_size, td);
  943. break;
  944. }
  945. if (ret < 0) {
  946. av_log(avctx, AV_LOG_ERROR, "decode_block() failed.\n");
  947. return ret;
  948. }
  949. src = td->uncompressed_data;
  950. }
  951. if (!s->is_luma) {
  952. channel_buffer[0] = src + td->xsize * s->channel_offsets[0];
  953. channel_buffer[1] = src + td->xsize * s->channel_offsets[1];
  954. channel_buffer[2] = src + td->xsize * s->channel_offsets[2];
  955. rgb_channel_count = 3;
  956. } else { /* put y data in the first channel_buffer */
  957. channel_buffer[0] = src + td->xsize * s->channel_offsets[1];
  958. rgb_channel_count = 1;
  959. }
  960. if (s->channel_offsets[3] >= 0)
  961. channel_buffer[3] = src + td->xsize * s->channel_offsets[3];
  962. ptr = p->data[0] + line * p->linesize[0] + (col * s->desc->nb_components * 2);
  963. for (i = 0;
  964. i < td->ysize; i++, ptr += p->linesize[0]) {
  965. const uint8_t * a;
  966. const uint8_t *rgb[3];
  967. for (c = 0; c < rgb_channel_count; c++){
  968. rgb[c] = channel_buffer[c];
  969. }
  970. if (channel_buffer[3])
  971. a = channel_buffer[3];
  972. ptr_x = (uint16_t *) ptr;
  973. // Zero out the start if xmin is not 0
  974. memset(ptr_x, 0, bxmin);
  975. ptr_x += s->xmin * s->desc->nb_components;
  976. if (s->pixel_type == EXR_FLOAT) {
  977. // 32-bit
  978. if (trc_func) {
  979. for (x = 0; x < td->xsize; x++) {
  980. union av_intfloat32 t;
  981. for (c = 0; c < rgb_channel_count; c++) {
  982. t.i = bytestream_get_le32(&rgb[c]);
  983. t.f = trc_func(t.f);
  984. *ptr_x++ = exr_flt2uint(t.i);
  985. }
  986. if (channel_buffer[3])
  987. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a));
  988. }
  989. } else {
  990. for (x = 0; x < td->xsize; x++) {
  991. union av_intfloat32 t;
  992. int c;
  993. for (c = 0; c < rgb_channel_count; c++) {
  994. t.i = bytestream_get_le32(&rgb[c]);
  995. if (t.f > 0.0f) /* avoid negative values */
  996. t.f = powf(t.f, one_gamma);
  997. *ptr_x++ = exr_flt2uint(t.i);
  998. }
  999. if (channel_buffer[3])
  1000. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a));
  1001. }
  1002. }
  1003. } else {
  1004. // 16-bit
  1005. for (x = 0; x < td->xsize; x++) {
  1006. int c;
  1007. for (c = 0; c < rgb_channel_count; c++) {
  1008. *ptr_x++ = s->gamma_table[bytestream_get_le16(&rgb[c])];
  1009. }
  1010. if (channel_buffer[3])
  1011. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&a));
  1012. }
  1013. }
  1014. // Zero out the end if xmax+1 is not w
  1015. memset(ptr_x, 0, axmax);
  1016. channel_buffer[0] += td->channel_line_size;
  1017. channel_buffer[1] += td->channel_line_size;
  1018. channel_buffer[2] += td->channel_line_size;
  1019. if (channel_buffer[3])
  1020. channel_buffer[3] += td->channel_line_size;
  1021. }
  1022. return 0;
  1023. }
  1024. /**
  1025. * Check if the variable name corresponds to its data type.
  1026. *
  1027. * @param s the EXRContext
  1028. * @param value_name name of the variable to check
  1029. * @param value_type type of the variable to check
  1030. * @param minimum_length minimum length of the variable data
  1031. *
  1032. * @return bytes to read containing variable data
  1033. * -1 if variable is not found
  1034. * 0 if buffer ended prematurely
  1035. */
  1036. static int check_header_variable(EXRContext *s,
  1037. const char *value_name,
  1038. const char *value_type,
  1039. unsigned int minimum_length)
  1040. {
  1041. int var_size = -1;
  1042. if (bytestream2_get_bytes_left(&s->gb) >= minimum_length &&
  1043. !strcmp(s->gb.buffer, value_name)) {
  1044. // found value_name, jump to value_type (null terminated strings)
  1045. s->gb.buffer += strlen(value_name) + 1;
  1046. if (!strcmp(s->gb.buffer, value_type)) {
  1047. s->gb.buffer += strlen(value_type) + 1;
  1048. var_size = bytestream2_get_le32(&s->gb);
  1049. // don't go read past boundaries
  1050. if (var_size > bytestream2_get_bytes_left(&s->gb))
  1051. var_size = 0;
  1052. } else {
  1053. // value_type not found, reset the buffer
  1054. s->gb.buffer -= strlen(value_name) + 1;
  1055. av_log(s->avctx, AV_LOG_WARNING,
  1056. "Unknown data type %s for header variable %s.\n",
  1057. value_type, value_name);
  1058. }
  1059. }
  1060. return var_size;
  1061. }
  1062. static int decode_header(EXRContext *s)
  1063. {
  1064. int magic_number, version, i, flags, sar = 0;
  1065. int layer_match = 0;
  1066. s->current_channel_offset = 0;
  1067. s->xmin = ~0;
  1068. s->xmax = ~0;
  1069. s->ymin = ~0;
  1070. s->ymax = ~0;
  1071. s->xdelta = ~0;
  1072. s->ydelta = ~0;
  1073. s->channel_offsets[0] = -1;
  1074. s->channel_offsets[1] = -1;
  1075. s->channel_offsets[2] = -1;
  1076. s->channel_offsets[3] = -1;
  1077. s->pixel_type = EXR_UNKNOWN;
  1078. s->compression = EXR_UNKN;
  1079. s->nb_channels = 0;
  1080. s->w = 0;
  1081. s->h = 0;
  1082. s->tile_attr.xSize = -1;
  1083. s->tile_attr.ySize = -1;
  1084. s->is_tile = 0;
  1085. s->is_luma = 0;
  1086. if (bytestream2_get_bytes_left(&s->gb) < 10) {
  1087. av_log(s->avctx, AV_LOG_ERROR, "Header too short to parse.\n");
  1088. return AVERROR_INVALIDDATA;
  1089. }
  1090. magic_number = bytestream2_get_le32(&s->gb);
  1091. if (magic_number != 20000630) {
  1092. /* As per documentation of OpenEXR, it is supposed to be
  1093. * int 20000630 little-endian */
  1094. av_log(s->avctx, AV_LOG_ERROR, "Wrong magic number %d.\n", magic_number);
  1095. return AVERROR_INVALIDDATA;
  1096. }
  1097. version = bytestream2_get_byte(&s->gb);
  1098. if (version != 2) {
  1099. avpriv_report_missing_feature(s->avctx, "Version %d", version);
  1100. return AVERROR_PATCHWELCOME;
  1101. }
  1102. flags = bytestream2_get_le24(&s->gb);
  1103. if (flags == 0x00)
  1104. s->is_tile = 0;
  1105. else if (flags & 0x02)
  1106. s->is_tile = 1;
  1107. else{
  1108. avpriv_report_missing_feature(s->avctx, "flags %d", flags);
  1109. return AVERROR_PATCHWELCOME;
  1110. }
  1111. // Parse the header
  1112. while (bytestream2_get_bytes_left(&s->gb) > 0 && *s->gb.buffer) {
  1113. int var_size;
  1114. if ((var_size = check_header_variable(s, "channels",
  1115. "chlist", 38)) >= 0) {
  1116. GetByteContext ch_gb;
  1117. if (!var_size)
  1118. return AVERROR_INVALIDDATA;
  1119. bytestream2_init(&ch_gb, s->gb.buffer, var_size);
  1120. while (bytestream2_get_bytes_left(&ch_gb) >= 19) {
  1121. EXRChannel *channel;
  1122. enum ExrPixelType current_pixel_type;
  1123. int channel_index = -1;
  1124. int xsub, ysub;
  1125. if (strcmp(s->layer, "") != 0) {
  1126. if (strncmp(ch_gb.buffer, s->layer, strlen(s->layer)) == 0) {
  1127. layer_match = 1;
  1128. av_log(s->avctx, AV_LOG_INFO,
  1129. "Channel match layer : %s.\n", ch_gb.buffer);
  1130. ch_gb.buffer += strlen(s->layer);
  1131. if (*ch_gb.buffer == '.')
  1132. ch_gb.buffer++; /* skip dot if not given */
  1133. } else {
  1134. av_log(s->avctx, AV_LOG_INFO,
  1135. "Channel doesn't match layer : %s.\n", ch_gb.buffer);
  1136. }
  1137. } else {
  1138. layer_match = 1;
  1139. }
  1140. if (layer_match) { /* only search channel if the layer match is valid */
  1141. if (!strcmp(ch_gb.buffer, "R") ||
  1142. !strcmp(ch_gb.buffer, "X") ||
  1143. !strcmp(ch_gb.buffer, "U")) {
  1144. channel_index = 0;
  1145. s->is_luma = 0;
  1146. } else if (!strcmp(ch_gb.buffer, "G") ||
  1147. !strcmp(ch_gb.buffer, "V")) {
  1148. channel_index = 1;
  1149. s->is_luma = 0;
  1150. } else if (!strcmp(ch_gb.buffer, "Y")) {
  1151. channel_index = 1;
  1152. s->is_luma = 1;
  1153. } else if (!strcmp(ch_gb.buffer, "B") ||
  1154. !strcmp(ch_gb.buffer, "Z") ||
  1155. !strcmp(ch_gb.buffer, "W")){
  1156. channel_index = 2;
  1157. s->is_luma = 0;
  1158. } else if (!strcmp(ch_gb.buffer, "A")) {
  1159. channel_index = 3;
  1160. } else {
  1161. av_log(s->avctx, AV_LOG_WARNING,
  1162. "Unsupported channel %.256s.\n", ch_gb.buffer);
  1163. }
  1164. }
  1165. /* skip until you get a 0 */
  1166. while (bytestream2_get_bytes_left(&ch_gb) > 0 &&
  1167. bytestream2_get_byte(&ch_gb))
  1168. continue;
  1169. if (bytestream2_get_bytes_left(&ch_gb) < 4) {
  1170. av_log(s->avctx, AV_LOG_ERROR, "Incomplete header.\n");
  1171. return AVERROR_INVALIDDATA;
  1172. }
  1173. current_pixel_type = bytestream2_get_le32(&ch_gb);
  1174. if (current_pixel_type >= EXR_UNKNOWN) {
  1175. avpriv_report_missing_feature(s->avctx, "Pixel type %d",
  1176. current_pixel_type);
  1177. return AVERROR_PATCHWELCOME;
  1178. }
  1179. bytestream2_skip(&ch_gb, 4);
  1180. xsub = bytestream2_get_le32(&ch_gb);
  1181. ysub = bytestream2_get_le32(&ch_gb);
  1182. if (xsub != 1 || ysub != 1) {
  1183. avpriv_report_missing_feature(s->avctx,
  1184. "Subsampling %dx%d",
  1185. xsub, ysub);
  1186. return AVERROR_PATCHWELCOME;
  1187. }
  1188. if (channel_index >= 0 && s->channel_offsets[channel_index] == -1) { /* channel has not been previously assigned */
  1189. if (s->pixel_type != EXR_UNKNOWN &&
  1190. s->pixel_type != current_pixel_type) {
  1191. av_log(s->avctx, AV_LOG_ERROR,
  1192. "RGB channels not of the same depth.\n");
  1193. return AVERROR_INVALIDDATA;
  1194. }
  1195. s->pixel_type = current_pixel_type;
  1196. s->channel_offsets[channel_index] = s->current_channel_offset;
  1197. }
  1198. s->channels = av_realloc(s->channels,
  1199. ++s->nb_channels * sizeof(EXRChannel));
  1200. if (!s->channels)
  1201. return AVERROR(ENOMEM);
  1202. channel = &s->channels[s->nb_channels - 1];
  1203. channel->pixel_type = current_pixel_type;
  1204. channel->xsub = xsub;
  1205. channel->ysub = ysub;
  1206. if (current_pixel_type == EXR_HALF) {
  1207. s->current_channel_offset += 2;
  1208. } else {/* Float or UINT32 */
  1209. s->current_channel_offset += 4;
  1210. }
  1211. }
  1212. /* Check if all channels are set with an offset or if the channels
  1213. * are causing an overflow */
  1214. if (!s->is_luma){/* if we expected to have at least 3 channels */
  1215. if (FFMIN3(s->channel_offsets[0],
  1216. s->channel_offsets[1],
  1217. s->channel_offsets[2]) < 0) {
  1218. if (s->channel_offsets[0] < 0)
  1219. av_log(s->avctx, AV_LOG_ERROR, "Missing red channel.\n");
  1220. if (s->channel_offsets[1] < 0)
  1221. av_log(s->avctx, AV_LOG_ERROR, "Missing green channel.\n");
  1222. if (s->channel_offsets[2] < 0)
  1223. av_log(s->avctx, AV_LOG_ERROR, "Missing blue channel.\n");
  1224. return AVERROR_INVALIDDATA;
  1225. }
  1226. }
  1227. // skip one last byte and update main gb
  1228. s->gb.buffer = ch_gb.buffer + 1;
  1229. continue;
  1230. } else if ((var_size = check_header_variable(s, "dataWindow", "box2i",
  1231. 31)) >= 0) {
  1232. if (!var_size)
  1233. return AVERROR_INVALIDDATA;
  1234. s->xmin = bytestream2_get_le32(&s->gb);
  1235. s->ymin = bytestream2_get_le32(&s->gb);
  1236. s->xmax = bytestream2_get_le32(&s->gb);
  1237. s->ymax = bytestream2_get_le32(&s->gb);
  1238. s->xdelta = (s->xmax - s->xmin) + 1;
  1239. s->ydelta = (s->ymax - s->ymin) + 1;
  1240. continue;
  1241. } else if ((var_size = check_header_variable(s, "displayWindow",
  1242. "box2i", 34)) >= 0) {
  1243. if (!var_size)
  1244. return AVERROR_INVALIDDATA;
  1245. bytestream2_skip(&s->gb, 8);
  1246. s->w = bytestream2_get_le32(&s->gb) + 1;
  1247. s->h = bytestream2_get_le32(&s->gb) + 1;
  1248. continue;
  1249. } else if ((var_size = check_header_variable(s, "lineOrder",
  1250. "lineOrder", 25)) >= 0) {
  1251. int line_order;
  1252. if (!var_size)
  1253. return AVERROR_INVALIDDATA;
  1254. line_order = bytestream2_get_byte(&s->gb);
  1255. av_log(s->avctx, AV_LOG_DEBUG, "line order: %d.\n", line_order);
  1256. if (line_order > 2) {
  1257. av_log(s->avctx, AV_LOG_ERROR, "Unknown line order.\n");
  1258. return AVERROR_INVALIDDATA;
  1259. }
  1260. continue;
  1261. } else if ((var_size = check_header_variable(s, "pixelAspectRatio",
  1262. "float", 31)) >= 0) {
  1263. if (!var_size)
  1264. return AVERROR_INVALIDDATA;
  1265. sar = bytestream2_get_le32(&s->gb);
  1266. continue;
  1267. } else if ((var_size = check_header_variable(s, "compression",
  1268. "compression", 29)) >= 0) {
  1269. if (!var_size)
  1270. return AVERROR_INVALIDDATA;
  1271. if (s->compression == EXR_UNKN)
  1272. s->compression = bytestream2_get_byte(&s->gb);
  1273. else
  1274. av_log(s->avctx, AV_LOG_WARNING,
  1275. "Found more than one compression attribute.\n");
  1276. continue;
  1277. } else if ((var_size = check_header_variable(s, "tiles",
  1278. "tiledesc", 22)) >= 0) {
  1279. char tileLevel;
  1280. if (!s->is_tile)
  1281. av_log(s->avctx, AV_LOG_WARNING,
  1282. "Found tile attribute and scanline flags. Exr will be interpreted as scanline.\n");
  1283. s->tile_attr.xSize = bytestream2_get_le32(&s->gb);
  1284. s->tile_attr.ySize = bytestream2_get_le32(&s->gb);
  1285. tileLevel = bytestream2_get_byte(&s->gb);
  1286. s->tile_attr.level_mode = tileLevel & 0x0f;
  1287. s->tile_attr.level_round = (tileLevel >> 4) & 0x0f;
  1288. if (s->tile_attr.level_mode >= EXR_TILE_LEVEL_UNKNOWN){
  1289. avpriv_report_missing_feature(s->avctx, "Tile level mode %d",
  1290. s->tile_attr.level_mode);
  1291. return AVERROR_PATCHWELCOME;
  1292. }
  1293. if (s->tile_attr.level_round >= EXR_TILE_ROUND_UNKNOWN) {
  1294. avpriv_report_missing_feature(s->avctx, "Tile level round %d",
  1295. s->tile_attr.level_round);
  1296. return AVERROR_PATCHWELCOME;
  1297. }
  1298. continue;
  1299. }
  1300. // Check if there are enough bytes for a header
  1301. if (bytestream2_get_bytes_left(&s->gb) <= 9) {
  1302. av_log(s->avctx, AV_LOG_ERROR, "Incomplete header\n");
  1303. return AVERROR_INVALIDDATA;
  1304. }
  1305. // Process unknown variables
  1306. for (i = 0; i < 2; i++) // value_name and value_type
  1307. while (bytestream2_get_byte(&s->gb) != 0);
  1308. // Skip variable length
  1309. bytestream2_skip(&s->gb, bytestream2_get_le32(&s->gb));
  1310. }
  1311. ff_set_sar(s->avctx, av_d2q(av_int2float(sar), 255));
  1312. if (s->compression == EXR_UNKN) {
  1313. av_log(s->avctx, AV_LOG_ERROR, "Missing compression attribute.\n");
  1314. return AVERROR_INVALIDDATA;
  1315. }
  1316. if (s->is_tile) {
  1317. if (s->tile_attr.xSize < 1 || s->tile_attr.ySize < 1) {
  1318. av_log(s->avctx, AV_LOG_ERROR, "Invalid tile attribute.\n");
  1319. return AVERROR_INVALIDDATA;
  1320. }
  1321. }
  1322. if (bytestream2_get_bytes_left(&s->gb) <= 0) {
  1323. av_log(s->avctx, AV_LOG_ERROR, "Incomplete frame.\n");
  1324. return AVERROR_INVALIDDATA;
  1325. }
  1326. // aaand we are done
  1327. bytestream2_skip(&s->gb, 1);
  1328. return 0;
  1329. }
  1330. static int decode_frame(AVCodecContext *avctx, void *data,
  1331. int *got_frame, AVPacket *avpkt)
  1332. {
  1333. EXRContext *s = avctx->priv_data;
  1334. ThreadFrame frame = { .f = data };
  1335. AVFrame *picture = data;
  1336. uint8_t *ptr;
  1337. int y, ret;
  1338. int out_line_size;
  1339. int nb_blocks;/* nb scanline or nb tile */
  1340. bytestream2_init(&s->gb, avpkt->data, avpkt->size);
  1341. if ((ret = decode_header(s)) < 0)
  1342. return ret;
  1343. switch (s->pixel_type) {
  1344. case EXR_FLOAT:
  1345. case EXR_HALF:
  1346. if (s->channel_offsets[3] >= 0) {
  1347. if (!s->is_luma) {
  1348. avctx->pix_fmt = AV_PIX_FMT_RGBA64;
  1349. } else {
  1350. avctx->pix_fmt = AV_PIX_FMT_YA16;
  1351. }
  1352. } else {
  1353. if (!s->is_luma) {
  1354. avctx->pix_fmt = AV_PIX_FMT_RGB48;
  1355. } else {
  1356. avctx->pix_fmt = AV_PIX_FMT_GRAY16;
  1357. }
  1358. }
  1359. break;
  1360. case EXR_UINT:
  1361. avpriv_request_sample(avctx, "32-bit unsigned int");
  1362. return AVERROR_PATCHWELCOME;
  1363. default:
  1364. av_log(avctx, AV_LOG_ERROR, "Missing channel list.\n");
  1365. return AVERROR_INVALIDDATA;
  1366. }
  1367. if (s->apply_trc_type != AVCOL_TRC_UNSPECIFIED)
  1368. avctx->color_trc = s->apply_trc_type;
  1369. switch (s->compression) {
  1370. case EXR_RAW:
  1371. case EXR_RLE:
  1372. case EXR_ZIP1:
  1373. s->scan_lines_per_block = 1;
  1374. break;
  1375. case EXR_PXR24:
  1376. case EXR_ZIP16:
  1377. s->scan_lines_per_block = 16;
  1378. break;
  1379. case EXR_PIZ:
  1380. case EXR_B44:
  1381. case EXR_B44A:
  1382. s->scan_lines_per_block = 32;
  1383. break;
  1384. default:
  1385. avpriv_report_missing_feature(avctx, "Compression %d", s->compression);
  1386. return AVERROR_PATCHWELCOME;
  1387. }
  1388. /* Verify the xmin, xmax, ymin, ymax and xdelta before setting
  1389. * the actual image size. */
  1390. if (s->xmin > s->xmax ||
  1391. s->ymin > s->ymax ||
  1392. s->xdelta != s->xmax - s->xmin + 1 ||
  1393. s->xmax >= s->w ||
  1394. s->ymax >= s->h) {
  1395. av_log(avctx, AV_LOG_ERROR, "Wrong or missing size information.\n");
  1396. return AVERROR_INVALIDDATA;
  1397. }
  1398. if ((ret = ff_set_dimensions(avctx, s->w, s->h)) < 0)
  1399. return ret;
  1400. s->desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  1401. if (!s->desc)
  1402. return AVERROR_INVALIDDATA;
  1403. out_line_size = avctx->width * 2 * s->desc->nb_components;
  1404. if (s->is_tile) {
  1405. nb_blocks = ((s->xdelta + s->tile_attr.xSize - 1) / s->tile_attr.xSize) *
  1406. ((s->ydelta + s->tile_attr.ySize - 1) / s->tile_attr.ySize);
  1407. } else { /* scanline */
  1408. nb_blocks = (s->ydelta + s->scan_lines_per_block - 1) /
  1409. s->scan_lines_per_block;
  1410. }
  1411. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
  1412. return ret;
  1413. if (bytestream2_get_bytes_left(&s->gb) < nb_blocks * 8)
  1414. return AVERROR_INVALIDDATA;
  1415. // save pointer we are going to use in decode_block
  1416. s->buf = avpkt->data;
  1417. s->buf_size = avpkt->size;
  1418. ptr = picture->data[0];
  1419. // Zero out the start if ymin is not 0
  1420. for (y = 0; y < s->ymin; y++) {
  1421. memset(ptr, 0, out_line_size);
  1422. ptr += picture->linesize[0];
  1423. }
  1424. s->picture = picture;
  1425. avctx->execute2(avctx, decode_block, s->thread_data, NULL, nb_blocks);
  1426. // Zero out the end if ymax+1 is not h
  1427. for (y = s->ymax + 1; y < avctx->height; y++) {
  1428. memset(ptr, 0, out_line_size);
  1429. ptr += picture->linesize[0];
  1430. }
  1431. picture->pict_type = AV_PICTURE_TYPE_I;
  1432. *got_frame = 1;
  1433. return avpkt->size;
  1434. }
  1435. static av_cold int decode_init(AVCodecContext *avctx)
  1436. {
  1437. EXRContext *s = avctx->priv_data;
  1438. uint32_t i;
  1439. union av_intfloat32 t;
  1440. float one_gamma = 1.0f / s->gamma;
  1441. avpriv_trc_function trc_func = NULL;
  1442. s->avctx = avctx;
  1443. trc_func = avpriv_get_trc_function_from_trc(s->apply_trc_type);
  1444. if (trc_func) {
  1445. for (i = 0; i < 65536; ++i) {
  1446. t = exr_half2float(i);
  1447. t.f = trc_func(t.f);
  1448. s->gamma_table[i] = exr_flt2uint(t.i);
  1449. }
  1450. } else {
  1451. if (one_gamma > 0.9999f && one_gamma < 1.0001f) {
  1452. for (i = 0; i < 65536; ++i)
  1453. s->gamma_table[i] = exr_halflt2uint(i);
  1454. } else {
  1455. for (i = 0; i < 65536; ++i) {
  1456. t = exr_half2float(i);
  1457. /* If negative value we reuse half value */
  1458. if (t.f <= 0.0f) {
  1459. s->gamma_table[i] = exr_halflt2uint(i);
  1460. } else {
  1461. t.f = powf(t.f, one_gamma);
  1462. s->gamma_table[i] = exr_flt2uint(t.i);
  1463. }
  1464. }
  1465. }
  1466. }
  1467. // allocate thread data, used for non EXR_RAW compression types
  1468. s->thread_data = av_mallocz_array(avctx->thread_count, sizeof(EXRThreadData));
  1469. if (!s->thread_data)
  1470. return AVERROR_INVALIDDATA;
  1471. return 0;
  1472. }
  1473. #if HAVE_THREADS
  1474. static int decode_init_thread_copy(AVCodecContext *avctx)
  1475. { EXRContext *s = avctx->priv_data;
  1476. // allocate thread data, used for non EXR_RAW compression types
  1477. s->thread_data = av_mallocz_array(avctx->thread_count, sizeof(EXRThreadData));
  1478. if (!s->thread_data)
  1479. return AVERROR_INVALIDDATA;
  1480. return 0;
  1481. }
  1482. #endif
  1483. static av_cold int decode_end(AVCodecContext *avctx)
  1484. {
  1485. EXRContext *s = avctx->priv_data;
  1486. int i;
  1487. for (i = 0; i < avctx->thread_count; i++) {
  1488. EXRThreadData *td = &s->thread_data[i];
  1489. av_freep(&td->uncompressed_data);
  1490. av_freep(&td->tmp);
  1491. av_freep(&td->bitmap);
  1492. av_freep(&td->lut);
  1493. }
  1494. av_freep(&s->thread_data);
  1495. av_freep(&s->channels);
  1496. return 0;
  1497. }
  1498. #define OFFSET(x) offsetof(EXRContext, x)
  1499. #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  1500. static const AVOption options[] = {
  1501. { "layer", "Set the decoding layer", OFFSET(layer),
  1502. AV_OPT_TYPE_STRING, { .str = "" }, 0, 0, VD },
  1503. { "gamma", "Set the float gamma value when decoding", OFFSET(gamma),
  1504. AV_OPT_TYPE_FLOAT, { .dbl = 1.0f }, 0.001, FLT_MAX, VD },
  1505. // XXX: Note the abuse of the enum using AVCOL_TRC_UNSPECIFIED to subsume the existing gamma option
  1506. { "apply_trc", "color transfer characteristics to apply to EXR linear input", OFFSET(apply_trc_type),
  1507. AV_OPT_TYPE_INT, {.i64 = AVCOL_TRC_UNSPECIFIED }, 1, AVCOL_TRC_NB-1, VD, "apply_trc_type"},
  1508. { "bt709", "BT.709", 0,
  1509. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT709 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1510. { "gamma", "gamma", 0,
  1511. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_UNSPECIFIED }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1512. { "gamma22", "BT.470 M", 0,
  1513. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_GAMMA22 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1514. { "gamma28", "BT.470 BG", 0,
  1515. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_GAMMA28 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1516. { "smpte170m", "SMPTE 170 M", 0,
  1517. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTE170M }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1518. { "smpte240m", "SMPTE 240 M", 0,
  1519. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTE240M }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1520. { "linear", "Linear", 0,
  1521. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LINEAR }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1522. { "log", "Log", 0,
  1523. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LOG }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1524. { "log_sqrt", "Log square root", 0,
  1525. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LOG_SQRT }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1526. { "iec61966_2_4", "IEC 61966-2-4", 0,
  1527. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_IEC61966_2_4 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1528. { "bt1361", "BT.1361", 0,
  1529. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT1361_ECG }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1530. { "iec61966_2_1", "IEC 61966-2-1", 0,
  1531. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_IEC61966_2_1 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1532. { "bt2020_10bit", "BT.2020 - 10 bit", 0,
  1533. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT2020_10 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1534. { "bt2020_12bit", "BT.2020 - 12 bit", 0,
  1535. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT2020_12 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1536. { "smpte2084", "SMPTE ST 2084", 0,
  1537. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTEST2084 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1538. { "smpte428_1", "SMPTE ST 428-1", 0,
  1539. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTEST428_1 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1540. { NULL },
  1541. };
  1542. static const AVClass exr_class = {
  1543. .class_name = "EXR",
  1544. .item_name = av_default_item_name,
  1545. .option = options,
  1546. .version = LIBAVUTIL_VERSION_INT,
  1547. };
  1548. AVCodec ff_exr_decoder = {
  1549. .name = "exr",
  1550. .long_name = NULL_IF_CONFIG_SMALL("OpenEXR image"),
  1551. .type = AVMEDIA_TYPE_VIDEO,
  1552. .id = AV_CODEC_ID_EXR,
  1553. .priv_data_size = sizeof(EXRContext),
  1554. .init = decode_init,
  1555. .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
  1556. .close = decode_end,
  1557. .decode = decode_frame,
  1558. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS |
  1559. AV_CODEC_CAP_SLICE_THREADS,
  1560. .priv_class = &exr_class,
  1561. };