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.

682 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. 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 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, flags, 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. 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. }
  310. // Process the dataWindow variable
  311. 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. buf += variable_buffer_data_size;
  320. continue;
  321. }
  322. // Process the displayWindow variable
  323. if (check_header_variable(avctx, &buf, buf_end, "displayWindow", "box2i", 34, &variable_buffer_data_size) >= 0) {
  324. if (!variable_buffer_data_size)
  325. return AVERROR_INVALIDDATA;
  326. w = AV_RL32(buf + 8) + 1;
  327. h = AV_RL32(buf + 12) + 1;
  328. buf += variable_buffer_data_size;
  329. continue;
  330. }
  331. // Process the lineOrder variable
  332. if (check_header_variable(avctx, &buf, buf_end, "lineOrder", "lineOrder", 25, &variable_buffer_data_size) >= 0) {
  333. if (!variable_buffer_data_size)
  334. return AVERROR_INVALIDDATA;
  335. if (*buf) {
  336. av_log(avctx, AV_LOG_ERROR, "Doesn't support this line order : %d\n", *buf);
  337. return AVERROR_PATCHWELCOME;
  338. }
  339. buf += variable_buffer_data_size;
  340. continue;
  341. }
  342. // Process the pixelAspectRatio variable
  343. if (check_header_variable(avctx, &buf, buf_end, "pixelAspectRatio", "float", 31, &variable_buffer_data_size) >= 0) {
  344. if (!variable_buffer_data_size)
  345. return AVERROR_INVALIDDATA;
  346. avctx->sample_aspect_ratio = av_d2q(av_int2float(AV_RL32(buf)), 255);
  347. buf += variable_buffer_data_size;
  348. continue;
  349. }
  350. // Process the compression variable
  351. if (check_header_variable(avctx, &buf, buf_end, "compression", "compression", 29, &variable_buffer_data_size) >= 0) {
  352. if (!variable_buffer_data_size)
  353. return AVERROR_INVALIDDATA;
  354. if (s->compr == -1)
  355. s->compr = *buf;
  356. else
  357. av_log(avctx, AV_LOG_WARNING, "Found more than one compression attribute\n");
  358. buf += variable_buffer_data_size;
  359. continue;
  360. }
  361. // Check if there is enough bytes for a header
  362. if (buf_end - buf <= 9) {
  363. av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
  364. return AVERROR_INVALIDDATA;
  365. }
  366. // Process unknown variables
  367. for (i = 0; i < 2; i++) {
  368. // Skip variable name/type
  369. while (++buf < buf_end)
  370. if (buf[0] == 0x0)
  371. break;
  372. }
  373. buf++;
  374. // Skip variable length
  375. if (buf_end - buf >= 5) {
  376. variable_buffer_data_size = get_header_variable_length(&buf, buf_end);
  377. if (!variable_buffer_data_size) {
  378. av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
  379. return AVERROR_INVALIDDATA;
  380. }
  381. buf += variable_buffer_data_size;
  382. }
  383. }
  384. if (s->compr == -1) {
  385. av_log(avctx, AV_LOG_ERROR, "Missing compression attribute\n");
  386. return AVERROR_INVALIDDATA;
  387. }
  388. if (buf >= buf_end) {
  389. av_log(avctx, AV_LOG_ERROR, "Incomplete frame\n");
  390. return AVERROR_INVALIDDATA;
  391. }
  392. buf++;
  393. switch (s->bits_per_color_id) {
  394. case 2: // 32-bit
  395. case 1: // 16-bit
  396. if (s->channel_offsets[3] >= 0)
  397. avctx->pix_fmt = AV_PIX_FMT_RGBA64;
  398. else
  399. avctx->pix_fmt = AV_PIX_FMT_RGB48;
  400. break;
  401. // 8-bit
  402. case 0:
  403. av_log_missing_feature(avctx, "8-bit OpenEXR", 1);
  404. return AVERROR_PATCHWELCOME;
  405. default:
  406. av_log(avctx, AV_LOG_ERROR, "Unknown color format : %d\n", s->bits_per_color_id);
  407. return AVERROR_INVALIDDATA;
  408. }
  409. switch (s->compr) {
  410. case EXR_RAW:
  411. case EXR_RLE:
  412. case EXR_ZIP1:
  413. scan_lines_per_block = 1;
  414. break;
  415. case EXR_ZIP16:
  416. scan_lines_per_block = 16;
  417. break;
  418. default:
  419. av_log(avctx, AV_LOG_ERROR, "Compression type %d is not supported\n", s->compr);
  420. return AVERROR_PATCHWELCOME;
  421. }
  422. if (s->picture.data[0])
  423. ff_thread_release_buffer(avctx, &s->picture);
  424. if (av_image_check_size(w, h, 0, avctx))
  425. return AVERROR_INVALIDDATA;
  426. // Verify the xmin, xmax, ymin, ymax and xdelta before setting the actual image size
  427. if (xmin > xmax || ymin > ymax || xdelta != xmax - xmin + 1 || xmax >= w || ymax >= h) {
  428. av_log(avctx, AV_LOG_ERROR, "Wrong sizing or missing size information\n");
  429. return AVERROR_INVALIDDATA;
  430. }
  431. if (w != avctx->width || h != avctx->height) {
  432. avcodec_set_dimensions(avctx, w, h);
  433. }
  434. desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  435. bxmin = xmin * 2 * desc->nb_components;
  436. axmax = (avctx->width - (xmax + 1)) * 2 * desc->nb_components;
  437. out_line_size = avctx->width * 2 * desc->nb_components;
  438. scan_line_size = xdelta * current_channel_offset;
  439. uncompressed_size = scan_line_size * scan_lines_per_block;
  440. if (s->compr != EXR_RAW) {
  441. av_fast_padded_malloc(&s->uncompressed_data, &s->uncompressed_size, uncompressed_size);
  442. av_fast_padded_malloc(&s->tmp, &s->tmp_size, uncompressed_size);
  443. if (!s->uncompressed_data || !s->tmp)
  444. return AVERROR(ENOMEM);
  445. }
  446. if ((ret = ff_thread_get_buffer(avctx, p)) < 0) {
  447. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  448. return ret;
  449. }
  450. ptr = p->data[0];
  451. stride = p->linesize[0];
  452. // Zero out the start if ymin is not 0
  453. for (y = 0; y < ymin; y++) {
  454. memset(ptr, 0, out_line_size);
  455. ptr += stride;
  456. }
  457. // Process the actual scan line blocks
  458. for (y = ymin; y <= ymax; y += scan_lines_per_block) {
  459. uint16_t *ptr_x = (uint16_t *)ptr;
  460. if (buf_end - buf > 8) {
  461. /* Read the lineoffset from the line offset table and add 8 bytes
  462. to skip the coordinates and data size fields */
  463. const uint64_t line_offset = bytestream_get_le64(&buf) + 8;
  464. int32_t data_size;
  465. // Check if the buffer has the required bytes needed from the offset
  466. if ((line_offset > buf_size) ||
  467. (s->compr == EXR_RAW && line_offset > avpkt->size - xdelta * current_channel_offset) ||
  468. (s->compr != EXR_RAW && line_offset > buf_size - (data_size = AV_RL32(avpkt->data + line_offset - 4)))) {
  469. // Line offset is probably wrong and not inside the buffer
  470. av_log(avctx, AV_LOG_WARNING, "Line offset for line %d is out of reach setting it to black\n", y);
  471. for (i = 0; i < scan_lines_per_block && y + i <= ymax; i++, ptr += stride) {
  472. ptr_x = (uint16_t *)ptr;
  473. memset(ptr_x, 0, out_line_size);
  474. }
  475. } else {
  476. const uint8_t *red_channel_buffer, *green_channel_buffer, *blue_channel_buffer, *alpha_channel_buffer = 0;
  477. if (scan_lines_per_block > 1)
  478. uncompressed_size = scan_line_size * FFMIN(scan_lines_per_block, ymax - y + 1);
  479. if ((s->compr == EXR_ZIP1 || s->compr == EXR_ZIP16) && data_size < uncompressed_size) {
  480. unsigned long dest_len = uncompressed_size;
  481. if (uncompress(s->tmp, &dest_len, avpkt->data + line_offset, data_size) != Z_OK ||
  482. dest_len != uncompressed_size) {
  483. av_log(avctx, AV_LOG_ERROR, "error during zlib decompression\n");
  484. return AVERROR(EINVAL);
  485. }
  486. } else if (s->compr == EXR_RLE && data_size < uncompressed_size) {
  487. if (rle_uncompress(avpkt->data + line_offset, data_size, s->tmp, uncompressed_size)) {
  488. av_log(avctx, AV_LOG_ERROR, "error during rle decompression\n");
  489. return AVERROR(EINVAL);
  490. }
  491. }
  492. if (s->compr != EXR_RAW && data_size < uncompressed_size) {
  493. predictor(s->tmp, uncompressed_size);
  494. reorder_pixels(s->tmp, s->uncompressed_data, uncompressed_size);
  495. red_channel_buffer = s->uncompressed_data + xdelta * s->channel_offsets[0];
  496. green_channel_buffer = s->uncompressed_data + xdelta * s->channel_offsets[1];
  497. blue_channel_buffer = s->uncompressed_data + xdelta * s->channel_offsets[2];
  498. if (s->channel_offsets[3] >= 0)
  499. alpha_channel_buffer = s->uncompressed_data + xdelta * s->channel_offsets[3];
  500. } else {
  501. red_channel_buffer = avpkt->data + line_offset + xdelta * s->channel_offsets[0];
  502. green_channel_buffer = avpkt->data + line_offset + xdelta * s->channel_offsets[1];
  503. blue_channel_buffer = avpkt->data + line_offset + xdelta * s->channel_offsets[2];
  504. if (s->channel_offsets[3] >= 0)
  505. alpha_channel_buffer = avpkt->data + line_offset + xdelta * s->channel_offsets[3];
  506. }
  507. for (i = 0; i < scan_lines_per_block && y + i <= ymax; i++, ptr += stride) {
  508. const uint8_t *r, *g, *b, *a;
  509. r = red_channel_buffer;
  510. g = green_channel_buffer;
  511. b = blue_channel_buffer;
  512. if (alpha_channel_buffer)
  513. a = alpha_channel_buffer;
  514. ptr_x = (uint16_t *)ptr;
  515. // Zero out the start if xmin is not 0
  516. memset(ptr_x, 0, bxmin);
  517. ptr_x += xmin * desc->nb_components;
  518. if (s->bits_per_color_id == 2) {
  519. // 32-bit
  520. for (x = 0; x < xdelta; x++) {
  521. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&r));
  522. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&g));
  523. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&b));
  524. if (alpha_channel_buffer)
  525. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a));
  526. }
  527. } else {
  528. // 16-bit
  529. for (x = 0; x < xdelta; x++) {
  530. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&r));
  531. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&g));
  532. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&b));
  533. if (alpha_channel_buffer)
  534. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&a));
  535. }
  536. }
  537. // Zero out the end if xmax+1 is not w
  538. memset(ptr_x, 0, axmax);
  539. red_channel_buffer += scan_line_size;
  540. green_channel_buffer += scan_line_size;
  541. blue_channel_buffer += scan_line_size;
  542. if (alpha_channel_buffer)
  543. alpha_channel_buffer += scan_line_size;
  544. }
  545. }
  546. }
  547. }
  548. // Zero out the end if ymax+1 is not h
  549. for (y = ymax + 1; y < avctx->height; y++) {
  550. memset(ptr, 0, out_line_size);
  551. ptr += stride;
  552. }
  553. *picture = s->picture;
  554. *got_frame = 1;
  555. return buf_size;
  556. }
  557. static av_cold int decode_init(AVCodecContext *avctx)
  558. {
  559. EXRContext *s = avctx->priv_data;
  560. avcodec_get_frame_defaults(&s->picture);
  561. avctx->coded_frame = &s->picture;
  562. return 0;
  563. }
  564. static av_cold int decode_end(AVCodecContext *avctx)
  565. {
  566. EXRContext *s = avctx->priv_data;
  567. if (s->picture.data[0])
  568. avctx->release_buffer(avctx, &s->picture);
  569. av_freep(&s->uncompressed_data);
  570. av_freep(&s->tmp);
  571. return 0;
  572. }
  573. AVCodec ff_exr_decoder = {
  574. .name = "exr",
  575. .type = AVMEDIA_TYPE_VIDEO,
  576. .id = AV_CODEC_ID_EXR,
  577. .priv_data_size = sizeof(EXRContext),
  578. .init = decode_init,
  579. .close = decode_end,
  580. .decode = decode_frame,
  581. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
  582. .long_name = NULL_IF_CONFIG_SMALL("OpenEXR image"),
  583. };