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.

768 lines
24KB

  1. /*
  2. * OpenEXR (.exr) image decoder
  3. * Copyright (c) 2009 Jimmy Christensen
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * OpenEXR decoder
  24. * @author Jimmy Christensen
  25. *
  26. * For more information on the OpenEXR format, visit:
  27. * http://openexr.com/
  28. *
  29. * exr_flt2uint() and exr_halflt2uint() is credited to Reimar Döffinger
  30. */
  31. #include <zlib.h>
  32. #include "avcodec.h"
  33. #include "bytestream.h"
  34. #include "mathops.h"
  35. #include "thread.h"
  36. #include "libavutil/imgutils.h"
  37. enum ExrCompr {
  38. EXR_RAW = 0,
  39. EXR_RLE = 1,
  40. EXR_ZIP1 = 2,
  41. EXR_ZIP16 = 3,
  42. EXR_PIZ = 4,
  43. EXR_B44 = 6,
  44. EXR_B44A = 7,
  45. };
  46. enum ExrPixelType {
  47. EXR_UINT,
  48. EXR_HALF,
  49. EXR_FLOAT
  50. };
  51. typedef struct EXRChannel {
  52. int xsub, ysub;
  53. enum ExrPixelType pixel_type;
  54. } EXRChannel;
  55. typedef struct EXRThreadData {
  56. uint8_t *uncompressed_data;
  57. int uncompressed_size;
  58. uint8_t *tmp;
  59. int tmp_size;
  60. } EXRThreadData;
  61. typedef struct EXRContext {
  62. AVFrame picture;
  63. int compr;
  64. int bits_per_color_id;
  65. int channel_offsets[4]; // 0 = red, 1 = green, 2 = blue and 3 = alpha
  66. const AVPixFmtDescriptor *desc;
  67. uint32_t xmax, xmin;
  68. uint32_t ymax, ymin;
  69. uint32_t xdelta, ydelta;
  70. uint64_t scan_line_size;
  71. int scan_lines_per_block;
  72. const uint8_t *buf, *table;
  73. int buf_size;
  74. EXRChannel *channels;
  75. int nb_channels;
  76. EXRThreadData *thread_data;
  77. int thread_data_size;
  78. } EXRContext;
  79. /**
  80. * Converts from 32-bit float as uint32_t to uint16_t
  81. *
  82. * @param v 32-bit float
  83. * @return normalized 16-bit unsigned int
  84. */
  85. static inline uint16_t exr_flt2uint(uint32_t v)
  86. {
  87. unsigned int exp = v >> 23;
  88. // "HACK": negative values result in exp< 0, so clipping them to 0
  89. // is also handled by this condition, avoids explicit check for sign bit.
  90. if (exp<= 127 + 7 - 24) // we would shift out all bits anyway
  91. return 0;
  92. if (exp >= 127)
  93. return 0xffff;
  94. v &= 0x007fffff;
  95. return (v + (1 << 23)) >> (127 + 7 - exp);
  96. }
  97. /**
  98. * Converts from 16-bit float as uint16_t to uint16_t
  99. *
  100. * @param v 16-bit float
  101. * @return normalized 16-bit unsigned int
  102. */
  103. static inline uint16_t exr_halflt2uint(uint16_t v)
  104. {
  105. unsigned exp = 14 - (v >> 10);
  106. if (exp >= 14) {
  107. if (exp == 14) return (v >> 9) & 1;
  108. else return (v & 0x8000) ? 0 : 0xffff;
  109. }
  110. v <<= 6;
  111. return (v + (1 << 16)) >> (exp + 1);
  112. }
  113. /**
  114. * Gets the size of the header variable
  115. *
  116. * @param **buf the current pointer location in the header where
  117. * the variable data starts
  118. * @param *buf_end pointer location of the end of the buffer
  119. * @return size of variable data
  120. */
  121. static unsigned int get_header_variable_length(const uint8_t **buf,
  122. const uint8_t *buf_end)
  123. {
  124. unsigned int variable_buffer_data_size = bytestream_get_le32(buf);
  125. if (variable_buffer_data_size >= buf_end - *buf)
  126. return 0;
  127. return variable_buffer_data_size;
  128. }
  129. /**
  130. * Checks if the variable name corresponds with it's data type
  131. *
  132. * @param *avctx the AVCodecContext
  133. * @param **buf the current pointer location in the header where
  134. * the variable name starts
  135. * @param *buf_end pointer location of the end of the buffer
  136. * @param *value_name name of the varible to check
  137. * @param *value_type type of the varible to check
  138. * @param minimum_length minimum length of the variable data
  139. * @param variable_buffer_data_size variable length read from the header
  140. * after it's checked
  141. * @return negative if variable is invalid
  142. */
  143. static int check_header_variable(AVCodecContext *avctx,
  144. const uint8_t **buf,
  145. const uint8_t *buf_end,
  146. const char *value_name,
  147. const char *value_type,
  148. unsigned int minimum_length,
  149. unsigned int *variable_buffer_data_size)
  150. {
  151. if (buf_end - *buf >= minimum_length && !strcmp(*buf, value_name)) {
  152. *buf += strlen(value_name)+1;
  153. if (!strcmp(*buf, value_type)) {
  154. *buf += strlen(value_type)+1;
  155. *variable_buffer_data_size = get_header_variable_length(buf, buf_end);
  156. if (!*variable_buffer_data_size)
  157. av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
  158. return 1;
  159. }
  160. *buf -= strlen(value_name)+1;
  161. av_log(avctx, AV_LOG_WARNING, "Unknown data type for header variable %s\n", value_name);
  162. }
  163. return -1;
  164. }
  165. static void predictor(uint8_t *src, int size)
  166. {
  167. uint8_t *t = src + 1;
  168. uint8_t *stop = src + size;
  169. while (t < stop) {
  170. int d = (int)t[-1] + (int)t[0] - 128;
  171. t[0] = d;
  172. ++t;
  173. }
  174. }
  175. static void reorder_pixels(uint8_t *src, uint8_t *dst, int size)
  176. {
  177. const int8_t *t1 = src;
  178. const int8_t *t2 = src + (size + 1) / 2;
  179. int8_t *s = dst;
  180. int8_t *stop = s + size;
  181. while (1) {
  182. if (s < stop)
  183. *(s++) = *(t1++);
  184. else
  185. break;
  186. if (s < stop)
  187. *(s++) = *(t2++);
  188. else
  189. break;
  190. }
  191. }
  192. static int zip_uncompress(const uint8_t *src, int compressed_size,
  193. int uncompressed_size, EXRThreadData *td)
  194. {
  195. unsigned long dest_len = uncompressed_size;
  196. if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK ||
  197. dest_len != uncompressed_size)
  198. return AVERROR(EINVAL);
  199. predictor(td->tmp, uncompressed_size);
  200. reorder_pixels(td->tmp, td->uncompressed_data, uncompressed_size);
  201. return 0;
  202. }
  203. static int rle_uncompress(const uint8_t *src, int compressed_size,
  204. int uncompressed_size, EXRThreadData *td)
  205. {
  206. int8_t *d = (int8_t *)td->tmp;
  207. const int8_t *s = (const int8_t *)src;
  208. int ssize = compressed_size;
  209. int dsize = uncompressed_size;
  210. int8_t *dend = d + dsize;
  211. int count;
  212. while (ssize > 0) {
  213. count = *s++;
  214. if (count < 0) {
  215. count = -count;
  216. if ((dsize -= count ) < 0 ||
  217. (ssize -= count + 1) < 0)
  218. return -1;
  219. while (count--)
  220. *d++ = *s++;
  221. } else {
  222. count++;
  223. if ((dsize -= count) < 0 ||
  224. (ssize -= 2 ) < 0)
  225. return -1;
  226. while (count--)
  227. *d++ = *s;
  228. s++;
  229. }
  230. }
  231. if (dend != d)
  232. return AVERROR_INVALIDDATA;
  233. predictor(td->tmp, uncompressed_size);
  234. reorder_pixels(td->tmp, td->uncompressed_data, uncompressed_size);
  235. return 0;
  236. }
  237. static int decode_block(AVCodecContext *avctx, void *tdata,
  238. int jobnr, int threadnr)
  239. {
  240. EXRContext *s = avctx->priv_data;
  241. AVFrame *const p = &s->picture;
  242. EXRThreadData *td = &s->thread_data[threadnr];
  243. const uint8_t *channel_buffer[4] = { 0 };
  244. const uint8_t *buf = s->buf;
  245. uint64_t line_offset, uncompressed_size;
  246. uint32_t xdelta = s->xdelta;
  247. uint16_t *ptr_x;
  248. uint8_t *ptr;
  249. int32_t data_size, line;
  250. const uint8_t *src;
  251. int axmax = (avctx->width - (s->xmax + 1)) * 2 * s->desc->nb_components;
  252. int bxmin = s->xmin * 2 * s->desc->nb_components;
  253. int ret, i, x, buf_size = s->buf_size;
  254. line_offset = AV_RL64(s->table + jobnr * 8);
  255. // Check if the buffer has the required bytes needed from the offset
  256. if (line_offset > buf_size - 8)
  257. return AVERROR_INVALIDDATA;
  258. src = buf + line_offset + 8;
  259. line = AV_RL32(src - 8);
  260. if (line < s->ymin || line > s->ymax)
  261. return AVERROR_INVALIDDATA;
  262. data_size = AV_RL32(src - 4);
  263. if (data_size <= 0 || data_size > buf_size)
  264. return AVERROR_INVALIDDATA;
  265. uncompressed_size = s->scan_line_size * FFMIN(s->scan_lines_per_block, s->ymax - line + 1);
  266. if ((s->compr == EXR_RAW && (data_size != uncompressed_size ||
  267. line_offset > buf_size - uncompressed_size)) ||
  268. (s->compr != EXR_RAW && (data_size > uncompressed_size ||
  269. line_offset > buf_size - data_size))) {
  270. return AVERROR_INVALIDDATA;
  271. }
  272. if (data_size < uncompressed_size) {
  273. av_fast_padded_malloc(&td->uncompressed_data, &td->uncompressed_size, uncompressed_size);
  274. av_fast_padded_malloc(&td->tmp, &td->tmp_size, uncompressed_size);
  275. if (!td->uncompressed_data || !td->tmp)
  276. return AVERROR(ENOMEM);
  277. switch (s->compr) {
  278. case EXR_ZIP1:
  279. case EXR_ZIP16:
  280. ret = zip_uncompress(src, data_size, uncompressed_size, td);
  281. break;
  282. case EXR_RLE:
  283. ret = rle_uncompress(src, data_size, uncompressed_size, td);
  284. }
  285. src = td->uncompressed_data;
  286. }
  287. channel_buffer[0] = src + xdelta * s->channel_offsets[0];
  288. channel_buffer[1] = src + xdelta * s->channel_offsets[1];
  289. channel_buffer[2] = src + xdelta * s->channel_offsets[2];
  290. if (s->channel_offsets[3] >= 0)
  291. channel_buffer[3] = src + xdelta * s->channel_offsets[3];
  292. ptr = p->data[0] + line * p->linesize[0];
  293. for (i = 0; i < s->scan_lines_per_block && line + i <= s->ymax; i++, ptr += p->linesize[0]) {
  294. const uint8_t *r, *g, *b, *a;
  295. r = channel_buffer[0];
  296. g = channel_buffer[1];
  297. b = channel_buffer[2];
  298. if (channel_buffer[3])
  299. a = channel_buffer[3];
  300. ptr_x = (uint16_t *)ptr;
  301. // Zero out the start if xmin is not 0
  302. memset(ptr_x, 0, bxmin);
  303. ptr_x += s->xmin * s->desc->nb_components;
  304. if (s->bits_per_color_id == 2) {
  305. // 32-bit
  306. for (x = 0; x < xdelta; x++) {
  307. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&r));
  308. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&g));
  309. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&b));
  310. if (channel_buffer[3])
  311. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a));
  312. }
  313. } else {
  314. // 16-bit
  315. for (x = 0; x < xdelta; x++) {
  316. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&r));
  317. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&g));
  318. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&b));
  319. if (channel_buffer[3])
  320. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&a));
  321. }
  322. }
  323. // Zero out the end if xmax+1 is not w
  324. memset(ptr_x, 0, axmax);
  325. channel_buffer[0] += s->scan_line_size;
  326. channel_buffer[1] += s->scan_line_size;
  327. channel_buffer[2] += s->scan_line_size;
  328. if (channel_buffer[3])
  329. channel_buffer[3] += s->scan_line_size;
  330. }
  331. return 0;
  332. }
  333. static int decode_frame(AVCodecContext *avctx,
  334. void *data,
  335. int *got_frame,
  336. AVPacket *avpkt)
  337. {
  338. const uint8_t *buf = avpkt->data;
  339. unsigned int buf_size = avpkt->size;
  340. const uint8_t *buf_end = buf + buf_size;
  341. EXRContext *const s = avctx->priv_data;
  342. AVFrame *picture = data;
  343. AVFrame *const p = &s->picture;
  344. uint8_t *ptr;
  345. int i, y, magic_number, version, flags, ret;
  346. int w = 0;
  347. int h = 0;
  348. int out_line_size;
  349. int scan_line_blocks;
  350. unsigned int current_channel_offset = 0;
  351. s->xmin = ~0;
  352. s->xmax = ~0;
  353. s->ymin = ~0;
  354. s->ymax = ~0;
  355. s->xdelta = ~0;
  356. s->ydelta = ~0;
  357. s->channel_offsets[0] = -1;
  358. s->channel_offsets[1] = -1;
  359. s->channel_offsets[2] = -1;
  360. s->channel_offsets[3] = -1;
  361. s->bits_per_color_id = -1;
  362. s->nb_channels = 0;
  363. s->compr = -1;
  364. s->buf = buf;
  365. s->buf_size = buf_size;
  366. if (buf_size < 10) {
  367. av_log(avctx, AV_LOG_ERROR, "Too short header to parse\n");
  368. return AVERROR_INVALIDDATA;
  369. }
  370. magic_number = bytestream_get_le32(&buf);
  371. if (magic_number != 20000630) { // As per documentation of OpenEXR it's supposed to be int 20000630 little-endian
  372. av_log(avctx, AV_LOG_ERROR, "Wrong magic number %d\n", magic_number);
  373. return AVERROR_INVALIDDATA;
  374. }
  375. version = bytestream_get_byte(&buf);
  376. if (version != 2) {
  377. av_log(avctx, AV_LOG_ERROR, "Unsupported version %d\n", version);
  378. return AVERROR_PATCHWELCOME;
  379. }
  380. flags = bytestream_get_le24(&buf);
  381. if (flags & 0x2) {
  382. av_log(avctx, AV_LOG_ERROR, "Tile based images are not supported\n");
  383. return AVERROR_PATCHWELCOME;
  384. }
  385. // Parse the header
  386. while (buf < buf_end && buf[0]) {
  387. unsigned int variable_buffer_data_size;
  388. // Process the channel list
  389. if (check_header_variable(avctx, &buf, buf_end, "channels", "chlist", 38, &variable_buffer_data_size) >= 0) {
  390. const uint8_t *channel_list_end;
  391. if (!variable_buffer_data_size)
  392. return AVERROR_INVALIDDATA;
  393. channel_list_end = buf + variable_buffer_data_size;
  394. while (channel_list_end - buf >= 19) {
  395. EXRChannel *channel;
  396. int current_bits_per_color_id = -1;
  397. int channel_index = -1;
  398. int xsub, ysub;
  399. if (!strcmp(buf, "R"))
  400. channel_index = 0;
  401. else if (!strcmp(buf, "G"))
  402. channel_index = 1;
  403. else if (!strcmp(buf, "B"))
  404. channel_index = 2;
  405. else if (!strcmp(buf, "A"))
  406. channel_index = 3;
  407. else
  408. av_log(avctx, AV_LOG_WARNING, "Unsupported channel %.256s\n", buf);
  409. while (bytestream_get_byte(&buf) && buf < channel_list_end)
  410. continue; /* skip */
  411. if (channel_list_end - * &buf < 4) {
  412. av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
  413. return AVERROR_INVALIDDATA;
  414. }
  415. current_bits_per_color_id = bytestream_get_le32(&buf);
  416. if (current_bits_per_color_id > 2) {
  417. av_log(avctx, AV_LOG_ERROR, "Unknown color format\n");
  418. return AVERROR_INVALIDDATA;
  419. }
  420. buf += 4;
  421. xsub = bytestream_get_le32(&buf);
  422. ysub = bytestream_get_le32(&buf);
  423. if (xsub != 1 || ysub != 1) {
  424. av_log(avctx, AV_LOG_ERROR, "Unsupported subsampling %dx%d\n", xsub, ysub);
  425. return AVERROR_PATCHWELCOME;
  426. }
  427. if (channel_index >= 0) {
  428. if (s->bits_per_color_id != -1 && s->bits_per_color_id != current_bits_per_color_id) {
  429. av_log(avctx, AV_LOG_ERROR, "RGB channels not of the same depth\n");
  430. return AVERROR_INVALIDDATA;
  431. }
  432. s->bits_per_color_id = current_bits_per_color_id;
  433. s->channel_offsets[channel_index] = current_channel_offset;
  434. }
  435. s->channels = av_realloc_f(s->channels, ++s->nb_channels, sizeof(EXRChannel));
  436. if (!s->channels)
  437. return AVERROR(ENOMEM);
  438. channel = &s->channels[s->nb_channels - 1];
  439. channel->pixel_type = current_bits_per_color_id;
  440. channel->xsub = xsub;
  441. channel->ysub = ysub;
  442. current_channel_offset += 1 << current_bits_per_color_id;
  443. }
  444. /* Check if all channels are set with an offset or if the channels
  445. * are causing an overflow */
  446. if (FFMIN3(s->channel_offsets[0],
  447. s->channel_offsets[1],
  448. s->channel_offsets[2]) < 0) {
  449. if (s->channel_offsets[0] < 0)
  450. av_log(avctx, AV_LOG_ERROR, "Missing red channel\n");
  451. if (s->channel_offsets[1] < 0)
  452. av_log(avctx, AV_LOG_ERROR, "Missing green channel\n");
  453. if (s->channel_offsets[2] < 0)
  454. av_log(avctx, AV_LOG_ERROR, "Missing blue channel\n");
  455. return AVERROR_INVALIDDATA;
  456. }
  457. buf = channel_list_end;
  458. continue;
  459. } else if (check_header_variable(avctx, &buf, buf_end, "dataWindow", "box2i", 31, &variable_buffer_data_size) >= 0) {
  460. if (!variable_buffer_data_size)
  461. return AVERROR_INVALIDDATA;
  462. s->xmin = AV_RL32(buf);
  463. s->ymin = AV_RL32(buf + 4);
  464. s->xmax = AV_RL32(buf + 8);
  465. s->ymax = AV_RL32(buf + 12);
  466. s->xdelta = (s->xmax - s->xmin) + 1;
  467. s->ydelta = (s->ymax - s->ymin) + 1;
  468. buf += variable_buffer_data_size;
  469. continue;
  470. } else if (check_header_variable(avctx, &buf, buf_end, "displayWindow", "box2i", 34, &variable_buffer_data_size) >= 0) {
  471. if (!variable_buffer_data_size)
  472. return AVERROR_INVALIDDATA;
  473. w = AV_RL32(buf + 8) + 1;
  474. h = AV_RL32(buf + 12) + 1;
  475. buf += variable_buffer_data_size;
  476. continue;
  477. } else if (check_header_variable(avctx, &buf, buf_end, "lineOrder", "lineOrder", 25, &variable_buffer_data_size) >= 0) {
  478. if (!variable_buffer_data_size)
  479. return AVERROR_INVALIDDATA;
  480. av_log(avctx, AV_LOG_DEBUG, "line order : %d\n", *buf);
  481. if (*buf > 2) {
  482. av_log(avctx, AV_LOG_ERROR, "Unknown line order\n");
  483. return AVERROR_INVALIDDATA;
  484. }
  485. buf += variable_buffer_data_size;
  486. continue;
  487. } else if (check_header_variable(avctx, &buf, buf_end, "pixelAspectRatio", "float", 31, &variable_buffer_data_size) >= 0) {
  488. if (!variable_buffer_data_size)
  489. return AVERROR_INVALIDDATA;
  490. avctx->sample_aspect_ratio = av_d2q(av_int2float(AV_RL32(buf)), 255);
  491. buf += variable_buffer_data_size;
  492. continue;
  493. } else if (check_header_variable(avctx, &buf, buf_end, "compression", "compression", 29, &variable_buffer_data_size) >= 0) {
  494. if (!variable_buffer_data_size)
  495. return AVERROR_INVALIDDATA;
  496. if (s->compr == -1)
  497. s->compr = *buf;
  498. else
  499. av_log(avctx, AV_LOG_WARNING, "Found more than one compression attribute\n");
  500. buf += variable_buffer_data_size;
  501. continue;
  502. }
  503. // Check if there is enough bytes for a header
  504. if (buf_end - buf <= 9) {
  505. av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
  506. return AVERROR_INVALIDDATA;
  507. }
  508. // Process unknown variables
  509. for (i = 0; i < 2; i++) {
  510. // Skip variable name/type
  511. while (++buf < buf_end)
  512. if (buf[0] == 0x0)
  513. break;
  514. }
  515. buf++;
  516. // Skip variable length
  517. if (buf_end - buf >= 5) {
  518. variable_buffer_data_size = get_header_variable_length(&buf, buf_end);
  519. if (!variable_buffer_data_size) {
  520. av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
  521. return AVERROR_INVALIDDATA;
  522. }
  523. buf += variable_buffer_data_size;
  524. }
  525. }
  526. if (s->compr == -1) {
  527. av_log(avctx, AV_LOG_ERROR, "Missing compression attribute\n");
  528. return AVERROR_INVALIDDATA;
  529. }
  530. if (buf >= buf_end) {
  531. av_log(avctx, AV_LOG_ERROR, "Incomplete frame\n");
  532. return AVERROR_INVALIDDATA;
  533. }
  534. buf++;
  535. switch (s->bits_per_color_id) {
  536. case 2: // 32-bit
  537. case 1: // 16-bit
  538. if (s->channel_offsets[3] >= 0)
  539. avctx->pix_fmt = AV_PIX_FMT_RGBA64;
  540. else
  541. avctx->pix_fmt = AV_PIX_FMT_RGB48;
  542. break;
  543. // 8-bit
  544. case 0:
  545. av_log_missing_feature(avctx, "8-bit OpenEXR", 1);
  546. return AVERROR_PATCHWELCOME;
  547. default:
  548. av_log(avctx, AV_LOG_ERROR, "Unknown color format : %d\n", s->bits_per_color_id);
  549. return AVERROR_INVALIDDATA;
  550. }
  551. switch (s->compr) {
  552. case EXR_RAW:
  553. case EXR_RLE:
  554. case EXR_ZIP1:
  555. s->scan_lines_per_block = 1;
  556. break;
  557. case EXR_ZIP16:
  558. s->scan_lines_per_block = 16;
  559. break;
  560. default:
  561. av_log(avctx, AV_LOG_ERROR, "Compression type %d is not supported\n", s->compr);
  562. return AVERROR_PATCHWELCOME;
  563. }
  564. if (s->picture.data[0])
  565. ff_thread_release_buffer(avctx, &s->picture);
  566. if (av_image_check_size(w, h, 0, avctx))
  567. return AVERROR_INVALIDDATA;
  568. // Verify the xmin, xmax, ymin, ymax and xdelta before setting the actual image size
  569. if (s->xmin > s->xmax ||
  570. s->ymin > s->ymax ||
  571. s->xdelta != s->xmax - s->xmin + 1 ||
  572. s->xmax >= w || s->ymax >= h) {
  573. av_log(avctx, AV_LOG_ERROR, "Wrong sizing or missing size information\n");
  574. return AVERROR_INVALIDDATA;
  575. }
  576. if (w != avctx->width || h != avctx->height) {
  577. avcodec_set_dimensions(avctx, w, h);
  578. }
  579. s->desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  580. out_line_size = avctx->width * 2 * s->desc->nb_components;
  581. s->scan_line_size = s->xdelta * current_channel_offset;
  582. scan_line_blocks = (s->ydelta + s->scan_lines_per_block - 1) / s->scan_lines_per_block;
  583. if (s->compr != EXR_RAW) {
  584. size_t thread_data_size, prev_size;
  585. EXRThreadData *m;
  586. prev_size = s->thread_data_size;
  587. if (av_size_mult(avctx->thread_count, sizeof(EXRThreadData), &thread_data_size))
  588. return AVERROR(EINVAL);
  589. m = av_fast_realloc(s->thread_data, &s->thread_data_size, thread_data_size);
  590. if (!m)
  591. return AVERROR(ENOMEM);
  592. s->thread_data = m;
  593. memset(s->thread_data + prev_size, 0, s->thread_data_size - prev_size);
  594. }
  595. if ((ret = ff_thread_get_buffer(avctx, p)) < 0) {
  596. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  597. return ret;
  598. }
  599. if (buf_end - buf < scan_line_blocks * 8)
  600. return AVERROR_INVALIDDATA;
  601. s->table = buf;
  602. ptr = p->data[0];
  603. // Zero out the start if ymin is not 0
  604. for (y = 0; y < s->ymin; y++) {
  605. memset(ptr, 0, out_line_size);
  606. ptr += p->linesize[0];
  607. }
  608. avctx->execute2(avctx, decode_block, s->thread_data, NULL, scan_line_blocks);
  609. // Zero out the end if ymax+1 is not h
  610. for (y = s->ymax + 1; y < avctx->height; y++) {
  611. memset(ptr, 0, out_line_size);
  612. ptr += p->linesize[0];
  613. }
  614. *picture = s->picture;
  615. *got_frame = 1;
  616. return buf_size;
  617. }
  618. static av_cold int decode_init(AVCodecContext *avctx)
  619. {
  620. EXRContext *s = avctx->priv_data;
  621. avcodec_get_frame_defaults(&s->picture);
  622. avctx->coded_frame = &s->picture;
  623. return 0;
  624. }
  625. static av_cold int decode_end(AVCodecContext *avctx)
  626. {
  627. EXRContext *s = avctx->priv_data;
  628. int i;
  629. if (s->picture.data[0])
  630. avctx->release_buffer(avctx, &s->picture);
  631. for (i = 0; i < s->thread_data_size / sizeof(EXRThreadData); i++) {
  632. EXRThreadData *td = &s->thread_data[i];
  633. av_free(td->uncompressed_data);
  634. av_free(td->tmp);
  635. }
  636. av_freep(&s->thread_data);
  637. s->thread_data_size = 0;
  638. av_freep(&s->channels);
  639. return 0;
  640. }
  641. AVCodec ff_exr_decoder = {
  642. .name = "exr",
  643. .type = AVMEDIA_TYPE_VIDEO,
  644. .id = AV_CODEC_ID_EXR,
  645. .priv_data_size = sizeof(EXRContext),
  646. .init = decode_init,
  647. .close = decode_end,
  648. .decode = decode_frame,
  649. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS | CODEC_CAP_SLICE_THREADS,
  650. .long_name = NULL_IF_CONFIG_SMALL("OpenEXR image"),
  651. };