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.

2094 lines
69KB

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