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.

1995 lines
66KB

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