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.

1452 lines
44KB

  1. /*
  2. * OpenEXR (.exr) image decoder
  3. * Copyright (c) 2009 Jimmy Christensen
  4. *
  5. * This file is part of Libav
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * OpenEXR decoder
  24. * @author Jimmy Christensen
  25. *
  26. * For more information on the OpenEXR format, visit:
  27. * http://openexr.com/
  28. *
  29. * exr_flt2uint() and exr_halflt2uint() is credited to Reimar Döffinger.
  30. * exr_half2float() is credited to Aaftab Munshi, Dan Ginsburg, Dave Shreiner.
  31. */
  32. #include <float.h>
  33. #include <zlib.h>
  34. #include "libavutil/imgutils.h"
  35. #include "libavutil/intfloat.h"
  36. #include "libavutil/opt.h"
  37. #include "avcodec.h"
  38. #include "bytestream.h"
  39. #include "get_bits.h"
  40. #include "internal.h"
  41. #include "mathops.h"
  42. #include "thread.h"
  43. enum ExrCompr {
  44. EXR_RAW,
  45. EXR_RLE,
  46. EXR_ZIP1,
  47. EXR_ZIP16,
  48. EXR_PIZ,
  49. EXR_PXR24,
  50. EXR_B44,
  51. EXR_B44A,
  52. EXR_UNKN,
  53. };
  54. enum ExrPixelType {
  55. EXR_UINT,
  56. EXR_HALF,
  57. EXR_FLOAT,
  58. EXR_UNKNOWN,
  59. };
  60. typedef struct EXRChannel {
  61. int xsub, ysub;
  62. enum ExrPixelType pixel_type;
  63. } EXRChannel;
  64. typedef struct EXRThreadData {
  65. uint8_t *uncompressed_data;
  66. int uncompressed_size;
  67. uint8_t *tmp;
  68. int tmp_size;
  69. uint8_t *bitmap;
  70. uint16_t *lut;
  71. } EXRThreadData;
  72. typedef struct EXRContext {
  73. AVClass *class;
  74. AVFrame *picture;
  75. AVCodecContext *avctx;
  76. enum ExrCompr compression;
  77. enum ExrPixelType pixel_type;
  78. int channel_offsets[4]; // 0 = red, 1 = green, 2 = blue and 3 = alpha
  79. const AVPixFmtDescriptor *desc;
  80. int w, h;
  81. uint32_t xmax, xmin;
  82. uint32_t ymax, ymin;
  83. uint32_t xdelta, ydelta;
  84. int ysize;
  85. uint64_t scan_line_size;
  86. int scan_lines_per_block;
  87. GetByteContext gb;
  88. const uint8_t *buf;
  89. int buf_size;
  90. EXRChannel *channels;
  91. int nb_channels;
  92. EXRThreadData *thread_data;
  93. const char *layer;
  94. float gamma;
  95. uint16_t gamma_table[65536];
  96. } EXRContext;
  97. /* -15 stored using a single precision bias of 127 */
  98. #define HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP 0x38000000
  99. /* max exponent value in single precision that will be converted
  100. * to Inf or Nan when stored as a half-float */
  101. #define HALF_FLOAT_MAX_BIASED_EXP_AS_SINGLE_FP_EXP 0x47800000
  102. /* 255 is the max exponent biased value */
  103. #define FLOAT_MAX_BIASED_EXP (0xFF << 23)
  104. #define HALF_FLOAT_MAX_BIASED_EXP (0x1F << 10)
  105. /**
  106. * Convert a half float as a uint16_t into a full float.
  107. *
  108. * @param hf half float as uint16_t
  109. *
  110. * @return float value
  111. */
  112. static union av_intfloat32 exr_half2float(uint16_t hf)
  113. {
  114. unsigned int sign = (unsigned int) (hf >> 15);
  115. unsigned int mantissa = (unsigned int) (hf & ((1 << 10) - 1));
  116. unsigned int exp = (unsigned int) (hf & HALF_FLOAT_MAX_BIASED_EXP);
  117. union av_intfloat32 f;
  118. if (exp == HALF_FLOAT_MAX_BIASED_EXP) {
  119. // we have a half-float NaN or Inf
  120. // half-float NaNs will be converted to a single precision NaN
  121. // half-float Infs will be converted to a single precision Inf
  122. exp = FLOAT_MAX_BIASED_EXP;
  123. if (mantissa)
  124. mantissa = (1 << 23) - 1; // set all bits to indicate a NaN
  125. } else if (exp == 0x0) {
  126. // convert half-float zero/denorm to single precision value
  127. if (mantissa) {
  128. mantissa <<= 1;
  129. exp = HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP;
  130. // check for leading 1 in denorm mantissa
  131. while ((mantissa & (1 << 10))) {
  132. // for every leading 0, decrement single precision exponent by 1
  133. // and shift half-float mantissa value to the left
  134. mantissa <<= 1;
  135. exp -= (1 << 23);
  136. }
  137. // clamp the mantissa to 10 bits
  138. mantissa &= ((1 << 10) - 1);
  139. // shift left to generate single-precision mantissa of 23 bits
  140. mantissa <<= 13;
  141. }
  142. } else {
  143. // shift left to generate single-precision mantissa of 23 bits
  144. mantissa <<= 13;
  145. // generate single precision biased exponent value
  146. exp = (exp << 13) + HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP;
  147. }
  148. f.i = (sign << 31) | exp | mantissa;
  149. return f;
  150. }
  151. /**
  152. * Convert from 32-bit float as uint32_t to uint16_t.
  153. *
  154. * @param v 32-bit float
  155. *
  156. * @return normalized 16-bit unsigned int
  157. */
  158. static inline uint16_t exr_flt2uint(uint32_t v)
  159. {
  160. unsigned int exp = v >> 23;
  161. // "HACK": negative values result in exp< 0, so clipping them to 0
  162. // is also handled by this condition, avoids explicit check for sign bit.
  163. if (exp <= 127 + 7 - 24) // we would shift out all bits anyway
  164. return 0;
  165. if (exp >= 127)
  166. return 0xffff;
  167. v &= 0x007fffff;
  168. return (v + (1 << 23)) >> (127 + 7 - exp);
  169. }
  170. /**
  171. * Convert from 16-bit float as uint16_t to uint16_t.
  172. *
  173. * @param v 16-bit float
  174. *
  175. * @return normalized 16-bit unsigned int
  176. */
  177. static inline uint16_t exr_halflt2uint(uint16_t v)
  178. {
  179. unsigned exp = 14 - (v >> 10);
  180. if (exp >= 14) {
  181. if (exp == 14)
  182. return (v >> 9) & 1;
  183. else
  184. return (v & 0x8000) ? 0 : 0xffff;
  185. }
  186. v <<= 6;
  187. return (v + (1 << 16)) >> (exp + 1);
  188. }
  189. static void predictor(uint8_t *src, int size)
  190. {
  191. uint8_t *t = src + 1;
  192. uint8_t *stop = src + size;
  193. while (t < stop) {
  194. int d = (int) t[-1] + (int) t[0] - 128;
  195. t[0] = d;
  196. ++t;
  197. }
  198. }
  199. static void reorder_pixels(uint8_t *src, uint8_t *dst, int size)
  200. {
  201. const int8_t *t1 = src;
  202. const int8_t *t2 = src + (size + 1) / 2;
  203. int8_t *s = dst;
  204. int8_t *stop = s + size;
  205. while (1) {
  206. if (s < stop)
  207. *(s++) = *(t1++);
  208. else
  209. break;
  210. if (s < stop)
  211. *(s++) = *(t2++);
  212. else
  213. break;
  214. }
  215. }
  216. static int zip_uncompress(const uint8_t *src, int compressed_size,
  217. int uncompressed_size, EXRThreadData *td)
  218. {
  219. unsigned long dest_len = uncompressed_size;
  220. if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK ||
  221. dest_len != uncompressed_size)
  222. return AVERROR_INVALIDDATA;
  223. predictor(td->tmp, uncompressed_size);
  224. reorder_pixels(td->tmp, td->uncompressed_data, uncompressed_size);
  225. return 0;
  226. }
  227. static int rle_uncompress(const uint8_t *src, int compressed_size,
  228. int uncompressed_size, EXRThreadData *td)
  229. {
  230. uint8_t *d = td->tmp;
  231. const int8_t *s = src;
  232. int ssize = compressed_size;
  233. int dsize = uncompressed_size;
  234. uint8_t *dend = d + dsize;
  235. int count;
  236. while (ssize > 0) {
  237. count = *s++;
  238. if (count < 0) {
  239. count = -count;
  240. if ((dsize -= count) < 0 ||
  241. (ssize -= count + 1) < 0)
  242. return AVERROR_INVALIDDATA;
  243. while (count--)
  244. *d++ = *s++;
  245. } else {
  246. count++;
  247. if ((dsize -= count) < 0 ||
  248. (ssize -= 2) < 0)
  249. return AVERROR_INVALIDDATA;
  250. while (count--)
  251. *d++ = *s;
  252. s++;
  253. }
  254. }
  255. if (dend != d)
  256. return AVERROR_INVALIDDATA;
  257. predictor(td->tmp, uncompressed_size);
  258. reorder_pixels(td->tmp, td->uncompressed_data, uncompressed_size);
  259. return 0;
  260. }
  261. #define USHORT_RANGE (1 << 16)
  262. #define BITMAP_SIZE (1 << 13)
  263. static uint16_t reverse_lut(const uint8_t *bitmap, uint16_t *lut)
  264. {
  265. int i, k = 0;
  266. for (i = 0; i < USHORT_RANGE; i++)
  267. if ((i == 0) || (bitmap[i >> 3] & (1 << (i & 7))))
  268. lut[k++] = i;
  269. i = k - 1;
  270. memset(lut + k, 0, (USHORT_RANGE - k) * 2);
  271. return i;
  272. }
  273. static void apply_lut(const uint16_t *lut, uint16_t *dst, int dsize)
  274. {
  275. int i;
  276. for (i = 0; i < dsize; ++i)
  277. dst[i] = lut[dst[i]];
  278. }
  279. #define HUF_ENCBITS 16 // literal (value) bit length
  280. #define HUF_DECBITS 14 // decoding bit size (>= 8)
  281. #define HUF_ENCSIZE ((1 << HUF_ENCBITS) + 1) // encoding table size
  282. #define HUF_DECSIZE (1 << HUF_DECBITS) // decoding table size
  283. #define HUF_DECMASK (HUF_DECSIZE - 1)
  284. typedef struct HufDec {
  285. int len;
  286. int lit;
  287. int *p;
  288. } HufDec;
  289. static void huf_canonical_code_table(uint64_t *hcode)
  290. {
  291. uint64_t c, n[59] = { 0 };
  292. int i;
  293. for (i = 0; i < HUF_ENCSIZE; ++i)
  294. n[hcode[i]] += 1;
  295. c = 0;
  296. for (i = 58; i > 0; --i) {
  297. uint64_t nc = ((c + n[i]) >> 1);
  298. n[i] = c;
  299. c = nc;
  300. }
  301. for (i = 0; i < HUF_ENCSIZE; ++i) {
  302. int l = hcode[i];
  303. if (l > 0)
  304. hcode[i] = l | (n[l]++ << 6);
  305. }
  306. }
  307. #define SHORT_ZEROCODE_RUN 59
  308. #define LONG_ZEROCODE_RUN 63
  309. #define SHORTEST_LONG_RUN (2 + LONG_ZEROCODE_RUN - SHORT_ZEROCODE_RUN)
  310. #define LONGEST_LONG_RUN (255 + SHORTEST_LONG_RUN)
  311. static int huf_unpack_enc_table(GetByteContext *gb,
  312. int32_t im, int32_t iM, uint64_t *hcode)
  313. {
  314. GetBitContext gbit;
  315. int ret = init_get_bits8(&gbit, gb->buffer, bytestream2_get_bytes_left(gb));
  316. if (ret < 0)
  317. return ret;
  318. for (; im <= iM; im++) {
  319. uint64_t l = hcode[im] = get_bits(&gbit, 6);
  320. if (l == LONG_ZEROCODE_RUN) {
  321. int zerun = get_bits(&gbit, 8) + SHORTEST_LONG_RUN;
  322. if (im + zerun > iM + 1)
  323. return AVERROR_INVALIDDATA;
  324. while (zerun--)
  325. hcode[im++] = 0;
  326. im--;
  327. } else if (l >= SHORT_ZEROCODE_RUN) {
  328. int zerun = l - SHORT_ZEROCODE_RUN + 2;
  329. if (im + zerun > iM + 1)
  330. return AVERROR_INVALIDDATA;
  331. while (zerun--)
  332. hcode[im++] = 0;
  333. im--;
  334. }
  335. }
  336. bytestream2_skip(gb, (get_bits_count(&gbit) + 7) / 8);
  337. huf_canonical_code_table(hcode);
  338. return 0;
  339. }
  340. static int huf_build_dec_table(const uint64_t *hcode, int im,
  341. int iM, HufDec *hdecod)
  342. {
  343. for (; im <= iM; im++) {
  344. uint64_t c = hcode[im] >> 6;
  345. int i, l = hcode[im] & 63;
  346. if (c >> l)
  347. return AVERROR_INVALIDDATA;
  348. if (l > HUF_DECBITS) {
  349. HufDec *pl = hdecod + (c >> (l - HUF_DECBITS));
  350. if (pl->len)
  351. return AVERROR_INVALIDDATA;
  352. pl->lit++;
  353. pl->p = av_realloc(pl->p, pl->lit * sizeof(int));
  354. if (!pl->p)
  355. return AVERROR(ENOMEM);
  356. pl->p[pl->lit - 1] = im;
  357. } else if (l) {
  358. HufDec *pl = hdecod + (c << (HUF_DECBITS - l));
  359. for (i = 1 << (HUF_DECBITS - l); i > 0; i--, pl++) {
  360. if (pl->len || pl->p)
  361. return AVERROR_INVALIDDATA;
  362. pl->len = l;
  363. pl->lit = im;
  364. }
  365. }
  366. }
  367. return 0;
  368. }
  369. #define get_char(c, lc, gb) \
  370. { \
  371. c = (c << 8) | bytestream2_get_byte(gb); \
  372. lc += 8; \
  373. }
  374. #define get_code(po, rlc, c, lc, gb, out, oe, outb) \
  375. { \
  376. if (po == rlc) { \
  377. if (lc < 8) \
  378. get_char(c, lc, gb); \
  379. lc -= 8; \
  380. \
  381. cs = c >> lc; \
  382. \
  383. if (out + cs > oe || out == outb) \
  384. return AVERROR_INVALIDDATA; \
  385. \
  386. s = out[-1]; \
  387. \
  388. while (cs-- > 0) \
  389. *out++ = s; \
  390. } else if (out < oe) { \
  391. *out++ = po; \
  392. } else { \
  393. return AVERROR_INVALIDDATA; \
  394. } \
  395. }
  396. static int huf_decode(const uint64_t *hcode, const HufDec *hdecod,
  397. GetByteContext *gb, int nbits,
  398. int rlc, int no, uint16_t *out)
  399. {
  400. uint64_t c = 0;
  401. uint16_t *outb = out;
  402. uint16_t *oe = out + no;
  403. const uint8_t *ie = gb->buffer + (nbits + 7) / 8; // input byte size
  404. uint8_t cs, s;
  405. int i, lc = 0;
  406. while (gb->buffer < ie) {
  407. get_char(c, lc, gb);
  408. while (lc >= HUF_DECBITS) {
  409. const HufDec pl = hdecod[(c >> (lc - HUF_DECBITS)) & HUF_DECMASK];
  410. if (pl.len) {
  411. lc -= pl.len;
  412. get_code(pl.lit, rlc, c, lc, gb, out, oe, outb);
  413. } else {
  414. int j;
  415. if (!pl.p)
  416. return AVERROR_INVALIDDATA;
  417. for (j = 0; j < pl.lit; j++) {
  418. int l = hcode[pl.p[j]] & 63;
  419. while (lc < l && bytestream2_get_bytes_left(gb) > 0)
  420. get_char(c, lc, gb);
  421. if (lc >= l) {
  422. if ((hcode[pl.p[j]] >> 6) ==
  423. ((c >> (lc - l)) & ((1LL << l) - 1))) {
  424. lc -= l;
  425. get_code(pl.p[j], rlc, c, lc, gb, out, oe, outb);
  426. break;
  427. }
  428. }
  429. }
  430. if (j == pl.lit)
  431. return AVERROR_INVALIDDATA;
  432. }
  433. }
  434. }
  435. i = (8 - nbits) & 7;
  436. c >>= i;
  437. lc -= i;
  438. while (lc > 0) {
  439. const HufDec pl = hdecod[(c << (HUF_DECBITS - lc)) & 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. return AVERROR_INVALIDDATA;
  445. }
  446. }
  447. if (out - outb != no)
  448. return AVERROR_INVALIDDATA;
  449. return 0;
  450. }
  451. static int huf_uncompress(GetByteContext *gb,
  452. uint16_t *dst, int dst_size)
  453. {
  454. int32_t src_size, im, iM;
  455. uint32_t nBits;
  456. uint64_t *freq;
  457. HufDec *hdec;
  458. int ret, i;
  459. src_size = bytestream2_get_le32(gb);
  460. im = bytestream2_get_le32(gb);
  461. iM = bytestream2_get_le32(gb);
  462. bytestream2_skip(gb, 4);
  463. nBits = bytestream2_get_le32(gb);
  464. if (im < 0 || im >= HUF_ENCSIZE ||
  465. iM < 0 || iM >= HUF_ENCSIZE ||
  466. src_size < 0)
  467. return AVERROR_INVALIDDATA;
  468. bytestream2_skip(gb, 4);
  469. freq = av_mallocz_array(HUF_ENCSIZE, sizeof(*freq));
  470. hdec = av_mallocz_array(HUF_DECSIZE, sizeof(*hdec));
  471. if (!freq || !hdec) {
  472. ret = AVERROR(ENOMEM);
  473. goto fail;
  474. }
  475. if ((ret = huf_unpack_enc_table(gb, im, iM, freq)) < 0)
  476. goto fail;
  477. if (nBits > 8 * bytestream2_get_bytes_left(gb)) {
  478. ret = AVERROR_INVALIDDATA;
  479. goto fail;
  480. }
  481. if ((ret = huf_build_dec_table(freq, im, iM, hdec)) < 0)
  482. goto fail;
  483. ret = huf_decode(freq, hdec, gb, nBits, iM, dst_size, dst);
  484. fail:
  485. for (i = 0; i < HUF_DECSIZE; i++)
  486. if (hdec)
  487. av_freep(&hdec[i].p);
  488. av_free(freq);
  489. av_free(hdec);
  490. return ret;
  491. }
  492. static inline void wdec14(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
  493. {
  494. int16_t ls = l;
  495. int16_t hs = h;
  496. int hi = hs;
  497. int ai = ls + (hi & 1) + (hi >> 1);
  498. int16_t as = ai;
  499. int16_t bs = ai - hi;
  500. *a = as;
  501. *b = bs;
  502. }
  503. #define NBITS 16
  504. #define A_OFFSET (1 << (NBITS - 1))
  505. #define MOD_MASK ((1 << NBITS) - 1)
  506. static inline void wdec16(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
  507. {
  508. int m = l;
  509. int d = h;
  510. int bb = (m - (d >> 1)) & MOD_MASK;
  511. int aa = (d + bb - A_OFFSET) & MOD_MASK;
  512. *b = bb;
  513. *a = aa;
  514. }
  515. static void wav_decode(uint16_t *in, int nx, int ox,
  516. int ny, int oy, uint16_t mx)
  517. {
  518. int w14 = (mx < (1 << 14));
  519. int n = (nx > ny) ? ny : nx;
  520. int p = 1;
  521. int p2;
  522. while (p <= n)
  523. p <<= 1;
  524. p >>= 1;
  525. p2 = p;
  526. p >>= 1;
  527. while (p >= 1) {
  528. uint16_t *py = in;
  529. uint16_t *ey = in + oy * (ny - p2);
  530. uint16_t i00, i01, i10, i11;
  531. int oy1 = oy * p;
  532. int oy2 = oy * p2;
  533. int ox1 = ox * p;
  534. int ox2 = ox * p2;
  535. for (; py <= ey; py += oy2) {
  536. uint16_t *px = py;
  537. uint16_t *ex = py + ox * (nx - p2);
  538. for (; px <= ex; px += ox2) {
  539. uint16_t *p01 = px + ox1;
  540. uint16_t *p10 = px + oy1;
  541. uint16_t *p11 = p10 + ox1;
  542. if (w14) {
  543. wdec14(*px, *p10, &i00, &i10);
  544. wdec14(*p01, *p11, &i01, &i11);
  545. wdec14(i00, i01, px, p01);
  546. wdec14(i10, i11, p10, p11);
  547. } else {
  548. wdec16(*px, *p10, &i00, &i10);
  549. wdec16(*p01, *p11, &i01, &i11);
  550. wdec16(i00, i01, px, p01);
  551. wdec16(i10, i11, p10, p11);
  552. }
  553. }
  554. if (nx & p) {
  555. uint16_t *p10 = px + oy1;
  556. if (w14)
  557. wdec14(*px, *p10, &i00, p10);
  558. else
  559. wdec16(*px, *p10, &i00, p10);
  560. *px = i00;
  561. }
  562. }
  563. if (ny & p) {
  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. if (w14)
  569. wdec14(*px, *p01, &i00, p01);
  570. else
  571. wdec16(*px, *p01, &i00, p01);
  572. *px = i00;
  573. }
  574. }
  575. p2 = p;
  576. p >>= 1;
  577. }
  578. }
  579. static int piz_uncompress(EXRContext *s, const uint8_t *src, int ssize,
  580. int dsize, EXRThreadData *td)
  581. {
  582. GetByteContext gb;
  583. uint16_t maxval, min_non_zero, max_non_zero;
  584. uint16_t *ptr;
  585. uint16_t *tmp = (uint16_t *)td->tmp;
  586. uint8_t *out;
  587. int ret, i, j;
  588. if (!td->bitmap)
  589. td->bitmap = av_malloc(BITMAP_SIZE);
  590. if (!td->lut)
  591. td->lut = av_malloc(1 << 17);
  592. if (!td->bitmap || !td->lut) {
  593. av_free(td->bitmap);
  594. av_free(td->lut);
  595. return AVERROR(ENOMEM);
  596. }
  597. bytestream2_init(&gb, src, ssize);
  598. min_non_zero = bytestream2_get_le16(&gb);
  599. max_non_zero = bytestream2_get_le16(&gb);
  600. if (max_non_zero >= BITMAP_SIZE)
  601. return AVERROR_INVALIDDATA;
  602. memset(td->bitmap, 0, FFMIN(min_non_zero, BITMAP_SIZE));
  603. if (min_non_zero <= max_non_zero)
  604. bytestream2_get_buffer(&gb, td->bitmap + min_non_zero,
  605. max_non_zero - min_non_zero + 1);
  606. memset(td->bitmap + max_non_zero, 0, BITMAP_SIZE - max_non_zero);
  607. maxval = reverse_lut(td->bitmap, td->lut);
  608. ret = huf_uncompress(&gb, tmp, dsize / sizeof(uint16_t));
  609. if (ret)
  610. return ret;
  611. ptr = tmp;
  612. for (i = 0; i < s->nb_channels; i++) {
  613. EXRChannel *channel = &s->channels[i];
  614. int size = channel->pixel_type;
  615. for (j = 0; j < size; j++)
  616. wav_decode(ptr + j, s->xdelta, size, s->ysize,
  617. s->xdelta * size, maxval);
  618. ptr += s->xdelta * s->ysize * size;
  619. }
  620. apply_lut(td->lut, tmp, dsize / sizeof(uint16_t));
  621. out = td->uncompressed_data;
  622. for (i = 0; i < s->ysize; i++)
  623. for (j = 0; j < s->nb_channels; j++) {
  624. uint16_t *in = tmp + j * s->xdelta * s->ysize + i * s->xdelta;
  625. memcpy(out, in, s->xdelta * 2);
  626. out += s->xdelta * 2;
  627. }
  628. return 0;
  629. }
  630. static int pxr24_uncompress(EXRContext *s, const uint8_t *src,
  631. int compressed_size, int uncompressed_size,
  632. EXRThreadData *td)
  633. {
  634. unsigned long dest_len = uncompressed_size;
  635. const uint8_t *in = td->tmp;
  636. uint8_t *out;
  637. int c, i, j;
  638. if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK ||
  639. dest_len != uncompressed_size)
  640. return AVERROR_INVALIDDATA;
  641. out = td->uncompressed_data;
  642. for (i = 0; i < s->ysize; i++)
  643. for (c = 0; c < s->nb_channels; c++) {
  644. EXRChannel *channel = &s->channels[c];
  645. const uint8_t *ptr[4];
  646. uint32_t pixel = 0;
  647. switch (channel->pixel_type) {
  648. case EXR_FLOAT:
  649. ptr[0] = in;
  650. ptr[1] = ptr[0] + s->xdelta;
  651. ptr[2] = ptr[1] + s->xdelta;
  652. in = ptr[2] + s->xdelta;
  653. for (j = 0; j < s->xdelta; ++j) {
  654. uint32_t diff = (*(ptr[0]++) << 24) |
  655. (*(ptr[1]++) << 16) |
  656. (*(ptr[2]++) << 8);
  657. pixel += diff;
  658. bytestream_put_le32(&out, pixel);
  659. }
  660. break;
  661. case EXR_HALF:
  662. ptr[0] = in;
  663. ptr[1] = ptr[0] + s->xdelta;
  664. in = ptr[1] + s->xdelta;
  665. for (j = 0; j < s->xdelta; j++) {
  666. uint32_t diff = (*(ptr[0]++) << 8) | *(ptr[1]++);
  667. pixel += diff;
  668. bytestream_put_le16(&out, pixel);
  669. }
  670. break;
  671. default:
  672. return AVERROR_INVALIDDATA;
  673. }
  674. }
  675. return 0;
  676. }
  677. static int decode_block(AVCodecContext *avctx, void *tdata,
  678. int jobnr, int threadnr)
  679. {
  680. EXRContext *s = avctx->priv_data;
  681. AVFrame *const p = s->picture;
  682. EXRThreadData *td = &s->thread_data[threadnr];
  683. const uint8_t *channel_buffer[4] = { 0 };
  684. const uint8_t *buf = s->buf;
  685. uint64_t line_offset, uncompressed_size;
  686. uint32_t xdelta = s->xdelta;
  687. uint16_t *ptr_x;
  688. uint8_t *ptr;
  689. uint32_t data_size, line;
  690. const uint8_t *src;
  691. int axmax = (avctx->width - (s->xmax + 1)) * 2 * s->desc->nb_components;
  692. int bxmin = s->xmin * 2 * s->desc->nb_components;
  693. int i, x, buf_size = s->buf_size;
  694. float one_gamma = 1.0f / s->gamma;
  695. int ret;
  696. line_offset = AV_RL64(s->gb.buffer + jobnr * 8);
  697. // Check if the buffer has the required bytes needed from the offset
  698. if (line_offset > buf_size - 8)
  699. return AVERROR_INVALIDDATA;
  700. src = buf + line_offset + 8;
  701. line = AV_RL32(src - 8);
  702. if (line < s->ymin || line > s->ymax)
  703. return AVERROR_INVALIDDATA;
  704. data_size = AV_RL32(src - 4);
  705. if (data_size <= 0 || data_size > buf_size)
  706. return AVERROR_INVALIDDATA;
  707. s->ysize = FFMIN(s->scan_lines_per_block, s->ymax - line + 1);
  708. uncompressed_size = s->scan_line_size * s->ysize;
  709. if ((s->compression == EXR_RAW && (data_size != uncompressed_size ||
  710. line_offset > buf_size - uncompressed_size)) ||
  711. (s->compression != EXR_RAW && (data_size > uncompressed_size ||
  712. line_offset > buf_size - data_size))) {
  713. return AVERROR_INVALIDDATA;
  714. }
  715. if (data_size < uncompressed_size) {
  716. av_fast_padded_malloc(&td->uncompressed_data,
  717. &td->uncompressed_size, uncompressed_size);
  718. av_fast_padded_malloc(&td->tmp, &td->tmp_size, uncompressed_size);
  719. if (!td->uncompressed_data || !td->tmp)
  720. return AVERROR(ENOMEM);
  721. ret = AVERROR_INVALIDDATA;
  722. switch (s->compression) {
  723. case EXR_ZIP1:
  724. case EXR_ZIP16:
  725. ret = zip_uncompress(src, data_size, uncompressed_size, td);
  726. break;
  727. case EXR_PIZ:
  728. ret = piz_uncompress(s, src, data_size, uncompressed_size, td);
  729. break;
  730. case EXR_PXR24:
  731. ret = pxr24_uncompress(s, src, data_size, uncompressed_size, td);
  732. break;
  733. case EXR_RLE:
  734. ret = rle_uncompress(src, data_size, uncompressed_size, td);
  735. }
  736. if (ret < 0) {
  737. av_log(avctx, AV_LOG_ERROR, "decode_block() failed.\n");
  738. return ret;
  739. }
  740. src = td->uncompressed_data;
  741. }
  742. channel_buffer[0] = src + xdelta * s->channel_offsets[0];
  743. channel_buffer[1] = src + xdelta * s->channel_offsets[1];
  744. channel_buffer[2] = src + xdelta * s->channel_offsets[2];
  745. if (s->channel_offsets[3] >= 0)
  746. channel_buffer[3] = src + xdelta * s->channel_offsets[3];
  747. ptr = p->data[0] + line * p->linesize[0];
  748. for (i = 0;
  749. i < s->scan_lines_per_block && line + i <= s->ymax;
  750. i++, ptr += p->linesize[0]) {
  751. const uint8_t *r, *g, *b, *a;
  752. r = channel_buffer[0];
  753. g = channel_buffer[1];
  754. b = channel_buffer[2];
  755. if (channel_buffer[3])
  756. a = channel_buffer[3];
  757. ptr_x = (uint16_t *) ptr;
  758. // Zero out the start if xmin is not 0
  759. memset(ptr_x, 0, bxmin);
  760. ptr_x += s->xmin * s->desc->nb_components;
  761. if (s->pixel_type == EXR_FLOAT) {
  762. // 32-bit
  763. for (x = 0; x < xdelta; x++) {
  764. union av_intfloat32 t;
  765. t.i = bytestream_get_le32(&r);
  766. if (t.f > 0.0f) /* avoid negative values */
  767. t.f = powf(t.f, one_gamma);
  768. *ptr_x++ = exr_flt2uint(t.i);
  769. t.i = bytestream_get_le32(&g);
  770. if (t.f > 0.0f)
  771. t.f = powf(t.f, one_gamma);
  772. *ptr_x++ = exr_flt2uint(t.i);
  773. t.i = bytestream_get_le32(&b);
  774. if (t.f > 0.0f)
  775. t.f = powf(t.f, one_gamma);
  776. *ptr_x++ = exr_flt2uint(t.i);
  777. if (channel_buffer[3])
  778. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a));
  779. }
  780. } else {
  781. // 16-bit
  782. for (x = 0; x < xdelta; x++) {
  783. *ptr_x++ = s->gamma_table[bytestream_get_le16(&r)];
  784. *ptr_x++ = s->gamma_table[bytestream_get_le16(&g)];
  785. *ptr_x++ = s->gamma_table[bytestream_get_le16(&b)];
  786. if (channel_buffer[3])
  787. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&a));
  788. }
  789. }
  790. // Zero out the end if xmax+1 is not w
  791. memset(ptr_x, 0, axmax);
  792. channel_buffer[0] += s->scan_line_size;
  793. channel_buffer[1] += s->scan_line_size;
  794. channel_buffer[2] += s->scan_line_size;
  795. if (channel_buffer[3])
  796. channel_buffer[3] += s->scan_line_size;
  797. }
  798. return 0;
  799. }
  800. /**
  801. * Check if the variable name corresponds to its data type.
  802. *
  803. * @param s the EXRContext
  804. * @param value_name name of the variable to check
  805. * @param value_type type of the variable to check
  806. * @param minimum_length minimum length of the variable data
  807. *
  808. * @return bytes to read containing variable data
  809. * -1 if variable is not found
  810. * 0 if buffer ended prematurely
  811. */
  812. static int check_header_variable(EXRContext *s,
  813. const char *value_name,
  814. const char *value_type,
  815. unsigned int minimum_length)
  816. {
  817. int var_size = -1;
  818. if (bytestream2_get_bytes_left(&s->gb) >= minimum_length &&
  819. !strcmp(s->gb.buffer, value_name)) {
  820. // found value_name, jump to value_type (null terminated strings)
  821. s->gb.buffer += strlen(value_name) + 1;
  822. if (!strcmp(s->gb.buffer, value_type)) {
  823. s->gb.buffer += strlen(value_type) + 1;
  824. var_size = bytestream2_get_le32(&s->gb);
  825. // don't go read past boundaries
  826. if (var_size > bytestream2_get_bytes_left(&s->gb))
  827. var_size = 0;
  828. } else {
  829. // value_type not found, reset the buffer
  830. s->gb.buffer -= strlen(value_name) + 1;
  831. av_log(s->avctx, AV_LOG_WARNING,
  832. "Unknown data type %s for header variable %s.\n",
  833. value_type, value_name);
  834. }
  835. }
  836. return var_size;
  837. }
  838. static int decode_header(EXRContext *s)
  839. {
  840. int current_channel_offset = 0;
  841. int magic_number, version, flags, i;
  842. if (bytestream2_get_bytes_left(&s->gb) < 10) {
  843. av_log(s->avctx, AV_LOG_ERROR, "Header too short to parse.\n");
  844. return AVERROR_INVALIDDATA;
  845. }
  846. magic_number = bytestream2_get_le32(&s->gb);
  847. if (magic_number != 20000630) {
  848. /* As per documentation of OpenEXR, it is supposed to be
  849. * int 20000630 little-endian */
  850. av_log(s->avctx, AV_LOG_ERROR, "Wrong magic number %d.\n", magic_number);
  851. return AVERROR_INVALIDDATA;
  852. }
  853. version = bytestream2_get_byte(&s->gb);
  854. if (version != 2) {
  855. avpriv_report_missing_feature(s->avctx, "Version %d", version);
  856. return AVERROR_PATCHWELCOME;
  857. }
  858. flags = bytestream2_get_le24(&s->gb);
  859. if (flags & 0x02) {
  860. avpriv_report_missing_feature(s->avctx, "Tile support");
  861. return AVERROR_PATCHWELCOME;
  862. }
  863. // Parse the header
  864. while (bytestream2_get_bytes_left(&s->gb) > 0 && *s->gb.buffer) {
  865. int var_size;
  866. if ((var_size = check_header_variable(s, "channels",
  867. "chlist", 38)) >= 0) {
  868. GetByteContext ch_gb;
  869. if (!var_size)
  870. return AVERROR_INVALIDDATA;
  871. bytestream2_init(&ch_gb, s->gb.buffer, var_size);
  872. while (bytestream2_get_bytes_left(&ch_gb) >= 19) {
  873. EXRChannel *channel;
  874. enum ExrPixelType current_pixel_type;
  875. int channel_index = -1;
  876. int xsub, ysub;
  877. if (strcmp(s->layer, "") != 0) {
  878. if (strncmp(ch_gb.buffer, s->layer, strlen(s->layer)) == 0) {
  879. ch_gb.buffer += strlen(s->layer);
  880. if (*ch_gb.buffer == '.')
  881. ch_gb.buffer++; /* skip dot if not given */
  882. av_log(s->avctx, AV_LOG_INFO,
  883. "Layer %s.%s matched.\n", s->layer, ch_gb.buffer);
  884. }
  885. }
  886. if (!strcmp(ch_gb.buffer, "R") ||
  887. !strcmp(ch_gb.buffer, "X") ||
  888. !strcmp(ch_gb.buffer, "U"))
  889. channel_index = 0;
  890. else if (!strcmp(ch_gb.buffer, "G") ||
  891. !strcmp(ch_gb.buffer, "Y") ||
  892. !strcmp(ch_gb.buffer, "V"))
  893. channel_index = 1;
  894. else if (!strcmp(ch_gb.buffer, "B") ||
  895. !strcmp(ch_gb.buffer, "Z") ||
  896. !strcmp(ch_gb.buffer, "W"))
  897. channel_index = 2;
  898. else if (!strcmp(ch_gb.buffer, "A"))
  899. channel_index = 3;
  900. else
  901. av_log(s->avctx, AV_LOG_WARNING,
  902. "Unsupported channel %.256s.\n", ch_gb.buffer);
  903. /* skip until you get a 0 */
  904. while (bytestream2_get_bytes_left(&ch_gb) > 0 &&
  905. bytestream2_get_byte(&ch_gb))
  906. continue;
  907. if (bytestream2_get_bytes_left(&ch_gb) < 4) {
  908. av_log(s->avctx, AV_LOG_ERROR, "Incomplete header.\n");
  909. return AVERROR_INVALIDDATA;
  910. }
  911. current_pixel_type = bytestream2_get_le32(&ch_gb);
  912. if (current_pixel_type >= EXR_UNKNOWN) {
  913. avpriv_report_missing_feature(s->avctx, "Pixel type %d",
  914. current_pixel_type);
  915. return AVERROR_PATCHWELCOME;
  916. }
  917. bytestream2_skip(&ch_gb, 4);
  918. xsub = bytestream2_get_le32(&ch_gb);
  919. ysub = bytestream2_get_le32(&ch_gb);
  920. if (xsub != 1 || ysub != 1) {
  921. avpriv_report_missing_feature(s->avctx,
  922. "Subsampling %dx%d",
  923. xsub, ysub);
  924. return AVERROR_PATCHWELCOME;
  925. }
  926. if (channel_index >= 0) {
  927. if (s->pixel_type != EXR_UNKNOWN &&
  928. s->pixel_type != current_pixel_type) {
  929. av_log(s->avctx, AV_LOG_ERROR,
  930. "RGB channels not of the same depth.\n");
  931. return AVERROR_INVALIDDATA;
  932. }
  933. s->pixel_type = current_pixel_type;
  934. s->channel_offsets[channel_index] = current_channel_offset;
  935. }
  936. s->channels = av_realloc(s->channels,
  937. ++s->nb_channels * sizeof(EXRChannel));
  938. if (!s->channels)
  939. return AVERROR(ENOMEM);
  940. channel = &s->channels[s->nb_channels - 1];
  941. channel->pixel_type = current_pixel_type;
  942. channel->xsub = xsub;
  943. channel->ysub = ysub;
  944. current_channel_offset += 1 << current_pixel_type;
  945. }
  946. /* Check if all channels are set with an offset or if the channels
  947. * are causing an overflow */
  948. if (FFMIN3(s->channel_offsets[0],
  949. s->channel_offsets[1],
  950. s->channel_offsets[2]) < 0) {
  951. if (s->channel_offsets[0] < 0)
  952. av_log(s->avctx, AV_LOG_ERROR, "Missing red channel.\n");
  953. if (s->channel_offsets[1] < 0)
  954. av_log(s->avctx, AV_LOG_ERROR, "Missing green channel.\n");
  955. if (s->channel_offsets[2] < 0)
  956. av_log(s->avctx, AV_LOG_ERROR, "Missing blue channel.\n");
  957. return AVERROR_INVALIDDATA;
  958. }
  959. // skip one last byte and update main gb
  960. s->gb.buffer = ch_gb.buffer + 1;
  961. continue;
  962. } else if ((var_size = check_header_variable(s, "dataWindow", "box2i",
  963. 31)) >= 0) {
  964. if (!var_size)
  965. return AVERROR_INVALIDDATA;
  966. s->xmin = bytestream2_get_le32(&s->gb);
  967. s->ymin = bytestream2_get_le32(&s->gb);
  968. s->xmax = bytestream2_get_le32(&s->gb);
  969. s->ymax = bytestream2_get_le32(&s->gb);
  970. s->xdelta = (s->xmax - s->xmin) + 1;
  971. s->ydelta = (s->ymax - s->ymin) + 1;
  972. continue;
  973. } else if ((var_size = check_header_variable(s, "displayWindow",
  974. "box2i", 34)) >= 0) {
  975. if (!var_size)
  976. return AVERROR_INVALIDDATA;
  977. bytestream2_skip(&s->gb, 8);
  978. s->w = bytestream2_get_le32(&s->gb) + 1;
  979. s->h = bytestream2_get_le32(&s->gb) + 1;
  980. continue;
  981. } else if ((var_size = check_header_variable(s, "lineOrder",
  982. "lineOrder", 25)) >= 0) {
  983. int line_order;
  984. if (!var_size)
  985. return AVERROR_INVALIDDATA;
  986. line_order = bytestream2_get_byte(&s->gb);
  987. av_log(s->avctx, AV_LOG_DEBUG, "line order: %d.\n", line_order);
  988. if (line_order > 2) {
  989. av_log(s->avctx, AV_LOG_ERROR, "Unknown line order.\n");
  990. return AVERROR_INVALIDDATA;
  991. }
  992. continue;
  993. } else if ((var_size = check_header_variable(s, "pixelAspectRatio",
  994. "float", 31)) >= 0) {
  995. if (!var_size)
  996. return AVERROR_INVALIDDATA;
  997. ff_set_sar(s->avctx,
  998. av_d2q(av_int2float(bytestream2_get_le32(&s->gb)), 255));
  999. continue;
  1000. } else if ((var_size = check_header_variable(s, "compression",
  1001. "compression", 29)) >= 0) {
  1002. if (!var_size)
  1003. return AVERROR_INVALIDDATA;
  1004. if (s->compression == EXR_UNKN)
  1005. s->compression = bytestream2_get_byte(&s->gb);
  1006. else
  1007. av_log(s->avctx, AV_LOG_WARNING,
  1008. "Found more than one compression attribute.\n");
  1009. continue;
  1010. }
  1011. // Check if there are enough bytes for a header
  1012. if (bytestream2_get_bytes_left(&s->gb) <= 9) {
  1013. av_log(s->avctx, AV_LOG_ERROR, "Incomplete header\n");
  1014. return AVERROR_INVALIDDATA;
  1015. }
  1016. // Process unknown variables
  1017. for (i = 0; i < 2; i++) // value_name and value_type
  1018. while (bytestream2_get_byte(&s->gb) != 0);
  1019. // Skip variable length
  1020. bytestream2_skip(&s->gb, bytestream2_get_le32(&s->gb));
  1021. }
  1022. if (s->compression == EXR_UNKN) {
  1023. av_log(s->avctx, AV_LOG_ERROR, "Missing compression attribute.\n");
  1024. return AVERROR_INVALIDDATA;
  1025. }
  1026. s->scan_line_size = s->xdelta * current_channel_offset;
  1027. if (bytestream2_get_bytes_left(&s->gb) <= 0) {
  1028. av_log(s->avctx, AV_LOG_ERROR, "Incomplete frame.\n");
  1029. return AVERROR_INVALIDDATA;
  1030. }
  1031. // aaand we are done
  1032. bytestream2_skip(&s->gb, 1);
  1033. return 0;
  1034. }
  1035. static int decode_frame(AVCodecContext *avctx, void *data,
  1036. int *got_frame, AVPacket *avpkt)
  1037. {
  1038. EXRContext *s = avctx->priv_data;
  1039. ThreadFrame frame = { .f = data };
  1040. AVFrame *picture = data;
  1041. uint8_t *ptr;
  1042. int y, ret;
  1043. int out_line_size;
  1044. int scan_line_blocks;
  1045. bytestream2_init(&s->gb, avpkt->data, avpkt->size);
  1046. if ((ret = decode_header(s)) < 0)
  1047. return ret;
  1048. switch (s->pixel_type) {
  1049. case EXR_FLOAT:
  1050. case EXR_HALF:
  1051. if (s->channel_offsets[3] >= 0)
  1052. avctx->pix_fmt = AV_PIX_FMT_RGBA64;
  1053. else
  1054. avctx->pix_fmt = AV_PIX_FMT_RGB48;
  1055. break;
  1056. case EXR_UINT:
  1057. avpriv_request_sample(avctx, "32-bit unsigned int");
  1058. return AVERROR_PATCHWELCOME;
  1059. default:
  1060. av_log(avctx, AV_LOG_ERROR, "Missing channel list.\n");
  1061. return AVERROR_INVALIDDATA;
  1062. }
  1063. switch (s->compression) {
  1064. case EXR_RAW:
  1065. case EXR_RLE:
  1066. case EXR_ZIP1:
  1067. s->scan_lines_per_block = 1;
  1068. break;
  1069. case EXR_PXR24:
  1070. case EXR_ZIP16:
  1071. s->scan_lines_per_block = 16;
  1072. break;
  1073. case EXR_PIZ:
  1074. s->scan_lines_per_block = 32;
  1075. break;
  1076. default:
  1077. avpriv_report_missing_feature(avctx, "Compression %d", s->compression);
  1078. return AVERROR_PATCHWELCOME;
  1079. }
  1080. /* Verify the xmin, xmax, ymin, ymax and xdelta before setting
  1081. * the actual image size. */
  1082. if (s->xmin > s->xmax ||
  1083. s->ymin > s->ymax ||
  1084. s->xdelta != s->xmax - s->xmin + 1 ||
  1085. s->xmax >= s->w ||
  1086. s->ymax >= s->h) {
  1087. av_log(avctx, AV_LOG_ERROR, "Wrong or missing size information.\n");
  1088. return AVERROR_INVALIDDATA;
  1089. }
  1090. if ((ret = ff_set_dimensions(avctx, s->w, s->h)) < 0)
  1091. return ret;
  1092. s->desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  1093. if (!s->desc)
  1094. return AVERROR_INVALIDDATA;
  1095. out_line_size = avctx->width * 2 * s->desc->nb_components;
  1096. scan_line_blocks = (s->ydelta + s->scan_lines_per_block - 1) /
  1097. s->scan_lines_per_block;
  1098. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
  1099. return ret;
  1100. if (bytestream2_get_bytes_left(&s->gb) < scan_line_blocks * 8)
  1101. return AVERROR_INVALIDDATA;
  1102. // save pointer we are going to use in decode_block
  1103. s->buf = avpkt->data;
  1104. s->buf_size = avpkt->size;
  1105. ptr = picture->data[0];
  1106. // Zero out the start if ymin is not 0
  1107. for (y = 0; y < s->ymin; y++) {
  1108. memset(ptr, 0, out_line_size);
  1109. ptr += picture->linesize[0];
  1110. }
  1111. s->picture = picture;
  1112. avctx->execute2(avctx, decode_block, s->thread_data, NULL, scan_line_blocks);
  1113. // Zero out the end if ymax+1 is not h
  1114. for (y = s->ymax + 1; y < avctx->height; y++) {
  1115. memset(ptr, 0, out_line_size);
  1116. ptr += picture->linesize[0];
  1117. }
  1118. picture->pict_type = AV_PICTURE_TYPE_I;
  1119. *got_frame = 1;
  1120. return avpkt->size;
  1121. }
  1122. static av_cold int decode_init(AVCodecContext *avctx)
  1123. {
  1124. EXRContext *s = avctx->priv_data;
  1125. uint32_t i;
  1126. union av_intfloat32 t;
  1127. float one_gamma = 1.0f / s->gamma;
  1128. s->avctx = avctx;
  1129. s->xmin = ~0;
  1130. s->xmax = ~0;
  1131. s->ymin = ~0;
  1132. s->ymax = ~0;
  1133. s->xdelta = ~0;
  1134. s->ydelta = ~0;
  1135. s->channel_offsets[0] = -1;
  1136. s->channel_offsets[1] = -1;
  1137. s->channel_offsets[2] = -1;
  1138. s->channel_offsets[3] = -1;
  1139. s->pixel_type = EXR_UNKNOWN;
  1140. s->compression = EXR_UNKN;
  1141. s->nb_channels = 0;
  1142. s->w = 0;
  1143. s->h = 0;
  1144. if (one_gamma > 0.9999f && one_gamma < 1.0001f) {
  1145. for (i = 0; i < 65536; ++i)
  1146. s->gamma_table[i] = exr_halflt2uint(i);
  1147. } else {
  1148. for (i = 0; i < 65536; ++i) {
  1149. t = exr_half2float(i);
  1150. /* If negative value we reuse half value */
  1151. if (t.f <= 0.0f) {
  1152. s->gamma_table[i] = exr_halflt2uint(i);
  1153. } else {
  1154. t.f = powf(t.f, one_gamma);
  1155. s->gamma_table[i] = exr_flt2uint(t.i);
  1156. }
  1157. }
  1158. }
  1159. // allocate thread data, used for non EXR_RAW compression types
  1160. s->thread_data = av_mallocz_array(avctx->thread_count, sizeof(EXRThreadData));
  1161. if (!s->thread_data)
  1162. return AVERROR_INVALIDDATA;
  1163. return 0;
  1164. }
  1165. static int decode_init_thread_copy(AVCodecContext *avctx)
  1166. { EXRContext *s = avctx->priv_data;
  1167. // allocate thread data, used for non EXR_RAW compression types
  1168. s->thread_data = av_mallocz_array(avctx->thread_count, sizeof(EXRThreadData));
  1169. if (!s->thread_data)
  1170. return AVERROR_INVALIDDATA;
  1171. return 0;
  1172. }
  1173. static av_cold int decode_end(AVCodecContext *avctx)
  1174. {
  1175. EXRContext *s = avctx->priv_data;
  1176. int i;
  1177. for (i = 0; i < avctx->thread_count; i++) {
  1178. EXRThreadData *td = &s->thread_data[i];
  1179. av_freep(&td->uncompressed_data);
  1180. av_freep(&td->tmp);
  1181. av_freep(&td->bitmap);
  1182. av_freep(&td->lut);
  1183. }
  1184. av_freep(&s->thread_data);
  1185. av_freep(&s->channels);
  1186. return 0;
  1187. }
  1188. #define OFFSET(x) offsetof(EXRContext, x)
  1189. #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  1190. static const AVOption options[] = {
  1191. { "layer", "Set the decoding layer", OFFSET(layer),
  1192. AV_OPT_TYPE_STRING, { .str = "" }, 0, 0, VD },
  1193. { "gamma", "Set the float gamma value when decoding", OFFSET(gamma),
  1194. AV_OPT_TYPE_FLOAT, { .dbl = 1.0f }, 0.001, FLT_MAX, VD },
  1195. { NULL },
  1196. };
  1197. static const AVClass exr_class = {
  1198. .class_name = "EXR",
  1199. .item_name = av_default_item_name,
  1200. .option = options,
  1201. .version = LIBAVUTIL_VERSION_INT,
  1202. };
  1203. AVCodec ff_exr_decoder = {
  1204. .name = "exr",
  1205. .long_name = NULL_IF_CONFIG_SMALL("OpenEXR image"),
  1206. .type = AVMEDIA_TYPE_VIDEO,
  1207. .id = AV_CODEC_ID_EXR,
  1208. .priv_data_size = sizeof(EXRContext),
  1209. .init = decode_init,
  1210. .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
  1211. .close = decode_end,
  1212. .decode = decode_frame,
  1213. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS |
  1214. AV_CODEC_CAP_SLICE_THREADS,
  1215. .priv_class = &exr_class,
  1216. };