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.

1799 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 = 0;
  661. const uint8_t *in = td->tmp;
  662. uint8_t *out;
  663. int c, i, j;
  664. for (i = 0; i < s->nb_channels; i++) {
  665. if (s->channels[i].pixel_type == EXR_FLOAT) {
  666. expected_len += (td->xsize * td->ysize * 3);/* PRX 24 store float in 24 bit instead of 32 */
  667. } else if (s->channels[i].pixel_type == EXR_HALF) {
  668. expected_len += (td->xsize * td->ysize * 2);
  669. } else {//UINT 32
  670. expected_len += (td->xsize * td->ysize * 4);
  671. }
  672. }
  673. dest_len = expected_len;
  674. if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK) {
  675. return AVERROR_INVALIDDATA;
  676. } else if (dest_len != expected_len) {
  677. return AVERROR_INVALIDDATA;
  678. }
  679. out = td->uncompressed_data;
  680. for (i = 0; i < td->ysize; i++)
  681. for (c = 0; c < s->nb_channels; c++) {
  682. EXRChannel *channel = &s->channels[c];
  683. const uint8_t *ptr[4];
  684. uint32_t pixel = 0;
  685. switch (channel->pixel_type) {
  686. case EXR_FLOAT:
  687. ptr[0] = in;
  688. ptr[1] = ptr[0] + td->xsize;
  689. ptr[2] = ptr[1] + td->xsize;
  690. in = ptr[2] + td->xsize;
  691. for (j = 0; j < td->xsize; ++j) {
  692. uint32_t diff = (*(ptr[0]++) << 24) |
  693. (*(ptr[1]++) << 16) |
  694. (*(ptr[2]++) << 8);
  695. pixel += diff;
  696. bytestream_put_le32(&out, pixel);
  697. }
  698. break;
  699. case EXR_HALF:
  700. ptr[0] = in;
  701. ptr[1] = ptr[0] + td->xsize;
  702. in = ptr[1] + td->xsize;
  703. for (j = 0; j < td->xsize; j++) {
  704. uint32_t diff = (*(ptr[0]++) << 8) | *(ptr[1]++);
  705. pixel += diff;
  706. bytestream_put_le16(&out, pixel);
  707. }
  708. break;
  709. default:
  710. return AVERROR_INVALIDDATA;
  711. }
  712. }
  713. return 0;
  714. }
  715. static void unpack_14(const uint8_t b[14], uint16_t s[16])
  716. {
  717. unsigned short shift = (b[ 2] >> 2);
  718. unsigned short bias = (0x20 << shift);
  719. int i;
  720. s[ 0] = (b[0] << 8) | b[1];
  721. s[ 4] = s[ 0] + ((((b[ 2] << 4) | (b[ 3] >> 4)) & 0x3f) << shift) - bias;
  722. s[ 8] = s[ 4] + ((((b[ 3] << 2) | (b[ 4] >> 6)) & 0x3f) << shift) - bias;
  723. s[12] = s[ 8] + ((b[ 4] & 0x3f) << shift) - bias;
  724. s[ 1] = s[ 0] + ((b[ 5] >> 2) << shift) - bias;
  725. s[ 5] = s[ 4] + ((((b[ 5] << 4) | (b[ 6] >> 4)) & 0x3f) << shift) - bias;
  726. s[ 9] = s[ 8] + ((((b[ 6] << 2) | (b[ 7] >> 6)) & 0x3f) << shift) - bias;
  727. s[13] = s[12] + ((b[ 7] & 0x3f) << shift) - bias;
  728. s[ 2] = s[ 1] + ((b[ 8] >> 2) << shift) - bias;
  729. s[ 6] = s[ 5] + ((((b[ 8] << 4) | (b[ 9] >> 4)) & 0x3f) << shift) - bias;
  730. s[10] = s[ 9] + ((((b[ 9] << 2) | (b[10] >> 6)) & 0x3f) << shift) - bias;
  731. s[14] = s[13] + ((b[10] & 0x3f) << shift) - bias;
  732. s[ 3] = s[ 2] + ((b[11] >> 2) << shift) - bias;
  733. s[ 7] = s[ 6] + ((((b[11] << 4) | (b[12] >> 4)) & 0x3f) << shift) - bias;
  734. s[11] = s[10] + ((((b[12] << 2) | (b[13] >> 6)) & 0x3f) << shift) - bias;
  735. s[15] = s[14] + ((b[13] & 0x3f) << shift) - bias;
  736. for (i = 0; i < 16; ++i) {
  737. if (s[i] & 0x8000)
  738. s[i] &= 0x7fff;
  739. else
  740. s[i] = ~s[i];
  741. }
  742. }
  743. static void unpack_3(const uint8_t b[3], uint16_t s[16])
  744. {
  745. int i;
  746. s[0] = (b[0] << 8) | b[1];
  747. if (s[0] & 0x8000)
  748. s[0] &= 0x7fff;
  749. else
  750. s[0] = ~s[0];
  751. for (i = 1; i < 16; i++)
  752. s[i] = s[0];
  753. }
  754. static int b44_uncompress(EXRContext *s, const uint8_t *src, int compressed_size,
  755. int uncompressed_size, EXRThreadData *td) {
  756. const int8_t *sr = src;
  757. int stayToUncompress = compressed_size;
  758. int nbB44BlockW, nbB44BlockH;
  759. int indexHgX, indexHgY, indexOut, indexTmp;
  760. uint16_t tmpBuffer[16]; /* B44 use 4x4 half float pixel */
  761. int c, iY, iX, y, x;
  762. /* calc B44 block count */
  763. nbB44BlockW = td->xsize / 4;
  764. if ((td->xsize % 4) != 0)
  765. nbB44BlockW++;
  766. nbB44BlockH = td->ysize / 4;
  767. if ((td->ysize % 4) != 0)
  768. nbB44BlockH++;
  769. for (c = 0; c < s->nb_channels; c++) {
  770. for (iY = 0; iY < nbB44BlockH; iY++) {
  771. for (iX = 0; iX < nbB44BlockW; iX++) {/* For each B44 block */
  772. if (stayToUncompress < 3) {
  773. av_log(s, AV_LOG_ERROR, "Not enough data for B44A block: %d", stayToUncompress);
  774. return AVERROR_INVALIDDATA;
  775. }
  776. if (src[compressed_size - stayToUncompress + 2] == 0xfc) { /* B44A block */
  777. unpack_3(sr, tmpBuffer);
  778. sr += 3;
  779. stayToUncompress -= 3;
  780. } else {/* B44 Block */
  781. if (stayToUncompress < 14) {
  782. av_log(s, AV_LOG_ERROR, "Not enough data for B44 block: %d", stayToUncompress);
  783. return AVERROR_INVALIDDATA;
  784. }
  785. unpack_14(sr, tmpBuffer);
  786. sr += 14;
  787. stayToUncompress -= 14;
  788. }
  789. /* copy data to uncompress buffer (B44 block can exceed target resolution)*/
  790. indexHgX = iX * 4;
  791. indexHgY = iY * 4;
  792. for (y = indexHgY; y < FFMIN(indexHgY + 4, td->ysize); y++) {
  793. for (x = indexHgX; x < FFMIN(indexHgX + 4, td->xsize); x++) {
  794. indexOut = (c * td->xsize + y * td->xsize * s->nb_channels + x) * 2;
  795. indexTmp = (y-indexHgY) * 4 + (x-indexHgX);
  796. td->uncompressed_data[indexOut] = tmpBuffer[indexTmp] & 0xff;
  797. td->uncompressed_data[indexOut + 1] = tmpBuffer[indexTmp] >> 8;
  798. }
  799. }
  800. }
  801. }
  802. }
  803. return 0;
  804. }
  805. static int decode_block(AVCodecContext *avctx, void *tdata,
  806. int jobnr, int threadnr)
  807. {
  808. EXRContext *s = avctx->priv_data;
  809. AVFrame *const p = s->picture;
  810. EXRThreadData *td = &s->thread_data[threadnr];
  811. const uint8_t *channel_buffer[4] = { 0 };
  812. const uint8_t *buf = s->buf;
  813. uint64_t line_offset, uncompressed_size;
  814. uint16_t *ptr_x;
  815. uint8_t *ptr;
  816. uint32_t data_size, line, col = 0;
  817. uint32_t tileX, tileY, tileLevelX, tileLevelY;
  818. int channel_line_size;
  819. const uint8_t *src;
  820. int axmax = (avctx->width - (s->xmax + 1)) * 2 * s->desc->nb_components; /* nb pixel to add at the right of the datawindow */
  821. int bxmin = s->xmin * 2 * s->desc->nb_components; /* nb pixel to add at the left of the datawindow */
  822. int i, x, buf_size = s->buf_size;
  823. float one_gamma = 1.0f / s->gamma;
  824. avpriv_trc_function trc_func = avpriv_get_trc_function_from_trc(s->apply_trc_type);
  825. int ret;
  826. line_offset = AV_RL64(s->gb.buffer + jobnr * 8);
  827. if (s->is_tile) {
  828. if (line_offset > buf_size - 20)
  829. return AVERROR_INVALIDDATA;
  830. src = buf + line_offset + 20;
  831. tileX = AV_RL32(src - 20);
  832. tileY = AV_RL32(src - 16);
  833. tileLevelX = AV_RL32(src - 12);
  834. tileLevelY = AV_RL32(src - 8);
  835. data_size = AV_RL32(src - 4);
  836. if (data_size <= 0 || data_size > buf_size)
  837. return AVERROR_INVALIDDATA;
  838. if (tileLevelX || tileLevelY) { /* tile level, is not the full res level */
  839. avpriv_report_missing_feature(s->avctx, "Subres tile before full res tile");
  840. return AVERROR_PATCHWELCOME;
  841. }
  842. line = s->tile_attr.ySize * tileY;
  843. col = s->tile_attr.xSize * tileX;
  844. td->ysize = FFMIN(s->tile_attr.ySize, s->ydelta - tileY * s->tile_attr.ySize);
  845. td->xsize = FFMIN(s->tile_attr.xSize, s->xdelta - tileX * s->tile_attr.xSize);
  846. if (col) { /* not the first tile of the line */
  847. bxmin = 0; /* doesn't add pixel at the left of the datawindow */
  848. }
  849. if ((col + td->xsize) != s->xdelta)/* not the last tile of the line */
  850. axmax = 0; /* doesn't add pixel at the right of the datawindow */
  851. channel_line_size = td->xsize * s->current_channel_offset;/* uncompress size of one line */
  852. uncompressed_size = channel_line_size * (uint64_t)td->ysize;/* uncompress size of the block */
  853. } else {
  854. if (line_offset > buf_size - 8)
  855. return AVERROR_INVALIDDATA;
  856. src = buf + line_offset + 8;
  857. line = AV_RL32(src - 8);
  858. if (line < s->ymin || line > s->ymax)
  859. return AVERROR_INVALIDDATA;
  860. data_size = AV_RL32(src - 4);
  861. if (data_size <= 0 || data_size > buf_size)
  862. return AVERROR_INVALIDDATA;
  863. td->ysize = FFMIN(s->scan_lines_per_block, s->ymax - line + 1); /* s->ydelta - line ?? */
  864. td->xsize = s->xdelta;
  865. channel_line_size = td->xsize * s->current_channel_offset;/* uncompress size of one line */
  866. uncompressed_size = channel_line_size * (uint64_t)td->ysize;/* uncompress size of the block */
  867. if ((s->compression == EXR_RAW && (data_size != uncompressed_size ||
  868. line_offset > buf_size - uncompressed_size)) ||
  869. (s->compression != EXR_RAW && (data_size > uncompressed_size ||
  870. line_offset > buf_size - data_size))) {
  871. return AVERROR_INVALIDDATA;
  872. }
  873. }
  874. if (data_size < uncompressed_size || s->is_tile) { /* td->tmp is use for tile reorganization */
  875. av_fast_padded_malloc(&td->tmp, &td->tmp_size, uncompressed_size);
  876. if (!td->tmp)
  877. return AVERROR(ENOMEM);
  878. }
  879. if (data_size < uncompressed_size) {
  880. av_fast_padded_malloc(&td->uncompressed_data,
  881. &td->uncompressed_size, uncompressed_size);
  882. if (!td->uncompressed_data)
  883. return AVERROR(ENOMEM);
  884. ret = AVERROR_INVALIDDATA;
  885. switch (s->compression) {
  886. case EXR_ZIP1:
  887. case EXR_ZIP16:
  888. ret = zip_uncompress(src, data_size, uncompressed_size, td);
  889. break;
  890. case EXR_PIZ:
  891. ret = piz_uncompress(s, src, data_size, uncompressed_size, td);
  892. break;
  893. case EXR_PXR24:
  894. ret = pxr24_uncompress(s, src, data_size, uncompressed_size, td);
  895. break;
  896. case EXR_RLE:
  897. ret = rle_uncompress(src, data_size, uncompressed_size, td);
  898. break;
  899. case EXR_B44:
  900. case EXR_B44A:
  901. ret = b44_uncompress(s, src, data_size, uncompressed_size, td);
  902. break;
  903. }
  904. if (ret < 0) {
  905. av_log(avctx, AV_LOG_ERROR, "decode_block() failed.\n");
  906. return ret;
  907. }
  908. src = td->uncompressed_data;
  909. }
  910. channel_buffer[0] = src + td->xsize * s->channel_offsets[0];
  911. channel_buffer[1] = src + td->xsize * s->channel_offsets[1];
  912. channel_buffer[2] = src + td->xsize * s->channel_offsets[2];
  913. if (s->channel_offsets[3] >= 0)
  914. channel_buffer[3] = src + td->xsize * s->channel_offsets[3];
  915. ptr = p->data[0] + line * p->linesize[0] + (col * s->desc->nb_components * 2);
  916. for (i = 0;
  917. i < td->ysize; i++, ptr += p->linesize[0]) {
  918. const uint8_t *r, *g, *b, *a;
  919. r = channel_buffer[0];
  920. g = channel_buffer[1];
  921. b = channel_buffer[2];
  922. if (channel_buffer[3])
  923. a = channel_buffer[3];
  924. ptr_x = (uint16_t *) ptr;
  925. // Zero out the start if xmin is not 0
  926. memset(ptr_x, 0, bxmin);
  927. ptr_x += s->xmin * s->desc->nb_components;
  928. if (s->pixel_type == EXR_FLOAT) {
  929. // 32-bit
  930. if (trc_func) {
  931. for (x = 0; x < td->xsize; x++) {
  932. union av_intfloat32 t;
  933. t.i = bytestream_get_le32(&r);
  934. t.f = trc_func(t.f);
  935. *ptr_x++ = exr_flt2uint(t.i);
  936. t.i = bytestream_get_le32(&g);
  937. t.f = trc_func(t.f);
  938. *ptr_x++ = exr_flt2uint(t.i);
  939. t.i = bytestream_get_le32(&b);
  940. t.f = trc_func(t.f);
  941. *ptr_x++ = exr_flt2uint(t.i);
  942. if (channel_buffer[3])
  943. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a));
  944. }
  945. } else {
  946. for (x = 0; x < td->xsize; x++) {
  947. union av_intfloat32 t;
  948. t.i = bytestream_get_le32(&r);
  949. if (t.f > 0.0f) /* avoid negative values */
  950. t.f = powf(t.f, one_gamma);
  951. *ptr_x++ = exr_flt2uint(t.i);
  952. t.i = bytestream_get_le32(&g);
  953. if (t.f > 0.0f)
  954. t.f = powf(t.f, one_gamma);
  955. *ptr_x++ = exr_flt2uint(t.i);
  956. t.i = bytestream_get_le32(&b);
  957. if (t.f > 0.0f)
  958. t.f = powf(t.f, one_gamma);
  959. *ptr_x++ = exr_flt2uint(t.i);
  960. if (channel_buffer[3])
  961. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a));
  962. }
  963. }
  964. } else {
  965. // 16-bit
  966. for (x = 0; x < td->xsize; x++) {
  967. *ptr_x++ = s->gamma_table[bytestream_get_le16(&r)];
  968. *ptr_x++ = s->gamma_table[bytestream_get_le16(&g)];
  969. *ptr_x++ = s->gamma_table[bytestream_get_le16(&b)];
  970. if (channel_buffer[3])
  971. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&a));
  972. }
  973. }
  974. // Zero out the end if xmax+1 is not w
  975. memset(ptr_x, 0, axmax);
  976. channel_buffer[0] += channel_line_size;
  977. channel_buffer[1] += channel_line_size;
  978. channel_buffer[2] += channel_line_size;
  979. if (channel_buffer[3])
  980. channel_buffer[3] += channel_line_size;
  981. }
  982. return 0;
  983. }
  984. /**
  985. * Check if the variable name corresponds to its data type.
  986. *
  987. * @param s the EXRContext
  988. * @param value_name name of the variable to check
  989. * @param value_type type of the variable to check
  990. * @param minimum_length minimum length of the variable data
  991. *
  992. * @return bytes to read containing variable data
  993. * -1 if variable is not found
  994. * 0 if buffer ended prematurely
  995. */
  996. static int check_header_variable(EXRContext *s,
  997. const char *value_name,
  998. const char *value_type,
  999. unsigned int minimum_length)
  1000. {
  1001. int var_size = -1;
  1002. if (bytestream2_get_bytes_left(&s->gb) >= minimum_length &&
  1003. !strcmp(s->gb.buffer, value_name)) {
  1004. // found value_name, jump to value_type (null terminated strings)
  1005. s->gb.buffer += strlen(value_name) + 1;
  1006. if (!strcmp(s->gb.buffer, value_type)) {
  1007. s->gb.buffer += strlen(value_type) + 1;
  1008. var_size = bytestream2_get_le32(&s->gb);
  1009. // don't go read past boundaries
  1010. if (var_size > bytestream2_get_bytes_left(&s->gb))
  1011. var_size = 0;
  1012. } else {
  1013. // value_type not found, reset the buffer
  1014. s->gb.buffer -= strlen(value_name) + 1;
  1015. av_log(s->avctx, AV_LOG_WARNING,
  1016. "Unknown data type %s for header variable %s.\n",
  1017. value_type, value_name);
  1018. }
  1019. }
  1020. return var_size;
  1021. }
  1022. static int decode_header(EXRContext *s)
  1023. {
  1024. int magic_number, version, i, flags, sar = 0;
  1025. int layer_match = 0;
  1026. s->current_channel_offset = 0;
  1027. s->xmin = ~0;
  1028. s->xmax = ~0;
  1029. s->ymin = ~0;
  1030. s->ymax = ~0;
  1031. s->xdelta = ~0;
  1032. s->ydelta = ~0;
  1033. s->channel_offsets[0] = -1;
  1034. s->channel_offsets[1] = -1;
  1035. s->channel_offsets[2] = -1;
  1036. s->channel_offsets[3] = -1;
  1037. s->pixel_type = EXR_UNKNOWN;
  1038. s->compression = EXR_UNKN;
  1039. s->nb_channels = 0;
  1040. s->w = 0;
  1041. s->h = 0;
  1042. s->tile_attr.xSize = -1;
  1043. s->tile_attr.ySize = -1;
  1044. s->is_tile = 0;
  1045. if (bytestream2_get_bytes_left(&s->gb) < 10) {
  1046. av_log(s->avctx, AV_LOG_ERROR, "Header too short to parse.\n");
  1047. return AVERROR_INVALIDDATA;
  1048. }
  1049. magic_number = bytestream2_get_le32(&s->gb);
  1050. if (magic_number != 20000630) {
  1051. /* As per documentation of OpenEXR, it is supposed to be
  1052. * int 20000630 little-endian */
  1053. av_log(s->avctx, AV_LOG_ERROR, "Wrong magic number %d.\n", magic_number);
  1054. return AVERROR_INVALIDDATA;
  1055. }
  1056. version = bytestream2_get_byte(&s->gb);
  1057. if (version != 2) {
  1058. avpriv_report_missing_feature(s->avctx, "Version %d", version);
  1059. return AVERROR_PATCHWELCOME;
  1060. }
  1061. flags = bytestream2_get_le24(&s->gb);
  1062. if (flags == 0x00)
  1063. s->is_tile = 0;
  1064. else if (flags & 0x02)
  1065. s->is_tile = 1;
  1066. else{
  1067. avpriv_report_missing_feature(s->avctx, "flags %d", flags);
  1068. return AVERROR_PATCHWELCOME;
  1069. }
  1070. // Parse the header
  1071. while (bytestream2_get_bytes_left(&s->gb) > 0 && *s->gb.buffer) {
  1072. int var_size;
  1073. if ((var_size = check_header_variable(s, "channels",
  1074. "chlist", 38)) >= 0) {
  1075. GetByteContext ch_gb;
  1076. if (!var_size)
  1077. return AVERROR_INVALIDDATA;
  1078. bytestream2_init(&ch_gb, s->gb.buffer, var_size);
  1079. while (bytestream2_get_bytes_left(&ch_gb) >= 19) {
  1080. EXRChannel *channel;
  1081. enum ExrPixelType current_pixel_type;
  1082. int channel_index = -1;
  1083. int xsub, ysub;
  1084. if (strcmp(s->layer, "") != 0) {
  1085. if (strncmp(ch_gb.buffer, s->layer, strlen(s->layer)) == 0) {
  1086. layer_match = 1;
  1087. av_log(s->avctx, AV_LOG_INFO,
  1088. "Channel match layer : %s.\n", ch_gb.buffer);
  1089. ch_gb.buffer += strlen(s->layer);
  1090. if (*ch_gb.buffer == '.')
  1091. ch_gb.buffer++; /* skip dot if not given */
  1092. } else {
  1093. av_log(s->avctx, AV_LOG_INFO,
  1094. "Channel doesn't match layer : %s.\n", ch_gb.buffer);
  1095. }
  1096. } else {
  1097. layer_match = 1;
  1098. }
  1099. if (layer_match) { /* only search channel if the layer match is valid */
  1100. if (!strcmp(ch_gb.buffer, "R") ||
  1101. !strcmp(ch_gb.buffer, "X") ||
  1102. !strcmp(ch_gb.buffer, "U"))
  1103. channel_index = 0;
  1104. else if (!strcmp(ch_gb.buffer, "G") ||
  1105. !strcmp(ch_gb.buffer, "Y") ||
  1106. !strcmp(ch_gb.buffer, "V"))
  1107. channel_index = 1;
  1108. else if (!strcmp(ch_gb.buffer, "B") ||
  1109. !strcmp(ch_gb.buffer, "Z") ||
  1110. !strcmp(ch_gb.buffer, "W"))
  1111. channel_index = 2;
  1112. else if (!strcmp(ch_gb.buffer, "A"))
  1113. channel_index = 3;
  1114. else
  1115. av_log(s->avctx, AV_LOG_WARNING,
  1116. "Unsupported channel %.256s.\n", ch_gb.buffer);
  1117. }
  1118. /* skip until you get a 0 */
  1119. while (bytestream2_get_bytes_left(&ch_gb) > 0 &&
  1120. bytestream2_get_byte(&ch_gb))
  1121. continue;
  1122. if (bytestream2_get_bytes_left(&ch_gb) < 4) {
  1123. av_log(s->avctx, AV_LOG_ERROR, "Incomplete header.\n");
  1124. return AVERROR_INVALIDDATA;
  1125. }
  1126. current_pixel_type = bytestream2_get_le32(&ch_gb);
  1127. if (current_pixel_type >= EXR_UNKNOWN) {
  1128. avpriv_report_missing_feature(s->avctx, "Pixel type %d",
  1129. current_pixel_type);
  1130. return AVERROR_PATCHWELCOME;
  1131. }
  1132. bytestream2_skip(&ch_gb, 4);
  1133. xsub = bytestream2_get_le32(&ch_gb);
  1134. ysub = bytestream2_get_le32(&ch_gb);
  1135. if (xsub != 1 || ysub != 1) {
  1136. avpriv_report_missing_feature(s->avctx,
  1137. "Subsampling %dx%d",
  1138. xsub, ysub);
  1139. return AVERROR_PATCHWELCOME;
  1140. }
  1141. if (s->channel_offsets[channel_index] == -1){/* channel have not been previously assign */
  1142. if (channel_index >= 0) {
  1143. if (s->pixel_type != EXR_UNKNOWN &&
  1144. s->pixel_type != current_pixel_type) {
  1145. av_log(s->avctx, AV_LOG_ERROR,
  1146. "RGB channels not of the same depth.\n");
  1147. return AVERROR_INVALIDDATA;
  1148. }
  1149. s->pixel_type = current_pixel_type;
  1150. s->channel_offsets[channel_index] = s->current_channel_offset;
  1151. }
  1152. }
  1153. s->channels = av_realloc(s->channels,
  1154. ++s->nb_channels * sizeof(EXRChannel));
  1155. if (!s->channels)
  1156. return AVERROR(ENOMEM);
  1157. channel = &s->channels[s->nb_channels - 1];
  1158. channel->pixel_type = current_pixel_type;
  1159. channel->xsub = xsub;
  1160. channel->ysub = ysub;
  1161. s->current_channel_offset += 1 << current_pixel_type;
  1162. }
  1163. /* Check if all channels are set with an offset or if the channels
  1164. * are causing an overflow */
  1165. if (FFMIN3(s->channel_offsets[0],
  1166. s->channel_offsets[1],
  1167. s->channel_offsets[2]) < 0) {
  1168. if (s->channel_offsets[0] < 0)
  1169. av_log(s->avctx, AV_LOG_ERROR, "Missing red channel.\n");
  1170. if (s->channel_offsets[1] < 0)
  1171. av_log(s->avctx, AV_LOG_ERROR, "Missing green channel.\n");
  1172. if (s->channel_offsets[2] < 0)
  1173. av_log(s->avctx, AV_LOG_ERROR, "Missing blue channel.\n");
  1174. return AVERROR_INVALIDDATA;
  1175. }
  1176. // skip one last byte and update main gb
  1177. s->gb.buffer = ch_gb.buffer + 1;
  1178. continue;
  1179. } else if ((var_size = check_header_variable(s, "dataWindow", "box2i",
  1180. 31)) >= 0) {
  1181. if (!var_size)
  1182. return AVERROR_INVALIDDATA;
  1183. s->xmin = bytestream2_get_le32(&s->gb);
  1184. s->ymin = bytestream2_get_le32(&s->gb);
  1185. s->xmax = bytestream2_get_le32(&s->gb);
  1186. s->ymax = bytestream2_get_le32(&s->gb);
  1187. s->xdelta = (s->xmax - s->xmin) + 1;
  1188. s->ydelta = (s->ymax - s->ymin) + 1;
  1189. continue;
  1190. } else if ((var_size = check_header_variable(s, "displayWindow",
  1191. "box2i", 34)) >= 0) {
  1192. if (!var_size)
  1193. return AVERROR_INVALIDDATA;
  1194. bytestream2_skip(&s->gb, 8);
  1195. s->w = bytestream2_get_le32(&s->gb) + 1;
  1196. s->h = bytestream2_get_le32(&s->gb) + 1;
  1197. continue;
  1198. } else if ((var_size = check_header_variable(s, "lineOrder",
  1199. "lineOrder", 25)) >= 0) {
  1200. int line_order;
  1201. if (!var_size)
  1202. return AVERROR_INVALIDDATA;
  1203. line_order = bytestream2_get_byte(&s->gb);
  1204. av_log(s->avctx, AV_LOG_DEBUG, "line order: %d.\n", line_order);
  1205. if (line_order > 2) {
  1206. av_log(s->avctx, AV_LOG_ERROR, "Unknown line order.\n");
  1207. return AVERROR_INVALIDDATA;
  1208. }
  1209. continue;
  1210. } else if ((var_size = check_header_variable(s, "pixelAspectRatio",
  1211. "float", 31)) >= 0) {
  1212. if (!var_size)
  1213. return AVERROR_INVALIDDATA;
  1214. sar = bytestream2_get_le32(&s->gb);
  1215. continue;
  1216. } else if ((var_size = check_header_variable(s, "compression",
  1217. "compression", 29)) >= 0) {
  1218. if (!var_size)
  1219. return AVERROR_INVALIDDATA;
  1220. if (s->compression == EXR_UNKN)
  1221. s->compression = bytestream2_get_byte(&s->gb);
  1222. else
  1223. av_log(s->avctx, AV_LOG_WARNING,
  1224. "Found more than one compression attribute.\n");
  1225. continue;
  1226. } else if ((var_size = check_header_variable(s, "tiles",
  1227. "tiledesc", 22)) >= 0) {
  1228. char tileLevel;
  1229. if (!s->is_tile)
  1230. av_log(s->avctx, AV_LOG_WARNING,
  1231. "Found tile attribute and scanline flags. Exr will be interpreted as scanline.\n");
  1232. s->tile_attr.xSize = bytestream2_get_le32(&s->gb);
  1233. s->tile_attr.ySize = bytestream2_get_le32(&s->gb);
  1234. tileLevel = bytestream2_get_byte(&s->gb);
  1235. s->tile_attr.level_mode = tileLevel & 0x0f;
  1236. s->tile_attr.level_round = (tileLevel >> 4) & 0x0f;
  1237. if (s->tile_attr.level_mode >= EXR_TILE_LEVEL_UNKNOWN){
  1238. avpriv_report_missing_feature(s->avctx, "Tile level mode %d",
  1239. s->tile_attr.level_mode);
  1240. return AVERROR_PATCHWELCOME;
  1241. }
  1242. if (s->tile_attr.level_round >= EXR_TILE_ROUND_UNKNOWN) {
  1243. avpriv_report_missing_feature(s->avctx, "Tile level round %d",
  1244. s->tile_attr.level_round);
  1245. return AVERROR_PATCHWELCOME;
  1246. }
  1247. continue;
  1248. }
  1249. // Check if there are enough bytes for a header
  1250. if (bytestream2_get_bytes_left(&s->gb) <= 9) {
  1251. av_log(s->avctx, AV_LOG_ERROR, "Incomplete header\n");
  1252. return AVERROR_INVALIDDATA;
  1253. }
  1254. // Process unknown variables
  1255. for (i = 0; i < 2; i++) // value_name and value_type
  1256. while (bytestream2_get_byte(&s->gb) != 0);
  1257. // Skip variable length
  1258. bytestream2_skip(&s->gb, bytestream2_get_le32(&s->gb));
  1259. }
  1260. ff_set_sar(s->avctx, av_d2q(av_int2float(sar), 255));
  1261. if (s->compression == EXR_UNKN) {
  1262. av_log(s->avctx, AV_LOG_ERROR, "Missing compression attribute.\n");
  1263. return AVERROR_INVALIDDATA;
  1264. }
  1265. if (s->is_tile) {
  1266. if (s->tile_attr.xSize < 1 || s->tile_attr.ySize < 1) {
  1267. av_log(s->avctx, AV_LOG_ERROR, "Invalid tile attribute.\n");
  1268. return AVERROR_INVALIDDATA;
  1269. }
  1270. }
  1271. if (bytestream2_get_bytes_left(&s->gb) <= 0) {
  1272. av_log(s->avctx, AV_LOG_ERROR, "Incomplete frame.\n");
  1273. return AVERROR_INVALIDDATA;
  1274. }
  1275. // aaand we are done
  1276. bytestream2_skip(&s->gb, 1);
  1277. return 0;
  1278. }
  1279. static int decode_frame(AVCodecContext *avctx, void *data,
  1280. int *got_frame, AVPacket *avpkt)
  1281. {
  1282. EXRContext *s = avctx->priv_data;
  1283. ThreadFrame frame = { .f = data };
  1284. AVFrame *picture = data;
  1285. uint8_t *ptr;
  1286. int y, ret;
  1287. int out_line_size;
  1288. int nb_blocks;/* nb scanline or nb tile */
  1289. bytestream2_init(&s->gb, avpkt->data, avpkt->size);
  1290. if ((ret = decode_header(s)) < 0)
  1291. return ret;
  1292. switch (s->pixel_type) {
  1293. case EXR_FLOAT:
  1294. case EXR_HALF:
  1295. if (s->channel_offsets[3] >= 0)
  1296. avctx->pix_fmt = AV_PIX_FMT_RGBA64;
  1297. else
  1298. avctx->pix_fmt = AV_PIX_FMT_RGB48;
  1299. break;
  1300. case EXR_UINT:
  1301. avpriv_request_sample(avctx, "32-bit unsigned int");
  1302. return AVERROR_PATCHWELCOME;
  1303. default:
  1304. av_log(avctx, AV_LOG_ERROR, "Missing channel list.\n");
  1305. return AVERROR_INVALIDDATA;
  1306. }
  1307. if (s->apply_trc_type != AVCOL_TRC_UNSPECIFIED)
  1308. avctx->color_trc = s->apply_trc_type;
  1309. switch (s->compression) {
  1310. case EXR_RAW:
  1311. case EXR_RLE:
  1312. case EXR_ZIP1:
  1313. s->scan_lines_per_block = 1;
  1314. break;
  1315. case EXR_PXR24:
  1316. case EXR_ZIP16:
  1317. s->scan_lines_per_block = 16;
  1318. break;
  1319. case EXR_PIZ:
  1320. case EXR_B44:
  1321. case EXR_B44A:
  1322. s->scan_lines_per_block = 32;
  1323. break;
  1324. default:
  1325. avpriv_report_missing_feature(avctx, "Compression %d", s->compression);
  1326. return AVERROR_PATCHWELCOME;
  1327. }
  1328. /* Verify the xmin, xmax, ymin, ymax and xdelta before setting
  1329. * the actual image size. */
  1330. if (s->xmin > s->xmax ||
  1331. s->ymin > s->ymax ||
  1332. s->xdelta != s->xmax - s->xmin + 1 ||
  1333. s->xmax >= s->w ||
  1334. s->ymax >= s->h) {
  1335. av_log(avctx, AV_LOG_ERROR, "Wrong or missing size information.\n");
  1336. return AVERROR_INVALIDDATA;
  1337. }
  1338. if ((ret = ff_set_dimensions(avctx, s->w, s->h)) < 0)
  1339. return ret;
  1340. s->desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  1341. if (!s->desc)
  1342. return AVERROR_INVALIDDATA;
  1343. out_line_size = avctx->width * 2 * s->desc->nb_components;
  1344. if (s->is_tile) {
  1345. nb_blocks = ((s->xdelta + s->tile_attr.xSize - 1) / s->tile_attr.xSize) *
  1346. ((s->ydelta + s->tile_attr.ySize - 1) / s->tile_attr.ySize);
  1347. } else { /* scanline */
  1348. nb_blocks = (s->ydelta + s->scan_lines_per_block - 1) /
  1349. s->scan_lines_per_block;
  1350. }
  1351. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
  1352. return ret;
  1353. if (bytestream2_get_bytes_left(&s->gb) < nb_blocks * 8)
  1354. return AVERROR_INVALIDDATA;
  1355. // save pointer we are going to use in decode_block
  1356. s->buf = avpkt->data;
  1357. s->buf_size = avpkt->size;
  1358. ptr = picture->data[0];
  1359. // Zero out the start if ymin is not 0
  1360. for (y = 0; y < s->ymin; y++) {
  1361. memset(ptr, 0, out_line_size);
  1362. ptr += picture->linesize[0];
  1363. }
  1364. s->picture = picture;
  1365. avctx->execute2(avctx, decode_block, s->thread_data, NULL, nb_blocks);
  1366. // Zero out the end if ymax+1 is not h
  1367. for (y = s->ymax + 1; y < avctx->height; y++) {
  1368. memset(ptr, 0, out_line_size);
  1369. ptr += picture->linesize[0];
  1370. }
  1371. picture->pict_type = AV_PICTURE_TYPE_I;
  1372. *got_frame = 1;
  1373. return avpkt->size;
  1374. }
  1375. static av_cold int decode_init(AVCodecContext *avctx)
  1376. {
  1377. EXRContext *s = avctx->priv_data;
  1378. uint32_t i;
  1379. union av_intfloat32 t;
  1380. float one_gamma = 1.0f / s->gamma;
  1381. avpriv_trc_function trc_func = NULL;
  1382. s->avctx = avctx;
  1383. trc_func = avpriv_get_trc_function_from_trc(s->apply_trc_type);
  1384. if (trc_func) {
  1385. for (i = 0; i < 65536; ++i) {
  1386. t = exr_half2float(i);
  1387. t.f = trc_func(t.f);
  1388. s->gamma_table[i] = exr_flt2uint(t.i);
  1389. }
  1390. } else {
  1391. if (one_gamma > 0.9999f && one_gamma < 1.0001f) {
  1392. for (i = 0; i < 65536; ++i)
  1393. s->gamma_table[i] = exr_halflt2uint(i);
  1394. } else {
  1395. for (i = 0; i < 65536; ++i) {
  1396. t = exr_half2float(i);
  1397. /* If negative value we reuse half value */
  1398. if (t.f <= 0.0f) {
  1399. s->gamma_table[i] = exr_halflt2uint(i);
  1400. } else {
  1401. t.f = powf(t.f, one_gamma);
  1402. s->gamma_table[i] = exr_flt2uint(t.i);
  1403. }
  1404. }
  1405. }
  1406. }
  1407. // allocate thread data, used for non EXR_RAW compreesion types
  1408. s->thread_data = av_mallocz_array(avctx->thread_count, sizeof(EXRThreadData));
  1409. if (!s->thread_data)
  1410. return AVERROR_INVALIDDATA;
  1411. return 0;
  1412. }
  1413. #if HAVE_THREADS
  1414. static int decode_init_thread_copy(AVCodecContext *avctx)
  1415. { EXRContext *s = avctx->priv_data;
  1416. // allocate thread data, used for non EXR_RAW compreesion types
  1417. s->thread_data = av_mallocz_array(avctx->thread_count, sizeof(EXRThreadData));
  1418. if (!s->thread_data)
  1419. return AVERROR_INVALIDDATA;
  1420. return 0;
  1421. }
  1422. #endif
  1423. static av_cold int decode_end(AVCodecContext *avctx)
  1424. {
  1425. EXRContext *s = avctx->priv_data;
  1426. int i;
  1427. for (i = 0; i < avctx->thread_count; i++) {
  1428. EXRThreadData *td = &s->thread_data[i];
  1429. av_freep(&td->uncompressed_data);
  1430. av_freep(&td->tmp);
  1431. av_freep(&td->bitmap);
  1432. av_freep(&td->lut);
  1433. }
  1434. av_freep(&s->thread_data);
  1435. av_freep(&s->channels);
  1436. return 0;
  1437. }
  1438. #define OFFSET(x) offsetof(EXRContext, x)
  1439. #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  1440. static const AVOption options[] = {
  1441. { "layer", "Set the decoding layer", OFFSET(layer),
  1442. AV_OPT_TYPE_STRING, { .str = "" }, 0, 0, VD },
  1443. { "gamma", "Set the float gamma value when decoding", OFFSET(gamma),
  1444. AV_OPT_TYPE_FLOAT, { .dbl = 1.0f }, 0.001, FLT_MAX, VD },
  1445. // XXX: Note the abuse of the enum using AVCOL_TRC_UNSPECIFIED to subsume the existing gamma option
  1446. { "apply_trc", "color transfer characteristics to apply to EXR linear input", OFFSET(apply_trc_type),
  1447. AV_OPT_TYPE_INT, {.i64 = AVCOL_TRC_UNSPECIFIED }, 1, AVCOL_TRC_NB-1, VD, "apply_trc_type"},
  1448. { "bt709", "BT.709", 0,
  1449. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT709 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1450. { "gamma", "gamma", 0,
  1451. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_UNSPECIFIED }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1452. { "gamma22", "BT.470 M", 0,
  1453. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_GAMMA22 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1454. { "gamma28", "BT.470 BG", 0,
  1455. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_GAMMA28 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1456. { "smpte170m", "SMPTE 170 M", 0,
  1457. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTE170M }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1458. { "smpte240m", "SMPTE 240 M", 0,
  1459. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTE240M }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1460. { "linear", "Linear", 0,
  1461. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LINEAR }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1462. { "log", "Log", 0,
  1463. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LOG }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1464. { "log_sqrt", "Log square root", 0,
  1465. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LOG_SQRT }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1466. { "iec61966_2_4", "IEC 61966-2-4", 0,
  1467. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_IEC61966_2_4 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1468. { "bt1361", "BT.1361", 0,
  1469. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT1361_ECG }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1470. { "iec61966_2_1", "IEC 61966-2-1", 0,
  1471. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_IEC61966_2_1 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1472. { "bt2020_10bit", "BT.2020 - 10 bit", 0,
  1473. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT2020_10 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1474. { "bt2020_12bit", "BT.2020 - 12 bit", 0,
  1475. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT2020_12 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1476. { "smpte2084", "SMPTE ST 2084", 0,
  1477. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTEST2084 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1478. { "smpte428_1", "SMPTE ST 428-1", 0,
  1479. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTEST428_1 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1480. { NULL },
  1481. };
  1482. static const AVClass exr_class = {
  1483. .class_name = "EXR",
  1484. .item_name = av_default_item_name,
  1485. .option = options,
  1486. .version = LIBAVUTIL_VERSION_INT,
  1487. };
  1488. AVCodec ff_exr_decoder = {
  1489. .name = "exr",
  1490. .long_name = NULL_IF_CONFIG_SMALL("OpenEXR image"),
  1491. .type = AVMEDIA_TYPE_VIDEO,
  1492. .id = AV_CODEC_ID_EXR,
  1493. .priv_data_size = sizeof(EXRContext),
  1494. .init = decode_init,
  1495. .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
  1496. .close = decode_end,
  1497. .decode = decode_frame,
  1498. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS |
  1499. AV_CODEC_CAP_SLICE_THREADS,
  1500. .priv_class = &exr_class,
  1501. };