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.

724 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 && line_offset > buf_size - data_size)) {
  240. return AVERROR_INVALIDDATA;
  241. }
  242. if (data_size < uncompressed_size) {
  243. av_fast_padded_malloc(&td->uncompressed_data, &td->uncompressed_size, uncompressed_size);
  244. av_fast_padded_malloc(&td->tmp, &td->tmp_size, uncompressed_size);
  245. if (!td->uncompressed_data || !td->tmp)
  246. return AVERROR(ENOMEM);
  247. }
  248. if ((s->compr == EXR_ZIP1 || s->compr == EXR_ZIP16) && data_size < uncompressed_size) {
  249. unsigned long dest_len = uncompressed_size;
  250. if (uncompress(td->tmp, &dest_len, src, data_size) != Z_OK ||
  251. dest_len != uncompressed_size) {
  252. av_log(avctx, AV_LOG_ERROR, "error during zlib decompression\n");
  253. return AVERROR(EINVAL);
  254. }
  255. } else if (s->compr == EXR_RLE && data_size < uncompressed_size) {
  256. if (rle_uncompress(src, data_size, td->tmp, uncompressed_size)) {
  257. av_log(avctx, AV_LOG_ERROR, "error during rle decompression\n");
  258. return AVERROR(EINVAL);
  259. }
  260. }
  261. if (s->compr != EXR_RAW && data_size < uncompressed_size) {
  262. predictor(td->tmp, uncompressed_size);
  263. reorder_pixels(td->tmp, td->uncompressed_data, uncompressed_size);
  264. channel_buffer[0] = td->uncompressed_data + xdelta * s->channel_offsets[0];
  265. channel_buffer[1] = td->uncompressed_data + xdelta * s->channel_offsets[1];
  266. channel_buffer[2] = td->uncompressed_data + xdelta * s->channel_offsets[2];
  267. if (s->channel_offsets[3] >= 0)
  268. channel_buffer[3] = td->uncompressed_data + xdelta * s->channel_offsets[3];
  269. } else {
  270. channel_buffer[0] = src + xdelta * s->channel_offsets[0];
  271. channel_buffer[1] = src + xdelta * s->channel_offsets[1];
  272. channel_buffer[2] = src + xdelta * s->channel_offsets[2];
  273. if (s->channel_offsets[3] >= 0)
  274. channel_buffer[3] = src + xdelta * s->channel_offsets[3];
  275. }
  276. ptr = p->data[0] + line * p->linesize[0];
  277. for (i = 0; i < s->scan_lines_per_block && line + i <= s->ymax; i++, ptr += p->linesize[0]) {
  278. const uint8_t *r, *g, *b, *a;
  279. r = channel_buffer[0];
  280. g = channel_buffer[1];
  281. b = channel_buffer[2];
  282. if (channel_buffer[3])
  283. a = channel_buffer[3];
  284. ptr_x = (uint16_t *)ptr;
  285. // Zero out the start if xmin is not 0
  286. memset(ptr_x, 0, bxmin);
  287. ptr_x += s->xmin * s->desc->nb_components;
  288. if (s->bits_per_color_id == 2) {
  289. // 32-bit
  290. for (x = 0; x < xdelta; x++) {
  291. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&r));
  292. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&g));
  293. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&b));
  294. if (channel_buffer[3])
  295. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a));
  296. }
  297. } else {
  298. // 16-bit
  299. for (x = 0; x < xdelta; x++) {
  300. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&r));
  301. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&g));
  302. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&b));
  303. if (channel_buffer[3])
  304. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&a));
  305. }
  306. }
  307. // Zero out the end if xmax+1 is not w
  308. memset(ptr_x, 0, axmax);
  309. channel_buffer[0] += s->scan_line_size;
  310. channel_buffer[1] += s->scan_line_size;
  311. channel_buffer[2] += s->scan_line_size;
  312. if (channel_buffer[3])
  313. channel_buffer[3] += s->scan_line_size;
  314. }
  315. return 0;
  316. }
  317. static int decode_frame(AVCodecContext *avctx,
  318. void *data,
  319. int *got_frame,
  320. AVPacket *avpkt)
  321. {
  322. const uint8_t *buf = avpkt->data;
  323. unsigned int buf_size = avpkt->size;
  324. const uint8_t *buf_end = buf + buf_size;
  325. EXRContext *const s = avctx->priv_data;
  326. AVFrame *picture = data;
  327. AVFrame *const p = &s->picture;
  328. uint8_t *ptr;
  329. int i, y, magic_number, version, flags, ret;
  330. int w = 0;
  331. int h = 0;
  332. int out_line_size;
  333. int scan_line_blocks;
  334. unsigned int current_channel_offset = 0;
  335. s->xmin = ~0;
  336. s->xmax = ~0;
  337. s->ymin = ~0;
  338. s->ymax = ~0;
  339. s->xdelta = ~0;
  340. s->ydelta = ~0;
  341. s->channel_offsets[0] = -1;
  342. s->channel_offsets[1] = -1;
  343. s->channel_offsets[2] = -1;
  344. s->channel_offsets[3] = -1;
  345. s->bits_per_color_id = -1;
  346. s->compr = -1;
  347. s->buf = buf;
  348. s->buf_size = buf_size;
  349. if (buf_size < 10) {
  350. av_log(avctx, AV_LOG_ERROR, "Too short header to parse\n");
  351. return AVERROR_INVALIDDATA;
  352. }
  353. magic_number = bytestream_get_le32(&buf);
  354. if (magic_number != 20000630) { // As per documentation of OpenEXR it's supposed to be int 20000630 little-endian
  355. av_log(avctx, AV_LOG_ERROR, "Wrong magic number %d\n", magic_number);
  356. return AVERROR_INVALIDDATA;
  357. }
  358. version = bytestream_get_byte(&buf);
  359. if (version != 2) {
  360. av_log(avctx, AV_LOG_ERROR, "Unsupported version %d\n", version);
  361. return AVERROR_PATCHWELCOME;
  362. }
  363. flags = bytestream_get_le24(&buf);
  364. if (flags & 0x2) {
  365. av_log(avctx, AV_LOG_ERROR, "Tile based images are not supported\n");
  366. return AVERROR_PATCHWELCOME;
  367. }
  368. // Parse the header
  369. while (buf < buf_end && buf[0]) {
  370. unsigned int variable_buffer_data_size;
  371. // Process the channel list
  372. if (check_header_variable(avctx, &buf, buf_end, "channels", "chlist", 38, &variable_buffer_data_size) >= 0) {
  373. const uint8_t *channel_list_end;
  374. if (!variable_buffer_data_size)
  375. return AVERROR_INVALIDDATA;
  376. channel_list_end = buf + variable_buffer_data_size;
  377. while (channel_list_end - buf >= 19) {
  378. int current_bits_per_color_id = -1;
  379. int channel_index = -1;
  380. if (!strcmp(buf, "R"))
  381. channel_index = 0;
  382. else if (!strcmp(buf, "G"))
  383. channel_index = 1;
  384. else if (!strcmp(buf, "B"))
  385. channel_index = 2;
  386. else if (!strcmp(buf, "A"))
  387. channel_index = 3;
  388. else
  389. av_log(avctx, AV_LOG_WARNING, "Unsupported channel %.256s\n", buf);
  390. while (bytestream_get_byte(&buf) && buf < channel_list_end)
  391. continue; /* skip */
  392. if (channel_list_end - * &buf < 4) {
  393. av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
  394. return AVERROR_INVALIDDATA;
  395. }
  396. current_bits_per_color_id = bytestream_get_le32(&buf);
  397. if (current_bits_per_color_id > 2) {
  398. av_log(avctx, AV_LOG_ERROR, "Unknown color format\n");
  399. return AVERROR_INVALIDDATA;
  400. }
  401. if (channel_index >= 0) {
  402. if (s->bits_per_color_id != -1 && s->bits_per_color_id != current_bits_per_color_id) {
  403. av_log(avctx, AV_LOG_ERROR, "RGB channels not of the same depth\n");
  404. return AVERROR_INVALIDDATA;
  405. }
  406. s->bits_per_color_id = current_bits_per_color_id;
  407. s->channel_offsets[channel_index] = current_channel_offset;
  408. }
  409. current_channel_offset += 1 << current_bits_per_color_id;
  410. buf += 12;
  411. }
  412. /* Check if all channels are set with an offset or if the channels
  413. * are causing an overflow */
  414. if (FFMIN3(s->channel_offsets[0],
  415. s->channel_offsets[1],
  416. s->channel_offsets[2]) < 0) {
  417. if (s->channel_offsets[0] < 0)
  418. av_log(avctx, AV_LOG_ERROR, "Missing red channel\n");
  419. if (s->channel_offsets[1] < 0)
  420. av_log(avctx, AV_LOG_ERROR, "Missing green channel\n");
  421. if (s->channel_offsets[2] < 0)
  422. av_log(avctx, AV_LOG_ERROR, "Missing blue channel\n");
  423. return AVERROR_INVALIDDATA;
  424. }
  425. buf = channel_list_end;
  426. continue;
  427. } else if (check_header_variable(avctx, &buf, buf_end, "dataWindow", "box2i", 31, &variable_buffer_data_size) >= 0) {
  428. if (!variable_buffer_data_size)
  429. return AVERROR_INVALIDDATA;
  430. s->xmin = AV_RL32(buf);
  431. s->ymin = AV_RL32(buf + 4);
  432. s->xmax = AV_RL32(buf + 8);
  433. s->ymax = AV_RL32(buf + 12);
  434. s->xdelta = (s->xmax - s->xmin) + 1;
  435. s->ydelta = (s->ymax - s->ymin) + 1;
  436. buf += variable_buffer_data_size;
  437. continue;
  438. } else if (check_header_variable(avctx, &buf, buf_end, "displayWindow", "box2i", 34, &variable_buffer_data_size) >= 0) {
  439. if (!variable_buffer_data_size)
  440. return AVERROR_INVALIDDATA;
  441. w = AV_RL32(buf + 8) + 1;
  442. h = AV_RL32(buf + 12) + 1;
  443. buf += variable_buffer_data_size;
  444. continue;
  445. } else if (check_header_variable(avctx, &buf, buf_end, "lineOrder", "lineOrder", 25, &variable_buffer_data_size) >= 0) {
  446. if (!variable_buffer_data_size)
  447. return AVERROR_INVALIDDATA;
  448. av_log(avctx, AV_LOG_DEBUG, "line order : %d\n", *buf);
  449. if (*buf > 2) {
  450. av_log(avctx, AV_LOG_ERROR, "Unknown line order\n");
  451. return AVERROR_INVALIDDATA;
  452. }
  453. buf += variable_buffer_data_size;
  454. continue;
  455. } else if (check_header_variable(avctx, &buf, buf_end, "pixelAspectRatio", "float", 31, &variable_buffer_data_size) >= 0) {
  456. if (!variable_buffer_data_size)
  457. return AVERROR_INVALIDDATA;
  458. avctx->sample_aspect_ratio = av_d2q(av_int2float(AV_RL32(buf)), 255);
  459. buf += variable_buffer_data_size;
  460. continue;
  461. } else if (check_header_variable(avctx, &buf, buf_end, "compression", "compression", 29, &variable_buffer_data_size) >= 0) {
  462. if (!variable_buffer_data_size)
  463. return AVERROR_INVALIDDATA;
  464. if (s->compr == -1)
  465. s->compr = *buf;
  466. else
  467. av_log(avctx, AV_LOG_WARNING, "Found more than one compression attribute\n");
  468. buf += variable_buffer_data_size;
  469. continue;
  470. }
  471. // Check if there is enough bytes for a header
  472. if (buf_end - buf <= 9) {
  473. av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
  474. return AVERROR_INVALIDDATA;
  475. }
  476. // Process unknown variables
  477. for (i = 0; i < 2; i++) {
  478. // Skip variable name/type
  479. while (++buf < buf_end)
  480. if (buf[0] == 0x0)
  481. break;
  482. }
  483. buf++;
  484. // Skip variable length
  485. if (buf_end - buf >= 5) {
  486. variable_buffer_data_size = get_header_variable_length(&buf, buf_end);
  487. if (!variable_buffer_data_size) {
  488. av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
  489. return AVERROR_INVALIDDATA;
  490. }
  491. buf += variable_buffer_data_size;
  492. }
  493. }
  494. if (s->compr == -1) {
  495. av_log(avctx, AV_LOG_ERROR, "Missing compression attribute\n");
  496. return AVERROR_INVALIDDATA;
  497. }
  498. if (buf >= buf_end) {
  499. av_log(avctx, AV_LOG_ERROR, "Incomplete frame\n");
  500. return AVERROR_INVALIDDATA;
  501. }
  502. buf++;
  503. switch (s->bits_per_color_id) {
  504. case 2: // 32-bit
  505. case 1: // 16-bit
  506. if (s->channel_offsets[3] >= 0)
  507. avctx->pix_fmt = AV_PIX_FMT_RGBA64;
  508. else
  509. avctx->pix_fmt = AV_PIX_FMT_RGB48;
  510. break;
  511. // 8-bit
  512. case 0:
  513. av_log_missing_feature(avctx, "8-bit OpenEXR", 1);
  514. return AVERROR_PATCHWELCOME;
  515. default:
  516. av_log(avctx, AV_LOG_ERROR, "Unknown color format : %d\n", s->bits_per_color_id);
  517. return AVERROR_INVALIDDATA;
  518. }
  519. switch (s->compr) {
  520. case EXR_RAW:
  521. case EXR_RLE:
  522. case EXR_ZIP1:
  523. s->scan_lines_per_block = 1;
  524. break;
  525. case EXR_ZIP16:
  526. s->scan_lines_per_block = 16;
  527. break;
  528. default:
  529. av_log(avctx, AV_LOG_ERROR, "Compression type %d is not supported\n", s->compr);
  530. return AVERROR_PATCHWELCOME;
  531. }
  532. if (s->picture.data[0])
  533. ff_thread_release_buffer(avctx, &s->picture);
  534. if (av_image_check_size(w, h, 0, avctx))
  535. return AVERROR_INVALIDDATA;
  536. // Verify the xmin, xmax, ymin, ymax and xdelta before setting the actual image size
  537. if (s->xmin > s->xmax ||
  538. s->ymin > s->ymax ||
  539. s->xdelta != s->xmax - s->xmin + 1 ||
  540. s->xmax >= w || s->ymax >= h) {
  541. av_log(avctx, AV_LOG_ERROR, "Wrong sizing or missing size information\n");
  542. return AVERROR_INVALIDDATA;
  543. }
  544. if (w != avctx->width || h != avctx->height) {
  545. avcodec_set_dimensions(avctx, w, h);
  546. }
  547. s->desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  548. out_line_size = avctx->width * 2 * s->desc->nb_components;
  549. s->scan_line_size = s->xdelta * current_channel_offset;
  550. scan_line_blocks = (s->ydelta + s->scan_lines_per_block - 1) / s->scan_lines_per_block;
  551. if (s->compr != EXR_RAW) {
  552. int thread_data_size, prev_size;
  553. EXRThreadData *m;
  554. prev_size = s->thread_data_size;
  555. if (av_size_mult(avctx->thread_count, sizeof(EXRThreadData), &thread_data_size))
  556. return AVERROR(EINVAL);
  557. m = av_fast_realloc(s->thread_data, &s->thread_data_size, thread_data_size);
  558. if (!m)
  559. return AVERROR(ENOMEM);
  560. s->thread_data = m;
  561. memset(s->thread_data + prev_size, 0, s->thread_data_size - prev_size);
  562. }
  563. if ((ret = ff_thread_get_buffer(avctx, p)) < 0) {
  564. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  565. return ret;
  566. }
  567. if (buf_end - buf < scan_line_blocks * 8)
  568. return AVERROR_INVALIDDATA;
  569. s->table = buf;
  570. ptr = p->data[0];
  571. // Zero out the start if ymin is not 0
  572. for (y = 0; y < s->ymin; y++) {
  573. memset(ptr, 0, out_line_size);
  574. ptr += p->linesize[0];
  575. }
  576. avctx->execute2(avctx, decode_block, s->thread_data, NULL, scan_line_blocks);
  577. // Zero out the end if ymax+1 is not h
  578. for (y = s->ymax + 1; y < avctx->height; y++) {
  579. memset(ptr, 0, out_line_size);
  580. ptr += p->linesize[0];
  581. }
  582. *picture = s->picture;
  583. *got_frame = 1;
  584. return buf_size;
  585. }
  586. static av_cold int decode_init(AVCodecContext *avctx)
  587. {
  588. EXRContext *s = avctx->priv_data;
  589. avcodec_get_frame_defaults(&s->picture);
  590. avctx->coded_frame = &s->picture;
  591. return 0;
  592. }
  593. static av_cold int decode_end(AVCodecContext *avctx)
  594. {
  595. EXRContext *s = avctx->priv_data;
  596. int i;
  597. if (s->picture.data[0])
  598. avctx->release_buffer(avctx, &s->picture);
  599. for (i = 0; i < s->thread_data_size / sizeof(EXRThreadData); i++) {
  600. EXRThreadData *td = &s->thread_data[i];
  601. av_free(td->uncompressed_data);
  602. av_free(td->tmp);
  603. }
  604. av_freep(&s->thread_data);
  605. s->thread_data_size = 0;
  606. return 0;
  607. }
  608. AVCodec ff_exr_decoder = {
  609. .name = "exr",
  610. .type = AVMEDIA_TYPE_VIDEO,
  611. .id = AV_CODEC_ID_EXR,
  612. .priv_data_size = sizeof(EXRContext),
  613. .init = decode_init,
  614. .close = decode_end,
  615. .decode = decode_frame,
  616. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS | CODEC_CAP_SLICE_THREADS,
  617. .long_name = NULL_IF_CONFIG_SMALL("OpenEXR image"),
  618. };