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.

1528 lines
48KB

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