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.

1455 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. init_get_bits8(&gbit, gb->buffer, bytestream2_get_bytes_left(gb));
  317. for (; im <= iM; im++) {
  318. uint64_t l = hcode[im] = get_bits(&gbit, 6);
  319. if (l == LONG_ZEROCODE_RUN) {
  320. int zerun = get_bits(&gbit, 8) + SHORTEST_LONG_RUN;
  321. if (im + zerun > iM + 1)
  322. return AVERROR_INVALIDDATA;
  323. while (zerun--)
  324. hcode[im++] = 0;
  325. im--;
  326. } else if (l >= SHORT_ZEROCODE_RUN) {
  327. int zerun = l - SHORT_ZEROCODE_RUN + 2;
  328. if (im + zerun > iM + 1)
  329. return AVERROR_INVALIDDATA;
  330. while (zerun--)
  331. hcode[im++] = 0;
  332. im--;
  333. }
  334. }
  335. bytestream2_skip(gb, (get_bits_count(&gbit) + 7) / 8);
  336. huf_canonical_code_table(hcode);
  337. return 0;
  338. }
  339. static int huf_build_dec_table(const uint64_t *hcode, int im,
  340. int iM, HufDec *hdecod)
  341. {
  342. for (; im <= iM; im++) {
  343. uint64_t c = hcode[im] >> 6;
  344. int i, l = hcode[im] & 63;
  345. if (c >> l)
  346. return AVERROR_INVALIDDATA;
  347. if (l > HUF_DECBITS) {
  348. HufDec *pl = hdecod + (c >> (l - HUF_DECBITS));
  349. if (pl->len)
  350. return AVERROR_INVALIDDATA;
  351. pl->lit++;
  352. pl->p = av_realloc(pl->p, pl->lit * sizeof(int));
  353. if (!pl->p)
  354. return AVERROR(ENOMEM);
  355. pl->p[pl->lit - 1] = im;
  356. } else if (l) {
  357. HufDec *pl = hdecod + (c << (HUF_DECBITS - l));
  358. for (i = 1 << (HUF_DECBITS - l); i > 0; i--, pl++) {
  359. if (pl->len || pl->p)
  360. return AVERROR_INVALIDDATA;
  361. pl->len = l;
  362. pl->lit = im;
  363. }
  364. }
  365. }
  366. return 0;
  367. }
  368. #define get_char(c, lc, gb) \
  369. { \
  370. c = (c << 8) | bytestream2_get_byte(gb); \
  371. lc += 8; \
  372. }
  373. #define get_code(po, rlc, c, lc, gb, out, oe) \
  374. { \
  375. if (po == rlc) { \
  376. if (lc < 8) \
  377. get_char(c, lc, gb); \
  378. lc -= 8; \
  379. \
  380. cs = c >> lc; \
  381. \
  382. if (out + cs > oe) \
  383. return AVERROR_INVALIDDATA; \
  384. \
  385. s = out[-1]; \
  386. \
  387. while (cs-- > 0) \
  388. *out++ = s; \
  389. } else if (out < oe) { \
  390. *out++ = po; \
  391. } else { \
  392. return AVERROR_INVALIDDATA; \
  393. } \
  394. }
  395. static int huf_decode(const uint64_t *hcode, const HufDec *hdecod,
  396. GetByteContext *gb, int nbits,
  397. int rlc, int no, uint16_t *out)
  398. {
  399. uint64_t c = 0;
  400. uint16_t *outb = out;
  401. uint16_t *oe = out + no;
  402. const uint8_t *ie = gb->buffer + (nbits + 7) / 8; // input byte size
  403. uint8_t cs, s;
  404. int i, lc = 0;
  405. while (gb->buffer < ie) {
  406. get_char(c, lc, gb);
  407. while (lc >= HUF_DECBITS) {
  408. const HufDec pl = hdecod[(c >> (lc - HUF_DECBITS)) & HUF_DECMASK];
  409. if (pl.len) {
  410. lc -= pl.len;
  411. get_code(pl.lit, rlc, c, lc, gb, out, oe);
  412. } else {
  413. int j;
  414. if (!pl.p)
  415. return AVERROR_INVALIDDATA;
  416. for (j = 0; j < pl.lit; j++) {
  417. int l = hcode[pl.p[j]] & 63;
  418. while (lc < l && bytestream2_get_bytes_left(gb) > 0)
  419. get_char(c, lc, gb);
  420. if (lc >= l) {
  421. if ((hcode[pl.p[j]] >> 6) ==
  422. ((c >> (lc - l)) & ((1LL << l) - 1))) {
  423. lc -= l;
  424. get_code(pl.p[j], rlc, c, lc, gb, out, oe);
  425. break;
  426. }
  427. }
  428. }
  429. if (j == pl.lit)
  430. return AVERROR_INVALIDDATA;
  431. }
  432. }
  433. }
  434. i = (8 - nbits) & 7;
  435. c >>= i;
  436. lc -= i;
  437. while (lc > 0) {
  438. const HufDec pl = hdecod[(c << (HUF_DECBITS - lc)) & HUF_DECMASK];
  439. if (pl.len) {
  440. lc -= pl.len;
  441. get_code(pl.lit, rlc, c, lc, gb, out, oe);
  442. } else {
  443. return AVERROR_INVALIDDATA;
  444. }
  445. }
  446. if (out - outb != no)
  447. return AVERROR_INVALIDDATA;
  448. return 0;
  449. }
  450. static int huf_uncompress(GetByteContext *gb,
  451. uint16_t *dst, int dst_size)
  452. {
  453. int32_t src_size, im, iM;
  454. uint32_t nBits;
  455. uint64_t *freq;
  456. HufDec *hdec;
  457. int ret, i;
  458. src_size = bytestream2_get_le32(gb);
  459. im = bytestream2_get_le32(gb);
  460. iM = bytestream2_get_le32(gb);
  461. bytestream2_skip(gb, 4);
  462. nBits = bytestream2_get_le32(gb);
  463. if (im < 0 || im >= HUF_ENCSIZE ||
  464. iM < 0 || iM >= HUF_ENCSIZE ||
  465. src_size < 0)
  466. return AVERROR_INVALIDDATA;
  467. bytestream2_skip(gb, 4);
  468. freq = av_mallocz_array(HUF_ENCSIZE, sizeof(*freq));
  469. hdec = av_mallocz_array(HUF_DECSIZE, sizeof(*hdec));
  470. if (!freq || !hdec) {
  471. ret = AVERROR(ENOMEM);
  472. goto fail;
  473. }
  474. if ((ret = huf_unpack_enc_table(gb, im, iM, freq)) < 0)
  475. goto fail;
  476. if (nBits > 8 * bytestream2_get_bytes_left(gb)) {
  477. ret = AVERROR_INVALIDDATA;
  478. goto fail;
  479. }
  480. if ((ret = huf_build_dec_table(freq, im, iM, hdec)) < 0)
  481. goto fail;
  482. ret = huf_decode(freq, hdec, gb, nBits, iM, dst_size, dst);
  483. fail:
  484. for (i = 0; i < HUF_DECSIZE; i++)
  485. if (hdec)
  486. av_freep(&hdec[i].p);
  487. av_free(freq);
  488. av_free(hdec);
  489. return ret;
  490. }
  491. static inline void wdec14(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
  492. {
  493. int16_t ls = l;
  494. int16_t hs = h;
  495. int hi = hs;
  496. int ai = ls + (hi & 1) + (hi >> 1);
  497. int16_t as = ai;
  498. int16_t bs = ai - hi;
  499. *a = as;
  500. *b = bs;
  501. }
  502. #define NBITS 16
  503. #define A_OFFSET (1 << (NBITS - 1))
  504. #define MOD_MASK ((1 << NBITS) - 1)
  505. static inline void wdec16(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
  506. {
  507. int m = l;
  508. int d = h;
  509. int bb = (m - (d >> 1)) & MOD_MASK;
  510. int aa = (d + bb - A_OFFSET) & MOD_MASK;
  511. *b = bb;
  512. *a = aa;
  513. }
  514. static void wav_decode(uint16_t *in, int nx, int ox,
  515. int ny, int oy, uint16_t mx)
  516. {
  517. int w14 = (mx < (1 << 14));
  518. int n = (nx > ny) ? ny : nx;
  519. int p = 1;
  520. int p2;
  521. while (p <= n)
  522. p <<= 1;
  523. p >>= 1;
  524. p2 = p;
  525. p >>= 1;
  526. while (p >= 1) {
  527. uint16_t *py = in;
  528. uint16_t *ey = in + oy * (ny - p2);
  529. uint16_t i00, i01, i10, i11;
  530. int oy1 = oy * p;
  531. int oy2 = oy * p2;
  532. int ox1 = ox * p;
  533. int ox2 = ox * p2;
  534. for (; py <= ey; py += oy2) {
  535. uint16_t *px = py;
  536. uint16_t *ex = py + ox * (nx - p2);
  537. for (; px <= ex; px += ox2) {
  538. uint16_t *p01 = px + ox1;
  539. uint16_t *p10 = px + oy1;
  540. uint16_t *p11 = p10 + ox1;
  541. if (w14) {
  542. wdec14(*px, *p10, &i00, &i10);
  543. wdec14(*p01, *p11, &i01, &i11);
  544. wdec14(i00, i01, px, p01);
  545. wdec14(i10, i11, p10, p11);
  546. } else {
  547. wdec16(*px, *p10, &i00, &i10);
  548. wdec16(*p01, *p11, &i01, &i11);
  549. wdec16(i00, i01, px, p01);
  550. wdec16(i10, i11, p10, p11);
  551. }
  552. }
  553. if (nx & p) {
  554. uint16_t *p10 = px + oy1;
  555. if (w14)
  556. wdec14(*px, *p10, &i00, p10);
  557. else
  558. wdec16(*px, *p10, &i00, p10);
  559. *px = i00;
  560. }
  561. }
  562. if (ny & p) {
  563. uint16_t *px = py;
  564. uint16_t *ex = py + ox * (nx - p2);
  565. for (; px <= ex; px += ox2) {
  566. uint16_t *p01 = px + ox1;
  567. if (w14)
  568. wdec14(*px, *p01, &i00, p01);
  569. else
  570. wdec16(*px, *p01, &i00, p01);
  571. *px = i00;
  572. }
  573. }
  574. p2 = p;
  575. p >>= 1;
  576. }
  577. }
  578. static int piz_uncompress(EXRContext *s, const uint8_t *src, int ssize,
  579. int dsize, EXRThreadData *td)
  580. {
  581. GetByteContext gb;
  582. uint16_t maxval, min_non_zero, max_non_zero;
  583. uint16_t *ptr;
  584. uint16_t *tmp = (uint16_t *)td->tmp;
  585. uint8_t *out;
  586. int ret, i, j;
  587. if (!td->bitmap)
  588. td->bitmap = av_malloc(BITMAP_SIZE);
  589. if (!td->lut)
  590. td->lut = av_malloc(1 << 17);
  591. if (!td->bitmap || !td->lut) {
  592. av_freep(&td->bitmap);
  593. av_freep(&td->lut);
  594. return AVERROR(ENOMEM);
  595. }
  596. bytestream2_init(&gb, src, ssize);
  597. min_non_zero = bytestream2_get_le16(&gb);
  598. max_non_zero = bytestream2_get_le16(&gb);
  599. if (max_non_zero >= BITMAP_SIZE)
  600. return AVERROR_INVALIDDATA;
  601. memset(td->bitmap, 0, FFMIN(min_non_zero, BITMAP_SIZE));
  602. if (min_non_zero <= max_non_zero)
  603. bytestream2_get_buffer(&gb, td->bitmap + min_non_zero,
  604. max_non_zero - min_non_zero + 1);
  605. memset(td->bitmap + max_non_zero, 0, BITMAP_SIZE - max_non_zero);
  606. maxval = reverse_lut(td->bitmap, td->lut);
  607. ret = huf_uncompress(&gb, tmp, dsize / sizeof(uint16_t));
  608. if (ret)
  609. return ret;
  610. ptr = tmp;
  611. for (i = 0; i < s->nb_channels; i++) {
  612. EXRChannel *channel = &s->channels[i];
  613. int size = channel->pixel_type;
  614. for (j = 0; j < size; j++)
  615. wav_decode(ptr + j, s->xdelta, size, s->ysize,
  616. s->xdelta * size, maxval);
  617. ptr += s->xdelta * s->ysize * size;
  618. }
  619. apply_lut(td->lut, tmp, dsize / sizeof(uint16_t));
  620. out = td->uncompressed_data;
  621. for (i = 0; i < s->ysize; i++)
  622. for (j = 0; j < s->nb_channels; j++) {
  623. uint16_t *in = tmp + j * s->xdelta * s->ysize + i * s->xdelta;
  624. memcpy(out, in, s->xdelta * 2);
  625. out += s->xdelta * 2;
  626. }
  627. return 0;
  628. }
  629. static int pxr24_uncompress(EXRContext *s, const uint8_t *src,
  630. int compressed_size, int uncompressed_size,
  631. EXRThreadData *td)
  632. {
  633. unsigned long dest_len = uncompressed_size;
  634. const uint8_t *in = td->tmp;
  635. uint8_t *out;
  636. int c, i, j;
  637. if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK ||
  638. dest_len != uncompressed_size)
  639. return AVERROR_INVALIDDATA;
  640. out = td->uncompressed_data;
  641. for (i = 0; i < s->ysize; i++)
  642. for (c = 0; c < s->nb_channels; c++) {
  643. EXRChannel *channel = &s->channels[c];
  644. const uint8_t *ptr[4];
  645. uint32_t pixel = 0;
  646. switch (channel->pixel_type) {
  647. case EXR_FLOAT:
  648. ptr[0] = in;
  649. ptr[1] = ptr[0] + s->xdelta;
  650. ptr[2] = ptr[1] + s->xdelta;
  651. in = ptr[2] + s->xdelta;
  652. for (j = 0; j < s->xdelta; ++j) {
  653. uint32_t diff = (*(ptr[0]++) << 24) |
  654. (*(ptr[1]++) << 16) |
  655. (*(ptr[2]++) << 8);
  656. pixel += diff;
  657. bytestream_put_le32(&out, pixel);
  658. }
  659. break;
  660. case EXR_HALF:
  661. ptr[0] = in;
  662. ptr[1] = ptr[0] + s->xdelta;
  663. in = ptr[1] + s->xdelta;
  664. for (j = 0; j < s->xdelta; j++) {
  665. uint32_t diff = (*(ptr[0]++) << 8) | *(ptr[1]++);
  666. pixel += diff;
  667. bytestream_put_le16(&out, pixel);
  668. }
  669. break;
  670. default:
  671. return AVERROR_INVALIDDATA;
  672. }
  673. }
  674. return 0;
  675. }
  676. static int decode_block(AVCodecContext *avctx, void *tdata,
  677. int jobnr, int threadnr)
  678. {
  679. EXRContext *s = avctx->priv_data;
  680. AVFrame *const p = s->picture;
  681. EXRThreadData *td = &s->thread_data[threadnr];
  682. const uint8_t *channel_buffer[4] = { 0 };
  683. const uint8_t *buf = s->buf;
  684. uint64_t line_offset, uncompressed_size;
  685. uint32_t xdelta = s->xdelta;
  686. uint16_t *ptr_x;
  687. uint8_t *ptr;
  688. uint32_t data_size, line;
  689. const uint8_t *src;
  690. int axmax = (avctx->width - (s->xmax + 1)) * 2 * s->desc->nb_components;
  691. int bxmin = s->xmin * 2 * s->desc->nb_components;
  692. int i, x, buf_size = s->buf_size;
  693. int ret;
  694. float one_gamma = 1.0f / s->gamma;
  695. line_offset = AV_RL64(s->gb.buffer + jobnr * 8);
  696. // Check if the buffer has the required bytes needed from the offset
  697. if (line_offset > buf_size - 8)
  698. return AVERROR_INVALIDDATA;
  699. src = buf + line_offset + 8;
  700. line = AV_RL32(src - 8);
  701. if (line < s->ymin || line > s->ymax)
  702. return AVERROR_INVALIDDATA;
  703. data_size = AV_RL32(src - 4);
  704. if (data_size <= 0 || data_size > buf_size)
  705. return AVERROR_INVALIDDATA;
  706. s->ysize = FFMIN(s->scan_lines_per_block, s->ymax - line + 1);
  707. uncompressed_size = s->scan_line_size * s->ysize;
  708. if ((s->compression == EXR_RAW && (data_size != uncompressed_size ||
  709. line_offset > buf_size - uncompressed_size)) ||
  710. (s->compression != EXR_RAW && (data_size > uncompressed_size ||
  711. line_offset > buf_size - data_size))) {
  712. return AVERROR_INVALIDDATA;
  713. }
  714. if (data_size < uncompressed_size) {
  715. av_fast_padded_malloc(&td->uncompressed_data,
  716. &td->uncompressed_size, uncompressed_size);
  717. av_fast_padded_malloc(&td->tmp, &td->tmp_size, uncompressed_size);
  718. if (!td->uncompressed_data || !td->tmp)
  719. return AVERROR(ENOMEM);
  720. ret = AVERROR_INVALIDDATA;
  721. switch (s->compression) {
  722. case EXR_ZIP1:
  723. case EXR_ZIP16:
  724. ret = zip_uncompress(src, data_size, uncompressed_size, td);
  725. break;
  726. case EXR_PIZ:
  727. ret = piz_uncompress(s, src, data_size, uncompressed_size, td);
  728. break;
  729. case EXR_PXR24:
  730. ret = pxr24_uncompress(s, src, data_size, uncompressed_size, td);
  731. break;
  732. case EXR_RLE:
  733. ret = rle_uncompress(src, data_size, uncompressed_size, td);
  734. }
  735. if (ret < 0) {
  736. av_log(avctx, AV_LOG_ERROR, "decode_block() failed.\n");
  737. return ret;
  738. }
  739. src = td->uncompressed_data;
  740. }
  741. channel_buffer[0] = src + xdelta * s->channel_offsets[0];
  742. channel_buffer[1] = src + xdelta * s->channel_offsets[1];
  743. channel_buffer[2] = src + xdelta * s->channel_offsets[2];
  744. if (s->channel_offsets[3] >= 0)
  745. channel_buffer[3] = src + xdelta * s->channel_offsets[3];
  746. ptr = p->data[0] + line * p->linesize[0];
  747. for (i = 0;
  748. i < s->scan_lines_per_block && line + i <= s->ymax;
  749. i++, ptr += p->linesize[0]) {
  750. const uint8_t *r, *g, *b, *a;
  751. r = channel_buffer[0];
  752. g = channel_buffer[1];
  753. b = channel_buffer[2];
  754. if (channel_buffer[3])
  755. a = channel_buffer[3];
  756. ptr_x = (uint16_t *) ptr;
  757. // Zero out the start if xmin is not 0
  758. memset(ptr_x, 0, bxmin);
  759. ptr_x += s->xmin * s->desc->nb_components;
  760. if (s->pixel_type == EXR_FLOAT) {
  761. // 32-bit
  762. for (x = 0; x < xdelta; x++) {
  763. union av_intfloat32 t;
  764. t.i = bytestream_get_le32(&r);
  765. if ( t.f > 0.0f ) /* avoid negative values */
  766. t.f = powf(t.f, one_gamma);
  767. *ptr_x++ = exr_flt2uint(t.i);
  768. t.i = bytestream_get_le32(&g);
  769. if ( t.f > 0.0f )
  770. t.f = powf(t.f, one_gamma);
  771. *ptr_x++ = exr_flt2uint(t.i);
  772. t.i = bytestream_get_le32(&b);
  773. if ( t.f > 0.0f )
  774. t.f = powf(t.f, one_gamma);
  775. *ptr_x++ = exr_flt2uint(t.i);
  776. if (channel_buffer[3])
  777. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a));
  778. }
  779. } else {
  780. // 16-bit
  781. for (x = 0; x < xdelta; x++) {
  782. *ptr_x++ = s->gamma_table[bytestream_get_le16(&r)];
  783. *ptr_x++ = s->gamma_table[bytestream_get_le16(&g)];
  784. *ptr_x++ = s->gamma_table[bytestream_get_le16(&b)];
  785. if (channel_buffer[3])
  786. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&a));
  787. }
  788. }
  789. // Zero out the end if xmax+1 is not w
  790. memset(ptr_x, 0, axmax);
  791. channel_buffer[0] += s->scan_line_size;
  792. channel_buffer[1] += s->scan_line_size;
  793. channel_buffer[2] += s->scan_line_size;
  794. if (channel_buffer[3])
  795. channel_buffer[3] += s->scan_line_size;
  796. }
  797. return 0;
  798. }
  799. /**
  800. * Check if the variable name corresponds to its data type.
  801. *
  802. * @param s the EXRContext
  803. * @param value_name name of the variable to check
  804. * @param value_type type of the variable to check
  805. * @param minimum_length minimum length of the variable data
  806. *
  807. * @return bytes to read containing variable data
  808. * -1 if variable is not found
  809. * 0 if buffer ended prematurely
  810. */
  811. static int check_header_variable(EXRContext *s,
  812. const char *value_name,
  813. const char *value_type,
  814. unsigned int minimum_length)
  815. {
  816. int var_size = -1;
  817. if (bytestream2_get_bytes_left(&s->gb) >= minimum_length &&
  818. !strcmp(s->gb.buffer, value_name)) {
  819. // found value_name, jump to value_type (null terminated strings)
  820. s->gb.buffer += strlen(value_name) + 1;
  821. if (!strcmp(s->gb.buffer, value_type)) {
  822. s->gb.buffer += strlen(value_type) + 1;
  823. var_size = bytestream2_get_le32(&s->gb);
  824. // don't go read past boundaries
  825. if (var_size > bytestream2_get_bytes_left(&s->gb))
  826. var_size = 0;
  827. } else {
  828. // value_type not found, reset the buffer
  829. s->gb.buffer -= strlen(value_name) + 1;
  830. av_log(s->avctx, AV_LOG_WARNING,
  831. "Unknown data type %s for header variable %s.\n",
  832. value_type, value_name);
  833. }
  834. }
  835. return var_size;
  836. }
  837. static int decode_header(EXRContext *s)
  838. {
  839. int current_channel_offset = 0;
  840. int magic_number, version, flags, i;
  841. if (bytestream2_get_bytes_left(&s->gb) < 10) {
  842. av_log(s->avctx, AV_LOG_ERROR, "Header too short to parse.\n");
  843. return AVERROR_INVALIDDATA;
  844. }
  845. magic_number = bytestream2_get_le32(&s->gb);
  846. if (magic_number != 20000630) {
  847. /* As per documentation of OpenEXR, it is supposed to be
  848. * int 20000630 little-endian */
  849. av_log(s->avctx, AV_LOG_ERROR, "Wrong magic number %d.\n", magic_number);
  850. return AVERROR_INVALIDDATA;
  851. }
  852. version = bytestream2_get_byte(&s->gb);
  853. if (version != 2) {
  854. avpriv_report_missing_feature(s->avctx, "Version %d", version);
  855. return AVERROR_PATCHWELCOME;
  856. }
  857. flags = bytestream2_get_le24(&s->gb);
  858. if (flags & 0x02) {
  859. avpriv_report_missing_feature(s->avctx, "Tile support");
  860. return AVERROR_PATCHWELCOME;
  861. }
  862. // Parse the header
  863. while (bytestream2_get_bytes_left(&s->gb) > 0 && *s->gb.buffer) {
  864. int var_size;
  865. if ((var_size = check_header_variable(s, "channels",
  866. "chlist", 38)) >= 0) {
  867. GetByteContext ch_gb;
  868. if (!var_size)
  869. return AVERROR_INVALIDDATA;
  870. bytestream2_init(&ch_gb, s->gb.buffer, var_size);
  871. while (bytestream2_get_bytes_left(&ch_gb) >= 19) {
  872. EXRChannel *channel;
  873. enum ExrPixelType current_pixel_type;
  874. int channel_index = -1;
  875. int xsub, ysub;
  876. if (strcmp(s->layer, "") != 0) {
  877. if (strncmp(ch_gb.buffer, s->layer, strlen(s->layer)) == 0) {
  878. ch_gb.buffer += strlen(s->layer);
  879. if (*ch_gb.buffer == '.')
  880. ch_gb.buffer++; /* skip dot if not given */
  881. av_log(s->avctx, AV_LOG_INFO,
  882. "Layer %s.%s matched.\n", s->layer, ch_gb.buffer);
  883. }
  884. }
  885. if (!strcmp(ch_gb.buffer, "R") ||
  886. !strcmp(ch_gb.buffer, "X") ||
  887. !strcmp(ch_gb.buffer, "U"))
  888. channel_index = 0;
  889. else if (!strcmp(ch_gb.buffer, "G") ||
  890. !strcmp(ch_gb.buffer, "Y") ||
  891. !strcmp(ch_gb.buffer, "V"))
  892. channel_index = 1;
  893. else if (!strcmp(ch_gb.buffer, "B") ||
  894. !strcmp(ch_gb.buffer, "Z") ||
  895. !strcmp(ch_gb.buffer, "W"))
  896. channel_index = 2;
  897. else if (!strcmp(ch_gb.buffer, "A"))
  898. channel_index = 3;
  899. else
  900. av_log(s->avctx, AV_LOG_WARNING,
  901. "Unsupported channel %.256s.\n", ch_gb.buffer);
  902. /* skip until you get a 0 */
  903. while (bytestream2_get_bytes_left(&ch_gb) > 0 &&
  904. bytestream2_get_byte(&ch_gb))
  905. continue;
  906. if (bytestream2_get_bytes_left(&ch_gb) < 4) {
  907. av_log(s->avctx, AV_LOG_ERROR, "Incomplete header.\n");
  908. return AVERROR_INVALIDDATA;
  909. }
  910. current_pixel_type = bytestream2_get_le32(&ch_gb);
  911. if (current_pixel_type >= EXR_UNKNOWN) {
  912. avpriv_report_missing_feature(s->avctx,
  913. "Pixel type %d.\n",
  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. uint32_t i;
  1125. union av_intfloat32 t;
  1126. EXRContext *s = avctx->priv_data;
  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. }
  1148. } else {
  1149. for ( i = 0; i < 65536; ++i ) {
  1150. t = exr_half2float(i);
  1151. /* If negative value we reuse half value */
  1152. if ( t.f <= 0.0f ) {
  1153. s->gamma_table[i] = exr_halflt2uint(i);
  1154. } else {
  1155. t.f = powf(t.f, one_gamma);
  1156. s->gamma_table[i] = exr_flt2uint(t.i);
  1157. }
  1158. }
  1159. }
  1160. // allocate thread data, used for non EXR_RAW compreesion types
  1161. s->thread_data = av_mallocz_array(avctx->thread_count, sizeof(EXRThreadData));
  1162. if (!s->thread_data)
  1163. return AVERROR_INVALIDDATA;
  1164. return 0;
  1165. }
  1166. static int decode_init_thread_copy(AVCodecContext *avctx)
  1167. { EXRContext *s = avctx->priv_data;
  1168. // allocate thread data, used for non EXR_RAW compreesion types
  1169. s->thread_data = av_mallocz_array(avctx->thread_count, sizeof(EXRThreadData));
  1170. if (!s->thread_data)
  1171. return AVERROR_INVALIDDATA;
  1172. return 0;
  1173. }
  1174. static av_cold int decode_end(AVCodecContext *avctx)
  1175. {
  1176. EXRContext *s = avctx->priv_data;
  1177. int i;
  1178. for (i = 0; i < avctx->thread_count; i++) {
  1179. EXRThreadData *td = &s->thread_data[i];
  1180. av_freep(&td->uncompressed_data);
  1181. av_freep(&td->tmp);
  1182. av_freep(&td->bitmap);
  1183. av_freep(&td->lut);
  1184. }
  1185. av_freep(&s->thread_data);
  1186. av_freep(&s->channels);
  1187. return 0;
  1188. }
  1189. #define OFFSET(x) offsetof(EXRContext, x)
  1190. #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  1191. static const AVOption options[] = {
  1192. { "layer", "Set the decoding layer", OFFSET(layer),
  1193. AV_OPT_TYPE_STRING, { .str = "" }, 0, 0, VD },
  1194. { "gamma", "Set the float gamma value when decoding (experimental/unsupported)", OFFSET(gamma),
  1195. AV_OPT_TYPE_FLOAT, { .dbl = 1.0f }, 0.001, FLT_MAX, VD },
  1196. { NULL },
  1197. };
  1198. static const AVClass exr_class = {
  1199. .class_name = "EXR",
  1200. .item_name = av_default_item_name,
  1201. .option = options,
  1202. .version = LIBAVUTIL_VERSION_INT,
  1203. };
  1204. AVCodec ff_exr_decoder = {
  1205. .name = "exr",
  1206. .long_name = NULL_IF_CONFIG_SMALL("OpenEXR image"),
  1207. .type = AVMEDIA_TYPE_VIDEO,
  1208. .id = AV_CODEC_ID_EXR,
  1209. .priv_data_size = sizeof(EXRContext),
  1210. .init = decode_init,
  1211. .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
  1212. .close = decode_end,
  1213. .decode = decode_frame,
  1214. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS |
  1215. CODEC_CAP_SLICE_THREADS,
  1216. .priv_class = &exr_class,
  1217. };