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.

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