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.

1310 lines
39KB

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