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.

1282 lines
38KB

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