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.

1886 lines
61KB

  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 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. #include "get_bits.h"
  45. #include "internal.h"
  46. #include "mathops.h"
  47. #include "thread.h"
  48. enum ExrCompr {
  49. EXR_RAW,
  50. EXR_RLE,
  51. EXR_ZIP1,
  52. EXR_ZIP16,
  53. EXR_PIZ,
  54. EXR_PXR24,
  55. EXR_B44,
  56. EXR_B44A,
  57. EXR_UNKN,
  58. };
  59. enum ExrPixelType {
  60. EXR_UINT,
  61. EXR_HALF,
  62. EXR_FLOAT,
  63. EXR_UNKNOWN,
  64. };
  65. enum ExrTileLevelMode {
  66. EXR_TILE_LEVEL_ONE,
  67. EXR_TILE_LEVEL_MIPMAP,
  68. EXR_TILE_LEVEL_RIPMAP,
  69. EXR_TILE_LEVEL_UNKNOWN,
  70. };
  71. enum ExrTileLevelRound {
  72. EXR_TILE_ROUND_UP,
  73. EXR_TILE_ROUND_DOWN,
  74. EXR_TILE_ROUND_UNKNOWN,
  75. };
  76. typedef struct EXRChannel {
  77. int xsub, ysub;
  78. enum ExrPixelType pixel_type;
  79. } EXRChannel;
  80. typedef struct EXRTileAttribute {
  81. int32_t xSize;
  82. int32_t ySize;
  83. enum ExrTileLevelMode level_mode;
  84. enum ExrTileLevelRound level_round;
  85. } EXRTileAttribute;
  86. typedef struct EXRThreadData {
  87. uint8_t *uncompressed_data;
  88. int uncompressed_size;
  89. uint8_t *tmp;
  90. int tmp_size;
  91. uint8_t *bitmap;
  92. uint16_t *lut;
  93. int ysize, xsize;
  94. int channel_line_size;
  95. } EXRThreadData;
  96. typedef struct EXRContext {
  97. AVClass *class;
  98. AVFrame *picture;
  99. AVCodecContext *avctx;
  100. enum ExrCompr compression;
  101. enum ExrPixelType pixel_type;
  102. int channel_offsets[4]; // 0 = red, 1 = green, 2 = blue and 3 = alpha
  103. const AVPixFmtDescriptor *desc;
  104. int w, h;
  105. uint32_t xmax, xmin;
  106. uint32_t ymax, ymin;
  107. uint32_t xdelta, ydelta;
  108. int scan_lines_per_block;
  109. EXRTileAttribute tile_attr; /* header data attribute of tile */
  110. int is_tile; /* 0 if scanline, 1 if tile */
  111. int is_luma;/* 1 if there is an Y plane */
  112. GetByteContext gb;
  113. const uint8_t *buf;
  114. int buf_size;
  115. EXRChannel *channels;
  116. int nb_channels;
  117. int current_channel_offset;
  118. EXRThreadData *thread_data;
  119. const char *layer;
  120. enum AVColorTransferCharacteristic apply_trc_type;
  121. float gamma;
  122. uint16_t gamma_table[65536];
  123. } EXRContext;
  124. /* -15 stored using a single precision bias of 127 */
  125. #define HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP 0x38000000
  126. /* max exponent value in single precision that will be converted
  127. * to Inf or Nan when stored as a half-float */
  128. #define HALF_FLOAT_MAX_BIASED_EXP_AS_SINGLE_FP_EXP 0x47800000
  129. /* 255 is the max exponent biased value */
  130. #define FLOAT_MAX_BIASED_EXP (0xFF << 23)
  131. #define HALF_FLOAT_MAX_BIASED_EXP (0x1F << 10)
  132. /**
  133. * Convert a half float as a uint16_t into a full float.
  134. *
  135. * @param hf half float as uint16_t
  136. *
  137. * @return float value
  138. */
  139. static union av_intfloat32 exr_half2float(uint16_t hf)
  140. {
  141. unsigned int sign = (unsigned int) (hf >> 15);
  142. unsigned int mantissa = (unsigned int) (hf & ((1 << 10) - 1));
  143. unsigned int exp = (unsigned int) (hf & HALF_FLOAT_MAX_BIASED_EXP);
  144. union av_intfloat32 f;
  145. if (exp == HALF_FLOAT_MAX_BIASED_EXP) {
  146. // we have a half-float NaN or Inf
  147. // half-float NaNs will be converted to a single precision NaN
  148. // half-float Infs will be converted to a single precision Inf
  149. exp = FLOAT_MAX_BIASED_EXP;
  150. if (mantissa)
  151. mantissa = (1 << 23) - 1; // set all bits to indicate a NaN
  152. } else if (exp == 0x0) {
  153. // convert half-float zero/denorm to single precision value
  154. if (mantissa) {
  155. mantissa <<= 1;
  156. exp = HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP;
  157. // check for leading 1 in denorm mantissa
  158. while ((mantissa & (1 << 10))) {
  159. // for every leading 0, decrement single precision exponent by 1
  160. // and shift half-float mantissa value to the left
  161. mantissa <<= 1;
  162. exp -= (1 << 23);
  163. }
  164. // clamp the mantissa to 10 bits
  165. mantissa &= ((1 << 10) - 1);
  166. // shift left to generate single-precision mantissa of 23 bits
  167. mantissa <<= 13;
  168. }
  169. } else {
  170. // shift left to generate single-precision mantissa of 23 bits
  171. mantissa <<= 13;
  172. // generate single precision biased exponent value
  173. exp = (exp << 13) + HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP;
  174. }
  175. f.i = (sign << 31) | exp | mantissa;
  176. return f;
  177. }
  178. /**
  179. * Convert from 32-bit float as uint32_t to uint16_t.
  180. *
  181. * @param v 32-bit float
  182. *
  183. * @return normalized 16-bit unsigned int
  184. */
  185. static inline uint16_t exr_flt2uint(uint32_t v)
  186. {
  187. unsigned int exp = v >> 23;
  188. // "HACK": negative values result in exp< 0, so clipping them to 0
  189. // is also handled by this condition, avoids explicit check for sign bit.
  190. if (exp <= 127 + 7 - 24) // we would shift out all bits anyway
  191. return 0;
  192. if (exp >= 127)
  193. return 0xffff;
  194. v &= 0x007fffff;
  195. return (v + (1 << 23)) >> (127 + 7 - exp);
  196. }
  197. /**
  198. * Convert from 16-bit float as uint16_t to uint16_t.
  199. *
  200. * @param v 16-bit float
  201. *
  202. * @return normalized 16-bit unsigned int
  203. */
  204. static inline uint16_t exr_halflt2uint(uint16_t v)
  205. {
  206. unsigned exp = 14 - (v >> 10);
  207. if (exp >= 14) {
  208. if (exp == 14)
  209. return (v >> 9) & 1;
  210. else
  211. return (v & 0x8000) ? 0 : 0xffff;
  212. }
  213. v <<= 6;
  214. return (v + (1 << 16)) >> (exp + 1);
  215. }
  216. static void predictor(uint8_t *src, int size)
  217. {
  218. uint8_t *t = src + 1;
  219. uint8_t *stop = src + size;
  220. while (t < stop) {
  221. int d = (int) t[-1] + (int) t[0] - 128;
  222. t[0] = d;
  223. ++t;
  224. }
  225. }
  226. static void reorder_pixels(uint8_t *src, uint8_t *dst, int size)
  227. {
  228. const int8_t *t1 = src;
  229. const int8_t *t2 = src + (size + 1) / 2;
  230. int8_t *s = dst;
  231. int8_t *stop = s + size;
  232. while (1) {
  233. if (s < stop)
  234. *(s++) = *(t1++);
  235. else
  236. break;
  237. if (s < stop)
  238. *(s++) = *(t2++);
  239. else
  240. break;
  241. }
  242. }
  243. static int zip_uncompress(const uint8_t *src, int compressed_size,
  244. int uncompressed_size, EXRThreadData *td)
  245. {
  246. unsigned long dest_len = uncompressed_size;
  247. if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK ||
  248. dest_len != uncompressed_size)
  249. return AVERROR_INVALIDDATA;
  250. predictor(td->tmp, uncompressed_size);
  251. reorder_pixels(td->tmp, td->uncompressed_data, uncompressed_size);
  252. return 0;
  253. }
  254. static int rle_uncompress(const uint8_t *src, int compressed_size,
  255. int uncompressed_size, EXRThreadData *td)
  256. {
  257. uint8_t *d = td->tmp;
  258. const int8_t *s = src;
  259. int ssize = compressed_size;
  260. int dsize = uncompressed_size;
  261. uint8_t *dend = d + dsize;
  262. int count;
  263. while (ssize > 0) {
  264. count = *s++;
  265. if (count < 0) {
  266. count = -count;
  267. if ((dsize -= count) < 0 ||
  268. (ssize -= count + 1) < 0)
  269. return AVERROR_INVALIDDATA;
  270. while (count--)
  271. *d++ = *s++;
  272. } else {
  273. count++;
  274. if ((dsize -= count) < 0 ||
  275. (ssize -= 2) < 0)
  276. return AVERROR_INVALIDDATA;
  277. while (count--)
  278. *d++ = *s;
  279. s++;
  280. }
  281. }
  282. if (dend != d)
  283. return AVERROR_INVALIDDATA;
  284. predictor(td->tmp, uncompressed_size);
  285. reorder_pixels(td->tmp, td->uncompressed_data, uncompressed_size);
  286. return 0;
  287. }
  288. #define USHORT_RANGE (1 << 16)
  289. #define BITMAP_SIZE (1 << 13)
  290. static uint16_t reverse_lut(const uint8_t *bitmap, uint16_t *lut)
  291. {
  292. int i, k = 0;
  293. for (i = 0; i < USHORT_RANGE; i++)
  294. if ((i == 0) || (bitmap[i >> 3] & (1 << (i & 7))))
  295. lut[k++] = i;
  296. i = k - 1;
  297. memset(lut + k, 0, (USHORT_RANGE - k) * 2);
  298. return i;
  299. }
  300. static void apply_lut(const uint16_t *lut, uint16_t *dst, int dsize)
  301. {
  302. int i;
  303. for (i = 0; i < dsize; ++i)
  304. dst[i] = lut[dst[i]];
  305. }
  306. #define HUF_ENCBITS 16 // literal (value) bit length
  307. #define HUF_DECBITS 14 // decoding bit size (>= 8)
  308. #define HUF_ENCSIZE ((1 << HUF_ENCBITS) + 1) // encoding table size
  309. #define HUF_DECSIZE (1 << HUF_DECBITS) // decoding table size
  310. #define HUF_DECMASK (HUF_DECSIZE - 1)
  311. typedef struct HufDec {
  312. int len;
  313. int lit;
  314. int *p;
  315. } HufDec;
  316. static void huf_canonical_code_table(uint64_t *hcode)
  317. {
  318. uint64_t c, n[59] = { 0 };
  319. int i;
  320. for (i = 0; i < HUF_ENCSIZE; ++i)
  321. n[hcode[i]] += 1;
  322. c = 0;
  323. for (i = 58; i > 0; --i) {
  324. uint64_t nc = ((c + n[i]) >> 1);
  325. n[i] = c;
  326. c = nc;
  327. }
  328. for (i = 0; i < HUF_ENCSIZE; ++i) {
  329. int l = hcode[i];
  330. if (l > 0)
  331. hcode[i] = l | (n[l]++ << 6);
  332. }
  333. }
  334. #define SHORT_ZEROCODE_RUN 59
  335. #define LONG_ZEROCODE_RUN 63
  336. #define SHORTEST_LONG_RUN (2 + LONG_ZEROCODE_RUN - SHORT_ZEROCODE_RUN)
  337. #define LONGEST_LONG_RUN (255 + SHORTEST_LONG_RUN)
  338. static int huf_unpack_enc_table(GetByteContext *gb,
  339. int32_t im, int32_t iM, uint64_t *hcode)
  340. {
  341. GetBitContext gbit;
  342. int ret = init_get_bits8(&gbit, gb->buffer, bytestream2_get_bytes_left(gb));
  343. if (ret < 0)
  344. return ret;
  345. for (; im <= iM; im++) {
  346. uint64_t l = hcode[im] = get_bits(&gbit, 6);
  347. if (l == LONG_ZEROCODE_RUN) {
  348. int zerun = get_bits(&gbit, 8) + SHORTEST_LONG_RUN;
  349. if (im + zerun > iM + 1)
  350. return AVERROR_INVALIDDATA;
  351. while (zerun--)
  352. hcode[im++] = 0;
  353. im--;
  354. } else if (l >= SHORT_ZEROCODE_RUN) {
  355. int zerun = l - SHORT_ZEROCODE_RUN + 2;
  356. if (im + zerun > iM + 1)
  357. return AVERROR_INVALIDDATA;
  358. while (zerun--)
  359. hcode[im++] = 0;
  360. im--;
  361. }
  362. }
  363. bytestream2_skip(gb, (get_bits_count(&gbit) + 7) / 8);
  364. huf_canonical_code_table(hcode);
  365. return 0;
  366. }
  367. static int huf_build_dec_table(const uint64_t *hcode, int im,
  368. int iM, HufDec *hdecod)
  369. {
  370. for (; im <= iM; im++) {
  371. uint64_t c = hcode[im] >> 6;
  372. int i, l = hcode[im] & 63;
  373. if (c >> l)
  374. return AVERROR_INVALIDDATA;
  375. if (l > HUF_DECBITS) {
  376. HufDec *pl = hdecod + (c >> (l - HUF_DECBITS));
  377. if (pl->len)
  378. return AVERROR_INVALIDDATA;
  379. pl->lit++;
  380. pl->p = av_realloc(pl->p, pl->lit * sizeof(int));
  381. if (!pl->p)
  382. return AVERROR(ENOMEM);
  383. pl->p[pl->lit - 1] = im;
  384. } else if (l) {
  385. HufDec *pl = hdecod + (c << (HUF_DECBITS - l));
  386. for (i = 1 << (HUF_DECBITS - l); i > 0; i--, pl++) {
  387. if (pl->len || pl->p)
  388. return AVERROR_INVALIDDATA;
  389. pl->len = l;
  390. pl->lit = im;
  391. }
  392. }
  393. }
  394. return 0;
  395. }
  396. #define get_char(c, lc, gb) \
  397. { \
  398. c = (c << 8) | bytestream2_get_byte(gb); \
  399. lc += 8; \
  400. }
  401. #define get_code(po, rlc, c, lc, gb, out, oe, outb) \
  402. { \
  403. if (po == rlc) { \
  404. if (lc < 8) \
  405. get_char(c, lc, gb); \
  406. lc -= 8; \
  407. \
  408. cs = c >> lc; \
  409. \
  410. if (out + cs > oe || out == outb) \
  411. return AVERROR_INVALIDDATA; \
  412. \
  413. s = out[-1]; \
  414. \
  415. while (cs-- > 0) \
  416. *out++ = s; \
  417. } else if (out < oe) { \
  418. *out++ = po; \
  419. } else { \
  420. return AVERROR_INVALIDDATA; \
  421. } \
  422. }
  423. static int huf_decode(const uint64_t *hcode, const HufDec *hdecod,
  424. GetByteContext *gb, int nbits,
  425. int rlc, int no, uint16_t *out)
  426. {
  427. uint64_t c = 0;
  428. uint16_t *outb = out;
  429. uint16_t *oe = out + no;
  430. const uint8_t *ie = gb->buffer + (nbits + 7) / 8; // input byte size
  431. uint8_t cs;
  432. uint16_t s;
  433. int i, lc = 0;
  434. while (gb->buffer < ie) {
  435. get_char(c, lc, gb);
  436. while (lc >= HUF_DECBITS) {
  437. const HufDec pl = hdecod[(c >> (lc - HUF_DECBITS)) & HUF_DECMASK];
  438. if (pl.len) {
  439. lc -= pl.len;
  440. get_code(pl.lit, rlc, c, lc, gb, out, oe, outb);
  441. } else {
  442. int j;
  443. if (!pl.p)
  444. return AVERROR_INVALIDDATA;
  445. for (j = 0; j < pl.lit; j++) {
  446. int l = hcode[pl.p[j]] & 63;
  447. while (lc < l && bytestream2_get_bytes_left(gb) > 0)
  448. get_char(c, lc, gb);
  449. if (lc >= l) {
  450. if ((hcode[pl.p[j]] >> 6) ==
  451. ((c >> (lc - l)) & ((1LL << l) - 1))) {
  452. lc -= l;
  453. get_code(pl.p[j], rlc, c, lc, gb, out, oe, outb);
  454. break;
  455. }
  456. }
  457. }
  458. if (j == pl.lit)
  459. return AVERROR_INVALIDDATA;
  460. }
  461. }
  462. }
  463. i = (8 - nbits) & 7;
  464. c >>= i;
  465. lc -= i;
  466. while (lc > 0) {
  467. const HufDec pl = hdecod[(c << (HUF_DECBITS - lc)) & HUF_DECMASK];
  468. if (pl.len) {
  469. lc -= pl.len;
  470. get_code(pl.lit, rlc, c, lc, gb, out, oe, outb);
  471. } else {
  472. return AVERROR_INVALIDDATA;
  473. }
  474. }
  475. if (out - outb != no)
  476. return AVERROR_INVALIDDATA;
  477. return 0;
  478. }
  479. static int huf_uncompress(GetByteContext *gb,
  480. uint16_t *dst, int dst_size)
  481. {
  482. int32_t src_size, im, iM;
  483. uint32_t nBits;
  484. uint64_t *freq;
  485. HufDec *hdec;
  486. int ret, i;
  487. src_size = bytestream2_get_le32(gb);
  488. im = bytestream2_get_le32(gb);
  489. iM = bytestream2_get_le32(gb);
  490. bytestream2_skip(gb, 4);
  491. nBits = bytestream2_get_le32(gb);
  492. if (im < 0 || im >= HUF_ENCSIZE ||
  493. iM < 0 || iM >= HUF_ENCSIZE ||
  494. src_size < 0)
  495. return AVERROR_INVALIDDATA;
  496. bytestream2_skip(gb, 4);
  497. freq = av_mallocz_array(HUF_ENCSIZE, sizeof(*freq));
  498. hdec = av_mallocz_array(HUF_DECSIZE, sizeof(*hdec));
  499. if (!freq || !hdec) {
  500. ret = AVERROR(ENOMEM);
  501. goto fail;
  502. }
  503. if ((ret = huf_unpack_enc_table(gb, im, iM, freq)) < 0)
  504. goto fail;
  505. if (nBits > 8 * bytestream2_get_bytes_left(gb)) {
  506. ret = AVERROR_INVALIDDATA;
  507. goto fail;
  508. }
  509. if ((ret = huf_build_dec_table(freq, im, iM, hdec)) < 0)
  510. goto fail;
  511. ret = huf_decode(freq, hdec, gb, nBits, iM, dst_size, dst);
  512. fail:
  513. for (i = 0; i < HUF_DECSIZE; i++)
  514. if (hdec)
  515. av_freep(&hdec[i].p);
  516. av_free(freq);
  517. av_free(hdec);
  518. return ret;
  519. }
  520. static inline void wdec14(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
  521. {
  522. int16_t ls = l;
  523. int16_t hs = h;
  524. int hi = hs;
  525. int ai = ls + (hi & 1) + (hi >> 1);
  526. int16_t as = ai;
  527. int16_t bs = ai - hi;
  528. *a = as;
  529. *b = bs;
  530. }
  531. #define NBITS 16
  532. #define A_OFFSET (1 << (NBITS - 1))
  533. #define MOD_MASK ((1 << NBITS) - 1)
  534. static inline void wdec16(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
  535. {
  536. int m = l;
  537. int d = h;
  538. int bb = (m - (d >> 1)) & MOD_MASK;
  539. int aa = (d + bb - A_OFFSET) & MOD_MASK;
  540. *b = bb;
  541. *a = aa;
  542. }
  543. static void wav_decode(uint16_t *in, int nx, int ox,
  544. int ny, int oy, uint16_t mx)
  545. {
  546. int w14 = (mx < (1 << 14));
  547. int n = (nx > ny) ? ny : nx;
  548. int p = 1;
  549. int p2;
  550. while (p <= n)
  551. p <<= 1;
  552. p >>= 1;
  553. p2 = p;
  554. p >>= 1;
  555. while (p >= 1) {
  556. uint16_t *py = in;
  557. uint16_t *ey = in + oy * (ny - p2);
  558. uint16_t i00, i01, i10, i11;
  559. int oy1 = oy * p;
  560. int oy2 = oy * p2;
  561. int ox1 = ox * p;
  562. int ox2 = ox * p2;
  563. for (; py <= ey; py += oy2) {
  564. uint16_t *px = py;
  565. uint16_t *ex = py + ox * (nx - p2);
  566. for (; px <= ex; px += ox2) {
  567. uint16_t *p01 = px + ox1;
  568. uint16_t *p10 = px + oy1;
  569. uint16_t *p11 = p10 + ox1;
  570. if (w14) {
  571. wdec14(*px, *p10, &i00, &i10);
  572. wdec14(*p01, *p11, &i01, &i11);
  573. wdec14(i00, i01, px, p01);
  574. wdec14(i10, i11, p10, p11);
  575. } else {
  576. wdec16(*px, *p10, &i00, &i10);
  577. wdec16(*p01, *p11, &i01, &i11);
  578. wdec16(i00, i01, px, p01);
  579. wdec16(i10, i11, p10, p11);
  580. }
  581. }
  582. if (nx & p) {
  583. uint16_t *p10 = px + oy1;
  584. if (w14)
  585. wdec14(*px, *p10, &i00, p10);
  586. else
  587. wdec16(*px, *p10, &i00, p10);
  588. *px = i00;
  589. }
  590. }
  591. if (ny & p) {
  592. uint16_t *px = py;
  593. uint16_t *ex = py + ox * (nx - p2);
  594. for (; px <= ex; px += ox2) {
  595. uint16_t *p01 = px + ox1;
  596. if (w14)
  597. wdec14(*px, *p01, &i00, p01);
  598. else
  599. wdec16(*px, *p01, &i00, p01);
  600. *px = i00;
  601. }
  602. }
  603. p2 = p;
  604. p >>= 1;
  605. }
  606. }
  607. static int piz_uncompress(EXRContext *s, const uint8_t *src, int ssize,
  608. int dsize, EXRThreadData *td)
  609. {
  610. GetByteContext gb;
  611. uint16_t maxval, min_non_zero, max_non_zero;
  612. uint16_t *ptr;
  613. uint16_t *tmp = (uint16_t *)td->tmp;
  614. uint8_t *out;
  615. int ret, i, j;
  616. int pixel_half_size;/* 1 for half, 2 for float and uint32 */
  617. EXRChannel *channel;
  618. int tmp_offset;
  619. if (!td->bitmap)
  620. td->bitmap = av_malloc(BITMAP_SIZE);
  621. if (!td->lut)
  622. td->lut = av_malloc(1 << 17);
  623. if (!td->bitmap || !td->lut) {
  624. av_freep(&td->bitmap);
  625. av_freep(&td->lut);
  626. return AVERROR(ENOMEM);
  627. }
  628. bytestream2_init(&gb, src, ssize);
  629. min_non_zero = bytestream2_get_le16(&gb);
  630. max_non_zero = bytestream2_get_le16(&gb);
  631. if (max_non_zero >= BITMAP_SIZE)
  632. return AVERROR_INVALIDDATA;
  633. memset(td->bitmap, 0, FFMIN(min_non_zero, BITMAP_SIZE));
  634. if (min_non_zero <= max_non_zero)
  635. bytestream2_get_buffer(&gb, td->bitmap + min_non_zero,
  636. max_non_zero - min_non_zero + 1);
  637. memset(td->bitmap + max_non_zero + 1, 0, BITMAP_SIZE - max_non_zero - 1);
  638. maxval = reverse_lut(td->bitmap, td->lut);
  639. ret = huf_uncompress(&gb, tmp, dsize / sizeof(uint16_t));
  640. if (ret)
  641. return ret;
  642. ptr = tmp;
  643. for (i = 0; i < s->nb_channels; i++) {
  644. channel = &s->channels[i];
  645. if (channel->pixel_type == EXR_HALF)
  646. pixel_half_size = 1;
  647. else
  648. pixel_half_size = 2;
  649. for (j = 0; j < pixel_half_size; j++)
  650. wav_decode(ptr + j, td->xsize, pixel_half_size, td->ysize,
  651. td->xsize * pixel_half_size, maxval);
  652. ptr += td->xsize * td->ysize * pixel_half_size;
  653. }
  654. apply_lut(td->lut, tmp, dsize / sizeof(uint16_t));
  655. out = td->uncompressed_data;
  656. for (i = 0; i < td->ysize; i++) {
  657. tmp_offset = 0;
  658. for (j = 0; j < s->nb_channels; j++) {
  659. uint16_t *in;
  660. EXRChannel *channel = &s->channels[j];
  661. if (channel->pixel_type == EXR_HALF)
  662. pixel_half_size = 1;
  663. else
  664. pixel_half_size = 2;
  665. in = tmp + tmp_offset * td->xsize * td->ysize + i * td->xsize * pixel_half_size;
  666. tmp_offset += pixel_half_size;
  667. memcpy(out, in, td->xsize * 2 * pixel_half_size);
  668. out += td->xsize * 2 * 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 tileX, tileY, tileLevelX, tileLevelY;
  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. tileX = AV_RL32(src - 20);
  881. tileY = AV_RL32(src - 16);
  882. tileLevelX = AV_RL32(src - 12);
  883. tileLevelY = 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 (tileLevelX || tileLevelY) { /* 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 * tileY;
  896. col = s->tile_attr.xSize * tileX;
  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 - tileY * s->tile_attr.ySize);
  901. td->xsize = FFMIN(s->tile_attr.xSize, s->xdelta - tileX * 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);
  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(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(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 {
  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. }
  1029. // Zero out the end if xmax+1 is not w
  1030. memset(ptr_x, 0, axmax);
  1031. channel_buffer[0] += td->channel_line_size;
  1032. channel_buffer[1] += td->channel_line_size;
  1033. channel_buffer[2] += td->channel_line_size;
  1034. if (channel_buffer[3])
  1035. channel_buffer[3] += td->channel_line_size;
  1036. }
  1037. return 0;
  1038. }
  1039. /**
  1040. * Check if the variable name corresponds to its data type.
  1041. *
  1042. * @param s the EXRContext
  1043. * @param value_name name of the variable to check
  1044. * @param value_type type of the variable to check
  1045. * @param minimum_length minimum length of the variable data
  1046. *
  1047. * @return bytes to read containing variable data
  1048. * -1 if variable is not found
  1049. * 0 if buffer ended prematurely
  1050. */
  1051. static int check_header_variable(EXRContext *s,
  1052. const char *value_name,
  1053. const char *value_type,
  1054. unsigned int minimum_length)
  1055. {
  1056. int var_size = -1;
  1057. if (bytestream2_get_bytes_left(&s->gb) >= minimum_length &&
  1058. !strcmp(s->gb.buffer, value_name)) {
  1059. // found value_name, jump to value_type (null terminated strings)
  1060. s->gb.buffer += strlen(value_name) + 1;
  1061. if (!strcmp(s->gb.buffer, value_type)) {
  1062. s->gb.buffer += strlen(value_type) + 1;
  1063. var_size = bytestream2_get_le32(&s->gb);
  1064. // don't go read past boundaries
  1065. if (var_size > bytestream2_get_bytes_left(&s->gb))
  1066. var_size = 0;
  1067. } else {
  1068. // value_type not found, reset the buffer
  1069. s->gb.buffer -= strlen(value_name) + 1;
  1070. av_log(s->avctx, AV_LOG_WARNING,
  1071. "Unknown data type %s for header variable %s.\n",
  1072. value_type, value_name);
  1073. }
  1074. }
  1075. return var_size;
  1076. }
  1077. static int decode_header(EXRContext *s)
  1078. {
  1079. int magic_number, version, i, flags, sar = 0;
  1080. int layer_match = 0;
  1081. s->current_channel_offset = 0;
  1082. s->xmin = ~0;
  1083. s->xmax = ~0;
  1084. s->ymin = ~0;
  1085. s->ymax = ~0;
  1086. s->xdelta = ~0;
  1087. s->ydelta = ~0;
  1088. s->channel_offsets[0] = -1;
  1089. s->channel_offsets[1] = -1;
  1090. s->channel_offsets[2] = -1;
  1091. s->channel_offsets[3] = -1;
  1092. s->pixel_type = EXR_UNKNOWN;
  1093. s->compression = EXR_UNKN;
  1094. s->nb_channels = 0;
  1095. s->w = 0;
  1096. s->h = 0;
  1097. s->tile_attr.xSize = -1;
  1098. s->tile_attr.ySize = -1;
  1099. s->is_tile = 0;
  1100. s->is_luma = 0;
  1101. if (bytestream2_get_bytes_left(&s->gb) < 10) {
  1102. av_log(s->avctx, AV_LOG_ERROR, "Header too short to parse.\n");
  1103. return AVERROR_INVALIDDATA;
  1104. }
  1105. magic_number = bytestream2_get_le32(&s->gb);
  1106. if (magic_number != 20000630) {
  1107. /* As per documentation of OpenEXR, it is supposed to be
  1108. * int 20000630 little-endian */
  1109. av_log(s->avctx, AV_LOG_ERROR, "Wrong magic number %d.\n", magic_number);
  1110. return AVERROR_INVALIDDATA;
  1111. }
  1112. version = bytestream2_get_byte(&s->gb);
  1113. if (version != 2) {
  1114. avpriv_report_missing_feature(s->avctx, "Version %d", version);
  1115. return AVERROR_PATCHWELCOME;
  1116. }
  1117. flags = bytestream2_get_le24(&s->gb);
  1118. if (flags == 0x00)
  1119. s->is_tile = 0;
  1120. else if (flags & 0x02)
  1121. s->is_tile = 1;
  1122. else{
  1123. avpriv_report_missing_feature(s->avctx, "flags %d", flags);
  1124. return AVERROR_PATCHWELCOME;
  1125. }
  1126. // Parse the header
  1127. while (bytestream2_get_bytes_left(&s->gb) > 0 && *s->gb.buffer) {
  1128. int var_size;
  1129. if ((var_size = check_header_variable(s, "channels",
  1130. "chlist", 38)) >= 0) {
  1131. GetByteContext ch_gb;
  1132. if (!var_size)
  1133. return AVERROR_INVALIDDATA;
  1134. bytestream2_init(&ch_gb, s->gb.buffer, var_size);
  1135. while (bytestream2_get_bytes_left(&ch_gb) >= 19) {
  1136. EXRChannel *channel;
  1137. enum ExrPixelType current_pixel_type;
  1138. int channel_index = -1;
  1139. int xsub, ysub;
  1140. if (strcmp(s->layer, "") != 0) {
  1141. if (strncmp(ch_gb.buffer, s->layer, strlen(s->layer)) == 0) {
  1142. layer_match = 1;
  1143. av_log(s->avctx, AV_LOG_INFO,
  1144. "Channel match layer : %s.\n", ch_gb.buffer);
  1145. ch_gb.buffer += strlen(s->layer);
  1146. if (*ch_gb.buffer == '.')
  1147. ch_gb.buffer++; /* skip dot if not given */
  1148. } else {
  1149. av_log(s->avctx, AV_LOG_INFO,
  1150. "Channel doesn't match layer : %s.\n", ch_gb.buffer);
  1151. }
  1152. } else {
  1153. layer_match = 1;
  1154. }
  1155. if (layer_match) { /* only search channel if the layer match is valid */
  1156. if (!strcmp(ch_gb.buffer, "R") ||
  1157. !strcmp(ch_gb.buffer, "X") ||
  1158. !strcmp(ch_gb.buffer, "U")) {
  1159. channel_index = 0;
  1160. s->is_luma = 0;
  1161. } else if (!strcmp(ch_gb.buffer, "G") ||
  1162. !strcmp(ch_gb.buffer, "V")) {
  1163. channel_index = 1;
  1164. s->is_luma = 0;
  1165. } else if (!strcmp(ch_gb.buffer, "Y")) {
  1166. channel_index = 1;
  1167. s->is_luma = 1;
  1168. } else if (!strcmp(ch_gb.buffer, "B") ||
  1169. !strcmp(ch_gb.buffer, "Z") ||
  1170. !strcmp(ch_gb.buffer, "W")){
  1171. channel_index = 2;
  1172. s->is_luma = 0;
  1173. } else if (!strcmp(ch_gb.buffer, "A")) {
  1174. channel_index = 3;
  1175. } else {
  1176. av_log(s->avctx, AV_LOG_WARNING,
  1177. "Unsupported channel %.256s.\n", ch_gb.buffer);
  1178. }
  1179. }
  1180. /* skip until you get a 0 */
  1181. while (bytestream2_get_bytes_left(&ch_gb) > 0 &&
  1182. bytestream2_get_byte(&ch_gb))
  1183. continue;
  1184. if (bytestream2_get_bytes_left(&ch_gb) < 4) {
  1185. av_log(s->avctx, AV_LOG_ERROR, "Incomplete header.\n");
  1186. return AVERROR_INVALIDDATA;
  1187. }
  1188. current_pixel_type = bytestream2_get_le32(&ch_gb);
  1189. if (current_pixel_type >= EXR_UNKNOWN) {
  1190. avpriv_report_missing_feature(s->avctx, "Pixel type %d",
  1191. current_pixel_type);
  1192. return AVERROR_PATCHWELCOME;
  1193. }
  1194. bytestream2_skip(&ch_gb, 4);
  1195. xsub = bytestream2_get_le32(&ch_gb);
  1196. ysub = bytestream2_get_le32(&ch_gb);
  1197. if (xsub != 1 || ysub != 1) {
  1198. avpriv_report_missing_feature(s->avctx,
  1199. "Subsampling %dx%d",
  1200. xsub, ysub);
  1201. return AVERROR_PATCHWELCOME;
  1202. }
  1203. if (channel_index >= 0 && s->channel_offsets[channel_index] == -1) { /* channel has not been previously assigned */
  1204. if (s->pixel_type != EXR_UNKNOWN &&
  1205. s->pixel_type != current_pixel_type) {
  1206. av_log(s->avctx, AV_LOG_ERROR,
  1207. "RGB channels not of the same depth.\n");
  1208. return AVERROR_INVALIDDATA;
  1209. }
  1210. s->pixel_type = current_pixel_type;
  1211. s->channel_offsets[channel_index] = s->current_channel_offset;
  1212. }
  1213. s->channels = av_realloc(s->channels,
  1214. ++s->nb_channels * sizeof(EXRChannel));
  1215. if (!s->channels)
  1216. return AVERROR(ENOMEM);
  1217. channel = &s->channels[s->nb_channels - 1];
  1218. channel->pixel_type = current_pixel_type;
  1219. channel->xsub = xsub;
  1220. channel->ysub = ysub;
  1221. if (current_pixel_type == EXR_HALF) {
  1222. s->current_channel_offset += 2;
  1223. } else {/* Float or UINT32 */
  1224. s->current_channel_offset += 4;
  1225. }
  1226. }
  1227. /* Check if all channels are set with an offset or if the channels
  1228. * are causing an overflow */
  1229. if (!s->is_luma){/* if we expected to have at least 3 channels */
  1230. if (FFMIN3(s->channel_offsets[0],
  1231. s->channel_offsets[1],
  1232. s->channel_offsets[2]) < 0) {
  1233. if (s->channel_offsets[0] < 0)
  1234. av_log(s->avctx, AV_LOG_ERROR, "Missing red channel.\n");
  1235. if (s->channel_offsets[1] < 0)
  1236. av_log(s->avctx, AV_LOG_ERROR, "Missing green channel.\n");
  1237. if (s->channel_offsets[2] < 0)
  1238. av_log(s->avctx, AV_LOG_ERROR, "Missing blue channel.\n");
  1239. return AVERROR_INVALIDDATA;
  1240. }
  1241. }
  1242. // skip one last byte and update main gb
  1243. s->gb.buffer = ch_gb.buffer + 1;
  1244. continue;
  1245. } else if ((var_size = check_header_variable(s, "dataWindow", "box2i",
  1246. 31)) >= 0) {
  1247. if (!var_size)
  1248. return AVERROR_INVALIDDATA;
  1249. s->xmin = bytestream2_get_le32(&s->gb);
  1250. s->ymin = bytestream2_get_le32(&s->gb);
  1251. s->xmax = bytestream2_get_le32(&s->gb);
  1252. s->ymax = bytestream2_get_le32(&s->gb);
  1253. s->xdelta = (s->xmax - s->xmin) + 1;
  1254. s->ydelta = (s->ymax - s->ymin) + 1;
  1255. continue;
  1256. } else if ((var_size = check_header_variable(s, "displayWindow",
  1257. "box2i", 34)) >= 0) {
  1258. if (!var_size)
  1259. return AVERROR_INVALIDDATA;
  1260. bytestream2_skip(&s->gb, 8);
  1261. s->w = bytestream2_get_le32(&s->gb) + 1;
  1262. s->h = bytestream2_get_le32(&s->gb) + 1;
  1263. continue;
  1264. } else if ((var_size = check_header_variable(s, "lineOrder",
  1265. "lineOrder", 25)) >= 0) {
  1266. int line_order;
  1267. if (!var_size)
  1268. return AVERROR_INVALIDDATA;
  1269. line_order = bytestream2_get_byte(&s->gb);
  1270. av_log(s->avctx, AV_LOG_DEBUG, "line order: %d.\n", line_order);
  1271. if (line_order > 2) {
  1272. av_log(s->avctx, AV_LOG_ERROR, "Unknown line order.\n");
  1273. return AVERROR_INVALIDDATA;
  1274. }
  1275. continue;
  1276. } else if ((var_size = check_header_variable(s, "pixelAspectRatio",
  1277. "float", 31)) >= 0) {
  1278. if (!var_size)
  1279. return AVERROR_INVALIDDATA;
  1280. sar = bytestream2_get_le32(&s->gb);
  1281. continue;
  1282. } else if ((var_size = check_header_variable(s, "compression",
  1283. "compression", 29)) >= 0) {
  1284. if (!var_size)
  1285. return AVERROR_INVALIDDATA;
  1286. if (s->compression == EXR_UNKN)
  1287. s->compression = bytestream2_get_byte(&s->gb);
  1288. else
  1289. av_log(s->avctx, AV_LOG_WARNING,
  1290. "Found more than one compression attribute.\n");
  1291. continue;
  1292. } else if ((var_size = check_header_variable(s, "tiles",
  1293. "tiledesc", 22)) >= 0) {
  1294. char tileLevel;
  1295. if (!s->is_tile)
  1296. av_log(s->avctx, AV_LOG_WARNING,
  1297. "Found tile attribute and scanline flags. Exr will be interpreted as scanline.\n");
  1298. s->tile_attr.xSize = bytestream2_get_le32(&s->gb);
  1299. s->tile_attr.ySize = bytestream2_get_le32(&s->gb);
  1300. tileLevel = bytestream2_get_byte(&s->gb);
  1301. s->tile_attr.level_mode = tileLevel & 0x0f;
  1302. s->tile_attr.level_round = (tileLevel >> 4) & 0x0f;
  1303. if (s->tile_attr.level_mode >= EXR_TILE_LEVEL_UNKNOWN){
  1304. avpriv_report_missing_feature(s->avctx, "Tile level mode %d",
  1305. s->tile_attr.level_mode);
  1306. return AVERROR_PATCHWELCOME;
  1307. }
  1308. if (s->tile_attr.level_round >= EXR_TILE_ROUND_UNKNOWN) {
  1309. avpriv_report_missing_feature(s->avctx, "Tile level round %d",
  1310. s->tile_attr.level_round);
  1311. return AVERROR_PATCHWELCOME;
  1312. }
  1313. continue;
  1314. }
  1315. // Check if there are enough bytes for a header
  1316. if (bytestream2_get_bytes_left(&s->gb) <= 9) {
  1317. av_log(s->avctx, AV_LOG_ERROR, "Incomplete header\n");
  1318. return AVERROR_INVALIDDATA;
  1319. }
  1320. // Process unknown variables
  1321. for (i = 0; i < 2; i++) // value_name and value_type
  1322. while (bytestream2_get_byte(&s->gb) != 0);
  1323. // Skip variable length
  1324. bytestream2_skip(&s->gb, bytestream2_get_le32(&s->gb));
  1325. }
  1326. ff_set_sar(s->avctx, av_d2q(av_int2float(sar), 255));
  1327. if (s->compression == EXR_UNKN) {
  1328. av_log(s->avctx, AV_LOG_ERROR, "Missing compression attribute.\n");
  1329. return AVERROR_INVALIDDATA;
  1330. }
  1331. if (s->is_tile) {
  1332. if (s->tile_attr.xSize < 1 || s->tile_attr.ySize < 1) {
  1333. av_log(s->avctx, AV_LOG_ERROR, "Invalid tile attribute.\n");
  1334. return AVERROR_INVALIDDATA;
  1335. }
  1336. }
  1337. if (bytestream2_get_bytes_left(&s->gb) <= 0) {
  1338. av_log(s->avctx, AV_LOG_ERROR, "Incomplete frame.\n");
  1339. return AVERROR_INVALIDDATA;
  1340. }
  1341. // aaand we are done
  1342. bytestream2_skip(&s->gb, 1);
  1343. return 0;
  1344. }
  1345. static int decode_frame(AVCodecContext *avctx, void *data,
  1346. int *got_frame, AVPacket *avpkt)
  1347. {
  1348. EXRContext *s = avctx->priv_data;
  1349. ThreadFrame frame = { .f = data };
  1350. AVFrame *picture = data;
  1351. uint8_t *ptr;
  1352. int y, ret;
  1353. int out_line_size;
  1354. int nb_blocks;/* nb scanline or nb tile */
  1355. bytestream2_init(&s->gb, avpkt->data, avpkt->size);
  1356. if ((ret = decode_header(s)) < 0)
  1357. return ret;
  1358. switch (s->pixel_type) {
  1359. case EXR_FLOAT:
  1360. case EXR_HALF:
  1361. if (s->channel_offsets[3] >= 0) {
  1362. if (!s->is_luma) {
  1363. avctx->pix_fmt = AV_PIX_FMT_RGBA64;
  1364. } else {
  1365. avctx->pix_fmt = AV_PIX_FMT_YA16;
  1366. }
  1367. } else {
  1368. if (!s->is_luma) {
  1369. avctx->pix_fmt = AV_PIX_FMT_RGB48;
  1370. } else {
  1371. avctx->pix_fmt = AV_PIX_FMT_GRAY16;
  1372. }
  1373. }
  1374. break;
  1375. case EXR_UINT:
  1376. avpriv_request_sample(avctx, "32-bit unsigned int");
  1377. return AVERROR_PATCHWELCOME;
  1378. default:
  1379. av_log(avctx, AV_LOG_ERROR, "Missing channel list.\n");
  1380. return AVERROR_INVALIDDATA;
  1381. }
  1382. if (s->apply_trc_type != AVCOL_TRC_UNSPECIFIED)
  1383. avctx->color_trc = s->apply_trc_type;
  1384. switch (s->compression) {
  1385. case EXR_RAW:
  1386. case EXR_RLE:
  1387. case EXR_ZIP1:
  1388. s->scan_lines_per_block = 1;
  1389. break;
  1390. case EXR_PXR24:
  1391. case EXR_ZIP16:
  1392. s->scan_lines_per_block = 16;
  1393. break;
  1394. case EXR_PIZ:
  1395. case EXR_B44:
  1396. case EXR_B44A:
  1397. s->scan_lines_per_block = 32;
  1398. break;
  1399. default:
  1400. avpriv_report_missing_feature(avctx, "Compression %d", s->compression);
  1401. return AVERROR_PATCHWELCOME;
  1402. }
  1403. /* Verify the xmin, xmax, ymin, ymax and xdelta before setting
  1404. * the actual image size. */
  1405. if (s->xmin > s->xmax ||
  1406. s->ymin > s->ymax ||
  1407. s->xdelta != s->xmax - s->xmin + 1 ||
  1408. s->xmax >= s->w ||
  1409. s->ymax >= s->h) {
  1410. av_log(avctx, AV_LOG_ERROR, "Wrong or missing size information.\n");
  1411. return AVERROR_INVALIDDATA;
  1412. }
  1413. if ((ret = ff_set_dimensions(avctx, s->w, s->h)) < 0)
  1414. return ret;
  1415. s->desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  1416. if (!s->desc)
  1417. return AVERROR_INVALIDDATA;
  1418. out_line_size = avctx->width * 2 * s->desc->nb_components;
  1419. if (s->is_tile) {
  1420. nb_blocks = ((s->xdelta + s->tile_attr.xSize - 1) / s->tile_attr.xSize) *
  1421. ((s->ydelta + s->tile_attr.ySize - 1) / s->tile_attr.ySize);
  1422. } else { /* scanline */
  1423. nb_blocks = (s->ydelta + s->scan_lines_per_block - 1) /
  1424. s->scan_lines_per_block;
  1425. }
  1426. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
  1427. return ret;
  1428. if (bytestream2_get_bytes_left(&s->gb) < nb_blocks * 8)
  1429. return AVERROR_INVALIDDATA;
  1430. // save pointer we are going to use in decode_block
  1431. s->buf = avpkt->data;
  1432. s->buf_size = avpkt->size;
  1433. ptr = picture->data[0];
  1434. // Zero out the start if ymin is not 0
  1435. for (y = 0; y < s->ymin; y++) {
  1436. memset(ptr, 0, out_line_size);
  1437. ptr += picture->linesize[0];
  1438. }
  1439. s->picture = picture;
  1440. avctx->execute2(avctx, decode_block, s->thread_data, NULL, nb_blocks);
  1441. // Zero out the end if ymax+1 is not h
  1442. ptr = picture->data[0] + ((s->ymax+1) * picture->linesize[0]);
  1443. for (y = s->ymax + 1; y < avctx->height; y++) {
  1444. memset(ptr, 0, out_line_size);
  1445. ptr += picture->linesize[0];
  1446. }
  1447. picture->pict_type = AV_PICTURE_TYPE_I;
  1448. *got_frame = 1;
  1449. return avpkt->size;
  1450. }
  1451. static av_cold int decode_init(AVCodecContext *avctx)
  1452. {
  1453. EXRContext *s = avctx->priv_data;
  1454. uint32_t i;
  1455. union av_intfloat32 t;
  1456. float one_gamma = 1.0f / s->gamma;
  1457. avpriv_trc_function trc_func = NULL;
  1458. s->avctx = avctx;
  1459. trc_func = avpriv_get_trc_function_from_trc(s->apply_trc_type);
  1460. if (trc_func) {
  1461. for (i = 0; i < 65536; ++i) {
  1462. t = exr_half2float(i);
  1463. t.f = trc_func(t.f);
  1464. s->gamma_table[i] = exr_flt2uint(t.i);
  1465. }
  1466. } else {
  1467. if (one_gamma > 0.9999f && one_gamma < 1.0001f) {
  1468. for (i = 0; i < 65536; ++i)
  1469. s->gamma_table[i] = exr_halflt2uint(i);
  1470. } else {
  1471. for (i = 0; i < 65536; ++i) {
  1472. t = exr_half2float(i);
  1473. /* If negative value we reuse half value */
  1474. if (t.f <= 0.0f) {
  1475. s->gamma_table[i] = exr_halflt2uint(i);
  1476. } else {
  1477. t.f = powf(t.f, one_gamma);
  1478. s->gamma_table[i] = exr_flt2uint(t.i);
  1479. }
  1480. }
  1481. }
  1482. }
  1483. // allocate thread data, used for non EXR_RAW compression types
  1484. s->thread_data = av_mallocz_array(avctx->thread_count, sizeof(EXRThreadData));
  1485. if (!s->thread_data)
  1486. return AVERROR_INVALIDDATA;
  1487. return 0;
  1488. }
  1489. #if HAVE_THREADS
  1490. static int decode_init_thread_copy(AVCodecContext *avctx)
  1491. { EXRContext *s = avctx->priv_data;
  1492. // allocate thread data, used for non EXR_RAW compression types
  1493. s->thread_data = av_mallocz_array(avctx->thread_count, sizeof(EXRThreadData));
  1494. if (!s->thread_data)
  1495. return AVERROR_INVALIDDATA;
  1496. return 0;
  1497. }
  1498. #endif
  1499. static av_cold int decode_end(AVCodecContext *avctx)
  1500. {
  1501. EXRContext *s = avctx->priv_data;
  1502. int i;
  1503. for (i = 0; i < avctx->thread_count; i++) {
  1504. EXRThreadData *td = &s->thread_data[i];
  1505. av_freep(&td->uncompressed_data);
  1506. av_freep(&td->tmp);
  1507. av_freep(&td->bitmap);
  1508. av_freep(&td->lut);
  1509. }
  1510. av_freep(&s->thread_data);
  1511. av_freep(&s->channels);
  1512. return 0;
  1513. }
  1514. #define OFFSET(x) offsetof(EXRContext, x)
  1515. #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  1516. static const AVOption options[] = {
  1517. { "layer", "Set the decoding layer", OFFSET(layer),
  1518. AV_OPT_TYPE_STRING, { .str = "" }, 0, 0, VD },
  1519. { "gamma", "Set the float gamma value when decoding", OFFSET(gamma),
  1520. AV_OPT_TYPE_FLOAT, { .dbl = 1.0f }, 0.001, FLT_MAX, VD },
  1521. // XXX: Note the abuse of the enum using AVCOL_TRC_UNSPECIFIED to subsume the existing gamma option
  1522. { "apply_trc", "color transfer characteristics to apply to EXR linear input", OFFSET(apply_trc_type),
  1523. AV_OPT_TYPE_INT, {.i64 = AVCOL_TRC_UNSPECIFIED }, 1, AVCOL_TRC_NB-1, VD, "apply_trc_type"},
  1524. { "bt709", "BT.709", 0,
  1525. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT709 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1526. { "gamma", "gamma", 0,
  1527. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_UNSPECIFIED }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1528. { "gamma22", "BT.470 M", 0,
  1529. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_GAMMA22 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1530. { "gamma28", "BT.470 BG", 0,
  1531. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_GAMMA28 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1532. { "smpte170m", "SMPTE 170 M", 0,
  1533. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTE170M }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1534. { "smpte240m", "SMPTE 240 M", 0,
  1535. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTE240M }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1536. { "linear", "Linear", 0,
  1537. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LINEAR }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1538. { "log", "Log", 0,
  1539. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LOG }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1540. { "log_sqrt", "Log square root", 0,
  1541. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LOG_SQRT }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1542. { "iec61966_2_4", "IEC 61966-2-4", 0,
  1543. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_IEC61966_2_4 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1544. { "bt1361", "BT.1361", 0,
  1545. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT1361_ECG }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1546. { "iec61966_2_1", "IEC 61966-2-1", 0,
  1547. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_IEC61966_2_1 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1548. { "bt2020_10bit", "BT.2020 - 10 bit", 0,
  1549. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT2020_10 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1550. { "bt2020_12bit", "BT.2020 - 12 bit", 0,
  1551. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT2020_12 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1552. { "smpte2084", "SMPTE ST 2084", 0,
  1553. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTEST2084 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1554. { "smpte428_1", "SMPTE ST 428-1", 0,
  1555. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTEST428_1 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1556. { NULL },
  1557. };
  1558. static const AVClass exr_class = {
  1559. .class_name = "EXR",
  1560. .item_name = av_default_item_name,
  1561. .option = options,
  1562. .version = LIBAVUTIL_VERSION_INT,
  1563. };
  1564. AVCodec ff_exr_decoder = {
  1565. .name = "exr",
  1566. .long_name = NULL_IF_CONFIG_SMALL("OpenEXR image"),
  1567. .type = AVMEDIA_TYPE_VIDEO,
  1568. .id = AV_CODEC_ID_EXR,
  1569. .priv_data_size = sizeof(EXRContext),
  1570. .init = decode_init,
  1571. .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
  1572. .close = decode_end,
  1573. .decode = decode_frame,
  1574. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS |
  1575. AV_CODEC_CAP_SLICE_THREADS,
  1576. .priv_class = &exr_class,
  1577. };