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.

1349 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. init_get_bits8(&gbit, gb->buffer, bytestream2_get_bytes_left(gb));
  257. for (; im <= iM; im++) {
  258. uint64_t l = hcode[im] = get_bits(&gbit, 6);
  259. if (l == LONG_ZEROCODE_RUN) {
  260. int zerun = get_bits(&gbit, 8) + SHORTEST_LONG_RUN;
  261. if (im + zerun > iM + 1)
  262. return AVERROR_INVALIDDATA;
  263. while (zerun--)
  264. hcode[im++] = 0;
  265. im--;
  266. } else if (l >= SHORT_ZEROCODE_RUN) {
  267. int zerun = l - SHORT_ZEROCODE_RUN + 2;
  268. if (im + zerun > iM + 1)
  269. return AVERROR_INVALIDDATA;
  270. while (zerun--)
  271. hcode[im++] = 0;
  272. im--;
  273. }
  274. }
  275. bytestream2_skip(gb, (get_bits_count(&gbit) + 7) / 8);
  276. huf_canonical_code_table(hcode);
  277. return 0;
  278. }
  279. static int huf_build_dec_table(const uint64_t *hcode, int im,
  280. int iM, HufDec *hdecod)
  281. {
  282. for (; im <= iM; im++) {
  283. uint64_t c = hcode[im] >> 6;
  284. int i, l = hcode[im] & 63;
  285. if (c >> l)
  286. return AVERROR_INVALIDDATA;
  287. if (l > HUF_DECBITS) {
  288. HufDec *pl = hdecod + (c >> (l - HUF_DECBITS));
  289. if (pl->len)
  290. return AVERROR_INVALIDDATA;
  291. pl->lit++;
  292. pl->p = av_realloc(pl->p, pl->lit * sizeof(int));
  293. if (!pl->p)
  294. return AVERROR(ENOMEM);
  295. pl->p[pl->lit - 1] = im;
  296. } else if (l) {
  297. HufDec *pl = hdecod + (c << (HUF_DECBITS - l));
  298. for (i = 1 << (HUF_DECBITS - l); i > 0; i--, pl++) {
  299. if (pl->len || pl->p)
  300. return AVERROR_INVALIDDATA;
  301. pl->len = l;
  302. pl->lit = im;
  303. }
  304. }
  305. }
  306. return 0;
  307. }
  308. #define get_char(c, lc, gb) \
  309. { \
  310. c = (c << 8) | bytestream2_get_byte(gb); \
  311. lc += 8; \
  312. }
  313. #define get_code(po, rlc, c, lc, gb, out, oe) \
  314. { \
  315. if (po == rlc) { \
  316. if (lc < 8) \
  317. get_char(c, lc, gb); \
  318. lc -= 8; \
  319. \
  320. cs = c >> lc; \
  321. \
  322. if (out + cs > oe) \
  323. return AVERROR_INVALIDDATA; \
  324. \
  325. s = out[-1]; \
  326. \
  327. while (cs-- > 0) \
  328. *out++ = s; \
  329. } else if (out < oe) { \
  330. *out++ = po; \
  331. } else { \
  332. return AVERROR_INVALIDDATA; \
  333. } \
  334. }
  335. static int huf_decode(const uint64_t *hcode, const HufDec *hdecod,
  336. GetByteContext *gb, int nbits,
  337. int rlc, int no, uint16_t *out)
  338. {
  339. uint64_t c = 0;
  340. uint16_t *outb = out;
  341. uint16_t *oe = out + no;
  342. const uint8_t *ie = gb->buffer + (nbits + 7) / 8; // input byte size
  343. uint8_t cs, s;
  344. int i, lc = 0;
  345. while (gb->buffer < ie) {
  346. get_char(c, lc, gb);
  347. while (lc >= HUF_DECBITS) {
  348. const HufDec pl = hdecod[(c >> (lc - HUF_DECBITS)) & HUF_DECMASK];
  349. if (pl.len) {
  350. lc -= pl.len;
  351. get_code(pl.lit, rlc, c, lc, gb, out, oe);
  352. } else {
  353. int j;
  354. if (!pl.p)
  355. return AVERROR_INVALIDDATA;
  356. for (j = 0; j < pl.lit; j++) {
  357. int l = hcode[pl.p[j]] & 63;
  358. while (lc < l && bytestream2_get_bytes_left(gb) > 0)
  359. get_char(c, lc, gb);
  360. if (lc >= l) {
  361. if ((hcode[pl.p[j]] >> 6) ==
  362. ((c >> (lc - l)) & ((1LL << l) - 1))) {
  363. lc -= l;
  364. get_code(pl.p[j], rlc, c, lc, gb, out, oe);
  365. break;
  366. }
  367. }
  368. }
  369. if (j == pl.lit)
  370. return AVERROR_INVALIDDATA;
  371. }
  372. }
  373. }
  374. i = (8 - nbits) & 7;
  375. c >>= i;
  376. lc -= i;
  377. while (lc > 0) {
  378. const HufDec pl = hdecod[(c << (HUF_DECBITS - lc)) & HUF_DECMASK];
  379. if (pl.len) {
  380. lc -= pl.len;
  381. get_code(pl.lit, rlc, c, lc, gb, out, oe);
  382. } else {
  383. return AVERROR_INVALIDDATA;
  384. }
  385. }
  386. if (out - outb != no)
  387. return AVERROR_INVALIDDATA;
  388. return 0;
  389. }
  390. static int huf_uncompress(GetByteContext *gb,
  391. uint16_t *dst, int dst_size)
  392. {
  393. int32_t src_size, im, iM;
  394. uint32_t nBits;
  395. uint64_t *freq;
  396. HufDec *hdec;
  397. int ret, i;
  398. src_size = bytestream2_get_le32(gb);
  399. im = bytestream2_get_le32(gb);
  400. iM = bytestream2_get_le32(gb);
  401. bytestream2_skip(gb, 4);
  402. nBits = bytestream2_get_le32(gb);
  403. if (im < 0 || im >= HUF_ENCSIZE ||
  404. iM < 0 || iM >= HUF_ENCSIZE ||
  405. src_size < 0)
  406. return AVERROR_INVALIDDATA;
  407. bytestream2_skip(gb, 4);
  408. freq = av_mallocz_array(HUF_ENCSIZE, sizeof(*freq));
  409. hdec = av_mallocz_array(HUF_DECSIZE, sizeof(*hdec));
  410. if (!freq || !hdec) {
  411. ret = AVERROR(ENOMEM);
  412. goto fail;
  413. }
  414. if ((ret = huf_unpack_enc_table(gb, im, iM, freq)) < 0)
  415. goto fail;
  416. if (nBits > 8 * bytestream2_get_bytes_left(gb)) {
  417. ret = AVERROR_INVALIDDATA;
  418. goto fail;
  419. }
  420. if ((ret = huf_build_dec_table(freq, im, iM, hdec)) < 0)
  421. goto fail;
  422. ret = huf_decode(freq, hdec, gb, nBits, iM, dst_size, dst);
  423. fail:
  424. for (i = 0; i < HUF_DECSIZE; i++)
  425. if (hdec)
  426. av_freep(&hdec[i].p);
  427. av_free(freq);
  428. av_free(hdec);
  429. return ret;
  430. }
  431. static inline void wdec14(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
  432. {
  433. int16_t ls = l;
  434. int16_t hs = h;
  435. int hi = hs;
  436. int ai = ls + (hi & 1) + (hi >> 1);
  437. int16_t as = ai;
  438. int16_t bs = ai - hi;
  439. *a = as;
  440. *b = bs;
  441. }
  442. #define NBITS 16
  443. #define A_OFFSET (1 << (NBITS - 1))
  444. #define MOD_MASK ((1 << NBITS) - 1)
  445. static inline void wdec16(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
  446. {
  447. int m = l;
  448. int d = h;
  449. int bb = (m - (d >> 1)) & MOD_MASK;
  450. int aa = (d + bb - A_OFFSET) & MOD_MASK;
  451. *b = bb;
  452. *a = aa;
  453. }
  454. static void wav_decode(uint16_t *in, int nx, int ox,
  455. int ny, int oy, uint16_t mx)
  456. {
  457. int w14 = (mx < (1 << 14));
  458. int n = (nx > ny) ? ny : nx;
  459. int p = 1;
  460. int p2;
  461. while (p <= n)
  462. p <<= 1;
  463. p >>= 1;
  464. p2 = p;
  465. p >>= 1;
  466. while (p >= 1) {
  467. uint16_t *py = in;
  468. uint16_t *ey = in + oy * (ny - p2);
  469. uint16_t i00, i01, i10, i11;
  470. int oy1 = oy * p;
  471. int oy2 = oy * p2;
  472. int ox1 = ox * p;
  473. int ox2 = ox * p2;
  474. for (; py <= ey; py += oy2) {
  475. uint16_t *px = py;
  476. uint16_t *ex = py + ox * (nx - p2);
  477. for (; px <= ex; px += ox2) {
  478. uint16_t *p01 = px + ox1;
  479. uint16_t *p10 = px + oy1;
  480. uint16_t *p11 = p10 + ox1;
  481. if (w14) {
  482. wdec14(*px, *p10, &i00, &i10);
  483. wdec14(*p01, *p11, &i01, &i11);
  484. wdec14(i00, i01, px, p01);
  485. wdec14(i10, i11, p10, p11);
  486. } else {
  487. wdec16(*px, *p10, &i00, &i10);
  488. wdec16(*p01, *p11, &i01, &i11);
  489. wdec16(i00, i01, px, p01);
  490. wdec16(i10, i11, p10, p11);
  491. }
  492. }
  493. if (nx & p) {
  494. uint16_t *p10 = px + oy1;
  495. if (w14)
  496. wdec14(*px, *p10, &i00, p10);
  497. else
  498. wdec16(*px, *p10, &i00, p10);
  499. *px = i00;
  500. }
  501. }
  502. if (ny & p) {
  503. uint16_t *px = py;
  504. uint16_t *ex = py + ox * (nx - p2);
  505. for (; px <= ex; px += ox2) {
  506. uint16_t *p01 = px + ox1;
  507. if (w14)
  508. wdec14(*px, *p01, &i00, p01);
  509. else
  510. wdec16(*px, *p01, &i00, p01);
  511. *px = i00;
  512. }
  513. }
  514. p2 = p;
  515. p >>= 1;
  516. }
  517. }
  518. static int piz_uncompress(EXRContext *s, const uint8_t *src, int ssize,
  519. int dsize, EXRThreadData *td)
  520. {
  521. GetByteContext gb;
  522. uint16_t maxval, min_non_zero, max_non_zero;
  523. uint16_t *ptr;
  524. uint16_t *tmp = (uint16_t *)td->tmp;
  525. uint8_t *out;
  526. int ret, i, j;
  527. if (!td->bitmap)
  528. td->bitmap = av_malloc(BITMAP_SIZE);
  529. if (!td->lut)
  530. td->lut = av_malloc(1 << 17);
  531. if (!td->bitmap || !td->lut) {
  532. av_free(td->bitmap);
  533. av_free(td->lut);
  534. return AVERROR(ENOMEM);
  535. }
  536. bytestream2_init(&gb, src, ssize);
  537. min_non_zero = bytestream2_get_le16(&gb);
  538. max_non_zero = bytestream2_get_le16(&gb);
  539. if (max_non_zero >= BITMAP_SIZE)
  540. return AVERROR_INVALIDDATA;
  541. memset(td->bitmap, 0, FFMIN(min_non_zero, BITMAP_SIZE));
  542. if (min_non_zero <= max_non_zero)
  543. bytestream2_get_buffer(&gb, td->bitmap + min_non_zero,
  544. max_non_zero - min_non_zero + 1);
  545. memset(td->bitmap + max_non_zero, 0, BITMAP_SIZE - max_non_zero);
  546. maxval = reverse_lut(td->bitmap, td->lut);
  547. ret = huf_uncompress(&gb, tmp, dsize / sizeof(uint16_t));
  548. if (ret)
  549. return ret;
  550. ptr = tmp;
  551. for (i = 0; i < s->nb_channels; i++) {
  552. EXRChannel *channel = &s->channels[i];
  553. int size = channel->pixel_type;
  554. for (j = 0; j < size; j++)
  555. wav_decode(ptr + j, s->xdelta, size, s->ysize,
  556. s->xdelta * size, maxval);
  557. ptr += s->xdelta * s->ysize * size;
  558. }
  559. apply_lut(td->lut, tmp, dsize / sizeof(uint16_t));
  560. out = td->uncompressed_data;
  561. for (i = 0; i < s->ysize; i++)
  562. for (j = 0; j < s->nb_channels; j++) {
  563. uint16_t *in = tmp + j * s->xdelta * s->ysize + i * s->xdelta;
  564. memcpy(out, in, s->xdelta * 2);
  565. out += s->xdelta * 2;
  566. }
  567. return 0;
  568. }
  569. static int pxr24_uncompress(EXRContext *s, const uint8_t *src,
  570. int compressed_size, int uncompressed_size,
  571. EXRThreadData *td)
  572. {
  573. unsigned long dest_len = uncompressed_size;
  574. const uint8_t *in = td->tmp;
  575. uint8_t *out;
  576. int c, i, j;
  577. if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK ||
  578. dest_len != uncompressed_size)
  579. return AVERROR_INVALIDDATA;
  580. out = td->uncompressed_data;
  581. for (i = 0; i < s->ysize; i++)
  582. for (c = 0; c < s->nb_channels; c++) {
  583. EXRChannel *channel = &s->channels[c];
  584. const uint8_t *ptr[4];
  585. uint32_t pixel = 0;
  586. switch (channel->pixel_type) {
  587. case EXR_FLOAT:
  588. ptr[0] = in;
  589. ptr[1] = ptr[0] + s->xdelta;
  590. ptr[2] = ptr[1] + s->xdelta;
  591. in = ptr[2] + s->xdelta;
  592. for (j = 0; j < s->xdelta; ++j) {
  593. uint32_t diff = (*(ptr[0]++) << 24) |
  594. (*(ptr[1]++) << 16) |
  595. (*(ptr[2]++) << 8);
  596. pixel += diff;
  597. bytestream_put_le32(&out, pixel);
  598. }
  599. break;
  600. case EXR_HALF:
  601. ptr[0] = in;
  602. ptr[1] = ptr[0] + s->xdelta;
  603. in = ptr[1] + s->xdelta;
  604. for (j = 0; j < s->xdelta; j++) {
  605. uint32_t diff = (*(ptr[0]++) << 8) | *(ptr[1]++);
  606. pixel += diff;
  607. bytestream_put_le16(&out, pixel);
  608. }
  609. break;
  610. default:
  611. return AVERROR_INVALIDDATA;
  612. }
  613. }
  614. return 0;
  615. }
  616. static int decode_block(AVCodecContext *avctx, void *tdata,
  617. int jobnr, int threadnr)
  618. {
  619. EXRContext *s = avctx->priv_data;
  620. AVFrame *const p = s->picture;
  621. EXRThreadData *td = &s->thread_data[threadnr];
  622. const uint8_t *channel_buffer[4] = { 0 };
  623. const uint8_t *buf = s->buf;
  624. uint64_t line_offset, uncompressed_size;
  625. uint32_t xdelta = s->xdelta;
  626. uint16_t *ptr_x;
  627. uint8_t *ptr;
  628. uint32_t data_size, line;
  629. const uint8_t *src;
  630. int axmax = (avctx->width - (s->xmax + 1)) * 2 * s->desc->nb_components;
  631. int bxmin = s->xmin * 2 * s->desc->nb_components;
  632. int i, x, buf_size = s->buf_size;
  633. int ret;
  634. line_offset = AV_RL64(s->gb.buffer + jobnr * 8);
  635. // Check if the buffer has the required bytes needed from the offset
  636. if (line_offset > buf_size - 8)
  637. return AVERROR_INVALIDDATA;
  638. src = buf + line_offset + 8;
  639. line = AV_RL32(src - 8);
  640. if (line < s->ymin || line > s->ymax)
  641. return AVERROR_INVALIDDATA;
  642. data_size = AV_RL32(src - 4);
  643. if (data_size <= 0 || data_size > buf_size)
  644. return AVERROR_INVALIDDATA;
  645. s->ysize = FFMIN(s->scan_lines_per_block, s->ymax - line + 1);
  646. uncompressed_size = s->scan_line_size * s->ysize;
  647. if ((s->compression == EXR_RAW && (data_size != uncompressed_size ||
  648. line_offset > buf_size - uncompressed_size)) ||
  649. (s->compression != EXR_RAW && (data_size > uncompressed_size ||
  650. line_offset > buf_size - data_size))) {
  651. return AVERROR_INVALIDDATA;
  652. }
  653. if (data_size < uncompressed_size) {
  654. av_fast_padded_malloc(&td->uncompressed_data,
  655. &td->uncompressed_size, uncompressed_size);
  656. av_fast_padded_malloc(&td->tmp, &td->tmp_size, uncompressed_size);
  657. if (!td->uncompressed_data || !td->tmp)
  658. return AVERROR(ENOMEM);
  659. ret = AVERROR_INVALIDDATA;
  660. switch (s->compression) {
  661. case EXR_ZIP1:
  662. case EXR_ZIP16:
  663. ret = zip_uncompress(src, data_size, uncompressed_size, td);
  664. break;
  665. case EXR_PIZ:
  666. ret = piz_uncompress(s, src, data_size, uncompressed_size, td);
  667. break;
  668. case EXR_PXR24:
  669. ret = pxr24_uncompress(s, src, data_size, uncompressed_size, td);
  670. break;
  671. case EXR_RLE:
  672. ret = rle_uncompress(src, data_size, uncompressed_size, td);
  673. }
  674. if (ret < 0) {
  675. av_log(avctx, AV_LOG_ERROR, "decode_block() failed.\n");
  676. return ret;
  677. }
  678. src = td->uncompressed_data;
  679. }
  680. channel_buffer[0] = src + xdelta * s->channel_offsets[0];
  681. channel_buffer[1] = src + xdelta * s->channel_offsets[1];
  682. channel_buffer[2] = src + xdelta * s->channel_offsets[2];
  683. if (s->channel_offsets[3] >= 0)
  684. channel_buffer[3] = src + xdelta * s->channel_offsets[3];
  685. ptr = p->data[0] + line * p->linesize[0];
  686. for (i = 0;
  687. i < s->scan_lines_per_block && line + i <= s->ymax;
  688. i++, ptr += p->linesize[0]) {
  689. const uint8_t *r, *g, *b, *a;
  690. r = channel_buffer[0];
  691. g = channel_buffer[1];
  692. b = channel_buffer[2];
  693. if (channel_buffer[3])
  694. a = channel_buffer[3];
  695. ptr_x = (uint16_t *) ptr;
  696. // Zero out the start if xmin is not 0
  697. memset(ptr_x, 0, bxmin);
  698. ptr_x += s->xmin * s->desc->nb_components;
  699. if (s->pixel_type == EXR_FLOAT) {
  700. // 32-bit
  701. for (x = 0; x < xdelta; x++) {
  702. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&r));
  703. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&g));
  704. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&b));
  705. if (channel_buffer[3])
  706. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a));
  707. }
  708. } else {
  709. // 16-bit
  710. for (x = 0; x < xdelta; x++) {
  711. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&r));
  712. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&g));
  713. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&b));
  714. if (channel_buffer[3])
  715. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&a));
  716. }
  717. }
  718. // Zero out the end if xmax+1 is not w
  719. memset(ptr_x, 0, axmax);
  720. channel_buffer[0] += s->scan_line_size;
  721. channel_buffer[1] += s->scan_line_size;
  722. channel_buffer[2] += s->scan_line_size;
  723. if (channel_buffer[3])
  724. channel_buffer[3] += s->scan_line_size;
  725. }
  726. return 0;
  727. }
  728. /**
  729. * Check if the variable name corresponds to its data type.
  730. *
  731. * @param s the EXRContext
  732. * @param value_name name of the variable to check
  733. * @param value_type type of the variable to check
  734. * @param minimum_length minimum length of the variable data
  735. *
  736. * @return bytes to read containing variable data
  737. * -1 if variable is not found
  738. * 0 if buffer ended prematurely
  739. */
  740. static int check_header_variable(EXRContext *s,
  741. const char *value_name,
  742. const char *value_type,
  743. unsigned int minimum_length)
  744. {
  745. int var_size = -1;
  746. if (bytestream2_get_bytes_left(&s->gb) >= minimum_length &&
  747. !strcmp(s->gb.buffer, value_name)) {
  748. // found value_name, jump to value_type (null terminated strings)
  749. s->gb.buffer += strlen(value_name) + 1;
  750. if (!strcmp(s->gb.buffer, value_type)) {
  751. s->gb.buffer += strlen(value_type) + 1;
  752. var_size = bytestream2_get_le32(&s->gb);
  753. // don't go read past boundaries
  754. if (var_size > bytestream2_get_bytes_left(&s->gb))
  755. var_size = 0;
  756. } else {
  757. // value_type not found, reset the buffer
  758. s->gb.buffer -= strlen(value_name) + 1;
  759. av_log(s->avctx, AV_LOG_WARNING,
  760. "Unknown data type %s for header variable %s.\n",
  761. value_type, value_name);
  762. }
  763. }
  764. return var_size;
  765. }
  766. static int decode_header(EXRContext *s)
  767. {
  768. int current_channel_offset = 0;
  769. int magic_number, version, flags, i;
  770. if (bytestream2_get_bytes_left(&s->gb) < 10) {
  771. av_log(s->avctx, AV_LOG_ERROR, "Header too short to parse.\n");
  772. return AVERROR_INVALIDDATA;
  773. }
  774. magic_number = bytestream2_get_le32(&s->gb);
  775. if (magic_number != 20000630) {
  776. /* As per documentation of OpenEXR, it is supposed to be
  777. * int 20000630 little-endian */
  778. av_log(s->avctx, AV_LOG_ERROR, "Wrong magic number %d.\n", magic_number);
  779. return AVERROR_INVALIDDATA;
  780. }
  781. version = bytestream2_get_byte(&s->gb);
  782. if (version != 2) {
  783. avpriv_report_missing_feature(s->avctx, "Version %d", version);
  784. return AVERROR_PATCHWELCOME;
  785. }
  786. flags = bytestream2_get_le24(&s->gb);
  787. if (flags & 0x02) {
  788. avpriv_report_missing_feature(s->avctx, "Tile support");
  789. return AVERROR_PATCHWELCOME;
  790. }
  791. // Parse the header
  792. while (bytestream2_get_bytes_left(&s->gb) > 0 && *s->gb.buffer) {
  793. int var_size;
  794. if ((var_size = check_header_variable(s, "channels",
  795. "chlist", 38)) >= 0) {
  796. GetByteContext ch_gb;
  797. if (!var_size)
  798. return AVERROR_INVALIDDATA;
  799. bytestream2_init(&ch_gb, s->gb.buffer, var_size);
  800. while (bytestream2_get_bytes_left(&ch_gb) >= 19) {
  801. EXRChannel *channel;
  802. enum ExrPixelType current_pixel_type;
  803. int channel_index = -1;
  804. int xsub, ysub;
  805. if (strcmp(s->layer, "") != 0) {
  806. if (strncmp(ch_gb.buffer, s->layer, strlen(s->layer)) == 0) {
  807. ch_gb.buffer += strlen(s->layer);
  808. if (*ch_gb.buffer == '.')
  809. ch_gb.buffer++; /* skip dot if not given */
  810. av_log(s->avctx, AV_LOG_INFO,
  811. "Layer %s.%s matched.\n", s->layer, ch_gb.buffer);
  812. }
  813. }
  814. if (!strcmp(ch_gb.buffer, "R") ||
  815. !strcmp(ch_gb.buffer, "X") ||
  816. !strcmp(ch_gb.buffer, "U"))
  817. channel_index = 0;
  818. else if (!strcmp(ch_gb.buffer, "G") ||
  819. !strcmp(ch_gb.buffer, "Y") ||
  820. !strcmp(ch_gb.buffer, "V"))
  821. channel_index = 1;
  822. else if (!strcmp(ch_gb.buffer, "B") ||
  823. !strcmp(ch_gb.buffer, "Z") ||
  824. !strcmp(ch_gb.buffer, "W"))
  825. channel_index = 2;
  826. else if (!strcmp(ch_gb.buffer, "A"))
  827. channel_index = 3;
  828. else
  829. av_log(s->avctx, AV_LOG_WARNING,
  830. "Unsupported channel %.256s.\n", ch_gb.buffer);
  831. /* skip until you get a 0 */
  832. while (bytestream2_get_bytes_left(&ch_gb) > 0 &&
  833. bytestream2_get_byte(&ch_gb))
  834. continue;
  835. if (bytestream2_get_bytes_left(&ch_gb) < 4) {
  836. av_log(s->avctx, AV_LOG_ERROR, "Incomplete header.\n");
  837. return AVERROR_INVALIDDATA;
  838. }
  839. current_pixel_type = bytestream2_get_le32(&ch_gb);
  840. if (current_pixel_type >= EXR_UNKNOWN) {
  841. avpriv_report_missing_feature(s->avctx,
  842. "Pixel type %d.\n",
  843. current_pixel_type);
  844. return AVERROR_PATCHWELCOME;
  845. }
  846. bytestream2_skip(&ch_gb, 4);
  847. xsub = bytestream2_get_le32(&ch_gb);
  848. ysub = bytestream2_get_le32(&ch_gb);
  849. if (xsub != 1 || ysub != 1) {
  850. avpriv_report_missing_feature(s->avctx,
  851. "Subsampling %dx%d",
  852. xsub, ysub);
  853. return AVERROR_PATCHWELCOME;
  854. }
  855. if (channel_index >= 0) {
  856. if (s->pixel_type != EXR_UNKNOWN &&
  857. s->pixel_type != current_pixel_type) {
  858. av_log(s->avctx, AV_LOG_ERROR,
  859. "RGB channels not of the same depth.\n");
  860. return AVERROR_INVALIDDATA;
  861. }
  862. s->pixel_type = current_pixel_type;
  863. s->channel_offsets[channel_index] = current_channel_offset;
  864. }
  865. s->channels = av_realloc(s->channels,
  866. ++s->nb_channels * sizeof(EXRChannel));
  867. if (!s->channels)
  868. return AVERROR(ENOMEM);
  869. channel = &s->channels[s->nb_channels - 1];
  870. channel->pixel_type = current_pixel_type;
  871. channel->xsub = xsub;
  872. channel->ysub = ysub;
  873. current_channel_offset += 1 << current_pixel_type;
  874. }
  875. /* Check if all channels are set with an offset or if the channels
  876. * are causing an overflow */
  877. if (FFMIN3(s->channel_offsets[0],
  878. s->channel_offsets[1],
  879. s->channel_offsets[2]) < 0) {
  880. if (s->channel_offsets[0] < 0)
  881. av_log(s->avctx, AV_LOG_ERROR, "Missing red channel.\n");
  882. if (s->channel_offsets[1] < 0)
  883. av_log(s->avctx, AV_LOG_ERROR, "Missing green channel.\n");
  884. if (s->channel_offsets[2] < 0)
  885. av_log(s->avctx, AV_LOG_ERROR, "Missing blue channel.\n");
  886. return AVERROR_INVALIDDATA;
  887. }
  888. // skip one last byte and update main gb
  889. s->gb.buffer = ch_gb.buffer + 1;
  890. continue;
  891. } else if ((var_size = check_header_variable(s, "dataWindow", "box2i",
  892. 31)) >= 0) {
  893. if (!var_size)
  894. return AVERROR_INVALIDDATA;
  895. s->xmin = bytestream2_get_le32(&s->gb);
  896. s->ymin = bytestream2_get_le32(&s->gb);
  897. s->xmax = bytestream2_get_le32(&s->gb);
  898. s->ymax = bytestream2_get_le32(&s->gb);
  899. s->xdelta = (s->xmax - s->xmin) + 1;
  900. s->ydelta = (s->ymax - s->ymin) + 1;
  901. continue;
  902. } else if ((var_size = check_header_variable(s, "displayWindow",
  903. "box2i", 34)) >= 0) {
  904. if (!var_size)
  905. return AVERROR_INVALIDDATA;
  906. bytestream2_skip(&s->gb, 8);
  907. s->w = bytestream2_get_le32(&s->gb) + 1;
  908. s->h = bytestream2_get_le32(&s->gb) + 1;
  909. continue;
  910. } else if ((var_size = check_header_variable(s, "lineOrder",
  911. "lineOrder", 25)) >= 0) {
  912. int line_order;
  913. if (!var_size)
  914. return AVERROR_INVALIDDATA;
  915. line_order = bytestream2_get_byte(&s->gb);
  916. av_log(s->avctx, AV_LOG_DEBUG, "line order: %d.\n", line_order);
  917. if (line_order > 2) {
  918. av_log(s->avctx, AV_LOG_ERROR, "Unknown line order.\n");
  919. return AVERROR_INVALIDDATA;
  920. }
  921. continue;
  922. } else if ((var_size = check_header_variable(s, "pixelAspectRatio",
  923. "float", 31)) >= 0) {
  924. if (!var_size)
  925. return AVERROR_INVALIDDATA;
  926. s->avctx->sample_aspect_ratio =
  927. av_d2q(av_int2float(bytestream2_get_le32(&s->gb)), 255);
  928. continue;
  929. } else if ((var_size = check_header_variable(s, "compression",
  930. "compression", 29)) >= 0) {
  931. if (!var_size)
  932. return AVERROR_INVALIDDATA;
  933. if (s->compression == EXR_UNKN)
  934. s->compression = bytestream2_get_byte(&s->gb);
  935. else
  936. av_log(s->avctx, AV_LOG_WARNING,
  937. "Found more than one compression attribute.\n");
  938. continue;
  939. }
  940. // Check if there are enough bytes for a header
  941. if (bytestream2_get_bytes_left(&s->gb) <= 9) {
  942. av_log(s->avctx, AV_LOG_ERROR, "Incomplete header\n");
  943. return AVERROR_INVALIDDATA;
  944. }
  945. // Process unknown variables
  946. for (i = 0; i < 2; i++) // value_name and value_type
  947. while (bytestream2_get_byte(&s->gb) != 0);
  948. // Skip variable length
  949. bytestream2_skip(&s->gb, bytestream2_get_le32(&s->gb));
  950. }
  951. if (s->compression == EXR_UNKN) {
  952. av_log(s->avctx, AV_LOG_ERROR, "Missing compression attribute.\n");
  953. return AVERROR_INVALIDDATA;
  954. }
  955. s->scan_line_size = s->xdelta * current_channel_offset;
  956. if (bytestream2_get_bytes_left(&s->gb) <= 0) {
  957. av_log(s->avctx, AV_LOG_ERROR, "Incomplete frame.\n");
  958. return AVERROR_INVALIDDATA;
  959. }
  960. // aaand we are done
  961. bytestream2_skip(&s->gb, 1);
  962. return 0;
  963. }
  964. static int decode_frame(AVCodecContext *avctx, void *data,
  965. int *got_frame, AVPacket *avpkt)
  966. {
  967. EXRContext *s = avctx->priv_data;
  968. ThreadFrame frame = { .f = data };
  969. AVFrame *picture = data;
  970. uint8_t *ptr;
  971. int y, ret;
  972. int out_line_size;
  973. int scan_line_blocks;
  974. bytestream2_init(&s->gb, avpkt->data, avpkt->size);
  975. if ((ret = decode_header(s)) < 0)
  976. return ret;
  977. switch (s->pixel_type) {
  978. case EXR_FLOAT:
  979. case EXR_HALF:
  980. if (s->channel_offsets[3] >= 0)
  981. avctx->pix_fmt = AV_PIX_FMT_RGBA64;
  982. else
  983. avctx->pix_fmt = AV_PIX_FMT_RGB48;
  984. break;
  985. case EXR_UINT:
  986. avpriv_request_sample(avctx, "32-bit unsigned int");
  987. return AVERROR_PATCHWELCOME;
  988. default:
  989. av_log(avctx, AV_LOG_ERROR, "Missing channel list.\n");
  990. return AVERROR_INVALIDDATA;
  991. }
  992. switch (s->compression) {
  993. case EXR_RAW:
  994. case EXR_RLE:
  995. case EXR_ZIP1:
  996. s->scan_lines_per_block = 1;
  997. break;
  998. case EXR_PXR24:
  999. case EXR_ZIP16:
  1000. s->scan_lines_per_block = 16;
  1001. break;
  1002. case EXR_PIZ:
  1003. s->scan_lines_per_block = 32;
  1004. break;
  1005. default:
  1006. avpriv_report_missing_feature(avctx, "Compression %d", s->compression);
  1007. return AVERROR_PATCHWELCOME;
  1008. }
  1009. /* Verify the xmin, xmax, ymin, ymax and xdelta before setting
  1010. * the actual image size. */
  1011. if (s->xmin > s->xmax ||
  1012. s->ymin > s->ymax ||
  1013. s->xdelta != s->xmax - s->xmin + 1 ||
  1014. s->xmax >= s->w ||
  1015. s->ymax >= s->h) {
  1016. av_log(avctx, AV_LOG_ERROR, "Wrong or missing size information.\n");
  1017. return AVERROR_INVALIDDATA;
  1018. }
  1019. if ((ret = ff_set_dimensions(avctx, s->w, s->h)) < 0)
  1020. return ret;
  1021. s->desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  1022. if (!s->desc)
  1023. return AVERROR_INVALIDDATA;
  1024. out_line_size = avctx->width * 2 * s->desc->nb_components;
  1025. scan_line_blocks = (s->ydelta + s->scan_lines_per_block - 1) /
  1026. s->scan_lines_per_block;
  1027. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
  1028. return ret;
  1029. if (bytestream2_get_bytes_left(&s->gb) < scan_line_blocks * 8)
  1030. return AVERROR_INVALIDDATA;
  1031. // save pointer we are going to use in decode_block
  1032. s->buf = avpkt->data;
  1033. s->buf_size = avpkt->size;
  1034. ptr = picture->data[0];
  1035. // Zero out the start if ymin is not 0
  1036. for (y = 0; y < s->ymin; y++) {
  1037. memset(ptr, 0, out_line_size);
  1038. ptr += picture->linesize[0];
  1039. }
  1040. s->picture = picture;
  1041. avctx->execute2(avctx, decode_block, s->thread_data, NULL, scan_line_blocks);
  1042. // Zero out the end if ymax+1 is not h
  1043. for (y = s->ymax + 1; y < avctx->height; y++) {
  1044. memset(ptr, 0, out_line_size);
  1045. ptr += picture->linesize[0];
  1046. }
  1047. picture->pict_type = AV_PICTURE_TYPE_I;
  1048. *got_frame = 1;
  1049. return avpkt->size;
  1050. }
  1051. static av_cold int decode_init(AVCodecContext *avctx)
  1052. {
  1053. EXRContext *s = avctx->priv_data;
  1054. s->avctx = avctx;
  1055. s->xmin = ~0;
  1056. s->xmax = ~0;
  1057. s->ymin = ~0;
  1058. s->ymax = ~0;
  1059. s->xdelta = ~0;
  1060. s->ydelta = ~0;
  1061. s->channel_offsets[0] = -1;
  1062. s->channel_offsets[1] = -1;
  1063. s->channel_offsets[2] = -1;
  1064. s->channel_offsets[3] = -1;
  1065. s->pixel_type = EXR_UNKNOWN;
  1066. s->compression = EXR_UNKN;
  1067. s->nb_channels = 0;
  1068. s->w = 0;
  1069. s->h = 0;
  1070. // allocate thread data, used for non EXR_RAW compreesion types
  1071. s->thread_data = av_mallocz_array(avctx->thread_count, sizeof(EXRThreadData));
  1072. if (!s->thread_data)
  1073. return AVERROR_INVALIDDATA;
  1074. return 0;
  1075. }
  1076. static int decode_init_thread_copy(AVCodecContext *avctx)
  1077. { EXRContext *s = avctx->priv_data;
  1078. // allocate thread data, used for non EXR_RAW compreesion types
  1079. s->thread_data = av_mallocz_array(avctx->thread_count, sizeof(EXRThreadData));
  1080. if (!s->thread_data)
  1081. return AVERROR_INVALIDDATA;
  1082. return 0;
  1083. }
  1084. static av_cold int decode_end(AVCodecContext *avctx)
  1085. {
  1086. EXRContext *s = avctx->priv_data;
  1087. int i;
  1088. for (i = 0; i < avctx->thread_count; i++) {
  1089. EXRThreadData *td = &s->thread_data[i];
  1090. av_freep(&td->uncompressed_data);
  1091. av_freep(&td->tmp);
  1092. av_freep(&td->bitmap);
  1093. av_freep(&td->lut);
  1094. }
  1095. av_freep(&s->thread_data);
  1096. av_freep(&s->channels);
  1097. return 0;
  1098. }
  1099. #define OFFSET(x) offsetof(EXRContext, x)
  1100. #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  1101. static const AVOption options[] = {
  1102. { "layer", "Set the decoding layer", OFFSET(layer),
  1103. AV_OPT_TYPE_STRING, { .str = "" }, 0, 0, VD },
  1104. { NULL },
  1105. };
  1106. static const AVClass exr_class = {
  1107. .class_name = "EXR",
  1108. .item_name = av_default_item_name,
  1109. .option = options,
  1110. .version = LIBAVUTIL_VERSION_INT,
  1111. };
  1112. AVCodec ff_exr_decoder = {
  1113. .name = "exr",
  1114. .long_name = NULL_IF_CONFIG_SMALL("OpenEXR image"),
  1115. .type = AVMEDIA_TYPE_VIDEO,
  1116. .id = AV_CODEC_ID_EXR,
  1117. .priv_data_size = sizeof(EXRContext),
  1118. .init = decode_init,
  1119. .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
  1120. .close = decode_end,
  1121. .decode = decode_frame,
  1122. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS |
  1123. CODEC_CAP_SLICE_THREADS,
  1124. .priv_class = &exr_class,
  1125. };