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.

1656 lines
53KB

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