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.

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