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.

1456 lines
44KB

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