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.

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