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.

1350 lines
41KB

  1. /*
  2. * OpenEXR (.exr) image decoder
  3. * Copyright (c) 2009 Jimmy Christensen
  4. *
  5. * This file is part of Libav
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * OpenEXR decoder
  24. * @author Jimmy Christensen
  25. *
  26. * For more information on the OpenEXR format, visit:
  27. * http://openexr.com/
  28. *
  29. * exr_flt2uint() and exr_halflt2uint() is credited to Reimar Döffinger
  30. */
  31. #include <zlib.h>
  32. #include "libavutil/imgutils.h"
  33. #include "libavutil/opt.h"
  34. #include "avcodec.h"
  35. #include "bytestream.h"
  36. #include "get_bits.h"
  37. #include "internal.h"
  38. #include "mathops.h"
  39. #include "thread.h"
  40. enum ExrCompr {
  41. EXR_RAW,
  42. EXR_RLE,
  43. EXR_ZIP1,
  44. EXR_ZIP16,
  45. EXR_PIZ,
  46. EXR_PXR24,
  47. EXR_B44,
  48. EXR_B44A,
  49. EXR_UNKN,
  50. };
  51. enum ExrPixelType {
  52. EXR_UINT,
  53. EXR_HALF,
  54. EXR_FLOAT,
  55. EXR_UNKNOWN,
  56. };
  57. typedef struct EXRChannel {
  58. int xsub, ysub;
  59. enum ExrPixelType pixel_type;
  60. } EXRChannel;
  61. typedef struct EXRThreadData {
  62. uint8_t *uncompressed_data;
  63. int uncompressed_size;
  64. uint8_t *tmp;
  65. int tmp_size;
  66. uint8_t *bitmap;
  67. uint16_t *lut;
  68. } EXRThreadData;
  69. typedef struct EXRContext {
  70. AVClass *class;
  71. AVFrame *picture;
  72. AVCodecContext *avctx;
  73. enum ExrCompr compression;
  74. enum ExrPixelType pixel_type;
  75. int channel_offsets[4]; // 0 = red, 1 = green, 2 = blue and 3 = alpha
  76. const AVPixFmtDescriptor *desc;
  77. int w, h;
  78. uint32_t xmax, xmin;
  79. uint32_t ymax, ymin;
  80. uint32_t xdelta, ydelta;
  81. int ysize;
  82. uint64_t scan_line_size;
  83. int scan_lines_per_block;
  84. GetByteContext gb;
  85. const uint8_t *buf;
  86. int buf_size;
  87. EXRChannel *channels;
  88. int nb_channels;
  89. EXRThreadData *thread_data;
  90. const char *layer;
  91. } EXRContext;
  92. /**
  93. * Convert from 32-bit float as uint32_t to uint16_t.
  94. *
  95. * @param v 32-bit float
  96. *
  97. * @return normalized 16-bit unsigned int
  98. */
  99. static inline uint16_t exr_flt2uint(uint32_t v)
  100. {
  101. unsigned int exp = v >> 23;
  102. // "HACK": negative values result in exp< 0, so clipping them to 0
  103. // is also handled by this condition, avoids explicit check for sign bit.
  104. if (exp <= 127 + 7 - 24) // we would shift out all bits anyway
  105. return 0;
  106. if (exp >= 127)
  107. return 0xffff;
  108. v &= 0x007fffff;
  109. return (v + (1 << 23)) >> (127 + 7 - exp);
  110. }
  111. /**
  112. * Convert from 16-bit float as uint16_t to uint16_t.
  113. *
  114. * @param v 16-bit float
  115. *
  116. * @return normalized 16-bit unsigned int
  117. */
  118. static inline uint16_t exr_halflt2uint(uint16_t v)
  119. {
  120. unsigned exp = 14 - (v >> 10);
  121. if (exp >= 14) {
  122. if (exp == 14)
  123. return (v >> 9) & 1;
  124. else
  125. return (v & 0x8000) ? 0 : 0xffff;
  126. }
  127. v <<= 6;
  128. return (v + (1 << 16)) >> (exp + 1);
  129. }
  130. static void predictor(uint8_t *src, int size)
  131. {
  132. uint8_t *t = src + 1;
  133. uint8_t *stop = src + size;
  134. while (t < stop) {
  135. int d = (int) t[-1] + (int) t[0] - 128;
  136. t[0] = d;
  137. ++t;
  138. }
  139. }
  140. static void reorder_pixels(uint8_t *src, uint8_t *dst, int size)
  141. {
  142. const int8_t *t1 = src;
  143. const int8_t *t2 = src + (size + 1) / 2;
  144. int8_t *s = dst;
  145. int8_t *stop = s + size;
  146. while (1) {
  147. if (s < stop)
  148. *(s++) = *(t1++);
  149. else
  150. break;
  151. if (s < stop)
  152. *(s++) = *(t2++);
  153. else
  154. break;
  155. }
  156. }
  157. static int zip_uncompress(const uint8_t *src, int compressed_size,
  158. int uncompressed_size, EXRThreadData *td)
  159. {
  160. unsigned long dest_len = uncompressed_size;
  161. if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK ||
  162. dest_len != uncompressed_size)
  163. return AVERROR_INVALIDDATA;
  164. predictor(td->tmp, uncompressed_size);
  165. reorder_pixels(td->tmp, td->uncompressed_data, uncompressed_size);
  166. return 0;
  167. }
  168. static int rle_uncompress(const uint8_t *src, int compressed_size,
  169. int uncompressed_size, EXRThreadData *td)
  170. {
  171. uint8_t *d = td->tmp;
  172. const int8_t *s = src;
  173. int ssize = compressed_size;
  174. int dsize = uncompressed_size;
  175. uint8_t *dend = d + dsize;
  176. int count;
  177. while (ssize > 0) {
  178. count = *s++;
  179. if (count < 0) {
  180. count = -count;
  181. if ((dsize -= count) < 0 ||
  182. (ssize -= count + 1) < 0)
  183. return AVERROR_INVALIDDATA;
  184. while (count--)
  185. *d++ = *s++;
  186. } else {
  187. count++;
  188. if ((dsize -= count) < 0 ||
  189. (ssize -= 2) < 0)
  190. return AVERROR_INVALIDDATA;
  191. while (count--)
  192. *d++ = *s;
  193. s++;
  194. }
  195. }
  196. if (dend != d)
  197. return AVERROR_INVALIDDATA;
  198. predictor(td->tmp, uncompressed_size);
  199. reorder_pixels(td->tmp, td->uncompressed_data, uncompressed_size);
  200. return 0;
  201. }
  202. #define USHORT_RANGE (1 << 16)
  203. #define BITMAP_SIZE (1 << 13)
  204. static uint16_t reverse_lut(const uint8_t *bitmap, uint16_t *lut)
  205. {
  206. int i, k = 0;
  207. for (i = 0; i < USHORT_RANGE; i++)
  208. if ((i == 0) || (bitmap[i >> 3] & (1 << (i & 7))))
  209. lut[k++] = i;
  210. i = k - 1;
  211. memset(lut + k, 0, (USHORT_RANGE - k) * 2);
  212. return i;
  213. }
  214. static void apply_lut(const uint16_t *lut, uint16_t *dst, int dsize)
  215. {
  216. int i;
  217. for (i = 0; i < dsize; ++i)
  218. dst[i] = lut[dst[i]];
  219. }
  220. #define HUF_ENCBITS 16 // literal (value) bit length
  221. #define HUF_DECBITS 14 // decoding bit size (>= 8)
  222. #define HUF_ENCSIZE ((1 << HUF_ENCBITS) + 1) // encoding table size
  223. #define HUF_DECSIZE (1 << HUF_DECBITS) // decoding table size
  224. #define HUF_DECMASK (HUF_DECSIZE - 1)
  225. typedef struct HufDec {
  226. int len;
  227. int lit;
  228. int *p;
  229. } HufDec;
  230. static void huf_canonical_code_table(uint64_t *hcode)
  231. {
  232. uint64_t c, n[59] = { 0 };
  233. int i;
  234. for (i = 0; i < HUF_ENCSIZE; ++i)
  235. n[hcode[i]] += 1;
  236. c = 0;
  237. for (i = 58; i > 0; --i) {
  238. uint64_t nc = ((c + n[i]) >> 1);
  239. n[i] = c;
  240. c = nc;
  241. }
  242. for (i = 0; i < HUF_ENCSIZE; ++i) {
  243. int l = hcode[i];
  244. if (l > 0)
  245. hcode[i] = l | (n[l]++ << 6);
  246. }
  247. }
  248. #define SHORT_ZEROCODE_RUN 59
  249. #define LONG_ZEROCODE_RUN 63
  250. #define SHORTEST_LONG_RUN (2 + LONG_ZEROCODE_RUN - SHORT_ZEROCODE_RUN)
  251. #define LONGEST_LONG_RUN (255 + SHORTEST_LONG_RUN)
  252. static int huf_unpack_enc_table(GetByteContext *gb,
  253. int32_t im, int32_t iM, uint64_t *hcode)
  254. {
  255. GetBitContext gbit;
  256. int ret = init_get_bits8(&gbit, gb->buffer, bytestream2_get_bytes_left(gb));
  257. if (ret < 0)
  258. return ret;
  259. for (; im <= iM; im++) {
  260. uint64_t l = hcode[im] = get_bits(&gbit, 6);
  261. if (l == LONG_ZEROCODE_RUN) {
  262. int zerun = get_bits(&gbit, 8) + SHORTEST_LONG_RUN;
  263. if (im + zerun > iM + 1)
  264. return AVERROR_INVALIDDATA;
  265. while (zerun--)
  266. hcode[im++] = 0;
  267. im--;
  268. } else if (l >= SHORT_ZEROCODE_RUN) {
  269. int zerun = l - SHORT_ZEROCODE_RUN + 2;
  270. if (im + zerun > iM + 1)
  271. return AVERROR_INVALIDDATA;
  272. while (zerun--)
  273. hcode[im++] = 0;
  274. im--;
  275. }
  276. }
  277. bytestream2_skip(gb, (get_bits_count(&gbit) + 7) / 8);
  278. huf_canonical_code_table(hcode);
  279. return 0;
  280. }
  281. static int huf_build_dec_table(const uint64_t *hcode, int im,
  282. int iM, HufDec *hdecod)
  283. {
  284. for (; im <= iM; im++) {
  285. uint64_t c = hcode[im] >> 6;
  286. int i, l = hcode[im] & 63;
  287. if (c >> l)
  288. return AVERROR_INVALIDDATA;
  289. if (l > HUF_DECBITS) {
  290. HufDec *pl = hdecod + (c >> (l - HUF_DECBITS));
  291. if (pl->len)
  292. return AVERROR_INVALIDDATA;
  293. pl->lit++;
  294. pl->p = av_realloc(pl->p, pl->lit * sizeof(int));
  295. if (!pl->p)
  296. return AVERROR(ENOMEM);
  297. pl->p[pl->lit - 1] = im;
  298. } else if (l) {
  299. HufDec *pl = hdecod + (c << (HUF_DECBITS - l));
  300. for (i = 1 << (HUF_DECBITS - l); i > 0; i--, pl++) {
  301. if (pl->len || pl->p)
  302. return AVERROR_INVALIDDATA;
  303. pl->len = l;
  304. pl->lit = im;
  305. }
  306. }
  307. }
  308. return 0;
  309. }
  310. #define get_char(c, lc, gb) \
  311. { \
  312. c = (c << 8) | bytestream2_get_byte(gb); \
  313. lc += 8; \
  314. }
  315. #define get_code(po, rlc, c, lc, gb, out, oe) \
  316. { \
  317. if (po == rlc) { \
  318. if (lc < 8) \
  319. get_char(c, lc, gb); \
  320. lc -= 8; \
  321. \
  322. cs = c >> lc; \
  323. \
  324. if (out + cs > oe) \
  325. return AVERROR_INVALIDDATA; \
  326. \
  327. s = out[-1]; \
  328. \
  329. while (cs-- > 0) \
  330. *out++ = s; \
  331. } else if (out < oe) { \
  332. *out++ = po; \
  333. } else { \
  334. return AVERROR_INVALIDDATA; \
  335. } \
  336. }
  337. static int huf_decode(const uint64_t *hcode, const HufDec *hdecod,
  338. GetByteContext *gb, int nbits,
  339. int rlc, int no, uint16_t *out)
  340. {
  341. uint64_t c = 0;
  342. uint16_t *outb = out;
  343. uint16_t *oe = out + no;
  344. const uint8_t *ie = gb->buffer + (nbits + 7) / 8; // input byte size
  345. uint8_t cs, s;
  346. int i, lc = 0;
  347. while (gb->buffer < ie) {
  348. get_char(c, lc, gb);
  349. while (lc >= HUF_DECBITS) {
  350. const HufDec pl = hdecod[(c >> (lc - HUF_DECBITS)) & HUF_DECMASK];
  351. if (pl.len) {
  352. lc -= pl.len;
  353. get_code(pl.lit, rlc, c, lc, gb, out, oe);
  354. } else {
  355. int j;
  356. if (!pl.p)
  357. return AVERROR_INVALIDDATA;
  358. for (j = 0; j < pl.lit; j++) {
  359. int l = hcode[pl.p[j]] & 63;
  360. while (lc < l && bytestream2_get_bytes_left(gb) > 0)
  361. get_char(c, lc, gb);
  362. if (lc >= l) {
  363. if ((hcode[pl.p[j]] >> 6) ==
  364. ((c >> (lc - l)) & ((1LL << l) - 1))) {
  365. lc -= l;
  366. get_code(pl.p[j], rlc, c, lc, gb, out, oe);
  367. break;
  368. }
  369. }
  370. }
  371. if (j == pl.lit)
  372. return AVERROR_INVALIDDATA;
  373. }
  374. }
  375. }
  376. i = (8 - nbits) & 7;
  377. c >>= i;
  378. lc -= i;
  379. while (lc > 0) {
  380. const HufDec pl = hdecod[(c << (HUF_DECBITS - lc)) & HUF_DECMASK];
  381. if (pl.len) {
  382. lc -= pl.len;
  383. get_code(pl.lit, rlc, c, lc, gb, out, oe);
  384. } else {
  385. return AVERROR_INVALIDDATA;
  386. }
  387. }
  388. if (out - outb != no)
  389. return AVERROR_INVALIDDATA;
  390. return 0;
  391. }
  392. static int huf_uncompress(GetByteContext *gb,
  393. uint16_t *dst, int dst_size)
  394. {
  395. int32_t src_size, im, iM;
  396. uint32_t nBits;
  397. uint64_t *freq;
  398. HufDec *hdec;
  399. int ret, i;
  400. src_size = bytestream2_get_le32(gb);
  401. im = bytestream2_get_le32(gb);
  402. iM = bytestream2_get_le32(gb);
  403. bytestream2_skip(gb, 4);
  404. nBits = bytestream2_get_le32(gb);
  405. if (im < 0 || im >= HUF_ENCSIZE ||
  406. iM < 0 || iM >= HUF_ENCSIZE ||
  407. src_size < 0)
  408. return AVERROR_INVALIDDATA;
  409. bytestream2_skip(gb, 4);
  410. freq = av_mallocz_array(HUF_ENCSIZE, sizeof(*freq));
  411. hdec = av_mallocz_array(HUF_DECSIZE, sizeof(*hdec));
  412. if (!freq || !hdec) {
  413. ret = AVERROR(ENOMEM);
  414. goto fail;
  415. }
  416. if ((ret = huf_unpack_enc_table(gb, im, iM, freq)) < 0)
  417. goto fail;
  418. if (nBits > 8 * bytestream2_get_bytes_left(gb)) {
  419. ret = AVERROR_INVALIDDATA;
  420. goto fail;
  421. }
  422. if ((ret = huf_build_dec_table(freq, im, iM, hdec)) < 0)
  423. goto fail;
  424. ret = huf_decode(freq, hdec, gb, nBits, iM, dst_size, dst);
  425. fail:
  426. for (i = 0; i < HUF_DECSIZE; i++)
  427. if (hdec)
  428. av_freep(&hdec[i].p);
  429. av_free(freq);
  430. av_free(hdec);
  431. return ret;
  432. }
  433. static inline void wdec14(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
  434. {
  435. int16_t ls = l;
  436. int16_t hs = h;
  437. int hi = hs;
  438. int ai = ls + (hi & 1) + (hi >> 1);
  439. int16_t as = ai;
  440. int16_t bs = ai - hi;
  441. *a = as;
  442. *b = bs;
  443. }
  444. #define NBITS 16
  445. #define A_OFFSET (1 << (NBITS - 1))
  446. #define MOD_MASK ((1 << NBITS) - 1)
  447. static inline void wdec16(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
  448. {
  449. int m = l;
  450. int d = h;
  451. int bb = (m - (d >> 1)) & MOD_MASK;
  452. int aa = (d + bb - A_OFFSET) & MOD_MASK;
  453. *b = bb;
  454. *a = aa;
  455. }
  456. static void wav_decode(uint16_t *in, int nx, int ox,
  457. int ny, int oy, uint16_t mx)
  458. {
  459. int w14 = (mx < (1 << 14));
  460. int n = (nx > ny) ? ny : nx;
  461. int p = 1;
  462. int p2;
  463. while (p <= n)
  464. p <<= 1;
  465. p >>= 1;
  466. p2 = p;
  467. p >>= 1;
  468. while (p >= 1) {
  469. uint16_t *py = in;
  470. uint16_t *ey = in + oy * (ny - p2);
  471. uint16_t i00, i01, i10, i11;
  472. int oy1 = oy * p;
  473. int oy2 = oy * p2;
  474. int ox1 = ox * p;
  475. int ox2 = ox * p2;
  476. for (; py <= ey; py += oy2) {
  477. uint16_t *px = py;
  478. uint16_t *ex = py + ox * (nx - p2);
  479. for (; px <= ex; px += ox2) {
  480. uint16_t *p01 = px + ox1;
  481. uint16_t *p10 = px + oy1;
  482. uint16_t *p11 = p10 + ox1;
  483. if (w14) {
  484. wdec14(*px, *p10, &i00, &i10);
  485. wdec14(*p01, *p11, &i01, &i11);
  486. wdec14(i00, i01, px, p01);
  487. wdec14(i10, i11, p10, p11);
  488. } else {
  489. wdec16(*px, *p10, &i00, &i10);
  490. wdec16(*p01, *p11, &i01, &i11);
  491. wdec16(i00, i01, px, p01);
  492. wdec16(i10, i11, p10, p11);
  493. }
  494. }
  495. if (nx & p) {
  496. uint16_t *p10 = px + oy1;
  497. if (w14)
  498. wdec14(*px, *p10, &i00, p10);
  499. else
  500. wdec16(*px, *p10, &i00, p10);
  501. *px = i00;
  502. }
  503. }
  504. if (ny & p) {
  505. uint16_t *px = py;
  506. uint16_t *ex = py + ox * (nx - p2);
  507. for (; px <= ex; px += ox2) {
  508. uint16_t *p01 = px + ox1;
  509. if (w14)
  510. wdec14(*px, *p01, &i00, p01);
  511. else
  512. wdec16(*px, *p01, &i00, p01);
  513. *px = i00;
  514. }
  515. }
  516. p2 = p;
  517. p >>= 1;
  518. }
  519. }
  520. static int piz_uncompress(EXRContext *s, const uint8_t *src, int ssize,
  521. int dsize, EXRThreadData *td)
  522. {
  523. GetByteContext gb;
  524. uint16_t maxval, min_non_zero, max_non_zero;
  525. uint16_t *ptr;
  526. uint16_t *tmp = (uint16_t *)td->tmp;
  527. uint8_t *out;
  528. int ret, i, j;
  529. if (!td->bitmap)
  530. td->bitmap = av_malloc(BITMAP_SIZE);
  531. if (!td->lut)
  532. td->lut = av_malloc(1 << 17);
  533. if (!td->bitmap || !td->lut) {
  534. av_free(td->bitmap);
  535. av_free(td->lut);
  536. return AVERROR(ENOMEM);
  537. }
  538. bytestream2_init(&gb, src, ssize);
  539. min_non_zero = bytestream2_get_le16(&gb);
  540. max_non_zero = bytestream2_get_le16(&gb);
  541. if (max_non_zero >= BITMAP_SIZE)
  542. return AVERROR_INVALIDDATA;
  543. memset(td->bitmap, 0, FFMIN(min_non_zero, BITMAP_SIZE));
  544. if (min_non_zero <= max_non_zero)
  545. bytestream2_get_buffer(&gb, td->bitmap + min_non_zero,
  546. max_non_zero - min_non_zero + 1);
  547. memset(td->bitmap + max_non_zero, 0, BITMAP_SIZE - max_non_zero);
  548. maxval = reverse_lut(td->bitmap, td->lut);
  549. ret = huf_uncompress(&gb, tmp, dsize / sizeof(uint16_t));
  550. if (ret)
  551. return ret;
  552. ptr = tmp;
  553. for (i = 0; i < s->nb_channels; i++) {
  554. EXRChannel *channel = &s->channels[i];
  555. int size = channel->pixel_type;
  556. for (j = 0; j < size; j++)
  557. wav_decode(ptr + j, s->xdelta, size, s->ysize,
  558. s->xdelta * size, maxval);
  559. ptr += s->xdelta * s->ysize * size;
  560. }
  561. apply_lut(td->lut, tmp, dsize / sizeof(uint16_t));
  562. out = td->uncompressed_data;
  563. for (i = 0; i < s->ysize; i++)
  564. for (j = 0; j < s->nb_channels; j++) {
  565. uint16_t *in = tmp + j * s->xdelta * s->ysize + i * s->xdelta;
  566. memcpy(out, in, s->xdelta * 2);
  567. out += s->xdelta * 2;
  568. }
  569. return 0;
  570. }
  571. static int pxr24_uncompress(EXRContext *s, const uint8_t *src,
  572. int compressed_size, int uncompressed_size,
  573. EXRThreadData *td)
  574. {
  575. unsigned long dest_len = uncompressed_size;
  576. const uint8_t *in = td->tmp;
  577. uint8_t *out;
  578. int c, i, j;
  579. if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK ||
  580. dest_len != uncompressed_size)
  581. return AVERROR_INVALIDDATA;
  582. out = td->uncompressed_data;
  583. for (i = 0; i < s->ysize; i++)
  584. for (c = 0; c < s->nb_channels; c++) {
  585. EXRChannel *channel = &s->channels[c];
  586. const uint8_t *ptr[4];
  587. uint32_t pixel = 0;
  588. switch (channel->pixel_type) {
  589. case EXR_FLOAT:
  590. ptr[0] = in;
  591. ptr[1] = ptr[0] + s->xdelta;
  592. ptr[2] = ptr[1] + s->xdelta;
  593. in = ptr[2] + s->xdelta;
  594. for (j = 0; j < s->xdelta; ++j) {
  595. uint32_t diff = (*(ptr[0]++) << 24) |
  596. (*(ptr[1]++) << 16) |
  597. (*(ptr[2]++) << 8);
  598. pixel += diff;
  599. bytestream_put_le32(&out, pixel);
  600. }
  601. break;
  602. case EXR_HALF:
  603. ptr[0] = in;
  604. ptr[1] = ptr[0] + s->xdelta;
  605. in = ptr[1] + s->xdelta;
  606. for (j = 0; j < s->xdelta; j++) {
  607. uint32_t diff = (*(ptr[0]++) << 8) | *(ptr[1]++);
  608. pixel += diff;
  609. bytestream_put_le16(&out, pixel);
  610. }
  611. break;
  612. default:
  613. return AVERROR_INVALIDDATA;
  614. }
  615. }
  616. return 0;
  617. }
  618. static int decode_block(AVCodecContext *avctx, void *tdata,
  619. int jobnr, int threadnr)
  620. {
  621. EXRContext *s = avctx->priv_data;
  622. AVFrame *const p = s->picture;
  623. EXRThreadData *td = &s->thread_data[threadnr];
  624. const uint8_t *channel_buffer[4] = { 0 };
  625. const uint8_t *buf = s->buf;
  626. uint64_t line_offset, uncompressed_size;
  627. uint32_t xdelta = s->xdelta;
  628. uint16_t *ptr_x;
  629. uint8_t *ptr;
  630. uint32_t data_size, line;
  631. const uint8_t *src;
  632. int axmax = (avctx->width - (s->xmax + 1)) * 2 * s->desc->nb_components;
  633. int bxmin = s->xmin * 2 * s->desc->nb_components;
  634. int i, x, buf_size = s->buf_size;
  635. int ret;
  636. line_offset = AV_RL64(s->gb.buffer + jobnr * 8);
  637. // Check if the buffer has the required bytes needed from the offset
  638. if (line_offset > buf_size - 8)
  639. return AVERROR_INVALIDDATA;
  640. src = buf + line_offset + 8;
  641. line = AV_RL32(src - 8);
  642. if (line < s->ymin || line > s->ymax)
  643. return AVERROR_INVALIDDATA;
  644. data_size = AV_RL32(src - 4);
  645. if (data_size <= 0 || data_size > buf_size)
  646. return AVERROR_INVALIDDATA;
  647. s->ysize = FFMIN(s->scan_lines_per_block, s->ymax - line + 1);
  648. uncompressed_size = s->scan_line_size * s->ysize;
  649. if ((s->compression == EXR_RAW && (data_size != uncompressed_size ||
  650. line_offset > buf_size - uncompressed_size)) ||
  651. (s->compression != EXR_RAW && (data_size > uncompressed_size ||
  652. line_offset > buf_size - data_size))) {
  653. return AVERROR_INVALIDDATA;
  654. }
  655. if (data_size < uncompressed_size) {
  656. av_fast_padded_malloc(&td->uncompressed_data,
  657. &td->uncompressed_size, uncompressed_size);
  658. av_fast_padded_malloc(&td->tmp, &td->tmp_size, uncompressed_size);
  659. if (!td->uncompressed_data || !td->tmp)
  660. return AVERROR(ENOMEM);
  661. ret = AVERROR_INVALIDDATA;
  662. switch (s->compression) {
  663. case EXR_ZIP1:
  664. case EXR_ZIP16:
  665. ret = zip_uncompress(src, data_size, uncompressed_size, td);
  666. break;
  667. case EXR_PIZ:
  668. ret = piz_uncompress(s, src, data_size, uncompressed_size, td);
  669. break;
  670. case EXR_PXR24:
  671. ret = pxr24_uncompress(s, src, data_size, uncompressed_size, td);
  672. break;
  673. case EXR_RLE:
  674. ret = rle_uncompress(src, data_size, uncompressed_size, td);
  675. }
  676. if (ret < 0) {
  677. av_log(avctx, AV_LOG_ERROR, "decode_block() failed.\n");
  678. return ret;
  679. }
  680. src = td->uncompressed_data;
  681. }
  682. channel_buffer[0] = src + xdelta * s->channel_offsets[0];
  683. channel_buffer[1] = src + xdelta * s->channel_offsets[1];
  684. channel_buffer[2] = src + xdelta * s->channel_offsets[2];
  685. if (s->channel_offsets[3] >= 0)
  686. channel_buffer[3] = src + xdelta * s->channel_offsets[3];
  687. ptr = p->data[0] + line * p->linesize[0];
  688. for (i = 0;
  689. i < s->scan_lines_per_block && line + i <= s->ymax;
  690. i++, ptr += p->linesize[0]) {
  691. const uint8_t *r, *g, *b, *a;
  692. r = channel_buffer[0];
  693. g = channel_buffer[1];
  694. b = channel_buffer[2];
  695. if (channel_buffer[3])
  696. a = channel_buffer[3];
  697. ptr_x = (uint16_t *) ptr;
  698. // Zero out the start if xmin is not 0
  699. memset(ptr_x, 0, bxmin);
  700. ptr_x += s->xmin * s->desc->nb_components;
  701. if (s->pixel_type == EXR_FLOAT) {
  702. // 32-bit
  703. for (x = 0; x < xdelta; x++) {
  704. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&r));
  705. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&g));
  706. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&b));
  707. if (channel_buffer[3])
  708. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a));
  709. }
  710. } else {
  711. // 16-bit
  712. for (x = 0; x < xdelta; x++) {
  713. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&r));
  714. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&g));
  715. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&b));
  716. if (channel_buffer[3])
  717. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&a));
  718. }
  719. }
  720. // Zero out the end if xmax+1 is not w
  721. memset(ptr_x, 0, axmax);
  722. channel_buffer[0] += s->scan_line_size;
  723. channel_buffer[1] += s->scan_line_size;
  724. channel_buffer[2] += s->scan_line_size;
  725. if (channel_buffer[3])
  726. channel_buffer[3] += s->scan_line_size;
  727. }
  728. return 0;
  729. }
  730. /**
  731. * Check if the variable name corresponds to its data type.
  732. *
  733. * @param s the EXRContext
  734. * @param value_name name of the variable to check
  735. * @param value_type type of the variable to check
  736. * @param minimum_length minimum length of the variable data
  737. *
  738. * @return bytes to read containing variable data
  739. * -1 if variable is not found
  740. * 0 if buffer ended prematurely
  741. */
  742. static int check_header_variable(EXRContext *s,
  743. const char *value_name,
  744. const char *value_type,
  745. unsigned int minimum_length)
  746. {
  747. int var_size = -1;
  748. if (bytestream2_get_bytes_left(&s->gb) >= minimum_length &&
  749. !strcmp(s->gb.buffer, value_name)) {
  750. // found value_name, jump to value_type (null terminated strings)
  751. s->gb.buffer += strlen(value_name) + 1;
  752. if (!strcmp(s->gb.buffer, value_type)) {
  753. s->gb.buffer += strlen(value_type) + 1;
  754. var_size = bytestream2_get_le32(&s->gb);
  755. // don't go read past boundaries
  756. if (var_size > bytestream2_get_bytes_left(&s->gb))
  757. var_size = 0;
  758. } else {
  759. // value_type not found, reset the buffer
  760. s->gb.buffer -= strlen(value_name) + 1;
  761. av_log(s->avctx, AV_LOG_WARNING,
  762. "Unknown data type %s for header variable %s.\n",
  763. value_type, value_name);
  764. }
  765. }
  766. return var_size;
  767. }
  768. static int decode_header(EXRContext *s)
  769. {
  770. int current_channel_offset = 0;
  771. int magic_number, version, flags, i;
  772. if (bytestream2_get_bytes_left(&s->gb) < 10) {
  773. av_log(s->avctx, AV_LOG_ERROR, "Header too short to parse.\n");
  774. return AVERROR_INVALIDDATA;
  775. }
  776. magic_number = bytestream2_get_le32(&s->gb);
  777. if (magic_number != 20000630) {
  778. /* As per documentation of OpenEXR, it is supposed to be
  779. * int 20000630 little-endian */
  780. av_log(s->avctx, AV_LOG_ERROR, "Wrong magic number %d.\n", magic_number);
  781. return AVERROR_INVALIDDATA;
  782. }
  783. version = bytestream2_get_byte(&s->gb);
  784. if (version != 2) {
  785. avpriv_report_missing_feature(s->avctx, "Version %d", version);
  786. return AVERROR_PATCHWELCOME;
  787. }
  788. flags = bytestream2_get_le24(&s->gb);
  789. if (flags & 0x02) {
  790. avpriv_report_missing_feature(s->avctx, "Tile support");
  791. return AVERROR_PATCHWELCOME;
  792. }
  793. // Parse the header
  794. while (bytestream2_get_bytes_left(&s->gb) > 0 && *s->gb.buffer) {
  795. int var_size;
  796. if ((var_size = check_header_variable(s, "channels",
  797. "chlist", 38)) >= 0) {
  798. GetByteContext ch_gb;
  799. if (!var_size)
  800. return AVERROR_INVALIDDATA;
  801. bytestream2_init(&ch_gb, s->gb.buffer, var_size);
  802. while (bytestream2_get_bytes_left(&ch_gb) >= 19) {
  803. EXRChannel *channel;
  804. enum ExrPixelType current_pixel_type;
  805. int channel_index = -1;
  806. int xsub, ysub;
  807. if (strcmp(s->layer, "") != 0) {
  808. if (strncmp(ch_gb.buffer, s->layer, strlen(s->layer)) == 0) {
  809. ch_gb.buffer += strlen(s->layer);
  810. if (*ch_gb.buffer == '.')
  811. ch_gb.buffer++; /* skip dot if not given */
  812. av_log(s->avctx, AV_LOG_INFO,
  813. "Layer %s.%s matched.\n", s->layer, ch_gb.buffer);
  814. }
  815. }
  816. if (!strcmp(ch_gb.buffer, "R") ||
  817. !strcmp(ch_gb.buffer, "X") ||
  818. !strcmp(ch_gb.buffer, "U"))
  819. channel_index = 0;
  820. else if (!strcmp(ch_gb.buffer, "G") ||
  821. !strcmp(ch_gb.buffer, "Y") ||
  822. !strcmp(ch_gb.buffer, "V"))
  823. channel_index = 1;
  824. else if (!strcmp(ch_gb.buffer, "B") ||
  825. !strcmp(ch_gb.buffer, "Z") ||
  826. !strcmp(ch_gb.buffer, "W"))
  827. channel_index = 2;
  828. else if (!strcmp(ch_gb.buffer, "A"))
  829. channel_index = 3;
  830. else
  831. av_log(s->avctx, AV_LOG_WARNING,
  832. "Unsupported channel %.256s.\n", ch_gb.buffer);
  833. /* skip until you get a 0 */
  834. while (bytestream2_get_bytes_left(&ch_gb) > 0 &&
  835. bytestream2_get_byte(&ch_gb))
  836. continue;
  837. if (bytestream2_get_bytes_left(&ch_gb) < 4) {
  838. av_log(s->avctx, AV_LOG_ERROR, "Incomplete header.\n");
  839. return AVERROR_INVALIDDATA;
  840. }
  841. current_pixel_type = bytestream2_get_le32(&ch_gb);
  842. if (current_pixel_type >= EXR_UNKNOWN) {
  843. avpriv_report_missing_feature(s->avctx,
  844. "Pixel type %d.\n",
  845. current_pixel_type);
  846. return AVERROR_PATCHWELCOME;
  847. }
  848. bytestream2_skip(&ch_gb, 4);
  849. xsub = bytestream2_get_le32(&ch_gb);
  850. ysub = bytestream2_get_le32(&ch_gb);
  851. if (xsub != 1 || ysub != 1) {
  852. avpriv_report_missing_feature(s->avctx,
  853. "Subsampling %dx%d",
  854. xsub, ysub);
  855. return AVERROR_PATCHWELCOME;
  856. }
  857. if (channel_index >= 0) {
  858. if (s->pixel_type != EXR_UNKNOWN &&
  859. s->pixel_type != current_pixel_type) {
  860. av_log(s->avctx, AV_LOG_ERROR,
  861. "RGB channels not of the same depth.\n");
  862. return AVERROR_INVALIDDATA;
  863. }
  864. s->pixel_type = current_pixel_type;
  865. s->channel_offsets[channel_index] = current_channel_offset;
  866. }
  867. s->channels = av_realloc(s->channels,
  868. ++s->nb_channels * sizeof(EXRChannel));
  869. if (!s->channels)
  870. return AVERROR(ENOMEM);
  871. channel = &s->channels[s->nb_channels - 1];
  872. channel->pixel_type = current_pixel_type;
  873. channel->xsub = xsub;
  874. channel->ysub = ysub;
  875. current_channel_offset += 1 << current_pixel_type;
  876. }
  877. /* Check if all channels are set with an offset or if the channels
  878. * are causing an overflow */
  879. if (FFMIN3(s->channel_offsets[0],
  880. s->channel_offsets[1],
  881. s->channel_offsets[2]) < 0) {
  882. if (s->channel_offsets[0] < 0)
  883. av_log(s->avctx, AV_LOG_ERROR, "Missing red channel.\n");
  884. if (s->channel_offsets[1] < 0)
  885. av_log(s->avctx, AV_LOG_ERROR, "Missing green channel.\n");
  886. if (s->channel_offsets[2] < 0)
  887. av_log(s->avctx, AV_LOG_ERROR, "Missing blue channel.\n");
  888. return AVERROR_INVALIDDATA;
  889. }
  890. // skip one last byte and update main gb
  891. s->gb.buffer = ch_gb.buffer + 1;
  892. continue;
  893. } else if ((var_size = check_header_variable(s, "dataWindow", "box2i",
  894. 31)) >= 0) {
  895. if (!var_size)
  896. return AVERROR_INVALIDDATA;
  897. s->xmin = bytestream2_get_le32(&s->gb);
  898. s->ymin = bytestream2_get_le32(&s->gb);
  899. s->xmax = bytestream2_get_le32(&s->gb);
  900. s->ymax = bytestream2_get_le32(&s->gb);
  901. s->xdelta = (s->xmax - s->xmin) + 1;
  902. s->ydelta = (s->ymax - s->ymin) + 1;
  903. continue;
  904. } else if ((var_size = check_header_variable(s, "displayWindow",
  905. "box2i", 34)) >= 0) {
  906. if (!var_size)
  907. return AVERROR_INVALIDDATA;
  908. bytestream2_skip(&s->gb, 8);
  909. s->w = bytestream2_get_le32(&s->gb) + 1;
  910. s->h = bytestream2_get_le32(&s->gb) + 1;
  911. continue;
  912. } else if ((var_size = check_header_variable(s, "lineOrder",
  913. "lineOrder", 25)) >= 0) {
  914. int line_order;
  915. if (!var_size)
  916. return AVERROR_INVALIDDATA;
  917. line_order = bytestream2_get_byte(&s->gb);
  918. av_log(s->avctx, AV_LOG_DEBUG, "line order: %d.\n", line_order);
  919. if (line_order > 2) {
  920. av_log(s->avctx, AV_LOG_ERROR, "Unknown line order.\n");
  921. return AVERROR_INVALIDDATA;
  922. }
  923. continue;
  924. } else if ((var_size = check_header_variable(s, "pixelAspectRatio",
  925. "float", 31)) >= 0) {
  926. if (!var_size)
  927. return AVERROR_INVALIDDATA;
  928. ff_set_sar(s->avctx,
  929. av_d2q(av_int2float(bytestream2_get_le32(&s->gb)), 255));
  930. continue;
  931. } else if ((var_size = check_header_variable(s, "compression",
  932. "compression", 29)) >= 0) {
  933. if (!var_size)
  934. return AVERROR_INVALIDDATA;
  935. if (s->compression == EXR_UNKN)
  936. s->compression = bytestream2_get_byte(&s->gb);
  937. else
  938. av_log(s->avctx, AV_LOG_WARNING,
  939. "Found more than one compression attribute.\n");
  940. continue;
  941. }
  942. // Check if there are enough bytes for a header
  943. if (bytestream2_get_bytes_left(&s->gb) <= 9) {
  944. av_log(s->avctx, AV_LOG_ERROR, "Incomplete header\n");
  945. return AVERROR_INVALIDDATA;
  946. }
  947. // Process unknown variables
  948. for (i = 0; i < 2; i++) // value_name and value_type
  949. while (bytestream2_get_byte(&s->gb) != 0);
  950. // Skip variable length
  951. bytestream2_skip(&s->gb, bytestream2_get_le32(&s->gb));
  952. }
  953. if (s->compression == EXR_UNKN) {
  954. av_log(s->avctx, AV_LOG_ERROR, "Missing compression attribute.\n");
  955. return AVERROR_INVALIDDATA;
  956. }
  957. s->scan_line_size = s->xdelta * current_channel_offset;
  958. if (bytestream2_get_bytes_left(&s->gb) <= 0) {
  959. av_log(s->avctx, AV_LOG_ERROR, "Incomplete frame.\n");
  960. return AVERROR_INVALIDDATA;
  961. }
  962. // aaand we are done
  963. bytestream2_skip(&s->gb, 1);
  964. return 0;
  965. }
  966. static int decode_frame(AVCodecContext *avctx, void *data,
  967. int *got_frame, AVPacket *avpkt)
  968. {
  969. EXRContext *s = avctx->priv_data;
  970. ThreadFrame frame = { .f = data };
  971. AVFrame *picture = data;
  972. uint8_t *ptr;
  973. int y, ret;
  974. int out_line_size;
  975. int scan_line_blocks;
  976. bytestream2_init(&s->gb, avpkt->data, avpkt->size);
  977. if ((ret = decode_header(s)) < 0)
  978. return ret;
  979. switch (s->pixel_type) {
  980. case EXR_FLOAT:
  981. case EXR_HALF:
  982. if (s->channel_offsets[3] >= 0)
  983. avctx->pix_fmt = AV_PIX_FMT_RGBA64;
  984. else
  985. avctx->pix_fmt = AV_PIX_FMT_RGB48;
  986. break;
  987. case EXR_UINT:
  988. avpriv_request_sample(avctx, "32-bit unsigned int");
  989. return AVERROR_PATCHWELCOME;
  990. default:
  991. av_log(avctx, AV_LOG_ERROR, "Missing channel list.\n");
  992. return AVERROR_INVALIDDATA;
  993. }
  994. switch (s->compression) {
  995. case EXR_RAW:
  996. case EXR_RLE:
  997. case EXR_ZIP1:
  998. s->scan_lines_per_block = 1;
  999. break;
  1000. case EXR_PXR24:
  1001. case EXR_ZIP16:
  1002. s->scan_lines_per_block = 16;
  1003. break;
  1004. case EXR_PIZ:
  1005. s->scan_lines_per_block = 32;
  1006. break;
  1007. default:
  1008. avpriv_report_missing_feature(avctx, "Compression %d", s->compression);
  1009. return AVERROR_PATCHWELCOME;
  1010. }
  1011. /* Verify the xmin, xmax, ymin, ymax and xdelta before setting
  1012. * the actual image size. */
  1013. if (s->xmin > s->xmax ||
  1014. s->ymin > s->ymax ||
  1015. s->xdelta != s->xmax - s->xmin + 1 ||
  1016. s->xmax >= s->w ||
  1017. s->ymax >= s->h) {
  1018. av_log(avctx, AV_LOG_ERROR, "Wrong or missing size information.\n");
  1019. return AVERROR_INVALIDDATA;
  1020. }
  1021. if ((ret = ff_set_dimensions(avctx, s->w, s->h)) < 0)
  1022. return ret;
  1023. s->desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  1024. if (!s->desc)
  1025. return AVERROR_INVALIDDATA;
  1026. out_line_size = avctx->width * 2 * s->desc->nb_components;
  1027. scan_line_blocks = (s->ydelta + s->scan_lines_per_block - 1) /
  1028. s->scan_lines_per_block;
  1029. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
  1030. return ret;
  1031. if (bytestream2_get_bytes_left(&s->gb) < scan_line_blocks * 8)
  1032. return AVERROR_INVALIDDATA;
  1033. // save pointer we are going to use in decode_block
  1034. s->buf = avpkt->data;
  1035. s->buf_size = avpkt->size;
  1036. ptr = picture->data[0];
  1037. // Zero out the start if ymin is not 0
  1038. for (y = 0; y < s->ymin; y++) {
  1039. memset(ptr, 0, out_line_size);
  1040. ptr += picture->linesize[0];
  1041. }
  1042. s->picture = picture;
  1043. avctx->execute2(avctx, decode_block, s->thread_data, NULL, scan_line_blocks);
  1044. // Zero out the end if ymax+1 is not h
  1045. for (y = s->ymax + 1; y < avctx->height; y++) {
  1046. memset(ptr, 0, out_line_size);
  1047. ptr += picture->linesize[0];
  1048. }
  1049. picture->pict_type = AV_PICTURE_TYPE_I;
  1050. *got_frame = 1;
  1051. return avpkt->size;
  1052. }
  1053. static av_cold int decode_init(AVCodecContext *avctx)
  1054. {
  1055. EXRContext *s = avctx->priv_data;
  1056. s->avctx = avctx;
  1057. s->xmin = ~0;
  1058. s->xmax = ~0;
  1059. s->ymin = ~0;
  1060. s->ymax = ~0;
  1061. s->xdelta = ~0;
  1062. s->ydelta = ~0;
  1063. s->channel_offsets[0] = -1;
  1064. s->channel_offsets[1] = -1;
  1065. s->channel_offsets[2] = -1;
  1066. s->channel_offsets[3] = -1;
  1067. s->pixel_type = EXR_UNKNOWN;
  1068. s->compression = EXR_UNKN;
  1069. s->nb_channels = 0;
  1070. s->w = 0;
  1071. s->h = 0;
  1072. // allocate thread data, used for non EXR_RAW compreesion types
  1073. s->thread_data = av_mallocz_array(avctx->thread_count, sizeof(EXRThreadData));
  1074. if (!s->thread_data)
  1075. return AVERROR_INVALIDDATA;
  1076. return 0;
  1077. }
  1078. static int decode_init_thread_copy(AVCodecContext *avctx)
  1079. { EXRContext *s = avctx->priv_data;
  1080. // allocate thread data, used for non EXR_RAW compreesion types
  1081. s->thread_data = av_mallocz_array(avctx->thread_count, sizeof(EXRThreadData));
  1082. if (!s->thread_data)
  1083. return AVERROR_INVALIDDATA;
  1084. return 0;
  1085. }
  1086. static av_cold int decode_end(AVCodecContext *avctx)
  1087. {
  1088. EXRContext *s = avctx->priv_data;
  1089. int i;
  1090. for (i = 0; i < avctx->thread_count; i++) {
  1091. EXRThreadData *td = &s->thread_data[i];
  1092. av_freep(&td->uncompressed_data);
  1093. av_freep(&td->tmp);
  1094. av_freep(&td->bitmap);
  1095. av_freep(&td->lut);
  1096. }
  1097. av_freep(&s->thread_data);
  1098. av_freep(&s->channels);
  1099. return 0;
  1100. }
  1101. #define OFFSET(x) offsetof(EXRContext, x)
  1102. #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  1103. static const AVOption options[] = {
  1104. { "layer", "Set the decoding layer", OFFSET(layer),
  1105. AV_OPT_TYPE_STRING, { .str = "" }, 0, 0, VD },
  1106. { NULL },
  1107. };
  1108. static const AVClass exr_class = {
  1109. .class_name = "EXR",
  1110. .item_name = av_default_item_name,
  1111. .option = options,
  1112. .version = LIBAVUTIL_VERSION_INT,
  1113. };
  1114. AVCodec ff_exr_decoder = {
  1115. .name = "exr",
  1116. .long_name = NULL_IF_CONFIG_SMALL("OpenEXR image"),
  1117. .type = AVMEDIA_TYPE_VIDEO,
  1118. .id = AV_CODEC_ID_EXR,
  1119. .priv_data_size = sizeof(EXRContext),
  1120. .init = decode_init,
  1121. .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
  1122. .close = decode_end,
  1123. .decode = decode_frame,
  1124. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS |
  1125. CODEC_CAP_SLICE_THREADS,
  1126. .priv_class = &exr_class,
  1127. };