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.

1966 lines
64KB

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