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.

2337 lines
76KB

  1. /*
  2. * OpenEXR (.exr) image decoder
  3. * Copyright (c) 2006 Industrial Light & Magic, a division of Lucas Digital Ltd. LLC
  4. * Copyright (c) 2009 Jimmy Christensen
  5. *
  6. * B44/B44A, Tile, UINT32 added by Jokyo Images support by CNC - French National Center for Cinema
  7. *
  8. * This file is part of FFmpeg.
  9. *
  10. * FFmpeg is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * FFmpeg is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with FFmpeg; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. /**
  25. * @file
  26. * OpenEXR decoder
  27. * @author Jimmy Christensen
  28. *
  29. * For more information on the OpenEXR format, visit:
  30. * http://openexr.com/
  31. */
  32. #include <float.h>
  33. #include <zlib.h>
  34. #include "libavutil/avassert.h"
  35. #include "libavutil/common.h"
  36. #include "libavutil/imgutils.h"
  37. #include "libavutil/intfloat.h"
  38. #include "libavutil/avstring.h"
  39. #include "libavutil/opt.h"
  40. #include "libavutil/color_utils.h"
  41. #include "avcodec.h"
  42. #include "bytestream.h"
  43. #if HAVE_BIGENDIAN
  44. #include "bswapdsp.h"
  45. #endif
  46. #include "exrdsp.h"
  47. #include "get_bits.h"
  48. #include "internal.h"
  49. #include "half2float.h"
  50. #include "mathops.h"
  51. #include "thread.h"
  52. enum ExrCompr {
  53. EXR_RAW,
  54. EXR_RLE,
  55. EXR_ZIP1,
  56. EXR_ZIP16,
  57. EXR_PIZ,
  58. EXR_PXR24,
  59. EXR_B44,
  60. EXR_B44A,
  61. EXR_DWAA,
  62. EXR_DWAB,
  63. EXR_UNKN,
  64. };
  65. enum ExrPixelType {
  66. EXR_UINT,
  67. EXR_HALF,
  68. EXR_FLOAT,
  69. EXR_UNKNOWN,
  70. };
  71. enum ExrTileLevelMode {
  72. EXR_TILE_LEVEL_ONE,
  73. EXR_TILE_LEVEL_MIPMAP,
  74. EXR_TILE_LEVEL_RIPMAP,
  75. EXR_TILE_LEVEL_UNKNOWN,
  76. };
  77. enum ExrTileLevelRound {
  78. EXR_TILE_ROUND_UP,
  79. EXR_TILE_ROUND_DOWN,
  80. EXR_TILE_ROUND_UNKNOWN,
  81. };
  82. typedef struct HuffEntry {
  83. uint8_t len;
  84. uint16_t sym;
  85. uint32_t code;
  86. } HuffEntry;
  87. typedef struct EXRChannel {
  88. int xsub, ysub;
  89. enum ExrPixelType pixel_type;
  90. } EXRChannel;
  91. typedef struct EXRTileAttribute {
  92. int32_t xSize;
  93. int32_t ySize;
  94. enum ExrTileLevelMode level_mode;
  95. enum ExrTileLevelRound level_round;
  96. } EXRTileAttribute;
  97. typedef struct EXRThreadData {
  98. uint8_t *uncompressed_data;
  99. int uncompressed_size;
  100. uint8_t *tmp;
  101. int tmp_size;
  102. uint8_t *bitmap;
  103. uint16_t *lut;
  104. uint8_t *ac_data;
  105. unsigned ac_size;
  106. uint8_t *dc_data;
  107. unsigned dc_size;
  108. uint8_t *rle_data;
  109. unsigned rle_size;
  110. uint8_t *rle_raw_data;
  111. unsigned rle_raw_size;
  112. float block[3][64];
  113. int ysize, xsize;
  114. int channel_line_size;
  115. int run_sym;
  116. HuffEntry *he;
  117. uint64_t *freq;
  118. VLC vlc;
  119. } EXRThreadData;
  120. typedef struct EXRContext {
  121. AVClass *class;
  122. AVFrame *picture;
  123. AVCodecContext *avctx;
  124. ExrDSPContext dsp;
  125. #if HAVE_BIGENDIAN
  126. BswapDSPContext bbdsp;
  127. #endif
  128. enum ExrCompr compression;
  129. enum ExrPixelType pixel_type;
  130. int channel_offsets[4]; // 0 = red, 1 = green, 2 = blue and 3 = alpha
  131. const AVPixFmtDescriptor *desc;
  132. int w, h;
  133. uint32_t sar;
  134. int32_t xmax, xmin;
  135. int32_t ymax, ymin;
  136. uint32_t xdelta, ydelta;
  137. int scan_lines_per_block;
  138. EXRTileAttribute tile_attr; /* header data attribute of tile */
  139. int is_tile; /* 0 if scanline, 1 if tile */
  140. int is_multipart;
  141. int current_part;
  142. int is_luma;/* 1 if there is an Y plane */
  143. GetByteContext gb;
  144. const uint8_t *buf;
  145. int buf_size;
  146. EXRChannel *channels;
  147. int nb_channels;
  148. int current_channel_offset;
  149. uint32_t chunk_count;
  150. EXRThreadData *thread_data;
  151. const char *layer;
  152. int selected_part;
  153. enum AVColorTransferCharacteristic apply_trc_type;
  154. float gamma;
  155. union av_intfloat32 gamma_table[65536];
  156. uint32_t mantissatable[2048];
  157. uint32_t exponenttable[64];
  158. uint16_t offsettable[64];
  159. } EXRContext;
  160. static int zip_uncompress(EXRContext *s, const uint8_t *src, int compressed_size,
  161. int uncompressed_size, EXRThreadData *td)
  162. {
  163. unsigned long dest_len = uncompressed_size;
  164. if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK ||
  165. dest_len != uncompressed_size)
  166. return AVERROR_INVALIDDATA;
  167. av_assert1(uncompressed_size % 2 == 0);
  168. s->dsp.predictor(td->tmp, uncompressed_size);
  169. s->dsp.reorder_pixels(td->uncompressed_data, td->tmp, uncompressed_size);
  170. return 0;
  171. }
  172. static int rle(uint8_t *dst, const uint8_t *src,
  173. int compressed_size, int uncompressed_size)
  174. {
  175. uint8_t *d = dst;
  176. const int8_t *s = src;
  177. int ssize = compressed_size;
  178. int dsize = uncompressed_size;
  179. uint8_t *dend = d + dsize;
  180. int count;
  181. while (ssize > 0) {
  182. count = *s++;
  183. if (count < 0) {
  184. count = -count;
  185. if ((dsize -= count) < 0 ||
  186. (ssize -= count + 1) < 0)
  187. return AVERROR_INVALIDDATA;
  188. while (count--)
  189. *d++ = *s++;
  190. } else {
  191. count++;
  192. if ((dsize -= count) < 0 ||
  193. (ssize -= 2) < 0)
  194. return AVERROR_INVALIDDATA;
  195. while (count--)
  196. *d++ = *s;
  197. s++;
  198. }
  199. }
  200. if (dend != d)
  201. return AVERROR_INVALIDDATA;
  202. return 0;
  203. }
  204. static int rle_uncompress(EXRContext *ctx, const uint8_t *src, int compressed_size,
  205. int uncompressed_size, EXRThreadData *td)
  206. {
  207. rle(td->tmp, src, compressed_size, uncompressed_size);
  208. av_assert1(uncompressed_size % 2 == 0);
  209. ctx->dsp.predictor(td->tmp, uncompressed_size);
  210. ctx->dsp.reorder_pixels(td->uncompressed_data, td->tmp, uncompressed_size);
  211. return 0;
  212. }
  213. #define USHORT_RANGE (1 << 16)
  214. #define BITMAP_SIZE (1 << 13)
  215. static uint16_t reverse_lut(const uint8_t *bitmap, uint16_t *lut)
  216. {
  217. int i, k = 0;
  218. for (i = 0; i < USHORT_RANGE; i++)
  219. if ((i == 0) || (bitmap[i >> 3] & (1 << (i & 7))))
  220. lut[k++] = i;
  221. i = k - 1;
  222. memset(lut + k, 0, (USHORT_RANGE - k) * 2);
  223. return i;
  224. }
  225. static void apply_lut(const uint16_t *lut, uint16_t *dst, int dsize)
  226. {
  227. int i;
  228. for (i = 0; i < dsize; ++i)
  229. dst[i] = lut[dst[i]];
  230. }
  231. #define HUF_ENCBITS 16 // literal (value) bit length
  232. #define HUF_ENCSIZE ((1 << HUF_ENCBITS) + 1) // encoding table size
  233. static void huf_canonical_code_table(uint64_t *freq)
  234. {
  235. uint64_t c, n[59] = { 0 };
  236. int i;
  237. for (i = 0; i < HUF_ENCSIZE; i++)
  238. n[freq[i]] += 1;
  239. c = 0;
  240. for (i = 58; i > 0; --i) {
  241. uint64_t nc = ((c + n[i]) >> 1);
  242. n[i] = c;
  243. c = nc;
  244. }
  245. for (i = 0; i < HUF_ENCSIZE; ++i) {
  246. int l = freq[i];
  247. if (l > 0)
  248. freq[i] = l | (n[l]++ << 6);
  249. }
  250. }
  251. #define SHORT_ZEROCODE_RUN 59
  252. #define LONG_ZEROCODE_RUN 63
  253. #define SHORTEST_LONG_RUN (2 + LONG_ZEROCODE_RUN - SHORT_ZEROCODE_RUN)
  254. #define LONGEST_LONG_RUN (255 + SHORTEST_LONG_RUN)
  255. static int huf_unpack_enc_table(GetByteContext *gb,
  256. int32_t im, int32_t iM, uint64_t *freq)
  257. {
  258. GetBitContext gbit;
  259. int ret = init_get_bits8(&gbit, gb->buffer, bytestream2_get_bytes_left(gb));
  260. if (ret < 0)
  261. return ret;
  262. for (; im <= iM; im++) {
  263. uint64_t l = freq[im] = get_bits(&gbit, 6);
  264. if (l == LONG_ZEROCODE_RUN) {
  265. int zerun = get_bits(&gbit, 8) + SHORTEST_LONG_RUN;
  266. if (im + zerun > iM + 1)
  267. return AVERROR_INVALIDDATA;
  268. while (zerun--)
  269. freq[im++] = 0;
  270. im--;
  271. } else if (l >= SHORT_ZEROCODE_RUN) {
  272. int zerun = l - SHORT_ZEROCODE_RUN + 2;
  273. if (im + zerun > iM + 1)
  274. return AVERROR_INVALIDDATA;
  275. while (zerun--)
  276. freq[im++] = 0;
  277. im--;
  278. }
  279. }
  280. bytestream2_skip(gb, (get_bits_count(&gbit) + 7) / 8);
  281. huf_canonical_code_table(freq);
  282. return 0;
  283. }
  284. static int huf_build_dec_table(EXRContext *s,
  285. EXRThreadData *td, int im, int iM)
  286. {
  287. int j = 0;
  288. td->run_sym = -1;
  289. for (int i = im; i < iM; i++) {
  290. td->he[j].sym = i;
  291. td->he[j].len = td->freq[i] & 63;
  292. td->he[j].code = td->freq[i] >> 6;
  293. if (td->he[j].len > 32) {
  294. avpriv_request_sample(s->avctx, "Too big code length");
  295. return AVERROR_PATCHWELCOME;
  296. }
  297. if (td->he[j].len > 0)
  298. j++;
  299. else
  300. td->run_sym = i;
  301. }
  302. if (im > 0)
  303. td->run_sym = 0;
  304. else if (iM < 65535)
  305. td->run_sym = 65535;
  306. if (td->run_sym == -1) {
  307. avpriv_request_sample(s->avctx, "No place for run symbol");
  308. return AVERROR_PATCHWELCOME;
  309. }
  310. td->he[j].sym = td->run_sym;
  311. td->he[j].len = td->freq[iM] & 63;
  312. if (td->he[j].len > 32) {
  313. avpriv_request_sample(s->avctx, "Too big code length");
  314. return AVERROR_PATCHWELCOME;
  315. }
  316. td->he[j].code = td->freq[iM] >> 6;
  317. j++;
  318. ff_free_vlc(&td->vlc);
  319. return ff_init_vlc_sparse(&td->vlc, 12, j,
  320. &td->he[0].len, sizeof(td->he[0]), sizeof(td->he[0].len),
  321. &td->he[0].code, sizeof(td->he[0]), sizeof(td->he[0].code),
  322. &td->he[0].sym, sizeof(td->he[0]), sizeof(td->he[0].sym), 0);
  323. }
  324. static int huf_decode(VLC *vlc, GetByteContext *gb, int nbits, int run_sym,
  325. int no, uint16_t *out)
  326. {
  327. GetBitContext gbit;
  328. int oe = 0;
  329. init_get_bits(&gbit, gb->buffer, nbits);
  330. while (get_bits_left(&gbit) > 0 && oe < no) {
  331. uint16_t x = get_vlc2(&gbit, vlc->table, 12, 2);
  332. if (x == run_sym) {
  333. int run = get_bits(&gbit, 8);
  334. uint16_t fill = out[oe - 1];
  335. while (run-- > 0)
  336. out[oe++] = fill;
  337. } else {
  338. out[oe++] = x;
  339. }
  340. }
  341. return 0;
  342. }
  343. static int huf_uncompress(EXRContext *s,
  344. EXRThreadData *td,
  345. GetByteContext *gb,
  346. uint16_t *dst, int dst_size)
  347. {
  348. int32_t im, iM;
  349. uint32_t nBits;
  350. int ret;
  351. im = bytestream2_get_le32(gb);
  352. iM = bytestream2_get_le32(gb);
  353. bytestream2_skip(gb, 4);
  354. nBits = bytestream2_get_le32(gb);
  355. if (im < 0 || im >= HUF_ENCSIZE ||
  356. iM < 0 || iM >= HUF_ENCSIZE)
  357. return AVERROR_INVALIDDATA;
  358. bytestream2_skip(gb, 4);
  359. if (!td->freq)
  360. td->freq = av_malloc_array(HUF_ENCSIZE, sizeof(*td->freq));
  361. if (!td->he)
  362. td->he = av_calloc(HUF_ENCSIZE, sizeof(*td->he));
  363. if (!td->freq || !td->he) {
  364. ret = AVERROR(ENOMEM);
  365. return ret;
  366. }
  367. memset(td->freq, 0, sizeof(*td->freq) * HUF_ENCSIZE);
  368. if ((ret = huf_unpack_enc_table(gb, im, iM, td->freq)) < 0)
  369. return ret;
  370. if (nBits > 8 * bytestream2_get_bytes_left(gb)) {
  371. ret = AVERROR_INVALIDDATA;
  372. return ret;
  373. }
  374. if ((ret = huf_build_dec_table(s, td, im, iM)) < 0)
  375. return ret;
  376. return huf_decode(&td->vlc, gb, nBits, td->run_sym, dst_size, dst);
  377. }
  378. static inline void wdec14(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
  379. {
  380. int16_t ls = l;
  381. int16_t hs = h;
  382. int hi = hs;
  383. int ai = ls + (hi & 1) + (hi >> 1);
  384. int16_t as = ai;
  385. int16_t bs = ai - hi;
  386. *a = as;
  387. *b = bs;
  388. }
  389. #define NBITS 16
  390. #define A_OFFSET (1 << (NBITS - 1))
  391. #define MOD_MASK ((1 << NBITS) - 1)
  392. static inline void wdec16(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
  393. {
  394. int m = l;
  395. int d = h;
  396. int bb = (m - (d >> 1)) & MOD_MASK;
  397. int aa = (d + bb - A_OFFSET) & MOD_MASK;
  398. *b = bb;
  399. *a = aa;
  400. }
  401. static void wav_decode(uint16_t *in, int nx, int ox,
  402. int ny, int oy, uint16_t mx)
  403. {
  404. int w14 = (mx < (1 << 14));
  405. int n = (nx > ny) ? ny : nx;
  406. int p = 1;
  407. int p2;
  408. while (p <= n)
  409. p <<= 1;
  410. p >>= 1;
  411. p2 = p;
  412. p >>= 1;
  413. while (p >= 1) {
  414. uint16_t *py = in;
  415. uint16_t *ey = in + oy * (ny - p2);
  416. uint16_t i00, i01, i10, i11;
  417. int oy1 = oy * p;
  418. int oy2 = oy * p2;
  419. int ox1 = ox * p;
  420. int ox2 = ox * p2;
  421. for (; py <= ey; py += oy2) {
  422. uint16_t *px = py;
  423. uint16_t *ex = py + ox * (nx - p2);
  424. for (; px <= ex; px += ox2) {
  425. uint16_t *p01 = px + ox1;
  426. uint16_t *p10 = px + oy1;
  427. uint16_t *p11 = p10 + ox1;
  428. if (w14) {
  429. wdec14(*px, *p10, &i00, &i10);
  430. wdec14(*p01, *p11, &i01, &i11);
  431. wdec14(i00, i01, px, p01);
  432. wdec14(i10, i11, p10, p11);
  433. } else {
  434. wdec16(*px, *p10, &i00, &i10);
  435. wdec16(*p01, *p11, &i01, &i11);
  436. wdec16(i00, i01, px, p01);
  437. wdec16(i10, i11, p10, p11);
  438. }
  439. }
  440. if (nx & p) {
  441. uint16_t *p10 = px + oy1;
  442. if (w14)
  443. wdec14(*px, *p10, &i00, p10);
  444. else
  445. wdec16(*px, *p10, &i00, p10);
  446. *px = i00;
  447. }
  448. }
  449. if (ny & p) {
  450. uint16_t *px = py;
  451. uint16_t *ex = py + ox * (nx - p2);
  452. for (; px <= ex; px += ox2) {
  453. uint16_t *p01 = px + ox1;
  454. if (w14)
  455. wdec14(*px, *p01, &i00, p01);
  456. else
  457. wdec16(*px, *p01, &i00, p01);
  458. *px = i00;
  459. }
  460. }
  461. p2 = p;
  462. p >>= 1;
  463. }
  464. }
  465. static int piz_uncompress(EXRContext *s, const uint8_t *src, int ssize,
  466. int dsize, EXRThreadData *td)
  467. {
  468. GetByteContext gb;
  469. uint16_t maxval, min_non_zero, max_non_zero;
  470. uint16_t *ptr;
  471. uint16_t *tmp = (uint16_t *)td->tmp;
  472. uint16_t *out;
  473. uint16_t *in;
  474. int ret, i, j;
  475. int pixel_half_size;/* 1 for half, 2 for float and uint32 */
  476. EXRChannel *channel;
  477. int tmp_offset;
  478. if (!td->bitmap)
  479. td->bitmap = av_malloc(BITMAP_SIZE);
  480. if (!td->lut)
  481. td->lut = av_malloc(1 << 17);
  482. if (!td->bitmap || !td->lut) {
  483. av_freep(&td->bitmap);
  484. av_freep(&td->lut);
  485. return AVERROR(ENOMEM);
  486. }
  487. bytestream2_init(&gb, src, ssize);
  488. min_non_zero = bytestream2_get_le16(&gb);
  489. max_non_zero = bytestream2_get_le16(&gb);
  490. if (max_non_zero >= BITMAP_SIZE)
  491. return AVERROR_INVALIDDATA;
  492. memset(td->bitmap, 0, FFMIN(min_non_zero, BITMAP_SIZE));
  493. if (min_non_zero <= max_non_zero)
  494. bytestream2_get_buffer(&gb, td->bitmap + min_non_zero,
  495. max_non_zero - min_non_zero + 1);
  496. memset(td->bitmap + max_non_zero + 1, 0, BITMAP_SIZE - max_non_zero - 1);
  497. maxval = reverse_lut(td->bitmap, td->lut);
  498. bytestream2_skip(&gb, 4);
  499. ret = huf_uncompress(s, td, &gb, tmp, dsize / sizeof(uint16_t));
  500. if (ret)
  501. return ret;
  502. ptr = tmp;
  503. for (i = 0; i < s->nb_channels; i++) {
  504. channel = &s->channels[i];
  505. if (channel->pixel_type == EXR_HALF)
  506. pixel_half_size = 1;
  507. else
  508. pixel_half_size = 2;
  509. for (j = 0; j < pixel_half_size; j++)
  510. wav_decode(ptr + j, td->xsize, pixel_half_size, td->ysize,
  511. td->xsize * pixel_half_size, maxval);
  512. ptr += td->xsize * td->ysize * pixel_half_size;
  513. }
  514. apply_lut(td->lut, tmp, dsize / sizeof(uint16_t));
  515. out = (uint16_t *)td->uncompressed_data;
  516. for (i = 0; i < td->ysize; i++) {
  517. tmp_offset = 0;
  518. for (j = 0; j < s->nb_channels; j++) {
  519. channel = &s->channels[j];
  520. if (channel->pixel_type == EXR_HALF)
  521. pixel_half_size = 1;
  522. else
  523. pixel_half_size = 2;
  524. in = tmp + tmp_offset * td->xsize * td->ysize + i * td->xsize * pixel_half_size;
  525. tmp_offset += pixel_half_size;
  526. #if HAVE_BIGENDIAN
  527. s->bbdsp.bswap16_buf(out, in, td->xsize * pixel_half_size);
  528. #else
  529. memcpy(out, in, td->xsize * 2 * pixel_half_size);
  530. #endif
  531. out += td->xsize * pixel_half_size;
  532. }
  533. }
  534. return 0;
  535. }
  536. static int pxr24_uncompress(EXRContext *s, const uint8_t *src,
  537. int compressed_size, int uncompressed_size,
  538. EXRThreadData *td)
  539. {
  540. unsigned long dest_len, expected_len = 0;
  541. const uint8_t *in = td->tmp;
  542. uint8_t *out;
  543. int c, i, j;
  544. for (i = 0; i < s->nb_channels; i++) {
  545. if (s->channels[i].pixel_type == EXR_FLOAT) {
  546. expected_len += (td->xsize * td->ysize * 3);/* PRX 24 store float in 24 bit instead of 32 */
  547. } else if (s->channels[i].pixel_type == EXR_HALF) {
  548. expected_len += (td->xsize * td->ysize * 2);
  549. } else {//UINT 32
  550. expected_len += (td->xsize * td->ysize * 4);
  551. }
  552. }
  553. dest_len = expected_len;
  554. if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK) {
  555. return AVERROR_INVALIDDATA;
  556. } else if (dest_len != expected_len) {
  557. return AVERROR_INVALIDDATA;
  558. }
  559. out = td->uncompressed_data;
  560. for (i = 0; i < td->ysize; i++)
  561. for (c = 0; c < s->nb_channels; c++) {
  562. EXRChannel *channel = &s->channels[c];
  563. const uint8_t *ptr[4];
  564. uint32_t pixel = 0;
  565. switch (channel->pixel_type) {
  566. case EXR_FLOAT:
  567. ptr[0] = in;
  568. ptr[1] = ptr[0] + td->xsize;
  569. ptr[2] = ptr[1] + td->xsize;
  570. in = ptr[2] + td->xsize;
  571. for (j = 0; j < td->xsize; ++j) {
  572. uint32_t diff = ((unsigned)*(ptr[0]++) << 24) |
  573. (*(ptr[1]++) << 16) |
  574. (*(ptr[2]++) << 8);
  575. pixel += diff;
  576. bytestream_put_le32(&out, pixel);
  577. }
  578. break;
  579. case EXR_HALF:
  580. ptr[0] = in;
  581. ptr[1] = ptr[0] + td->xsize;
  582. in = ptr[1] + td->xsize;
  583. for (j = 0; j < td->xsize; j++) {
  584. uint32_t diff = (*(ptr[0]++) << 8) | *(ptr[1]++);
  585. pixel += diff;
  586. bytestream_put_le16(&out, pixel);
  587. }
  588. break;
  589. case EXR_UINT:
  590. ptr[0] = in;
  591. ptr[1] = ptr[0] + s->xdelta;
  592. ptr[2] = ptr[1] + s->xdelta;
  593. ptr[3] = ptr[2] + s->xdelta;
  594. in = ptr[3] + s->xdelta;
  595. for (j = 0; j < s->xdelta; ++j) {
  596. uint32_t diff = ((uint32_t)*(ptr[0]++) << 24) |
  597. (*(ptr[1]++) << 16) |
  598. (*(ptr[2]++) << 8 ) |
  599. (*(ptr[3]++));
  600. pixel += diff;
  601. bytestream_put_le32(&out, pixel);
  602. }
  603. break;
  604. default:
  605. return AVERROR_INVALIDDATA;
  606. }
  607. }
  608. return 0;
  609. }
  610. static void unpack_14(const uint8_t b[14], uint16_t s[16])
  611. {
  612. unsigned short shift = (b[ 2] >> 2) & 15;
  613. unsigned short bias = (0x20 << shift);
  614. int i;
  615. s[ 0] = (b[0] << 8) | b[1];
  616. s[ 4] = s[ 0] + ((((b[ 2] << 4) | (b[ 3] >> 4)) & 0x3f) << shift) - bias;
  617. s[ 8] = s[ 4] + ((((b[ 3] << 2) | (b[ 4] >> 6)) & 0x3f) << shift) - bias;
  618. s[12] = s[ 8] + ((b[ 4] & 0x3f) << shift) - bias;
  619. s[ 1] = s[ 0] + ((b[ 5] >> 2) << shift) - bias;
  620. s[ 5] = s[ 4] + ((((b[ 5] << 4) | (b[ 6] >> 4)) & 0x3f) << shift) - bias;
  621. s[ 9] = s[ 8] + ((((b[ 6] << 2) | (b[ 7] >> 6)) & 0x3f) << shift) - bias;
  622. s[13] = s[12] + ((b[ 7] & 0x3f) << shift) - bias;
  623. s[ 2] = s[ 1] + ((b[ 8] >> 2) << shift) - bias;
  624. s[ 6] = s[ 5] + ((((b[ 8] << 4) | (b[ 9] >> 4)) & 0x3f) << shift) - bias;
  625. s[10] = s[ 9] + ((((b[ 9] << 2) | (b[10] >> 6)) & 0x3f) << shift) - bias;
  626. s[14] = s[13] + ((b[10] & 0x3f) << shift) - bias;
  627. s[ 3] = s[ 2] + ((b[11] >> 2) << shift) - bias;
  628. s[ 7] = s[ 6] + ((((b[11] << 4) | (b[12] >> 4)) & 0x3f) << shift) - bias;
  629. s[11] = s[10] + ((((b[12] << 2) | (b[13] >> 6)) & 0x3f) << shift) - bias;
  630. s[15] = s[14] + ((b[13] & 0x3f) << shift) - bias;
  631. for (i = 0; i < 16; ++i) {
  632. if (s[i] & 0x8000)
  633. s[i] &= 0x7fff;
  634. else
  635. s[i] = ~s[i];
  636. }
  637. }
  638. static void unpack_3(const uint8_t b[3], uint16_t s[16])
  639. {
  640. int i;
  641. s[0] = (b[0] << 8) | b[1];
  642. if (s[0] & 0x8000)
  643. s[0] &= 0x7fff;
  644. else
  645. s[0] = ~s[0];
  646. for (i = 1; i < 16; i++)
  647. s[i] = s[0];
  648. }
  649. static int b44_uncompress(EXRContext *s, const uint8_t *src, int compressed_size,
  650. int uncompressed_size, EXRThreadData *td) {
  651. const int8_t *sr = src;
  652. int stay_to_uncompress = compressed_size;
  653. int nb_b44_block_w, nb_b44_block_h;
  654. int index_tl_x, index_tl_y, index_out, index_tmp;
  655. uint16_t tmp_buffer[16]; /* B44 use 4x4 half float pixel */
  656. int c, iY, iX, y, x;
  657. int target_channel_offset = 0;
  658. /* calc B44 block count */
  659. nb_b44_block_w = td->xsize / 4;
  660. if ((td->xsize % 4) != 0)
  661. nb_b44_block_w++;
  662. nb_b44_block_h = td->ysize / 4;
  663. if ((td->ysize % 4) != 0)
  664. nb_b44_block_h++;
  665. for (c = 0; c < s->nb_channels; c++) {
  666. if (s->channels[c].pixel_type == EXR_HALF) {/* B44 only compress half float data */
  667. for (iY = 0; iY < nb_b44_block_h; iY++) {
  668. for (iX = 0; iX < nb_b44_block_w; iX++) {/* For each B44 block */
  669. if (stay_to_uncompress < 3) {
  670. av_log(s, AV_LOG_ERROR, "Not enough data for B44A block: %d", stay_to_uncompress);
  671. return AVERROR_INVALIDDATA;
  672. }
  673. if (src[compressed_size - stay_to_uncompress + 2] == 0xfc) { /* B44A block */
  674. unpack_3(sr, tmp_buffer);
  675. sr += 3;
  676. stay_to_uncompress -= 3;
  677. } else {/* B44 Block */
  678. if (stay_to_uncompress < 14) {
  679. av_log(s, AV_LOG_ERROR, "Not enough data for B44 block: %d", stay_to_uncompress);
  680. return AVERROR_INVALIDDATA;
  681. }
  682. unpack_14(sr, tmp_buffer);
  683. sr += 14;
  684. stay_to_uncompress -= 14;
  685. }
  686. /* copy data to uncompress buffer (B44 block can exceed target resolution)*/
  687. index_tl_x = iX * 4;
  688. index_tl_y = iY * 4;
  689. for (y = index_tl_y; y < FFMIN(index_tl_y + 4, td->ysize); y++) {
  690. for (x = index_tl_x; x < FFMIN(index_tl_x + 4, td->xsize); x++) {
  691. index_out = target_channel_offset * td->xsize + y * td->channel_line_size + 2 * x;
  692. index_tmp = (y-index_tl_y) * 4 + (x-index_tl_x);
  693. td->uncompressed_data[index_out] = tmp_buffer[index_tmp] & 0xff;
  694. td->uncompressed_data[index_out + 1] = tmp_buffer[index_tmp] >> 8;
  695. }
  696. }
  697. }
  698. }
  699. target_channel_offset += 2;
  700. } else {/* Float or UINT 32 channel */
  701. if (stay_to_uncompress < td->ysize * td->xsize * 4) {
  702. av_log(s, AV_LOG_ERROR, "Not enough data for uncompress channel: %d", stay_to_uncompress);
  703. return AVERROR_INVALIDDATA;
  704. }
  705. for (y = 0; y < td->ysize; y++) {
  706. index_out = target_channel_offset * td->xsize + y * td->channel_line_size;
  707. memcpy(&td->uncompressed_data[index_out], sr, td->xsize * 4);
  708. sr += td->xsize * 4;
  709. }
  710. target_channel_offset += 4;
  711. stay_to_uncompress -= td->ysize * td->xsize * 4;
  712. }
  713. }
  714. return 0;
  715. }
  716. static int ac_uncompress(EXRContext *s, GetByteContext *gb, float *block)
  717. {
  718. int ret = 0, n = 1;
  719. while (n < 64) {
  720. uint16_t val = bytestream2_get_ne16(gb);
  721. if (val == 0xff00) {
  722. n = 64;
  723. } else if ((val >> 8) == 0xff) {
  724. n += val & 0xff;
  725. } else {
  726. ret = n;
  727. block[ff_zigzag_direct[n]] = av_int2float(half2float(val,
  728. s->mantissatable,
  729. s->exponenttable,
  730. s->offsettable));
  731. n++;
  732. }
  733. }
  734. return ret;
  735. }
  736. static void idct_1d(float *blk, int step)
  737. {
  738. const float a = .5f * cosf( M_PI / 4.f);
  739. const float b = .5f * cosf( M_PI / 16.f);
  740. const float c = .5f * cosf( M_PI / 8.f);
  741. const float d = .5f * cosf(3.f*M_PI / 16.f);
  742. const float e = .5f * cosf(5.f*M_PI / 16.f);
  743. const float f = .5f * cosf(3.f*M_PI / 8.f);
  744. const float g = .5f * cosf(7.f*M_PI / 16.f);
  745. float alpha[4], beta[4], theta[4], gamma[4];
  746. alpha[0] = c * blk[2 * step];
  747. alpha[1] = f * blk[2 * step];
  748. alpha[2] = c * blk[6 * step];
  749. alpha[3] = f * blk[6 * step];
  750. beta[0] = b * blk[1 * step] + d * blk[3 * step] + e * blk[5 * step] + g * blk[7 * step];
  751. beta[1] = d * blk[1 * step] - g * blk[3 * step] - b * blk[5 * step] - e * blk[7 * step];
  752. beta[2] = e * blk[1 * step] - b * blk[3 * step] + g * blk[5 * step] + d * blk[7 * step];
  753. beta[3] = g * blk[1 * step] - e * blk[3 * step] + d * blk[5 * step] - b * blk[7 * step];
  754. theta[0] = a * (blk[0 * step] + blk[4 * step]);
  755. theta[3] = a * (blk[0 * step] - blk[4 * step]);
  756. theta[1] = alpha[0] + alpha[3];
  757. theta[2] = alpha[1] - alpha[2];
  758. gamma[0] = theta[0] + theta[1];
  759. gamma[1] = theta[3] + theta[2];
  760. gamma[2] = theta[3] - theta[2];
  761. gamma[3] = theta[0] - theta[1];
  762. blk[0 * step] = gamma[0] + beta[0];
  763. blk[1 * step] = gamma[1] + beta[1];
  764. blk[2 * step] = gamma[2] + beta[2];
  765. blk[3 * step] = gamma[3] + beta[3];
  766. blk[4 * step] = gamma[3] - beta[3];
  767. blk[5 * step] = gamma[2] - beta[2];
  768. blk[6 * step] = gamma[1] - beta[1];
  769. blk[7 * step] = gamma[0] - beta[0];
  770. }
  771. static void dct_inverse(float *block)
  772. {
  773. for (int i = 0; i < 8; i++)
  774. idct_1d(block + i, 8);
  775. for (int i = 0; i < 8; i++) {
  776. idct_1d(block, 1);
  777. block += 8;
  778. }
  779. }
  780. static void convert(float y, float u, float v,
  781. float *b, float *g, float *r)
  782. {
  783. *r = y + 1.5747f * v;
  784. *g = y - 0.1873f * u - 0.4682f * v;
  785. *b = y + 1.8556f * u;
  786. }
  787. static float to_linear(float x, float scale)
  788. {
  789. float ax = fabsf(x);
  790. if (ax <= 1.f) {
  791. return FFSIGN(x) * powf(ax, 2.2f * scale);
  792. } else {
  793. const float log_base = expf(2.2f * scale);
  794. return FFSIGN(x) * powf(log_base, ax - 1.f);
  795. }
  796. }
  797. static int dwa_uncompress(EXRContext *s, const uint8_t *src, int compressed_size,
  798. int uncompressed_size, EXRThreadData *td)
  799. {
  800. int64_t version, lo_usize, lo_size;
  801. int64_t ac_size, dc_size, rle_usize, rle_csize, rle_raw_size;
  802. int64_t ac_count, dc_count, ac_compression;
  803. const int dc_w = td->xsize >> 3;
  804. const int dc_h = td->ysize >> 3;
  805. GetByteContext gb, agb;
  806. int skip, ret;
  807. if (compressed_size <= 88)
  808. return AVERROR_INVALIDDATA;
  809. version = AV_RL64(src + 0);
  810. if (version != 2)
  811. return AVERROR_INVALIDDATA;
  812. lo_usize = AV_RL64(src + 8);
  813. lo_size = AV_RL64(src + 16);
  814. ac_size = AV_RL64(src + 24);
  815. dc_size = AV_RL64(src + 32);
  816. rle_csize = AV_RL64(src + 40);
  817. rle_usize = AV_RL64(src + 48);
  818. rle_raw_size = AV_RL64(src + 56);
  819. ac_count = AV_RL64(src + 64);
  820. dc_count = AV_RL64(src + 72);
  821. ac_compression = AV_RL64(src + 80);
  822. if (compressed_size < 88LL + lo_size + ac_size + dc_size + rle_csize)
  823. return AVERROR_INVALIDDATA;
  824. bytestream2_init(&gb, src + 88, compressed_size - 88);
  825. skip = bytestream2_get_le16(&gb);
  826. if (skip < 2)
  827. return AVERROR_INVALIDDATA;
  828. bytestream2_skip(&gb, skip - 2);
  829. if (lo_size > 0) {
  830. if (lo_usize > uncompressed_size)
  831. return AVERROR_INVALIDDATA;
  832. bytestream2_skip(&gb, lo_size);
  833. }
  834. if (ac_size > 0) {
  835. unsigned long dest_len = ac_count * 2LL;
  836. GetByteContext agb = gb;
  837. if (ac_count > 3LL * td->xsize * s->scan_lines_per_block)
  838. return AVERROR_INVALIDDATA;
  839. av_fast_padded_malloc(&td->ac_data, &td->ac_size, dest_len);
  840. if (!td->ac_data)
  841. return AVERROR(ENOMEM);
  842. switch (ac_compression) {
  843. case 0:
  844. ret = huf_uncompress(s, td, &agb, (int16_t *)td->ac_data, ac_count);
  845. if (ret < 0)
  846. return ret;
  847. break;
  848. case 1:
  849. if (uncompress(td->ac_data, &dest_len, agb.buffer, ac_size) != Z_OK ||
  850. dest_len != ac_count * 2LL)
  851. return AVERROR_INVALIDDATA;
  852. break;
  853. default:
  854. return AVERROR_INVALIDDATA;
  855. }
  856. bytestream2_skip(&gb, ac_size);
  857. }
  858. if (dc_size > 0) {
  859. unsigned long dest_len = dc_count * 2LL;
  860. GetByteContext agb = gb;
  861. if (dc_count > (6LL * td->xsize * td->ysize + 63) / 64)
  862. return AVERROR_INVALIDDATA;
  863. av_fast_padded_malloc(&td->dc_data, &td->dc_size, FFALIGN(dest_len, 64) * 2);
  864. if (!td->dc_data)
  865. return AVERROR(ENOMEM);
  866. if (uncompress(td->dc_data + FFALIGN(dest_len, 64), &dest_len, agb.buffer, dc_size) != Z_OK ||
  867. (dest_len != dc_count * 2LL))
  868. return AVERROR_INVALIDDATA;
  869. s->dsp.predictor(td->dc_data + FFALIGN(dest_len, 64), dest_len);
  870. s->dsp.reorder_pixels(td->dc_data, td->dc_data + FFALIGN(dest_len, 64), dest_len);
  871. bytestream2_skip(&gb, dc_size);
  872. }
  873. if (rle_raw_size > 0 && rle_csize > 0 && rle_usize > 0) {
  874. unsigned long dest_len = rle_usize;
  875. av_fast_padded_malloc(&td->rle_data, &td->rle_size, rle_usize);
  876. if (!td->rle_data)
  877. return AVERROR(ENOMEM);
  878. av_fast_padded_malloc(&td->rle_raw_data, &td->rle_raw_size, rle_raw_size);
  879. if (!td->rle_raw_data)
  880. return AVERROR(ENOMEM);
  881. if (uncompress(td->rle_data, &dest_len, gb.buffer, rle_csize) != Z_OK ||
  882. (dest_len != rle_usize))
  883. return AVERROR_INVALIDDATA;
  884. ret = rle(td->rle_raw_data, td->rle_data, rle_usize, rle_raw_size);
  885. if (ret < 0)
  886. return ret;
  887. bytestream2_skip(&gb, rle_csize);
  888. }
  889. bytestream2_init(&agb, td->ac_data, ac_count * 2);
  890. for (int y = 0; y < td->ysize; y += 8) {
  891. for (int x = 0; x < td->xsize; x += 8) {
  892. memset(td->block, 0, sizeof(td->block));
  893. for (int j = 0; j < 3; j++) {
  894. float *block = td->block[j];
  895. const int idx = (x >> 3) + (y >> 3) * dc_w + dc_w * dc_h * j;
  896. uint16_t *dc = (uint16_t *)td->dc_data;
  897. union av_intfloat32 dc_val;
  898. dc_val.i = half2float(dc[idx], s->mantissatable,
  899. s->exponenttable, s->offsettable);
  900. block[0] = dc_val.f;
  901. ac_uncompress(s, &agb, block);
  902. dct_inverse(block);
  903. }
  904. {
  905. const float scale = s->pixel_type == EXR_FLOAT ? 2.f : 1.f;
  906. const int o = s->nb_channels == 4;
  907. float *bo = ((float *)td->uncompressed_data) +
  908. y * td->xsize * s->nb_channels + td->xsize * (o + 0) + x;
  909. float *go = ((float *)td->uncompressed_data) +
  910. y * td->xsize * s->nb_channels + td->xsize * (o + 1) + x;
  911. float *ro = ((float *)td->uncompressed_data) +
  912. y * td->xsize * s->nb_channels + td->xsize * (o + 2) + x;
  913. float *yb = td->block[0];
  914. float *ub = td->block[1];
  915. float *vb = td->block[2];
  916. for (int yy = 0; yy < 8; yy++) {
  917. for (int xx = 0; xx < 8; xx++) {
  918. const int idx = xx + yy * 8;
  919. convert(yb[idx], ub[idx], vb[idx], &bo[xx], &go[xx], &ro[xx]);
  920. bo[xx] = to_linear(bo[xx], scale);
  921. go[xx] = to_linear(go[xx], scale);
  922. ro[xx] = to_linear(ro[xx], scale);
  923. }
  924. bo += td->xsize * s->nb_channels;
  925. go += td->xsize * s->nb_channels;
  926. ro += td->xsize * s->nb_channels;
  927. }
  928. }
  929. }
  930. }
  931. if (s->nb_channels < 4)
  932. return 0;
  933. for (int y = 0; y < td->ysize && td->rle_raw_data; y++) {
  934. uint32_t *ao = ((uint32_t *)td->uncompressed_data) + y * td->xsize * s->nb_channels;
  935. uint8_t *ai0 = td->rle_raw_data + y * td->xsize;
  936. uint8_t *ai1 = td->rle_raw_data + y * td->xsize + rle_raw_size / 2;
  937. for (int x = 0; x < td->xsize; x++) {
  938. uint16_t ha = ai0[x] | (ai1[x] << 8);
  939. ao[x] = half2float(ha, s->mantissatable, s->exponenttable, s->offsettable);
  940. }
  941. }
  942. return 0;
  943. }
  944. static int decode_block(AVCodecContext *avctx, void *tdata,
  945. int jobnr, int threadnr)
  946. {
  947. EXRContext *s = avctx->priv_data;
  948. AVFrame *const p = s->picture;
  949. EXRThreadData *td = &s->thread_data[threadnr];
  950. const uint8_t *channel_buffer[4] = { 0 };
  951. const uint8_t *buf = s->buf;
  952. uint64_t line_offset, uncompressed_size;
  953. uint8_t *ptr;
  954. uint32_t data_size;
  955. int line, col = 0;
  956. uint64_t tile_x, tile_y, tile_level_x, tile_level_y;
  957. const uint8_t *src;
  958. int step = s->desc->flags & AV_PIX_FMT_FLAG_FLOAT ? 4 : 2 * s->desc->nb_components;
  959. int bxmin = 0, axmax = 0, window_xoffset = 0;
  960. int window_xmin, window_xmax, window_ymin, window_ymax;
  961. int data_xoffset, data_yoffset, data_window_offset, xsize, ysize;
  962. int i, x, buf_size = s->buf_size;
  963. int c, rgb_channel_count;
  964. float one_gamma = 1.0f / s->gamma;
  965. avpriv_trc_function trc_func = avpriv_get_trc_function_from_trc(s->apply_trc_type);
  966. int ret;
  967. line_offset = AV_RL64(s->gb.buffer + jobnr * 8);
  968. if (s->is_tile) {
  969. if (buf_size < 20 || line_offset > buf_size - 20)
  970. return AVERROR_INVALIDDATA;
  971. src = buf + line_offset + 20;
  972. if (s->is_multipart)
  973. src += 4;
  974. tile_x = AV_RL32(src - 20);
  975. tile_y = AV_RL32(src - 16);
  976. tile_level_x = AV_RL32(src - 12);
  977. tile_level_y = AV_RL32(src - 8);
  978. data_size = AV_RL32(src - 4);
  979. if (data_size <= 0 || data_size > buf_size - line_offset - 20)
  980. return AVERROR_INVALIDDATA;
  981. if (tile_level_x || tile_level_y) { /* tile level, is not the full res level */
  982. avpriv_report_missing_feature(s->avctx, "Subres tile before full res tile");
  983. return AVERROR_PATCHWELCOME;
  984. }
  985. line = s->ymin + s->tile_attr.ySize * tile_y;
  986. col = s->tile_attr.xSize * tile_x;
  987. if (line < s->ymin || line > s->ymax ||
  988. s->xmin + col < s->xmin || s->xmin + col > s->xmax)
  989. return AVERROR_INVALIDDATA;
  990. td->ysize = FFMIN(s->tile_attr.ySize, s->ydelta - tile_y * s->tile_attr.ySize);
  991. td->xsize = FFMIN(s->tile_attr.xSize, s->xdelta - tile_x * s->tile_attr.xSize);
  992. if (td->xsize * (uint64_t)s->current_channel_offset > INT_MAX)
  993. return AVERROR_INVALIDDATA;
  994. td->channel_line_size = td->xsize * s->current_channel_offset;/* uncompress size of one line */
  995. uncompressed_size = td->channel_line_size * (uint64_t)td->ysize;/* uncompress size of the block */
  996. } else {
  997. if (buf_size < 8 || line_offset > buf_size - 8)
  998. return AVERROR_INVALIDDATA;
  999. src = buf + line_offset + 8;
  1000. if (s->is_multipart)
  1001. src += 4;
  1002. line = AV_RL32(src - 8);
  1003. if (line < s->ymin || line > s->ymax)
  1004. return AVERROR_INVALIDDATA;
  1005. data_size = AV_RL32(src - 4);
  1006. if (data_size <= 0 || data_size > buf_size - line_offset - 8)
  1007. return AVERROR_INVALIDDATA;
  1008. td->ysize = FFMIN(s->scan_lines_per_block, s->ymax - line + 1); /* s->ydelta - line ?? */
  1009. td->xsize = s->xdelta;
  1010. if (td->xsize * (uint64_t)s->current_channel_offset > INT_MAX)
  1011. return AVERROR_INVALIDDATA;
  1012. td->channel_line_size = td->xsize * s->current_channel_offset;/* uncompress size of one line */
  1013. uncompressed_size = td->channel_line_size * (uint64_t)td->ysize;/* uncompress size of the block */
  1014. if ((s->compression == EXR_RAW && (data_size != uncompressed_size ||
  1015. line_offset > buf_size - uncompressed_size)) ||
  1016. (s->compression != EXR_RAW && (data_size > uncompressed_size ||
  1017. line_offset > buf_size - data_size))) {
  1018. return AVERROR_INVALIDDATA;
  1019. }
  1020. }
  1021. window_xmin = FFMIN(avctx->width, FFMAX(0, s->xmin + col));
  1022. window_xmax = FFMIN(avctx->width, FFMAX(0, s->xmin + col + td->xsize));
  1023. window_ymin = FFMIN(avctx->height, FFMAX(0, line ));
  1024. window_ymax = FFMIN(avctx->height, FFMAX(0, line + td->ysize));
  1025. xsize = window_xmax - window_xmin;
  1026. ysize = window_ymax - window_ymin;
  1027. /* tile or scanline not visible skip decoding */
  1028. if (xsize <= 0 || ysize <= 0)
  1029. return 0;
  1030. /* is the first tile or is a scanline */
  1031. if(col == 0) {
  1032. window_xmin = 0;
  1033. /* pixels to add at the left of the display window */
  1034. window_xoffset = FFMAX(0, s->xmin);
  1035. /* bytes to add at the left of the display window */
  1036. bxmin = window_xoffset * step;
  1037. }
  1038. /* is the last tile or is a scanline */
  1039. if(col + td->xsize == s->xdelta) {
  1040. window_xmax = avctx->width;
  1041. /* bytes to add at the right of the display window */
  1042. axmax = FFMAX(0, (avctx->width - (s->xmax + 1))) * step;
  1043. }
  1044. if (data_size < uncompressed_size || s->is_tile) { /* td->tmp is use for tile reorganization */
  1045. av_fast_padded_malloc(&td->tmp, &td->tmp_size, uncompressed_size);
  1046. if (!td->tmp)
  1047. return AVERROR(ENOMEM);
  1048. }
  1049. if (data_size < uncompressed_size) {
  1050. av_fast_padded_malloc(&td->uncompressed_data,
  1051. &td->uncompressed_size, uncompressed_size + 64);/* Force 64 padding for AVX2 reorder_pixels dst */
  1052. if (!td->uncompressed_data)
  1053. return AVERROR(ENOMEM);
  1054. ret = AVERROR_INVALIDDATA;
  1055. switch (s->compression) {
  1056. case EXR_ZIP1:
  1057. case EXR_ZIP16:
  1058. ret = zip_uncompress(s, src, data_size, uncompressed_size, td);
  1059. break;
  1060. case EXR_PIZ:
  1061. ret = piz_uncompress(s, src, data_size, uncompressed_size, td);
  1062. break;
  1063. case EXR_PXR24:
  1064. ret = pxr24_uncompress(s, src, data_size, uncompressed_size, td);
  1065. break;
  1066. case EXR_RLE:
  1067. ret = rle_uncompress(s, src, data_size, uncompressed_size, td);
  1068. break;
  1069. case EXR_B44:
  1070. case EXR_B44A:
  1071. ret = b44_uncompress(s, src, data_size, uncompressed_size, td);
  1072. break;
  1073. case EXR_DWAA:
  1074. case EXR_DWAB:
  1075. ret = dwa_uncompress(s, src, data_size, uncompressed_size, td);
  1076. break;
  1077. }
  1078. if (ret < 0) {
  1079. av_log(avctx, AV_LOG_ERROR, "decode_block() failed.\n");
  1080. return ret;
  1081. }
  1082. src = td->uncompressed_data;
  1083. }
  1084. /* offsets to crop data outside display window */
  1085. data_xoffset = FFABS(FFMIN(0, s->xmin + col)) * (s->pixel_type == EXR_HALF ? 2 : 4);
  1086. data_yoffset = FFABS(FFMIN(0, line));
  1087. data_window_offset = (data_yoffset * td->channel_line_size) + data_xoffset;
  1088. if (!s->is_luma) {
  1089. channel_buffer[0] = src + (td->xsize * s->channel_offsets[0]) + data_window_offset;
  1090. channel_buffer[1] = src + (td->xsize * s->channel_offsets[1]) + data_window_offset;
  1091. channel_buffer[2] = src + (td->xsize * s->channel_offsets[2]) + data_window_offset;
  1092. rgb_channel_count = 3;
  1093. } else { /* put y data in the first channel_buffer */
  1094. channel_buffer[0] = src + (td->xsize * s->channel_offsets[1]) + data_window_offset;
  1095. rgb_channel_count = 1;
  1096. }
  1097. if (s->channel_offsets[3] >= 0)
  1098. channel_buffer[3] = src + (td->xsize * s->channel_offsets[3]) + data_window_offset;
  1099. if (s->desc->flags & AV_PIX_FMT_FLAG_FLOAT) {
  1100. /* todo: change this when a floating point pixel format with luma with alpha is implemented */
  1101. int channel_count = s->channel_offsets[3] >= 0 ? 4 : rgb_channel_count;
  1102. if (s->is_luma) {
  1103. channel_buffer[1] = channel_buffer[0];
  1104. channel_buffer[2] = channel_buffer[0];
  1105. }
  1106. for (c = 0; c < channel_count; c++) {
  1107. int plane = s->desc->comp[c].plane;
  1108. ptr = p->data[plane] + window_ymin * p->linesize[plane] + (window_xmin * 4);
  1109. for (i = 0; i < ysize; i++, ptr += p->linesize[plane]) {
  1110. const uint8_t *src;
  1111. union av_intfloat32 *ptr_x;
  1112. src = channel_buffer[c];
  1113. ptr_x = (union av_intfloat32 *)ptr;
  1114. // Zero out the start if xmin is not 0
  1115. memset(ptr_x, 0, bxmin);
  1116. ptr_x += window_xoffset;
  1117. if (s->pixel_type == EXR_FLOAT ||
  1118. s->compression == EXR_DWAA ||
  1119. s->compression == EXR_DWAB) {
  1120. // 32-bit
  1121. union av_intfloat32 t;
  1122. if (trc_func && c < 3) {
  1123. for (x = 0; x < xsize; x++) {
  1124. t.i = bytestream_get_le32(&src);
  1125. t.f = trc_func(t.f);
  1126. *ptr_x++ = t;
  1127. }
  1128. } else if (one_gamma != 1.f) {
  1129. for (x = 0; x < xsize; x++) {
  1130. t.i = bytestream_get_le32(&src);
  1131. if (t.f > 0.0f && c < 3) /* avoid negative values */
  1132. t.f = powf(t.f, one_gamma);
  1133. *ptr_x++ = t;
  1134. }
  1135. } else {
  1136. for (x = 0; x < xsize; x++) {
  1137. t.i = bytestream_get_le32(&src);
  1138. *ptr_x++ = t;
  1139. }
  1140. }
  1141. } else if (s->pixel_type == EXR_HALF) {
  1142. // 16-bit
  1143. if (c < 3 || !trc_func) {
  1144. for (x = 0; x < xsize; x++) {
  1145. *ptr_x++ = s->gamma_table[bytestream_get_le16(&src)];
  1146. }
  1147. } else {
  1148. for (x = 0; x < xsize; x++) {
  1149. ptr_x[0].i = half2float(bytestream_get_le16(&src),
  1150. s->mantissatable,
  1151. s->exponenttable,
  1152. s->offsettable);
  1153. ptr_x++;
  1154. }
  1155. }
  1156. }
  1157. // Zero out the end if xmax+1 is not w
  1158. memset(ptr_x, 0, axmax);
  1159. channel_buffer[c] += td->channel_line_size;
  1160. }
  1161. }
  1162. } else {
  1163. av_assert1(s->pixel_type == EXR_UINT);
  1164. ptr = p->data[0] + window_ymin * p->linesize[0] + (window_xmin * s->desc->nb_components * 2);
  1165. for (i = 0; i < ysize; i++, ptr += p->linesize[0]) {
  1166. const uint8_t * a;
  1167. const uint8_t *rgb[3];
  1168. uint16_t *ptr_x;
  1169. for (c = 0; c < rgb_channel_count; c++) {
  1170. rgb[c] = channel_buffer[c];
  1171. }
  1172. if (channel_buffer[3])
  1173. a = channel_buffer[3];
  1174. ptr_x = (uint16_t *) ptr;
  1175. // Zero out the start if xmin is not 0
  1176. memset(ptr_x, 0, bxmin);
  1177. ptr_x += window_xoffset * s->desc->nb_components;
  1178. for (x = 0; x < xsize; x++) {
  1179. for (c = 0; c < rgb_channel_count; c++) {
  1180. *ptr_x++ = bytestream_get_le32(&rgb[c]) >> 16;
  1181. }
  1182. if (channel_buffer[3])
  1183. *ptr_x++ = bytestream_get_le32(&a) >> 16;
  1184. }
  1185. // Zero out the end if xmax+1 is not w
  1186. memset(ptr_x, 0, axmax);
  1187. channel_buffer[0] += td->channel_line_size;
  1188. channel_buffer[1] += td->channel_line_size;
  1189. channel_buffer[2] += td->channel_line_size;
  1190. if (channel_buffer[3])
  1191. channel_buffer[3] += td->channel_line_size;
  1192. }
  1193. }
  1194. return 0;
  1195. }
  1196. static void skip_header_chunk(EXRContext *s)
  1197. {
  1198. GetByteContext *gb = &s->gb;
  1199. while (bytestream2_get_bytes_left(gb) > 0) {
  1200. if (!bytestream2_peek_byte(gb))
  1201. break;
  1202. // Process unknown variables
  1203. for (int i = 0; i < 2; i++) // value_name and value_type
  1204. while (bytestream2_get_byte(gb) != 0);
  1205. // Skip variable length
  1206. bytestream2_skip(gb, bytestream2_get_le32(gb));
  1207. }
  1208. }
  1209. /**
  1210. * Check if the variable name corresponds to its data type.
  1211. *
  1212. * @param s the EXRContext
  1213. * @param value_name name of the variable to check
  1214. * @param value_type type of the variable to check
  1215. * @param minimum_length minimum length of the variable data
  1216. *
  1217. * @return bytes to read containing variable data
  1218. * -1 if variable is not found
  1219. * 0 if buffer ended prematurely
  1220. */
  1221. static int check_header_variable(EXRContext *s,
  1222. const char *value_name,
  1223. const char *value_type,
  1224. unsigned int minimum_length)
  1225. {
  1226. GetByteContext *gb = &s->gb;
  1227. int var_size = -1;
  1228. if (bytestream2_get_bytes_left(gb) >= minimum_length &&
  1229. !strcmp(gb->buffer, value_name)) {
  1230. // found value_name, jump to value_type (null terminated strings)
  1231. gb->buffer += strlen(value_name) + 1;
  1232. if (!strcmp(gb->buffer, value_type)) {
  1233. gb->buffer += strlen(value_type) + 1;
  1234. var_size = bytestream2_get_le32(gb);
  1235. // don't go read past boundaries
  1236. if (var_size > bytestream2_get_bytes_left(gb))
  1237. var_size = 0;
  1238. } else {
  1239. // value_type not found, reset the buffer
  1240. gb->buffer -= strlen(value_name) + 1;
  1241. av_log(s->avctx, AV_LOG_WARNING,
  1242. "Unknown data type %s for header variable %s.\n",
  1243. value_type, value_name);
  1244. }
  1245. }
  1246. return var_size;
  1247. }
  1248. static int decode_header(EXRContext *s, AVFrame *frame)
  1249. {
  1250. AVDictionary *metadata = NULL;
  1251. GetByteContext *gb = &s->gb;
  1252. int magic_number, version, flags;
  1253. int layer_match = 0;
  1254. int ret;
  1255. int dup_channels = 0;
  1256. s->current_channel_offset = 0;
  1257. s->xmin = ~0;
  1258. s->xmax = ~0;
  1259. s->ymin = ~0;
  1260. s->ymax = ~0;
  1261. s->xdelta = ~0;
  1262. s->ydelta = ~0;
  1263. s->channel_offsets[0] = -1;
  1264. s->channel_offsets[1] = -1;
  1265. s->channel_offsets[2] = -1;
  1266. s->channel_offsets[3] = -1;
  1267. s->pixel_type = EXR_UNKNOWN;
  1268. s->compression = EXR_UNKN;
  1269. s->nb_channels = 0;
  1270. s->w = 0;
  1271. s->h = 0;
  1272. s->tile_attr.xSize = -1;
  1273. s->tile_attr.ySize = -1;
  1274. s->is_tile = 0;
  1275. s->is_multipart = 0;
  1276. s->is_luma = 0;
  1277. s->current_part = 0;
  1278. if (bytestream2_get_bytes_left(gb) < 10) {
  1279. av_log(s->avctx, AV_LOG_ERROR, "Header too short to parse.\n");
  1280. return AVERROR_INVALIDDATA;
  1281. }
  1282. magic_number = bytestream2_get_le32(gb);
  1283. if (magic_number != 20000630) {
  1284. /* As per documentation of OpenEXR, it is supposed to be
  1285. * int 20000630 little-endian */
  1286. av_log(s->avctx, AV_LOG_ERROR, "Wrong magic number %d.\n", magic_number);
  1287. return AVERROR_INVALIDDATA;
  1288. }
  1289. version = bytestream2_get_byte(gb);
  1290. if (version != 2) {
  1291. avpriv_report_missing_feature(s->avctx, "Version %d", version);
  1292. return AVERROR_PATCHWELCOME;
  1293. }
  1294. flags = bytestream2_get_le24(gb);
  1295. if (flags & 0x02)
  1296. s->is_tile = 1;
  1297. if (flags & 0x10)
  1298. s->is_multipart = 1;
  1299. if (flags & 0x08) {
  1300. avpriv_report_missing_feature(s->avctx, "deep data");
  1301. return AVERROR_PATCHWELCOME;
  1302. }
  1303. // Parse the header
  1304. while (bytestream2_get_bytes_left(gb) > 0) {
  1305. int var_size;
  1306. while (s->is_multipart && s->current_part < s->selected_part &&
  1307. bytestream2_get_bytes_left(gb) > 0) {
  1308. if (bytestream2_peek_byte(gb)) {
  1309. skip_header_chunk(s);
  1310. } else {
  1311. bytestream2_skip(gb, 1);
  1312. if (!bytestream2_peek_byte(gb))
  1313. break;
  1314. }
  1315. bytestream2_skip(gb, 1);
  1316. s->current_part++;
  1317. }
  1318. if (!bytestream2_peek_byte(gb)) {
  1319. if (!s->is_multipart)
  1320. break;
  1321. bytestream2_skip(gb, 1);
  1322. if (s->current_part == s->selected_part) {
  1323. while (bytestream2_get_bytes_left(gb) > 0) {
  1324. if (bytestream2_peek_byte(gb)) {
  1325. skip_header_chunk(s);
  1326. } else {
  1327. bytestream2_skip(gb, 1);
  1328. if (!bytestream2_peek_byte(gb))
  1329. break;
  1330. }
  1331. }
  1332. }
  1333. if (!bytestream2_peek_byte(gb))
  1334. break;
  1335. s->current_part++;
  1336. }
  1337. if ((var_size = check_header_variable(s, "channels",
  1338. "chlist", 38)) >= 0) {
  1339. GetByteContext ch_gb;
  1340. if (!var_size) {
  1341. ret = AVERROR_INVALIDDATA;
  1342. goto fail;
  1343. }
  1344. bytestream2_init(&ch_gb, gb->buffer, var_size);
  1345. while (bytestream2_get_bytes_left(&ch_gb) >= 19) {
  1346. EXRChannel *channel;
  1347. enum ExrPixelType current_pixel_type;
  1348. int channel_index = -1;
  1349. int xsub, ysub;
  1350. if (strcmp(s->layer, "") != 0) {
  1351. if (strncmp(ch_gb.buffer, s->layer, strlen(s->layer)) == 0) {
  1352. layer_match = 1;
  1353. av_log(s->avctx, AV_LOG_INFO,
  1354. "Channel match layer : %s.\n", ch_gb.buffer);
  1355. ch_gb.buffer += strlen(s->layer);
  1356. if (*ch_gb.buffer == '.')
  1357. ch_gb.buffer++; /* skip dot if not given */
  1358. } else {
  1359. layer_match = 0;
  1360. av_log(s->avctx, AV_LOG_INFO,
  1361. "Channel doesn't match layer : %s.\n", ch_gb.buffer);
  1362. }
  1363. } else {
  1364. layer_match = 1;
  1365. }
  1366. if (layer_match) { /* only search channel if the layer match is valid */
  1367. if (!av_strcasecmp(ch_gb.buffer, "R") ||
  1368. !av_strcasecmp(ch_gb.buffer, "X") ||
  1369. !av_strcasecmp(ch_gb.buffer, "U")) {
  1370. channel_index = 0;
  1371. s->is_luma = 0;
  1372. } else if (!av_strcasecmp(ch_gb.buffer, "G") ||
  1373. !av_strcasecmp(ch_gb.buffer, "V")) {
  1374. channel_index = 1;
  1375. s->is_luma = 0;
  1376. } else if (!av_strcasecmp(ch_gb.buffer, "Y")) {
  1377. channel_index = 1;
  1378. s->is_luma = 1;
  1379. } else if (!av_strcasecmp(ch_gb.buffer, "B") ||
  1380. !av_strcasecmp(ch_gb.buffer, "Z") ||
  1381. !av_strcasecmp(ch_gb.buffer, "W")) {
  1382. channel_index = 2;
  1383. s->is_luma = 0;
  1384. } else if (!av_strcasecmp(ch_gb.buffer, "A")) {
  1385. channel_index = 3;
  1386. } else {
  1387. av_log(s->avctx, AV_LOG_WARNING,
  1388. "Unsupported channel %.256s.\n", ch_gb.buffer);
  1389. }
  1390. }
  1391. /* skip until you get a 0 */
  1392. while (bytestream2_get_bytes_left(&ch_gb) > 0 &&
  1393. bytestream2_get_byte(&ch_gb))
  1394. continue;
  1395. if (bytestream2_get_bytes_left(&ch_gb) < 4) {
  1396. av_log(s->avctx, AV_LOG_ERROR, "Incomplete header.\n");
  1397. ret = AVERROR_INVALIDDATA;
  1398. goto fail;
  1399. }
  1400. current_pixel_type = bytestream2_get_le32(&ch_gb);
  1401. if (current_pixel_type >= EXR_UNKNOWN) {
  1402. avpriv_report_missing_feature(s->avctx, "Pixel type %d",
  1403. current_pixel_type);
  1404. ret = AVERROR_PATCHWELCOME;
  1405. goto fail;
  1406. }
  1407. bytestream2_skip(&ch_gb, 4);
  1408. xsub = bytestream2_get_le32(&ch_gb);
  1409. ysub = bytestream2_get_le32(&ch_gb);
  1410. if (xsub != 1 || ysub != 1) {
  1411. avpriv_report_missing_feature(s->avctx,
  1412. "Subsampling %dx%d",
  1413. xsub, ysub);
  1414. ret = AVERROR_PATCHWELCOME;
  1415. goto fail;
  1416. }
  1417. if (channel_index >= 0 && s->channel_offsets[channel_index] == -1) { /* channel has not been previously assigned */
  1418. if (s->pixel_type != EXR_UNKNOWN &&
  1419. s->pixel_type != current_pixel_type) {
  1420. av_log(s->avctx, AV_LOG_ERROR,
  1421. "RGB channels not of the same depth.\n");
  1422. ret = AVERROR_INVALIDDATA;
  1423. goto fail;
  1424. }
  1425. s->pixel_type = current_pixel_type;
  1426. s->channel_offsets[channel_index] = s->current_channel_offset;
  1427. } else if (channel_index >= 0) {
  1428. av_log(s->avctx, AV_LOG_WARNING,
  1429. "Multiple channels with index %d.\n", channel_index);
  1430. if (++dup_channels > 10) {
  1431. ret = AVERROR_INVALIDDATA;
  1432. goto fail;
  1433. }
  1434. }
  1435. s->channels = av_realloc(s->channels,
  1436. ++s->nb_channels * sizeof(EXRChannel));
  1437. if (!s->channels) {
  1438. ret = AVERROR(ENOMEM);
  1439. goto fail;
  1440. }
  1441. channel = &s->channels[s->nb_channels - 1];
  1442. channel->pixel_type = current_pixel_type;
  1443. channel->xsub = xsub;
  1444. channel->ysub = ysub;
  1445. if (current_pixel_type == EXR_HALF) {
  1446. s->current_channel_offset += 2;
  1447. } else {/* Float or UINT32 */
  1448. s->current_channel_offset += 4;
  1449. }
  1450. }
  1451. /* Check if all channels are set with an offset or if the channels
  1452. * are causing an overflow */
  1453. if (!s->is_luma) {/* if we expected to have at least 3 channels */
  1454. if (FFMIN3(s->channel_offsets[0],
  1455. s->channel_offsets[1],
  1456. s->channel_offsets[2]) < 0) {
  1457. if (s->channel_offsets[0] < 0)
  1458. av_log(s->avctx, AV_LOG_ERROR, "Missing red channel.\n");
  1459. if (s->channel_offsets[1] < 0)
  1460. av_log(s->avctx, AV_LOG_ERROR, "Missing green channel.\n");
  1461. if (s->channel_offsets[2] < 0)
  1462. av_log(s->avctx, AV_LOG_ERROR, "Missing blue channel.\n");
  1463. ret = AVERROR_INVALIDDATA;
  1464. goto fail;
  1465. }
  1466. }
  1467. // skip one last byte and update main gb
  1468. gb->buffer = ch_gb.buffer + 1;
  1469. continue;
  1470. } else if ((var_size = check_header_variable(s, "dataWindow", "box2i",
  1471. 31)) >= 0) {
  1472. int xmin, ymin, xmax, ymax;
  1473. if (!var_size) {
  1474. ret = AVERROR_INVALIDDATA;
  1475. goto fail;
  1476. }
  1477. xmin = bytestream2_get_le32(gb);
  1478. ymin = bytestream2_get_le32(gb);
  1479. xmax = bytestream2_get_le32(gb);
  1480. ymax = bytestream2_get_le32(gb);
  1481. if (xmin > xmax || ymin > ymax ||
  1482. (unsigned)xmax - xmin >= INT_MAX ||
  1483. (unsigned)ymax - ymin >= INT_MAX) {
  1484. ret = AVERROR_INVALIDDATA;
  1485. goto fail;
  1486. }
  1487. s->xmin = xmin;
  1488. s->xmax = xmax;
  1489. s->ymin = ymin;
  1490. s->ymax = ymax;
  1491. s->xdelta = (s->xmax - s->xmin) + 1;
  1492. s->ydelta = (s->ymax - s->ymin) + 1;
  1493. continue;
  1494. } else if ((var_size = check_header_variable(s, "displayWindow",
  1495. "box2i", 34)) >= 0) {
  1496. int32_t sx, sy, dx, dy;
  1497. if (!var_size) {
  1498. ret = AVERROR_INVALIDDATA;
  1499. goto fail;
  1500. }
  1501. sx = bytestream2_get_le32(gb);
  1502. sy = bytestream2_get_le32(gb);
  1503. dx = bytestream2_get_le32(gb);
  1504. dy = bytestream2_get_le32(gb);
  1505. s->w = dx - sx + 1;
  1506. s->h = dy - sy + 1;
  1507. continue;
  1508. } else if ((var_size = check_header_variable(s, "lineOrder",
  1509. "lineOrder", 25)) >= 0) {
  1510. int line_order;
  1511. if (!var_size) {
  1512. ret = AVERROR_INVALIDDATA;
  1513. goto fail;
  1514. }
  1515. line_order = bytestream2_get_byte(gb);
  1516. av_log(s->avctx, AV_LOG_DEBUG, "line order: %d.\n", line_order);
  1517. if (line_order > 2) {
  1518. av_log(s->avctx, AV_LOG_ERROR, "Unknown line order.\n");
  1519. ret = AVERROR_INVALIDDATA;
  1520. goto fail;
  1521. }
  1522. continue;
  1523. } else if ((var_size = check_header_variable(s, "pixelAspectRatio",
  1524. "float", 31)) >= 0) {
  1525. if (!var_size) {
  1526. ret = AVERROR_INVALIDDATA;
  1527. goto fail;
  1528. }
  1529. s->sar = bytestream2_get_le32(gb);
  1530. continue;
  1531. } else if ((var_size = check_header_variable(s, "compression",
  1532. "compression", 29)) >= 0) {
  1533. if (!var_size) {
  1534. ret = AVERROR_INVALIDDATA;
  1535. goto fail;
  1536. }
  1537. if (s->compression == EXR_UNKN)
  1538. s->compression = bytestream2_get_byte(gb);
  1539. else {
  1540. bytestream2_skip(gb, 1);
  1541. av_log(s->avctx, AV_LOG_WARNING,
  1542. "Found more than one compression attribute.\n");
  1543. }
  1544. continue;
  1545. } else if ((var_size = check_header_variable(s, "tiles",
  1546. "tiledesc", 22)) >= 0) {
  1547. char tileLevel;
  1548. if (!s->is_tile)
  1549. av_log(s->avctx, AV_LOG_WARNING,
  1550. "Found tile attribute and scanline flags. Exr will be interpreted as scanline.\n");
  1551. s->tile_attr.xSize = bytestream2_get_le32(gb);
  1552. s->tile_attr.ySize = bytestream2_get_le32(gb);
  1553. tileLevel = bytestream2_get_byte(gb);
  1554. s->tile_attr.level_mode = tileLevel & 0x0f;
  1555. s->tile_attr.level_round = (tileLevel >> 4) & 0x0f;
  1556. if (s->tile_attr.level_mode >= EXR_TILE_LEVEL_UNKNOWN) {
  1557. avpriv_report_missing_feature(s->avctx, "Tile level mode %d",
  1558. s->tile_attr.level_mode);
  1559. ret = AVERROR_PATCHWELCOME;
  1560. goto fail;
  1561. }
  1562. if (s->tile_attr.level_round >= EXR_TILE_ROUND_UNKNOWN) {
  1563. avpriv_report_missing_feature(s->avctx, "Tile level round %d",
  1564. s->tile_attr.level_round);
  1565. ret = AVERROR_PATCHWELCOME;
  1566. goto fail;
  1567. }
  1568. continue;
  1569. } else if ((var_size = check_header_variable(s, "writer",
  1570. "string", 1)) >= 0) {
  1571. uint8_t key[256] = { 0 };
  1572. bytestream2_get_buffer(gb, key, FFMIN(sizeof(key) - 1, var_size));
  1573. av_dict_set(&metadata, "writer", key, 0);
  1574. continue;
  1575. } else if ((var_size = check_header_variable(s, "framesPerSecond",
  1576. "rational", 33)) >= 0) {
  1577. if (!var_size) {
  1578. ret = AVERROR_INVALIDDATA;
  1579. goto fail;
  1580. }
  1581. s->avctx->framerate.num = bytestream2_get_le32(gb);
  1582. s->avctx->framerate.den = bytestream2_get_le32(gb);
  1583. continue;
  1584. } else if ((var_size = check_header_variable(s, "chunkCount",
  1585. "int", 23)) >= 0) {
  1586. s->chunk_count = bytestream2_get_le32(gb);
  1587. continue;
  1588. } else if ((var_size = check_header_variable(s, "type",
  1589. "string", 16)) >= 0) {
  1590. uint8_t key[256] = { 0 };
  1591. bytestream2_get_buffer(gb, key, FFMIN(sizeof(key) - 1, var_size));
  1592. if (strncmp("scanlineimage", key, var_size) &&
  1593. strncmp("tiledimage", key, var_size))
  1594. return AVERROR_PATCHWELCOME;
  1595. continue;
  1596. } else if ((var_size = check_header_variable(s, "preview",
  1597. "preview", 16)) >= 0) {
  1598. uint32_t pw = bytestream2_get_le32(gb);
  1599. uint32_t ph = bytestream2_get_le32(gb);
  1600. int64_t psize = 4LL * pw * ph;
  1601. if (psize >= bytestream2_get_bytes_left(gb))
  1602. return AVERROR_INVALIDDATA;
  1603. bytestream2_skip(gb, psize);
  1604. continue;
  1605. }
  1606. // Check if there are enough bytes for a header
  1607. if (bytestream2_get_bytes_left(gb) <= 9) {
  1608. av_log(s->avctx, AV_LOG_ERROR, "Incomplete header\n");
  1609. ret = AVERROR_INVALIDDATA;
  1610. goto fail;
  1611. }
  1612. // Process unknown variables
  1613. {
  1614. uint8_t name[256] = { 0 };
  1615. uint8_t type[256] = { 0 };
  1616. uint8_t value[256] = { 0 };
  1617. int i = 0, size;
  1618. while (bytestream2_get_bytes_left(gb) > 0 &&
  1619. bytestream2_peek_byte(gb) && i < 255) {
  1620. name[i++] = bytestream2_get_byte(gb);
  1621. }
  1622. bytestream2_skip(gb, 1);
  1623. i = 0;
  1624. while (bytestream2_get_bytes_left(gb) > 0 &&
  1625. bytestream2_peek_byte(gb) && i < 255) {
  1626. type[i++] = bytestream2_get_byte(gb);
  1627. }
  1628. bytestream2_skip(gb, 1);
  1629. size = bytestream2_get_le32(gb);
  1630. bytestream2_get_buffer(gb, value, FFMIN(sizeof(value) - 1, size));
  1631. if (!strcmp(type, "string"))
  1632. av_dict_set(&metadata, name, value, 0);
  1633. }
  1634. }
  1635. if (s->compression == EXR_UNKN) {
  1636. av_log(s->avctx, AV_LOG_ERROR, "Missing compression attribute.\n");
  1637. ret = AVERROR_INVALIDDATA;
  1638. goto fail;
  1639. }
  1640. if (s->is_tile) {
  1641. if (s->tile_attr.xSize < 1 || s->tile_attr.ySize < 1) {
  1642. av_log(s->avctx, AV_LOG_ERROR, "Invalid tile attribute.\n");
  1643. ret = AVERROR_INVALIDDATA;
  1644. goto fail;
  1645. }
  1646. }
  1647. if (bytestream2_get_bytes_left(gb) <= 0) {
  1648. av_log(s->avctx, AV_LOG_ERROR, "Incomplete frame.\n");
  1649. ret = AVERROR_INVALIDDATA;
  1650. goto fail;
  1651. }
  1652. frame->metadata = metadata;
  1653. // aaand we are done
  1654. bytestream2_skip(gb, 1);
  1655. return 0;
  1656. fail:
  1657. av_dict_free(&metadata);
  1658. return ret;
  1659. }
  1660. static int decode_frame(AVCodecContext *avctx, void *data,
  1661. int *got_frame, AVPacket *avpkt)
  1662. {
  1663. EXRContext *s = avctx->priv_data;
  1664. GetByteContext *gb = &s->gb;
  1665. ThreadFrame frame = { .f = data };
  1666. AVFrame *picture = data;
  1667. uint8_t *ptr;
  1668. int i, y, ret, ymax;
  1669. int planes;
  1670. int out_line_size;
  1671. int nb_blocks; /* nb scanline or nb tile */
  1672. uint64_t start_offset_table;
  1673. uint64_t start_next_scanline;
  1674. PutByteContext offset_table_writer;
  1675. bytestream2_init(gb, avpkt->data, avpkt->size);
  1676. if ((ret = decode_header(s, picture)) < 0)
  1677. return ret;
  1678. if ((s->compression == EXR_DWAA || s->compression == EXR_DWAB) &&
  1679. s->pixel_type == EXR_HALF) {
  1680. s->current_channel_offset *= 2;
  1681. for (int i = 0; i < 4; i++)
  1682. s->channel_offsets[i] *= 2;
  1683. }
  1684. switch (s->pixel_type) {
  1685. case EXR_FLOAT:
  1686. case EXR_HALF:
  1687. if (s->channel_offsets[3] >= 0) {
  1688. if (!s->is_luma) {
  1689. avctx->pix_fmt = AV_PIX_FMT_GBRAPF32;
  1690. } else {
  1691. /* todo: change this when a floating point pixel format with luma with alpha is implemented */
  1692. avctx->pix_fmt = AV_PIX_FMT_GBRAPF32;
  1693. }
  1694. } else {
  1695. if (!s->is_luma) {
  1696. avctx->pix_fmt = AV_PIX_FMT_GBRPF32;
  1697. } else {
  1698. avctx->pix_fmt = AV_PIX_FMT_GRAYF32;
  1699. }
  1700. }
  1701. break;
  1702. case EXR_UINT:
  1703. if (s->channel_offsets[3] >= 0) {
  1704. if (!s->is_luma) {
  1705. avctx->pix_fmt = AV_PIX_FMT_RGBA64;
  1706. } else {
  1707. avctx->pix_fmt = AV_PIX_FMT_YA16;
  1708. }
  1709. } else {
  1710. if (!s->is_luma) {
  1711. avctx->pix_fmt = AV_PIX_FMT_RGB48;
  1712. } else {
  1713. avctx->pix_fmt = AV_PIX_FMT_GRAY16;
  1714. }
  1715. }
  1716. break;
  1717. default:
  1718. av_log(avctx, AV_LOG_ERROR, "Missing channel list.\n");
  1719. return AVERROR_INVALIDDATA;
  1720. }
  1721. if (s->apply_trc_type != AVCOL_TRC_UNSPECIFIED)
  1722. avctx->color_trc = s->apply_trc_type;
  1723. switch (s->compression) {
  1724. case EXR_RAW:
  1725. case EXR_RLE:
  1726. case EXR_ZIP1:
  1727. s->scan_lines_per_block = 1;
  1728. break;
  1729. case EXR_PXR24:
  1730. case EXR_ZIP16:
  1731. s->scan_lines_per_block = 16;
  1732. break;
  1733. case EXR_PIZ:
  1734. case EXR_B44:
  1735. case EXR_B44A:
  1736. case EXR_DWAA:
  1737. s->scan_lines_per_block = 32;
  1738. break;
  1739. case EXR_DWAB:
  1740. s->scan_lines_per_block = 256;
  1741. break;
  1742. default:
  1743. avpriv_report_missing_feature(avctx, "Compression %d", s->compression);
  1744. return AVERROR_PATCHWELCOME;
  1745. }
  1746. /* Verify the xmin, xmax, ymin and ymax before setting the actual image size.
  1747. * It's possible for the data window can larger or outside the display window */
  1748. if (s->xmin > s->xmax || s->ymin > s->ymax ||
  1749. s->ydelta == 0xFFFFFFFF || s->xdelta == 0xFFFFFFFF) {
  1750. av_log(avctx, AV_LOG_ERROR, "Wrong or missing size information.\n");
  1751. return AVERROR_INVALIDDATA;
  1752. }
  1753. if ((ret = ff_set_dimensions(avctx, s->w, s->h)) < 0)
  1754. return ret;
  1755. ff_set_sar(s->avctx, av_d2q(av_int2float(s->sar), 255));
  1756. s->desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  1757. if (!s->desc)
  1758. return AVERROR_INVALIDDATA;
  1759. if (s->desc->flags & AV_PIX_FMT_FLAG_FLOAT) {
  1760. planes = s->desc->nb_components;
  1761. out_line_size = avctx->width * 4;
  1762. } else {
  1763. planes = 1;
  1764. out_line_size = avctx->width * 2 * s->desc->nb_components;
  1765. }
  1766. if (s->is_tile) {
  1767. nb_blocks = ((s->xdelta + s->tile_attr.xSize - 1) / s->tile_attr.xSize) *
  1768. ((s->ydelta + s->tile_attr.ySize - 1) / s->tile_attr.ySize);
  1769. } else { /* scanline */
  1770. nb_blocks = (s->ydelta + s->scan_lines_per_block - 1) /
  1771. s->scan_lines_per_block;
  1772. }
  1773. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
  1774. return ret;
  1775. if (bytestream2_get_bytes_left(gb)/8 < nb_blocks)
  1776. return AVERROR_INVALIDDATA;
  1777. // check offset table and recreate it if need
  1778. if (!s->is_tile && bytestream2_peek_le64(gb) == 0) {
  1779. av_log(s->avctx, AV_LOG_DEBUG, "recreating invalid scanline offset table\n");
  1780. start_offset_table = bytestream2_tell(gb);
  1781. start_next_scanline = start_offset_table + nb_blocks * 8;
  1782. bytestream2_init_writer(&offset_table_writer, &avpkt->data[start_offset_table], nb_blocks * 8);
  1783. for (y = 0; y < nb_blocks; y++) {
  1784. /* write offset of prev scanline in offset table */
  1785. bytestream2_put_le64(&offset_table_writer, start_next_scanline);
  1786. /* get len of next scanline */
  1787. bytestream2_seek(gb, start_next_scanline + 4, SEEK_SET);/* skip line number */
  1788. start_next_scanline += (bytestream2_get_le32(gb) + 8);
  1789. }
  1790. bytestream2_seek(gb, start_offset_table, SEEK_SET);
  1791. }
  1792. // save pointer we are going to use in decode_block
  1793. s->buf = avpkt->data;
  1794. s->buf_size = avpkt->size;
  1795. // Zero out the start if ymin is not 0
  1796. for (i = 0; i < planes; i++) {
  1797. ptr = picture->data[i];
  1798. for (y = 0; y < FFMIN(s->ymin, s->h); y++) {
  1799. memset(ptr, 0, out_line_size);
  1800. ptr += picture->linesize[i];
  1801. }
  1802. }
  1803. s->picture = picture;
  1804. avctx->execute2(avctx, decode_block, s->thread_data, NULL, nb_blocks);
  1805. ymax = FFMAX(0, s->ymax + 1);
  1806. // Zero out the end if ymax+1 is not h
  1807. if (ymax < avctx->height)
  1808. for (i = 0; i < planes; i++) {
  1809. ptr = picture->data[i] + (ymax * picture->linesize[i]);
  1810. for (y = ymax; y < avctx->height; y++) {
  1811. memset(ptr, 0, out_line_size);
  1812. ptr += picture->linesize[i];
  1813. }
  1814. }
  1815. picture->pict_type = AV_PICTURE_TYPE_I;
  1816. *got_frame = 1;
  1817. return avpkt->size;
  1818. }
  1819. static av_cold int decode_init(AVCodecContext *avctx)
  1820. {
  1821. EXRContext *s = avctx->priv_data;
  1822. uint32_t i;
  1823. union av_intfloat32 t;
  1824. float one_gamma = 1.0f / s->gamma;
  1825. avpriv_trc_function trc_func = NULL;
  1826. half2float_table(s->mantissatable, s->exponenttable, s->offsettable);
  1827. s->avctx = avctx;
  1828. ff_exrdsp_init(&s->dsp);
  1829. #if HAVE_BIGENDIAN
  1830. ff_bswapdsp_init(&s->bbdsp);
  1831. #endif
  1832. trc_func = avpriv_get_trc_function_from_trc(s->apply_trc_type);
  1833. if (trc_func) {
  1834. for (i = 0; i < 65536; ++i) {
  1835. t.i = half2float(i, s->mantissatable, s->exponenttable, s->offsettable);
  1836. t.f = trc_func(t.f);
  1837. s->gamma_table[i] = t;
  1838. }
  1839. } else {
  1840. if (one_gamma > 0.9999f && one_gamma < 1.0001f) {
  1841. for (i = 0; i < 65536; ++i) {
  1842. s->gamma_table[i].i = half2float(i, s->mantissatable, s->exponenttable, s->offsettable);
  1843. }
  1844. } else {
  1845. for (i = 0; i < 65536; ++i) {
  1846. t.i = half2float(i, s->mantissatable, s->exponenttable, s->offsettable);
  1847. /* If negative value we reuse half value */
  1848. if (t.f <= 0.0f) {
  1849. s->gamma_table[i] = t;
  1850. } else {
  1851. t.f = powf(t.f, one_gamma);
  1852. s->gamma_table[i] = t;
  1853. }
  1854. }
  1855. }
  1856. }
  1857. // allocate thread data, used for non EXR_RAW compression types
  1858. s->thread_data = av_mallocz_array(avctx->thread_count, sizeof(EXRThreadData));
  1859. if (!s->thread_data)
  1860. return AVERROR_INVALIDDATA;
  1861. return 0;
  1862. }
  1863. static av_cold int decode_end(AVCodecContext *avctx)
  1864. {
  1865. EXRContext *s = avctx->priv_data;
  1866. int i;
  1867. for (i = 0; i < avctx->thread_count; i++) {
  1868. EXRThreadData *td = &s->thread_data[i];
  1869. av_freep(&td->uncompressed_data);
  1870. av_freep(&td->tmp);
  1871. av_freep(&td->bitmap);
  1872. av_freep(&td->lut);
  1873. av_freep(&td->he);
  1874. av_freep(&td->freq);
  1875. av_freep(&td->ac_data);
  1876. av_freep(&td->dc_data);
  1877. av_freep(&td->rle_data);
  1878. av_freep(&td->rle_raw_data);
  1879. ff_free_vlc(&td->vlc);
  1880. }
  1881. av_freep(&s->thread_data);
  1882. av_freep(&s->channels);
  1883. return 0;
  1884. }
  1885. #define OFFSET(x) offsetof(EXRContext, x)
  1886. #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  1887. static const AVOption options[] = {
  1888. { "layer", "Set the decoding layer", OFFSET(layer),
  1889. AV_OPT_TYPE_STRING, { .str = "" }, 0, 0, VD },
  1890. { "part", "Set the decoding part", OFFSET(selected_part),
  1891. AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VD },
  1892. { "gamma", "Set the float gamma value when decoding", OFFSET(gamma),
  1893. AV_OPT_TYPE_FLOAT, { .dbl = 1.0f }, 0.001, FLT_MAX, VD },
  1894. // XXX: Note the abuse of the enum using AVCOL_TRC_UNSPECIFIED to subsume the existing gamma option
  1895. { "apply_trc", "color transfer characteristics to apply to EXR linear input", OFFSET(apply_trc_type),
  1896. AV_OPT_TYPE_INT, {.i64 = AVCOL_TRC_UNSPECIFIED }, 1, AVCOL_TRC_NB-1, VD, "apply_trc_type"},
  1897. { "bt709", "BT.709", 0,
  1898. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT709 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1899. { "gamma", "gamma", 0,
  1900. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_UNSPECIFIED }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1901. { "gamma22", "BT.470 M", 0,
  1902. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_GAMMA22 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1903. { "gamma28", "BT.470 BG", 0,
  1904. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_GAMMA28 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1905. { "smpte170m", "SMPTE 170 M", 0,
  1906. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTE170M }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1907. { "smpte240m", "SMPTE 240 M", 0,
  1908. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTE240M }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1909. { "linear", "Linear", 0,
  1910. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LINEAR }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1911. { "log", "Log", 0,
  1912. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LOG }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1913. { "log_sqrt", "Log square root", 0,
  1914. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LOG_SQRT }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1915. { "iec61966_2_4", "IEC 61966-2-4", 0,
  1916. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_IEC61966_2_4 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1917. { "bt1361", "BT.1361", 0,
  1918. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT1361_ECG }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1919. { "iec61966_2_1", "IEC 61966-2-1", 0,
  1920. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_IEC61966_2_1 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1921. { "bt2020_10bit", "BT.2020 - 10 bit", 0,
  1922. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT2020_10 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1923. { "bt2020_12bit", "BT.2020 - 12 bit", 0,
  1924. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT2020_12 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1925. { "smpte2084", "SMPTE ST 2084", 0,
  1926. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTEST2084 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1927. { "smpte428_1", "SMPTE ST 428-1", 0,
  1928. AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTEST428_1 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
  1929. { NULL },
  1930. };
  1931. static const AVClass exr_class = {
  1932. .class_name = "EXR",
  1933. .item_name = av_default_item_name,
  1934. .option = options,
  1935. .version = LIBAVUTIL_VERSION_INT,
  1936. };
  1937. AVCodec ff_exr_decoder = {
  1938. .name = "exr",
  1939. .long_name = NULL_IF_CONFIG_SMALL("OpenEXR image"),
  1940. .type = AVMEDIA_TYPE_VIDEO,
  1941. .id = AV_CODEC_ID_EXR,
  1942. .priv_data_size = sizeof(EXRContext),
  1943. .init = decode_init,
  1944. .close = decode_end,
  1945. .decode = decode_frame,
  1946. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS |
  1947. AV_CODEC_CAP_SLICE_THREADS,
  1948. .priv_class = &exr_class,
  1949. };