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.

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