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
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. 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. const int8_t *s = (const 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 uint8_t *src;
  207. const AVPixFmtDescriptor *desc;
  208. EXRContext *const s = avctx->priv_data;
  209. AVFrame *picture = data;
  210. AVFrame *const p = &s->picture;
  211. uint8_t *ptr;
  212. int i, x, y, stride, magic_number, version, flags, ret;
  213. int w = 0;
  214. int h = 0;
  215. unsigned int xmin = ~0;
  216. unsigned int xmax = ~0;
  217. unsigned int ymin = ~0;
  218. unsigned int ymax = ~0;
  219. unsigned int xdelta = ~0;
  220. unsigned int ydelta = ~0;
  221. int out_line_size;
  222. int bxmin, axmax;
  223. int scan_lines_per_block;
  224. unsigned long scan_line_size;
  225. unsigned long uncompressed_size;
  226. unsigned int current_channel_offset = 0;
  227. s->channel_offsets[0] = -1;
  228. s->channel_offsets[1] = -1;
  229. s->channel_offsets[2] = -1;
  230. s->channel_offsets[3] = -1;
  231. s->bits_per_color_id = -1;
  232. s->compr = -1;
  233. if (buf_size < 10) {
  234. av_log(avctx, AV_LOG_ERROR, "Too short header to parse\n");
  235. return AVERROR_INVALIDDATA;
  236. }
  237. magic_number = bytestream_get_le32(&buf);
  238. if (magic_number != 20000630) { // As per documentation of OpenEXR it's supposed to be int 20000630 little-endian
  239. av_log(avctx, AV_LOG_ERROR, "Wrong magic number %d\n", magic_number);
  240. return AVERROR_INVALIDDATA;
  241. }
  242. version = bytestream_get_byte(&buf);
  243. if (version != 2) {
  244. av_log(avctx, AV_LOG_ERROR, "Unsupported version %d\n", version);
  245. return AVERROR_PATCHWELCOME;
  246. }
  247. flags = bytestream_get_le24(&buf);
  248. if (flags & 0x2) {
  249. av_log(avctx, AV_LOG_ERROR, "Tile based images are not supported\n");
  250. return AVERROR_PATCHWELCOME;
  251. }
  252. // Parse the header
  253. while (buf < buf_end && buf[0]) {
  254. unsigned int variable_buffer_data_size;
  255. // Process the channel list
  256. if (check_header_variable(avctx, &buf, buf_end, "channels", "chlist", 38, &variable_buffer_data_size) >= 0) {
  257. const uint8_t *channel_list_end;
  258. if (!variable_buffer_data_size)
  259. return AVERROR_INVALIDDATA;
  260. channel_list_end = buf + variable_buffer_data_size;
  261. while (channel_list_end - buf >= 19) {
  262. int current_bits_per_color_id = -1;
  263. int channel_index = -1;
  264. if (!strcmp(buf, "R"))
  265. channel_index = 0;
  266. else if (!strcmp(buf, "G"))
  267. channel_index = 1;
  268. else if (!strcmp(buf, "B"))
  269. channel_index = 2;
  270. else if (!strcmp(buf, "A"))
  271. channel_index = 3;
  272. else
  273. av_log(avctx, AV_LOG_WARNING, "Unsupported channel %.256s\n", buf);
  274. while (bytestream_get_byte(&buf) && buf < channel_list_end)
  275. continue; /* skip */
  276. if (channel_list_end - * &buf < 4) {
  277. av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
  278. return AVERROR_INVALIDDATA;
  279. }
  280. current_bits_per_color_id = bytestream_get_le32(&buf);
  281. if (current_bits_per_color_id > 2) {
  282. av_log(avctx, AV_LOG_ERROR, "Unknown color format\n");
  283. return AVERROR_INVALIDDATA;
  284. }
  285. if (channel_index >= 0) {
  286. if (s->bits_per_color_id != -1 && s->bits_per_color_id != current_bits_per_color_id) {
  287. av_log(avctx, AV_LOG_ERROR, "RGB channels not of the same depth\n");
  288. return AVERROR_INVALIDDATA;
  289. }
  290. s->bits_per_color_id = current_bits_per_color_id;
  291. s->channel_offsets[channel_index] = current_channel_offset;
  292. }
  293. current_channel_offset += 1 << current_bits_per_color_id;
  294. buf += 12;
  295. }
  296. /* Check if all channels are set with an offset or if the channels
  297. * are causing an overflow */
  298. if (FFMIN3(s->channel_offsets[0],
  299. s->channel_offsets[1],
  300. s->channel_offsets[2]) < 0) {
  301. if (s->channel_offsets[0] < 0)
  302. av_log(avctx, AV_LOG_ERROR, "Missing red channel\n");
  303. if (s->channel_offsets[1] < 0)
  304. av_log(avctx, AV_LOG_ERROR, "Missing green channel\n");
  305. if (s->channel_offsets[2] < 0)
  306. av_log(avctx, AV_LOG_ERROR, "Missing blue channel\n");
  307. return AVERROR_INVALIDDATA;
  308. }
  309. buf = channel_list_end;
  310. continue;
  311. } else if (check_header_variable(avctx, &buf, buf_end, "dataWindow", "box2i", 31, &variable_buffer_data_size) >= 0) {
  312. if (!variable_buffer_data_size)
  313. return AVERROR_INVALIDDATA;
  314. xmin = AV_RL32(buf);
  315. ymin = AV_RL32(buf + 4);
  316. xmax = AV_RL32(buf + 8);
  317. ymax = AV_RL32(buf + 12);
  318. xdelta = (xmax-xmin) + 1;
  319. ydelta = (ymax-ymin) + 1;
  320. buf += variable_buffer_data_size;
  321. continue;
  322. } else if (check_header_variable(avctx, &buf, buf_end, "displayWindow", "box2i", 34, &variable_buffer_data_size) >= 0) {
  323. if (!variable_buffer_data_size)
  324. return AVERROR_INVALIDDATA;
  325. w = AV_RL32(buf + 8) + 1;
  326. h = AV_RL32(buf + 12) + 1;
  327. buf += variable_buffer_data_size;
  328. continue;
  329. } else if (check_header_variable(avctx, &buf, buf_end, "lineOrder", "lineOrder", 25, &variable_buffer_data_size) >= 0) {
  330. if (!variable_buffer_data_size)
  331. return AVERROR_INVALIDDATA;
  332. av_log(avctx, AV_LOG_DEBUG, "line order : %d\n", *buf);
  333. if (*buf > 2) {
  334. av_log(avctx, AV_LOG_ERROR, "Unknown line order\n");
  335. return AVERROR_INVALIDDATA;
  336. }
  337. buf += variable_buffer_data_size;
  338. continue;
  339. } else if (check_header_variable(avctx, &buf, buf_end, "pixelAspectRatio", "float", 31, &variable_buffer_data_size) >= 0) {
  340. if (!variable_buffer_data_size)
  341. return AVERROR_INVALIDDATA;
  342. avctx->sample_aspect_ratio = av_d2q(av_int2float(AV_RL32(buf)), 255);
  343. buf += variable_buffer_data_size;
  344. continue;
  345. } else 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. if (buf_end - buf < (ydelta + scan_lines_per_block - 1) / scan_lines_per_block * 8)
  452. return AVERROR_INVALIDDATA;
  453. // Process the actual scan line blocks
  454. for (y = ymin; y <= ymax; y += scan_lines_per_block) {
  455. uint16_t *ptr_x;
  456. const uint8_t *red_channel_buffer, *green_channel_buffer, *blue_channel_buffer, *alpha_channel_buffer = 0;
  457. const uint64_t line_offset = bytestream_get_le64(&buf);
  458. int32_t data_size, line;
  459. // Check if the buffer has the required bytes needed from the offset
  460. if (line_offset > (uint64_t)buf_size - 8)
  461. return AVERROR_INVALIDDATA;
  462. src = avpkt->data + line_offset + 8;
  463. line = AV_RL32(src - 8);
  464. if (line < ymin || line > ymax)
  465. return AVERROR_INVALIDDATA;
  466. data_size = AV_RL32(src - 4);
  467. if (data_size <= 0 || data_size > buf_size)
  468. return AVERROR_INVALIDDATA;
  469. if ((s->compr == EXR_RAW && (data_size != uncompressed_size ||
  470. line_offset > buf_size - uncompressed_size)) ||
  471. (s->compr != EXR_RAW && line_offset > buf_size - data_size)) {
  472. return AVERROR_INVALIDDATA;
  473. }
  474. if (scan_lines_per_block > 1)
  475. uncompressed_size = scan_line_size * FFMIN(scan_lines_per_block, ymax - y + 1);
  476. if ((s->compr == EXR_ZIP1 || s->compr == EXR_ZIP16) && data_size < uncompressed_size) {
  477. unsigned long dest_len = uncompressed_size;
  478. if (uncompress(s->tmp, &dest_len, src, data_size) != Z_OK ||
  479. dest_len != uncompressed_size) {
  480. av_log(avctx, AV_LOG_ERROR, "error during zlib decompression\n");
  481. return AVERROR(EINVAL);
  482. }
  483. } else if (s->compr == EXR_RLE && data_size < uncompressed_size) {
  484. if (rle_uncompress(src, data_size, s->tmp, uncompressed_size)) {
  485. av_log(avctx, AV_LOG_ERROR, "error during rle decompression\n");
  486. return AVERROR(EINVAL);
  487. }
  488. }
  489. if (s->compr != EXR_RAW && data_size < uncompressed_size) {
  490. predictor(s->tmp, uncompressed_size);
  491. reorder_pixels(s->tmp, s->uncompressed_data, uncompressed_size);
  492. red_channel_buffer = s->uncompressed_data + xdelta * s->channel_offsets[0];
  493. green_channel_buffer = s->uncompressed_data + xdelta * s->channel_offsets[1];
  494. blue_channel_buffer = s->uncompressed_data + xdelta * s->channel_offsets[2];
  495. if (s->channel_offsets[3] >= 0)
  496. alpha_channel_buffer = s->uncompressed_data + xdelta * s->channel_offsets[3];
  497. } else {
  498. red_channel_buffer = src + xdelta * s->channel_offsets[0];
  499. green_channel_buffer = src + xdelta * s->channel_offsets[1];
  500. blue_channel_buffer = src + xdelta * s->channel_offsets[2];
  501. if (s->channel_offsets[3] >= 0)
  502. alpha_channel_buffer = src + xdelta * s->channel_offsets[3];
  503. }
  504. ptr = p->data[0] + line * stride;
  505. for (i = 0; i < scan_lines_per_block && y + i <= ymax; i++, ptr += stride) {
  506. const uint8_t *r, *g, *b, *a;
  507. r = red_channel_buffer;
  508. g = green_channel_buffer;
  509. b = blue_channel_buffer;
  510. if (alpha_channel_buffer)
  511. a = alpha_channel_buffer;
  512. ptr_x = (uint16_t *)ptr;
  513. // Zero out the start if xmin is not 0
  514. memset(ptr_x, 0, bxmin);
  515. ptr_x += xmin * desc->nb_components;
  516. if (s->bits_per_color_id == 2) {
  517. // 32-bit
  518. for (x = 0; x < xdelta; x++) {
  519. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&r));
  520. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&g));
  521. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&b));
  522. if (alpha_channel_buffer)
  523. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a));
  524. }
  525. } else {
  526. // 16-bit
  527. for (x = 0; x < xdelta; x++) {
  528. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&r));
  529. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&g));
  530. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&b));
  531. if (alpha_channel_buffer)
  532. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&a));
  533. }
  534. }
  535. // Zero out the end if xmax+1 is not w
  536. memset(ptr_x, 0, axmax);
  537. red_channel_buffer += scan_line_size;
  538. green_channel_buffer += scan_line_size;
  539. blue_channel_buffer += scan_line_size;
  540. if (alpha_channel_buffer)
  541. alpha_channel_buffer += scan_line_size;
  542. }
  543. }
  544. // Zero out the end if ymax+1 is not h
  545. for (y = ymax + 1; y < avctx->height; y++) {
  546. memset(ptr, 0, out_line_size);
  547. ptr += stride;
  548. }
  549. *picture = s->picture;
  550. *got_frame = 1;
  551. return buf_size;
  552. }
  553. static av_cold int decode_init(AVCodecContext *avctx)
  554. {
  555. EXRContext *s = avctx->priv_data;
  556. avcodec_get_frame_defaults(&s->picture);
  557. avctx->coded_frame = &s->picture;
  558. return 0;
  559. }
  560. static av_cold int decode_end(AVCodecContext *avctx)
  561. {
  562. EXRContext *s = avctx->priv_data;
  563. if (s->picture.data[0])
  564. avctx->release_buffer(avctx, &s->picture);
  565. av_freep(&s->uncompressed_data);
  566. av_freep(&s->tmp);
  567. return 0;
  568. }
  569. AVCodec ff_exr_decoder = {
  570. .name = "exr",
  571. .type = AVMEDIA_TYPE_VIDEO,
  572. .id = AV_CODEC_ID_EXR,
  573. .priv_data_size = sizeof(EXRContext),
  574. .init = decode_init,
  575. .close = decode_end,
  576. .decode = decode_frame,
  577. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
  578. .long_name = NULL_IF_CONFIG_SMALL("OpenEXR image"),
  579. };