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.

728 lines
23KB

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