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.

1888 lines
61KB

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