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.

1923 lines
63KB

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