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.

814 lines
25KB

  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. #include "libavutil/avassert.h"
  38. enum ExrCompr {
  39. EXR_RAW = 0,
  40. EXR_RLE = 1,
  41. EXR_ZIP1 = 2,
  42. EXR_ZIP16 = 3,
  43. EXR_PIZ = 4,
  44. EXR_PXR24 = 5,
  45. EXR_B44 = 6,
  46. EXR_B44A = 7,
  47. };
  48. enum ExrPixelType {
  49. EXR_UINT,
  50. EXR_HALF,
  51. EXR_FLOAT
  52. };
  53. typedef struct EXRChannel {
  54. int xsub, ysub;
  55. enum ExrPixelType pixel_type;
  56. } EXRChannel;
  57. typedef struct EXRThreadData {
  58. uint8_t *uncompressed_data;
  59. int uncompressed_size;
  60. uint8_t *tmp;
  61. int tmp_size;
  62. } EXRThreadData;
  63. typedef struct EXRContext {
  64. AVFrame *picture;
  65. int compr;
  66. enum ExrPixelType pixel_type;
  67. int channel_offsets[4]; // 0 = red, 1 = green, 2 = blue and 3 = alpha
  68. const AVPixFmtDescriptor *desc;
  69. uint32_t xmax, xmin;
  70. uint32_t ymax, ymin;
  71. uint32_t xdelta, ydelta;
  72. int ysize;
  73. uint64_t scan_line_size;
  74. int scan_lines_per_block;
  75. const uint8_t *buf, *table;
  76. int buf_size;
  77. EXRChannel *channels;
  78. int nb_channels;
  79. EXRThreadData *thread_data;
  80. int thread_data_size;
  81. } EXRContext;
  82. /**
  83. * Converts from 32-bit float as uint32_t to uint16_t
  84. *
  85. * @param v 32-bit float
  86. * @return normalized 16-bit unsigned int
  87. */
  88. static inline uint16_t exr_flt2uint(uint32_t v)
  89. {
  90. unsigned int exp = v >> 23;
  91. // "HACK": negative values result in exp< 0, so clipping them to 0
  92. // is also handled by this condition, avoids explicit check for sign bit.
  93. if (exp<= 127 + 7 - 24) // we would shift out all bits anyway
  94. return 0;
  95. if (exp >= 127)
  96. return 0xffff;
  97. v &= 0x007fffff;
  98. return (v + (1 << 23)) >> (127 + 7 - exp);
  99. }
  100. /**
  101. * Converts from 16-bit float as uint16_t to uint16_t
  102. *
  103. * @param v 16-bit float
  104. * @return normalized 16-bit unsigned int
  105. */
  106. static inline uint16_t exr_halflt2uint(uint16_t v)
  107. {
  108. unsigned exp = 14 - (v >> 10);
  109. if (exp >= 14) {
  110. if (exp == 14) return (v >> 9) & 1;
  111. else return (v & 0x8000) ? 0 : 0xffff;
  112. }
  113. v <<= 6;
  114. return (v + (1 << 16)) >> (exp + 1);
  115. }
  116. /**
  117. * Gets the size of the header variable
  118. *
  119. * @param **buf the current pointer location in the header where
  120. * the variable data starts
  121. * @param *buf_end pointer location of the end of the buffer
  122. * @return size of variable data
  123. */
  124. static unsigned int get_header_variable_length(const uint8_t **buf,
  125. const uint8_t *buf_end)
  126. {
  127. unsigned int variable_buffer_data_size = bytestream_get_le32(buf);
  128. if (variable_buffer_data_size >= buf_end - *buf)
  129. return 0;
  130. return variable_buffer_data_size;
  131. }
  132. /**
  133. * Checks if the variable name corresponds with it's data type
  134. *
  135. * @param *avctx the AVCodecContext
  136. * @param **buf the current pointer location in the header where
  137. * the variable name starts
  138. * @param *buf_end pointer location of the end of the buffer
  139. * @param *value_name name of the varible to check
  140. * @param *value_type type of the varible to check
  141. * @param minimum_length minimum length of the variable data
  142. * @param variable_buffer_data_size variable length read from the header
  143. * after it's checked
  144. * @return negative if variable is invalid
  145. */
  146. static int check_header_variable(AVCodecContext *avctx,
  147. const uint8_t **buf,
  148. const uint8_t *buf_end,
  149. const char *value_name,
  150. const char *value_type,
  151. unsigned int minimum_length,
  152. unsigned int *variable_buffer_data_size)
  153. {
  154. if (buf_end - *buf >= minimum_length && !strcmp(*buf, value_name)) {
  155. *buf += strlen(value_name)+1;
  156. if (!strcmp(*buf, value_type)) {
  157. *buf += strlen(value_type)+1;
  158. *variable_buffer_data_size = get_header_variable_length(buf, buf_end);
  159. if (!*variable_buffer_data_size)
  160. av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
  161. return 1;
  162. }
  163. *buf -= strlen(value_name)+1;
  164. av_log(avctx, AV_LOG_WARNING, "Unknown data type for header variable %s\n", value_name);
  165. }
  166. return -1;
  167. }
  168. static void predictor(uint8_t *src, int size)
  169. {
  170. uint8_t *t = src + 1;
  171. uint8_t *stop = src + size;
  172. while (t < stop) {
  173. int d = (int)t[-1] + (int)t[0] - 128;
  174. t[0] = d;
  175. ++t;
  176. }
  177. }
  178. static void reorder_pixels(uint8_t *src, uint8_t *dst, int size)
  179. {
  180. const int8_t *t1 = src;
  181. const int8_t *t2 = src + (size + 1) / 2;
  182. int8_t *s = dst;
  183. int8_t *stop = s + size;
  184. while (1) {
  185. if (s < stop)
  186. *(s++) = *(t1++);
  187. else
  188. break;
  189. if (s < stop)
  190. *(s++) = *(t2++);
  191. else
  192. break;
  193. }
  194. }
  195. static int zip_uncompress(const uint8_t *src, int compressed_size,
  196. int uncompressed_size, EXRThreadData *td)
  197. {
  198. unsigned long dest_len = uncompressed_size;
  199. if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK ||
  200. dest_len != uncompressed_size)
  201. return AVERROR(EINVAL);
  202. predictor(td->tmp, uncompressed_size);
  203. reorder_pixels(td->tmp, td->uncompressed_data, uncompressed_size);
  204. return 0;
  205. }
  206. static int rle_uncompress(const uint8_t *src, int compressed_size,
  207. int uncompressed_size, EXRThreadData *td)
  208. {
  209. int8_t *d = (int8_t *)td->tmp;
  210. const int8_t *s = (const int8_t *)src;
  211. int ssize = compressed_size;
  212. int dsize = uncompressed_size;
  213. int8_t *dend = d + dsize;
  214. int count;
  215. while (ssize > 0) {
  216. count = *s++;
  217. if (count < 0) {
  218. count = -count;
  219. if ((dsize -= count ) < 0 ||
  220. (ssize -= count + 1) < 0)
  221. return -1;
  222. while (count--)
  223. *d++ = *s++;
  224. } else {
  225. count++;
  226. if ((dsize -= count) < 0 ||
  227. (ssize -= 2 ) < 0)
  228. return -1;
  229. while (count--)
  230. *d++ = *s;
  231. s++;
  232. }
  233. }
  234. if (dend != d)
  235. return AVERROR_INVALIDDATA;
  236. predictor(td->tmp, uncompressed_size);
  237. reorder_pixels(td->tmp, td->uncompressed_data, uncompressed_size);
  238. return 0;
  239. }
  240. static int pxr24_uncompress(EXRContext *s, const uint8_t *src,
  241. int compressed_size, int uncompressed_size,
  242. EXRThreadData *td)
  243. {
  244. unsigned long dest_len = uncompressed_size;
  245. const uint8_t *in = td->tmp;
  246. uint8_t *out;
  247. int c, i, j;
  248. if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK ||
  249. dest_len != uncompressed_size)
  250. return AVERROR(EINVAL);
  251. out = td->uncompressed_data;
  252. for (i = 0; i < s->ysize; i++) {
  253. for (c = 0; c < s->nb_channels; c++) {
  254. EXRChannel *channel = &s->channels[c];
  255. const uint8_t *ptr[4];
  256. uint32_t pixel = 0;
  257. switch (channel->pixel_type) {
  258. case EXR_FLOAT:
  259. ptr[0] = in;
  260. ptr[1] = ptr[0] + s->xdelta;
  261. ptr[2] = ptr[1] + s->xdelta;
  262. in = ptr[2] + s->xdelta;
  263. for (j = 0; j < s->xdelta; ++j) {
  264. uint32_t diff = (*(ptr[0]++) << 24) |
  265. (*(ptr[1]++) << 16) |
  266. (*(ptr[2]++) << 8);
  267. pixel += diff;
  268. bytestream_put_le32(&out, pixel);
  269. }
  270. break;
  271. case EXR_HALF:
  272. ptr[0] = in;
  273. ptr[1] = ptr[0] + s->xdelta;
  274. in = ptr[1] + s->xdelta;
  275. for (j = 0; j < s->xdelta; j++) {
  276. uint32_t diff = (*(ptr[0]++) << 8) | *(ptr[1]++);
  277. pixel += diff;
  278. bytestream_put_le16(&out, pixel);
  279. }
  280. break;
  281. default:
  282. av_assert1(0);
  283. }
  284. }
  285. }
  286. return 0;
  287. }
  288. static int decode_block(AVCodecContext *avctx, void *tdata,
  289. int jobnr, int threadnr)
  290. {
  291. EXRContext *s = avctx->priv_data;
  292. AVFrame *const p = s->picture;
  293. EXRThreadData *td = &s->thread_data[threadnr];
  294. const uint8_t *channel_buffer[4] = { 0 };
  295. const uint8_t *buf = s->buf;
  296. uint64_t line_offset, uncompressed_size;
  297. uint32_t xdelta = s->xdelta;
  298. uint16_t *ptr_x;
  299. uint8_t *ptr;
  300. int32_t data_size, line;
  301. const uint8_t *src;
  302. int axmax = (avctx->width - (s->xmax + 1)) * 2 * s->desc->nb_components;
  303. int bxmin = s->xmin * 2 * s->desc->nb_components;
  304. int i, x, buf_size = s->buf_size;
  305. int av_unused ret;
  306. line_offset = AV_RL64(s->table + jobnr * 8);
  307. // Check if the buffer has the required bytes needed from the offset
  308. if (line_offset > buf_size - 8)
  309. return AVERROR_INVALIDDATA;
  310. src = buf + line_offset + 8;
  311. line = AV_RL32(src - 8);
  312. if (line < s->ymin || line > s->ymax)
  313. return AVERROR_INVALIDDATA;
  314. data_size = AV_RL32(src - 4);
  315. if (data_size <= 0 || data_size > buf_size)
  316. return AVERROR_INVALIDDATA;
  317. s->ysize = FFMIN(s->scan_lines_per_block, s->ymax - line + 1);
  318. uncompressed_size = s->scan_line_size * s->ysize;
  319. if ((s->compr == EXR_RAW && (data_size != uncompressed_size ||
  320. line_offset > buf_size - uncompressed_size)) ||
  321. (s->compr != EXR_RAW && (data_size > uncompressed_size ||
  322. line_offset > buf_size - data_size))) {
  323. return AVERROR_INVALIDDATA;
  324. }
  325. if (data_size < uncompressed_size) {
  326. av_fast_padded_malloc(&td->uncompressed_data, &td->uncompressed_size, uncompressed_size);
  327. av_fast_padded_malloc(&td->tmp, &td->tmp_size, uncompressed_size);
  328. if (!td->uncompressed_data || !td->tmp)
  329. return AVERROR(ENOMEM);
  330. switch (s->compr) {
  331. case EXR_ZIP1:
  332. case EXR_ZIP16:
  333. ret = zip_uncompress(src, data_size, uncompressed_size, td);
  334. break;
  335. case EXR_PXR24:
  336. ret = pxr24_uncompress(s, src, data_size, uncompressed_size, td);
  337. break;
  338. case EXR_RLE:
  339. ret = rle_uncompress(src, data_size, uncompressed_size, td);
  340. }
  341. src = td->uncompressed_data;
  342. }
  343. channel_buffer[0] = src + xdelta * s->channel_offsets[0];
  344. channel_buffer[1] = src + xdelta * s->channel_offsets[1];
  345. channel_buffer[2] = src + xdelta * s->channel_offsets[2];
  346. if (s->channel_offsets[3] >= 0)
  347. channel_buffer[3] = src + xdelta * s->channel_offsets[3];
  348. ptr = p->data[0] + line * p->linesize[0];
  349. for (i = 0; i < s->scan_lines_per_block && line + i <= s->ymax; i++, ptr += p->linesize[0]) {
  350. const uint8_t *r, *g, *b, *a;
  351. r = channel_buffer[0];
  352. g = channel_buffer[1];
  353. b = channel_buffer[2];
  354. if (channel_buffer[3])
  355. a = channel_buffer[3];
  356. ptr_x = (uint16_t *)ptr;
  357. // Zero out the start if xmin is not 0
  358. memset(ptr_x, 0, bxmin);
  359. ptr_x += s->xmin * s->desc->nb_components;
  360. if (s->pixel_type == EXR_FLOAT) {
  361. // 32-bit
  362. for (x = 0; x < xdelta; x++) {
  363. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&r));
  364. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&g));
  365. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&b));
  366. if (channel_buffer[3])
  367. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a));
  368. }
  369. } else {
  370. // 16-bit
  371. for (x = 0; x < xdelta; x++) {
  372. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&r));
  373. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&g));
  374. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&b));
  375. if (channel_buffer[3])
  376. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&a));
  377. }
  378. }
  379. // Zero out the end if xmax+1 is not w
  380. memset(ptr_x, 0, axmax);
  381. channel_buffer[0] += s->scan_line_size;
  382. channel_buffer[1] += s->scan_line_size;
  383. channel_buffer[2] += s->scan_line_size;
  384. if (channel_buffer[3])
  385. channel_buffer[3] += s->scan_line_size;
  386. }
  387. return 0;
  388. }
  389. static int decode_frame(AVCodecContext *avctx,
  390. void *data,
  391. int *got_frame,
  392. AVPacket *avpkt)
  393. {
  394. const uint8_t *buf = avpkt->data;
  395. unsigned int buf_size = avpkt->size;
  396. const uint8_t *buf_end = buf + buf_size;
  397. EXRContext *const s = avctx->priv_data;
  398. ThreadFrame frame = { .f = data };
  399. AVFrame *picture = data;
  400. uint8_t *ptr;
  401. int i, y, magic_number, version, flags, ret;
  402. int w = 0;
  403. int h = 0;
  404. int out_line_size;
  405. int scan_line_blocks;
  406. unsigned int current_channel_offset = 0;
  407. s->xmin = ~0;
  408. s->xmax = ~0;
  409. s->ymin = ~0;
  410. s->ymax = ~0;
  411. s->xdelta = ~0;
  412. s->ydelta = ~0;
  413. s->channel_offsets[0] = -1;
  414. s->channel_offsets[1] = -1;
  415. s->channel_offsets[2] = -1;
  416. s->channel_offsets[3] = -1;
  417. s->pixel_type = -1;
  418. s->nb_channels = 0;
  419. s->compr = -1;
  420. s->buf = buf;
  421. s->buf_size = buf_size;
  422. if (buf_size < 10) {
  423. av_log(avctx, AV_LOG_ERROR, "Too short header to parse\n");
  424. return AVERROR_INVALIDDATA;
  425. }
  426. magic_number = bytestream_get_le32(&buf);
  427. if (magic_number != 20000630) { // As per documentation of OpenEXR it's supposed to be int 20000630 little-endian
  428. av_log(avctx, AV_LOG_ERROR, "Wrong magic number %d\n", magic_number);
  429. return AVERROR_INVALIDDATA;
  430. }
  431. version = bytestream_get_byte(&buf);
  432. if (version != 2) {
  433. av_log(avctx, AV_LOG_ERROR, "Unsupported version %d\n", version);
  434. return AVERROR_PATCHWELCOME;
  435. }
  436. flags = bytestream_get_le24(&buf);
  437. if (flags & 0x2) {
  438. av_log(avctx, AV_LOG_ERROR, "Tile based images are not supported\n");
  439. return AVERROR_PATCHWELCOME;
  440. }
  441. // Parse the header
  442. while (buf < buf_end && buf[0]) {
  443. unsigned int variable_buffer_data_size;
  444. // Process the channel list
  445. if (check_header_variable(avctx, &buf, buf_end, "channels", "chlist", 38, &variable_buffer_data_size) >= 0) {
  446. const uint8_t *channel_list_end;
  447. if (!variable_buffer_data_size)
  448. return AVERROR_INVALIDDATA;
  449. channel_list_end = buf + variable_buffer_data_size;
  450. while (channel_list_end - buf >= 19) {
  451. EXRChannel *channel;
  452. int current_pixel_type = -1;
  453. int channel_index = -1;
  454. int xsub, ysub;
  455. if (!strcmp(buf, "R"))
  456. channel_index = 0;
  457. else if (!strcmp(buf, "G"))
  458. channel_index = 1;
  459. else if (!strcmp(buf, "B"))
  460. channel_index = 2;
  461. else if (!strcmp(buf, "A"))
  462. channel_index = 3;
  463. else
  464. av_log(avctx, AV_LOG_WARNING, "Unsupported channel %.256s\n", buf);
  465. while (bytestream_get_byte(&buf) && buf < channel_list_end)
  466. continue; /* skip */
  467. if (channel_list_end - * &buf < 4) {
  468. av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
  469. return AVERROR_INVALIDDATA;
  470. }
  471. current_pixel_type = bytestream_get_le32(&buf);
  472. if (current_pixel_type > 2) {
  473. av_log(avctx, AV_LOG_ERROR, "Unknown pixel type\n");
  474. return AVERROR_INVALIDDATA;
  475. }
  476. buf += 4;
  477. xsub = bytestream_get_le32(&buf);
  478. ysub = bytestream_get_le32(&buf);
  479. if (xsub != 1 || ysub != 1) {
  480. av_log(avctx, AV_LOG_ERROR, "Unsupported subsampling %dx%d\n", xsub, ysub);
  481. return AVERROR_PATCHWELCOME;
  482. }
  483. if (channel_index >= 0) {
  484. if (s->pixel_type != -1 && s->pixel_type != current_pixel_type) {
  485. av_log(avctx, AV_LOG_ERROR, "RGB channels not of the same depth\n");
  486. return AVERROR_INVALIDDATA;
  487. }
  488. s->pixel_type = current_pixel_type;
  489. s->channel_offsets[channel_index] = current_channel_offset;
  490. }
  491. s->channels = av_realloc_f(s->channels, ++s->nb_channels, sizeof(EXRChannel));
  492. if (!s->channels)
  493. return AVERROR(ENOMEM);
  494. channel = &s->channels[s->nb_channels - 1];
  495. channel->pixel_type = current_pixel_type;
  496. channel->xsub = xsub;
  497. channel->ysub = ysub;
  498. current_channel_offset += 1 << current_pixel_type;
  499. }
  500. /* Check if all channels are set with an offset or if the channels
  501. * are causing an overflow */
  502. if (FFMIN3(s->channel_offsets[0],
  503. s->channel_offsets[1],
  504. s->channel_offsets[2]) < 0) {
  505. if (s->channel_offsets[0] < 0)
  506. av_log(avctx, AV_LOG_ERROR, "Missing red channel\n");
  507. if (s->channel_offsets[1] < 0)
  508. av_log(avctx, AV_LOG_ERROR, "Missing green channel\n");
  509. if (s->channel_offsets[2] < 0)
  510. av_log(avctx, AV_LOG_ERROR, "Missing blue channel\n");
  511. return AVERROR_INVALIDDATA;
  512. }
  513. buf = channel_list_end;
  514. continue;
  515. } else if (check_header_variable(avctx, &buf, buf_end, "dataWindow", "box2i", 31, &variable_buffer_data_size) >= 0) {
  516. if (!variable_buffer_data_size)
  517. return AVERROR_INVALIDDATA;
  518. s->xmin = AV_RL32(buf);
  519. s->ymin = AV_RL32(buf + 4);
  520. s->xmax = AV_RL32(buf + 8);
  521. s->ymax = AV_RL32(buf + 12);
  522. s->xdelta = (s->xmax - s->xmin) + 1;
  523. s->ydelta = (s->ymax - s->ymin) + 1;
  524. buf += variable_buffer_data_size;
  525. continue;
  526. } else if (check_header_variable(avctx, &buf, buf_end, "displayWindow", "box2i", 34, &variable_buffer_data_size) >= 0) {
  527. if (!variable_buffer_data_size)
  528. return AVERROR_INVALIDDATA;
  529. w = AV_RL32(buf + 8) + 1;
  530. h = AV_RL32(buf + 12) + 1;
  531. buf += variable_buffer_data_size;
  532. continue;
  533. } else if (check_header_variable(avctx, &buf, buf_end, "lineOrder", "lineOrder", 25, &variable_buffer_data_size) >= 0) {
  534. if (!variable_buffer_data_size)
  535. return AVERROR_INVALIDDATA;
  536. av_log(avctx, AV_LOG_DEBUG, "line order : %d\n", *buf);
  537. if (*buf > 2) {
  538. av_log(avctx, AV_LOG_ERROR, "Unknown line order\n");
  539. return AVERROR_INVALIDDATA;
  540. }
  541. buf += variable_buffer_data_size;
  542. continue;
  543. } else if (check_header_variable(avctx, &buf, buf_end, "pixelAspectRatio", "float", 31, &variable_buffer_data_size) >= 0) {
  544. if (!variable_buffer_data_size)
  545. return AVERROR_INVALIDDATA;
  546. avctx->sample_aspect_ratio = av_d2q(av_int2float(AV_RL32(buf)), 255);
  547. buf += variable_buffer_data_size;
  548. continue;
  549. } else if (check_header_variable(avctx, &buf, buf_end, "compression", "compression", 29, &variable_buffer_data_size) >= 0) {
  550. if (!variable_buffer_data_size)
  551. return AVERROR_INVALIDDATA;
  552. if (s->compr == -1)
  553. s->compr = *buf;
  554. else
  555. av_log(avctx, AV_LOG_WARNING, "Found more than one compression attribute\n");
  556. buf += variable_buffer_data_size;
  557. continue;
  558. }
  559. // Check if there is enough bytes for a header
  560. if (buf_end - buf <= 9) {
  561. av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
  562. return AVERROR_INVALIDDATA;
  563. }
  564. // Process unknown variables
  565. for (i = 0; i < 2; i++) {
  566. // Skip variable name/type
  567. while (++buf < buf_end)
  568. if (buf[0] == 0x0)
  569. break;
  570. }
  571. buf++;
  572. // Skip variable length
  573. if (buf_end - buf >= 5) {
  574. variable_buffer_data_size = get_header_variable_length(&buf, buf_end);
  575. if (!variable_buffer_data_size) {
  576. av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
  577. return AVERROR_INVALIDDATA;
  578. }
  579. buf += variable_buffer_data_size;
  580. }
  581. }
  582. if (s->compr == -1) {
  583. av_log(avctx, AV_LOG_ERROR, "Missing compression attribute\n");
  584. return AVERROR_INVALIDDATA;
  585. }
  586. if (buf >= buf_end) {
  587. av_log(avctx, AV_LOG_ERROR, "Incomplete frame\n");
  588. return AVERROR_INVALIDDATA;
  589. }
  590. buf++;
  591. switch (s->pixel_type) {
  592. case EXR_FLOAT:
  593. case EXR_HALF:
  594. if (s->channel_offsets[3] >= 0)
  595. avctx->pix_fmt = AV_PIX_FMT_RGBA64;
  596. else
  597. avctx->pix_fmt = AV_PIX_FMT_RGB48;
  598. break;
  599. case EXR_UINT:
  600. avpriv_request_sample(avctx, "32-bit unsigned int");
  601. return AVERROR_PATCHWELCOME;
  602. default:
  603. av_log(avctx, AV_LOG_ERROR, "Missing channel list\n");
  604. return AVERROR_INVALIDDATA;
  605. }
  606. switch (s->compr) {
  607. case EXR_RAW:
  608. case EXR_RLE:
  609. case EXR_ZIP1:
  610. s->scan_lines_per_block = 1;
  611. break;
  612. case EXR_PXR24:
  613. case EXR_ZIP16:
  614. s->scan_lines_per_block = 16;
  615. break;
  616. default:
  617. av_log(avctx, AV_LOG_ERROR, "Compression type %d is not supported\n", s->compr);
  618. return AVERROR_PATCHWELCOME;
  619. }
  620. if (av_image_check_size(w, h, 0, avctx))
  621. return AVERROR_INVALIDDATA;
  622. // Verify the xmin, xmax, ymin, ymax and xdelta before setting the actual image size
  623. if (s->xmin > s->xmax ||
  624. s->ymin > s->ymax ||
  625. s->xdelta != s->xmax - s->xmin + 1 ||
  626. s->xmax >= w || s->ymax >= h) {
  627. av_log(avctx, AV_LOG_ERROR, "Wrong sizing or missing size information\n");
  628. return AVERROR_INVALIDDATA;
  629. }
  630. if (w != avctx->width || h != avctx->height) {
  631. avcodec_set_dimensions(avctx, w, h);
  632. }
  633. s->desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  634. out_line_size = avctx->width * 2 * s->desc->nb_components;
  635. s->scan_line_size = s->xdelta * current_channel_offset;
  636. scan_line_blocks = (s->ydelta + s->scan_lines_per_block - 1) / s->scan_lines_per_block;
  637. if (s->compr != EXR_RAW) {
  638. size_t thread_data_size, prev_size;
  639. EXRThreadData *m;
  640. prev_size = s->thread_data_size;
  641. if (av_size_mult(avctx->thread_count, sizeof(EXRThreadData), &thread_data_size))
  642. return AVERROR(EINVAL);
  643. m = av_fast_realloc(s->thread_data, &s->thread_data_size, thread_data_size);
  644. if (!m)
  645. return AVERROR(ENOMEM);
  646. s->thread_data = m;
  647. memset(s->thread_data + prev_size, 0, s->thread_data_size - prev_size);
  648. }
  649. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
  650. return ret;
  651. if (buf_end - buf < scan_line_blocks * 8)
  652. return AVERROR_INVALIDDATA;
  653. s->table = buf;
  654. ptr = picture->data[0];
  655. // Zero out the start if ymin is not 0
  656. for (y = 0; y < s->ymin; y++) {
  657. memset(ptr, 0, out_line_size);
  658. ptr += picture->linesize[0];
  659. }
  660. s->picture = picture;
  661. avctx->execute2(avctx, decode_block, s->thread_data, NULL, scan_line_blocks);
  662. // Zero out the end if ymax+1 is not h
  663. for (y = s->ymax + 1; y < avctx->height; y++) {
  664. memset(ptr, 0, out_line_size);
  665. ptr += picture->linesize[0];
  666. }
  667. *got_frame = 1;
  668. return buf_size;
  669. }
  670. static av_cold int decode_end(AVCodecContext *avctx)
  671. {
  672. EXRContext *s = avctx->priv_data;
  673. int i;
  674. for (i = 0; i < s->thread_data_size / sizeof(EXRThreadData); i++) {
  675. EXRThreadData *td = &s->thread_data[i];
  676. av_free(td->uncompressed_data);
  677. av_free(td->tmp);
  678. }
  679. av_freep(&s->thread_data);
  680. s->thread_data_size = 0;
  681. av_freep(&s->channels);
  682. return 0;
  683. }
  684. AVCodec ff_exr_decoder = {
  685. .name = "exr",
  686. .type = AVMEDIA_TYPE_VIDEO,
  687. .id = AV_CODEC_ID_EXR,
  688. .priv_data_size = sizeof(EXRContext),
  689. .close = decode_end,
  690. .decode = decode_frame,
  691. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS | CODEC_CAP_SLICE_THREADS,
  692. .long_name = NULL_IF_CONFIG_SMALL("OpenEXR image"),
  693. };