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.

2052 lines
67KB

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