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.

675 lines
22KB

  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. return 1;
  136. }
  137. *buf -= strlen(value_name)+1;
  138. av_log(avctx, AV_LOG_WARNING, "Unknown data type for header variable %s\n", value_name);
  139. }
  140. return -1;
  141. }
  142. static void predictor(uint8_t *src, int size)
  143. {
  144. uint8_t *t = src + 1;
  145. uint8_t *stop = src + size;
  146. while (t < stop) {
  147. int d = (int)t[-1] + (int)t[0] - 128;
  148. t[0] = d;
  149. ++t;
  150. }
  151. }
  152. static void reorder_pixels(uint8_t *src, uint8_t *dst, int size)
  153. {
  154. const int8_t *t1 = src;
  155. const int8_t *t2 = src + (size + 1) / 2;
  156. int8_t *s = dst;
  157. int8_t *stop = s + size;
  158. while (1) {
  159. if (s < stop)
  160. *(s++) = *(t1++);
  161. else
  162. break;
  163. if (s < stop)
  164. *(s++) = *(t2++);
  165. else
  166. break;
  167. }
  168. }
  169. static int rle_uncompress(const uint8_t *src, int ssize, uint8_t *dst, int dsize)
  170. {
  171. int8_t *d = (int8_t *)dst;
  172. const int8_t *s = (const int8_t *)src;
  173. int8_t *dend = d + dsize;
  174. int count;
  175. while (ssize > 0) {
  176. count = *s++;
  177. if (count < 0) {
  178. count = -count;
  179. if ((dsize -= count ) < 0 ||
  180. (ssize -= count + 1) < 0)
  181. return -1;
  182. while (count--)
  183. *d++ = *s++;
  184. } else {
  185. count++;
  186. if ((dsize -= count) < 0 ||
  187. (ssize -= 2 ) < 0)
  188. return -1;
  189. while (count--)
  190. *d++ = *s;
  191. s++;
  192. }
  193. }
  194. return dend != d;
  195. }
  196. static int decode_frame(AVCodecContext *avctx,
  197. void *data,
  198. int *got_frame,
  199. AVPacket *avpkt)
  200. {
  201. const uint8_t *buf = avpkt->data;
  202. unsigned int buf_size = avpkt->size;
  203. const uint8_t *buf_end = buf + buf_size;
  204. const uint8_t *src;
  205. const AVPixFmtDescriptor *desc;
  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, flags, 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. unsigned int ydelta = ~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. s->compr = -1;
  231. if (buf_size < 10) {
  232. av_log(avctx, AV_LOG_ERROR, "Too short header to parse\n");
  233. return AVERROR_INVALIDDATA;
  234. }
  235. magic_number = bytestream_get_le32(&buf);
  236. if (magic_number != 20000630) { // As per documentation of OpenEXR it's supposed to be int 20000630 little-endian
  237. av_log(avctx, AV_LOG_ERROR, "Wrong magic number %d\n", magic_number);
  238. return AVERROR_INVALIDDATA;
  239. }
  240. version = bytestream_get_byte(&buf);
  241. if (version != 2) {
  242. av_log(avctx, AV_LOG_ERROR, "Unsupported version %d\n", version);
  243. return AVERROR_PATCHWELCOME;
  244. }
  245. flags = bytestream_get_le24(&buf);
  246. if (flags & 0x2) {
  247. av_log(avctx, AV_LOG_ERROR, "Tile based images are not supported\n");
  248. return AVERROR_PATCHWELCOME;
  249. }
  250. // Parse the header
  251. while (buf < buf_end && buf[0]) {
  252. unsigned int variable_buffer_data_size;
  253. // Process the channel list
  254. if (check_header_variable(avctx, &buf, buf_end, "channels", "chlist", 38, &variable_buffer_data_size) >= 0) {
  255. const uint8_t *channel_list_end;
  256. if (!variable_buffer_data_size)
  257. return AVERROR_INVALIDDATA;
  258. channel_list_end = buf + variable_buffer_data_size;
  259. while (channel_list_end - buf >= 19) {
  260. int current_bits_per_color_id = -1;
  261. int channel_index = -1;
  262. if (!strcmp(buf, "R"))
  263. channel_index = 0;
  264. else if (!strcmp(buf, "G"))
  265. channel_index = 1;
  266. else if (!strcmp(buf, "B"))
  267. channel_index = 2;
  268. else if (!strcmp(buf, "A"))
  269. channel_index = 3;
  270. else
  271. av_log(avctx, AV_LOG_WARNING, "Unsupported channel %.256s\n", buf);
  272. while (bytestream_get_byte(&buf) && buf < channel_list_end)
  273. continue; /* skip */
  274. if (channel_list_end - * &buf < 4) {
  275. av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
  276. return AVERROR_INVALIDDATA;
  277. }
  278. current_bits_per_color_id = bytestream_get_le32(&buf);
  279. if (current_bits_per_color_id > 2) {
  280. av_log(avctx, AV_LOG_ERROR, "Unknown color format\n");
  281. return AVERROR_INVALIDDATA;
  282. }
  283. if (channel_index >= 0) {
  284. if (s->bits_per_color_id != -1 && s->bits_per_color_id != current_bits_per_color_id) {
  285. av_log(avctx, AV_LOG_ERROR, "RGB channels not of the same depth\n");
  286. return AVERROR_INVALIDDATA;
  287. }
  288. s->bits_per_color_id = current_bits_per_color_id;
  289. s->channel_offsets[channel_index] = current_channel_offset;
  290. }
  291. current_channel_offset += 1 << current_bits_per_color_id;
  292. buf += 12;
  293. }
  294. /* Check if all channels are set with an offset or if the channels
  295. * are causing an overflow */
  296. if (FFMIN3(s->channel_offsets[0],
  297. s->channel_offsets[1],
  298. s->channel_offsets[2]) < 0) {
  299. if (s->channel_offsets[0] < 0)
  300. av_log(avctx, AV_LOG_ERROR, "Missing red channel\n");
  301. if (s->channel_offsets[1] < 0)
  302. av_log(avctx, AV_LOG_ERROR, "Missing green channel\n");
  303. if (s->channel_offsets[2] < 0)
  304. av_log(avctx, AV_LOG_ERROR, "Missing blue channel\n");
  305. return AVERROR_INVALIDDATA;
  306. }
  307. buf = channel_list_end;
  308. continue;
  309. } else if (check_header_variable(avctx, &buf, buf_end, "dataWindow", "box2i", 31, &variable_buffer_data_size) >= 0) {
  310. if (!variable_buffer_data_size)
  311. return AVERROR_INVALIDDATA;
  312. xmin = AV_RL32(buf);
  313. ymin = AV_RL32(buf + 4);
  314. xmax = AV_RL32(buf + 8);
  315. ymax = AV_RL32(buf + 12);
  316. xdelta = (xmax-xmin) + 1;
  317. ydelta = (ymax-ymin) + 1;
  318. buf += variable_buffer_data_size;
  319. continue;
  320. } else if (check_header_variable(avctx, &buf, buf_end, "displayWindow", "box2i", 34, &variable_buffer_data_size) >= 0) {
  321. if (!variable_buffer_data_size)
  322. return AVERROR_INVALIDDATA;
  323. w = AV_RL32(buf + 8) + 1;
  324. h = AV_RL32(buf + 12) + 1;
  325. buf += variable_buffer_data_size;
  326. continue;
  327. } else if (check_header_variable(avctx, &buf, buf_end, "lineOrder", "lineOrder", 25, &variable_buffer_data_size) >= 0) {
  328. if (!variable_buffer_data_size)
  329. return AVERROR_INVALIDDATA;
  330. av_log(avctx, AV_LOG_DEBUG, "line order : %d\n", *buf);
  331. if (*buf > 2) {
  332. av_log(avctx, AV_LOG_ERROR, "Unknown line order\n");
  333. return AVERROR_INVALIDDATA;
  334. }
  335. buf += variable_buffer_data_size;
  336. continue;
  337. } else 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. } else if (check_header_variable(avctx, &buf, buf_end, "compression", "compression", 29, &variable_buffer_data_size) >= 0) {
  344. if (!variable_buffer_data_size)
  345. return AVERROR_INVALIDDATA;
  346. if (s->compr == -1)
  347. s->compr = *buf;
  348. else
  349. av_log(avctx, AV_LOG_WARNING, "Found more than one compression attribute\n");
  350. buf += variable_buffer_data_size;
  351. continue;
  352. }
  353. // Check if there is enough bytes for a header
  354. if (buf_end - buf <= 9) {
  355. av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
  356. return AVERROR_INVALIDDATA;
  357. }
  358. // Process unknown variables
  359. for (i = 0; i < 2; i++) {
  360. // Skip variable name/type
  361. while (++buf < buf_end)
  362. if (buf[0] == 0x0)
  363. break;
  364. }
  365. buf++;
  366. // Skip variable length
  367. if (buf_end - buf >= 5) {
  368. variable_buffer_data_size = get_header_variable_length(&buf, buf_end);
  369. if (!variable_buffer_data_size) {
  370. av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
  371. return AVERROR_INVALIDDATA;
  372. }
  373. buf += variable_buffer_data_size;
  374. }
  375. }
  376. if (s->compr == -1) {
  377. av_log(avctx, AV_LOG_ERROR, "Missing compression attribute\n");
  378. return AVERROR_INVALIDDATA;
  379. }
  380. if (buf >= buf_end) {
  381. av_log(avctx, AV_LOG_ERROR, "Incomplete frame\n");
  382. return AVERROR_INVALIDDATA;
  383. }
  384. buf++;
  385. switch (s->bits_per_color_id) {
  386. case 2: // 32-bit
  387. case 1: // 16-bit
  388. if (s->channel_offsets[3] >= 0)
  389. avctx->pix_fmt = AV_PIX_FMT_RGBA64;
  390. else
  391. avctx->pix_fmt = AV_PIX_FMT_RGB48;
  392. break;
  393. // 8-bit
  394. case 0:
  395. av_log_missing_feature(avctx, "8-bit OpenEXR", 1);
  396. return AVERROR_PATCHWELCOME;
  397. default:
  398. av_log(avctx, AV_LOG_ERROR, "Unknown color format : %d\n", s->bits_per_color_id);
  399. return AVERROR_INVALIDDATA;
  400. }
  401. switch (s->compr) {
  402. case EXR_RAW:
  403. case EXR_RLE:
  404. case EXR_ZIP1:
  405. scan_lines_per_block = 1;
  406. break;
  407. case EXR_ZIP16:
  408. scan_lines_per_block = 16;
  409. break;
  410. default:
  411. av_log(avctx, AV_LOG_ERROR, "Compression type %d is not supported\n", s->compr);
  412. return AVERROR_PATCHWELCOME;
  413. }
  414. if (s->picture.data[0])
  415. ff_thread_release_buffer(avctx, &s->picture);
  416. if (av_image_check_size(w, h, 0, avctx))
  417. return AVERROR_INVALIDDATA;
  418. // Verify the xmin, xmax, ymin, ymax and xdelta before setting the actual image size
  419. if (xmin > xmax || ymin > ymax || xdelta != xmax - xmin + 1 || xmax >= w || ymax >= h) {
  420. av_log(avctx, AV_LOG_ERROR, "Wrong sizing or missing size information\n");
  421. return AVERROR_INVALIDDATA;
  422. }
  423. if (w != avctx->width || h != avctx->height) {
  424. avcodec_set_dimensions(avctx, w, h);
  425. }
  426. desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  427. bxmin = xmin * 2 * desc->nb_components;
  428. axmax = (avctx->width - (xmax + 1)) * 2 * desc->nb_components;
  429. out_line_size = avctx->width * 2 * desc->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. if (buf_end - buf < (ydelta + scan_lines_per_block - 1) / scan_lines_per_block * 8)
  450. return AVERROR_INVALIDDATA;
  451. // Process the actual scan line blocks
  452. for (y = ymin; y <= ymax; y += scan_lines_per_block) {
  453. uint16_t *ptr_x;
  454. const uint8_t *red_channel_buffer, *green_channel_buffer, *blue_channel_buffer, *alpha_channel_buffer = 0;
  455. const uint64_t line_offset = bytestream_get_le64(&buf);
  456. int32_t data_size, line;
  457. // Check if the buffer has the required bytes needed from the offset
  458. if (line_offset > (uint64_t)buf_size - 8)
  459. return AVERROR_INVALIDDATA;
  460. src = avpkt->data + line_offset + 8;
  461. line = AV_RL32(src - 8);
  462. if (line < ymin || line > ymax)
  463. return AVERROR_INVALIDDATA;
  464. data_size = AV_RL32(src - 4);
  465. if (data_size <= 0 || data_size > buf_size)
  466. return AVERROR_INVALIDDATA;
  467. if ((s->compr == EXR_RAW && (data_size != uncompressed_size ||
  468. line_offset > buf_size - uncompressed_size)) ||
  469. (s->compr != EXR_RAW && line_offset > buf_size - data_size)) {
  470. return AVERROR_INVALIDDATA;
  471. }
  472. if (scan_lines_per_block > 1)
  473. uncompressed_size = scan_line_size * FFMIN(scan_lines_per_block, ymax - y + 1);
  474. if ((s->compr == EXR_ZIP1 || s->compr == EXR_ZIP16) && data_size < uncompressed_size) {
  475. unsigned long dest_len = uncompressed_size;
  476. if (uncompress(s->tmp, &dest_len, src, data_size) != Z_OK ||
  477. dest_len != uncompressed_size) {
  478. av_log(avctx, AV_LOG_ERROR, "error during zlib decompression\n");
  479. return AVERROR(EINVAL);
  480. }
  481. } else if (s->compr == EXR_RLE && data_size < uncompressed_size) {
  482. if (rle_uncompress(src, data_size, s->tmp, uncompressed_size)) {
  483. av_log(avctx, AV_LOG_ERROR, "error during rle decompression\n");
  484. return AVERROR(EINVAL);
  485. }
  486. }
  487. if (s->compr != EXR_RAW && data_size < uncompressed_size) {
  488. predictor(s->tmp, uncompressed_size);
  489. reorder_pixels(s->tmp, s->uncompressed_data, uncompressed_size);
  490. red_channel_buffer = s->uncompressed_data + xdelta * s->channel_offsets[0];
  491. green_channel_buffer = s->uncompressed_data + xdelta * s->channel_offsets[1];
  492. blue_channel_buffer = s->uncompressed_data + xdelta * s->channel_offsets[2];
  493. if (s->channel_offsets[3] >= 0)
  494. alpha_channel_buffer = s->uncompressed_data + xdelta * s->channel_offsets[3];
  495. } else {
  496. red_channel_buffer = src + xdelta * s->channel_offsets[0];
  497. green_channel_buffer = src + xdelta * s->channel_offsets[1];
  498. blue_channel_buffer = src + xdelta * s->channel_offsets[2];
  499. if (s->channel_offsets[3] >= 0)
  500. alpha_channel_buffer = src + xdelta * s->channel_offsets[3];
  501. }
  502. ptr = p->data[0] + line * stride;
  503. for (i = 0; i < scan_lines_per_block && y + i <= ymax; i++, ptr += stride) {
  504. const uint8_t *r, *g, *b, *a;
  505. r = red_channel_buffer;
  506. g = green_channel_buffer;
  507. b = blue_channel_buffer;
  508. if (alpha_channel_buffer)
  509. a = alpha_channel_buffer;
  510. ptr_x = (uint16_t *)ptr;
  511. // Zero out the start if xmin is not 0
  512. memset(ptr_x, 0, bxmin);
  513. ptr_x += xmin * desc->nb_components;
  514. if (s->bits_per_color_id == 2) {
  515. // 32-bit
  516. for (x = 0; x < xdelta; x++) {
  517. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&r));
  518. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&g));
  519. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&b));
  520. if (alpha_channel_buffer)
  521. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a));
  522. }
  523. } else {
  524. // 16-bit
  525. for (x = 0; x < xdelta; x++) {
  526. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&r));
  527. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&g));
  528. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&b));
  529. if (alpha_channel_buffer)
  530. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&a));
  531. }
  532. }
  533. // Zero out the end if xmax+1 is not w
  534. memset(ptr_x, 0, axmax);
  535. red_channel_buffer += scan_line_size;
  536. green_channel_buffer += scan_line_size;
  537. blue_channel_buffer += scan_line_size;
  538. if (alpha_channel_buffer)
  539. alpha_channel_buffer += scan_line_size;
  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. return 0;
  557. }
  558. static av_cold int decode_end(AVCodecContext *avctx)
  559. {
  560. EXRContext *s = avctx->priv_data;
  561. if (s->picture.data[0])
  562. avctx->release_buffer(avctx, &s->picture);
  563. av_freep(&s->uncompressed_data);
  564. av_freep(&s->tmp);
  565. return 0;
  566. }
  567. AVCodec ff_exr_decoder = {
  568. .name = "exr",
  569. .type = AVMEDIA_TYPE_VIDEO,
  570. .id = AV_CODEC_ID_EXR,
  571. .priv_data_size = sizeof(EXRContext),
  572. .init = decode_init,
  573. .close = decode_end,
  574. .decode = decode_frame,
  575. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
  576. .long_name = NULL_IF_CONFIG_SMALL("OpenEXR image"),
  577. };