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.

1794 lines
58KB

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