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.

1816 lines
59KB

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