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.

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