PageRenderTime 57ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/ffmpeg/libavcodec/exr.c

http://github.com/xbmc/xbmc
C | 830 lines | 624 code | 124 blank | 82 comment | 148 complexity | 5d5e50592f8d09dba58c5327587b77c5 MD5 | raw file
Possible License(s): GPL-3.0, CC-BY-SA-3.0, LGPL-2.0, 0BSD, Unlicense, GPL-2.0, AGPL-1.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0
  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 ret, i, x, buf_size = s->buf_size;
  305. line_offset = AV_RL64(s->table + jobnr * 8);
  306. // Check if the buffer has the required bytes needed from the offset
  307. if (line_offset > buf_size - 8)
  308. return AVERROR_INVALIDDATA;
  309. src = buf + line_offset + 8;
  310. line = AV_RL32(src - 8);
  311. if (line < s->ymin || line > s->ymax)
  312. return AVERROR_INVALIDDATA;
  313. data_size = AV_RL32(src - 4);
  314. if (data_size <= 0 || data_size > buf_size)
  315. return AVERROR_INVALIDDATA;
  316. s->ysize = FFMIN(s->scan_lines_per_block, s->ymax - line + 1);
  317. uncompressed_size = s->scan_line_size * s->ysize;
  318. if ((s->compr == EXR_RAW && (data_size != uncompressed_size ||
  319. line_offset > buf_size - uncompressed_size)) ||
  320. (s->compr != EXR_RAW && (data_size > uncompressed_size ||
  321. line_offset > buf_size - data_size))) {
  322. return AVERROR_INVALIDDATA;
  323. }
  324. if (data_size < uncompressed_size) {
  325. av_fast_padded_malloc(&td->uncompressed_data, &td->uncompressed_size, uncompressed_size);
  326. av_fast_padded_malloc(&td->tmp, &td->tmp_size, uncompressed_size);
  327. if (!td->uncompressed_data || !td->tmp)
  328. return AVERROR(ENOMEM);
  329. switch (s->compr) {
  330. case EXR_ZIP1:
  331. case EXR_ZIP16:
  332. ret = zip_uncompress(src, data_size, uncompressed_size, td);
  333. break;
  334. case EXR_PXR24:
  335. ret = pxr24_uncompress(s, src, data_size, uncompressed_size, td);
  336. break;
  337. case EXR_RLE:
  338. ret = rle_uncompress(src, data_size, uncompressed_size, td);
  339. }
  340. src = td->uncompressed_data;
  341. }
  342. channel_buffer[0] = src + xdelta * s->channel_offsets[0];
  343. channel_buffer[1] = src + xdelta * s->channel_offsets[1];
  344. channel_buffer[2] = src + xdelta * s->channel_offsets[2];
  345. if (s->channel_offsets[3] >= 0)
  346. channel_buffer[3] = src + xdelta * s->channel_offsets[3];
  347. ptr = p->data[0] + line * p->linesize[0];
  348. for (i = 0; i < s->scan_lines_per_block && line + i <= s->ymax; i++, ptr += p->linesize[0]) {
  349. const uint8_t *r, *g, *b, *a;
  350. r = channel_buffer[0];
  351. g = channel_buffer[1];
  352. b = channel_buffer[2];
  353. if (channel_buffer[3])
  354. a = channel_buffer[3];
  355. ptr_x = (uint16_t *)ptr;
  356. // Zero out the start if xmin is not 0
  357. memset(ptr_x, 0, bxmin);
  358. ptr_x += s->xmin * s->desc->nb_components;
  359. if (s->pixel_type == EXR_FLOAT) {
  360. // 32-bit
  361. for (x = 0; x < xdelta; x++) {
  362. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&r));
  363. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&g));
  364. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&b));
  365. if (channel_buffer[3])
  366. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a));
  367. }
  368. } else {
  369. // 16-bit
  370. for (x = 0; x < xdelta; x++) {
  371. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&r));
  372. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&g));
  373. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&b));
  374. if (channel_buffer[3])
  375. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&a));
  376. }
  377. }
  378. // Zero out the end if xmax+1 is not w
  379. memset(ptr_x, 0, axmax);
  380. channel_buffer[0] += s->scan_line_size;
  381. channel_buffer[1] += s->scan_line_size;
  382. channel_buffer[2] += s->scan_line_size;
  383. if (channel_buffer[3])
  384. channel_buffer[3] += s->scan_line_size;
  385. }
  386. return 0;
  387. }
  388. static int decode_frame(AVCodecContext *avctx,
  389. void *data,
  390. int *got_frame,
  391. AVPacket *avpkt)
  392. {
  393. const uint8_t *buf = avpkt->data;
  394. unsigned int buf_size = avpkt->size;
  395. const uint8_t *buf_end = buf + buf_size;
  396. EXRContext *const s = avctx->priv_data;
  397. AVFrame *picture = data;
  398. AVFrame *const p = &s->picture;
  399. uint8_t *ptr;
  400. int i, y, magic_number, version, flags, ret;
  401. int w = 0;
  402. int h = 0;
  403. int out_line_size;
  404. int scan_line_blocks;
  405. unsigned int current_channel_offset = 0;
  406. s->xmin = ~0;
  407. s->xmax = ~0;
  408. s->ymin = ~0;
  409. s->ymax = ~0;
  410. s->xdelta = ~0;
  411. s->ydelta = ~0;
  412. s->channel_offsets[0] = -1;
  413. s->channel_offsets[1] = -1;
  414. s->channel_offsets[2] = -1;
  415. s->channel_offsets[3] = -1;
  416. s->pixel_type = -1;
  417. s->nb_channels = 0;
  418. s->compr = -1;
  419. s->buf = buf;
  420. s->buf_size = buf_size;
  421. if (buf_size < 10) {
  422. av_log(avctx, AV_LOG_ERROR, "Too short header to parse\n");
  423. return AVERROR_INVALIDDATA;
  424. }
  425. magic_number = bytestream_get_le32(&buf);
  426. if (magic_number != 20000630) { // As per documentation of OpenEXR it's supposed to be int 20000630 little-endian
  427. av_log(avctx, AV_LOG_ERROR, "Wrong magic number %d\n", magic_number);
  428. return AVERROR_INVALIDDATA;
  429. }
  430. version = bytestream_get_byte(&buf);
  431. if (version != 2) {
  432. av_log(avctx, AV_LOG_ERROR, "Unsupported version %d\n", version);
  433. return AVERROR_PATCHWELCOME;
  434. }
  435. flags = bytestream_get_le24(&buf);
  436. if (flags & 0x2) {
  437. av_log(avctx, AV_LOG_ERROR, "Tile based images are not supported\n");
  438. return AVERROR_PATCHWELCOME;
  439. }
  440. // Parse the header
  441. while (buf < buf_end && buf[0]) {
  442. unsigned int variable_buffer_data_size;
  443. // Process the channel list
  444. if (check_header_variable(avctx, &buf, buf_end, "channels", "chlist", 38, &variable_buffer_data_size) >= 0) {
  445. const uint8_t *channel_list_end;
  446. if (!variable_buffer_data_size)
  447. return AVERROR_INVALIDDATA;
  448. channel_list_end = buf + variable_buffer_data_size;
  449. while (channel_list_end - buf >= 19) {
  450. EXRChannel *channel;
  451. int current_pixel_type = -1;
  452. int channel_index = -1;
  453. int xsub, ysub;
  454. if (!strcmp(buf, "R"))
  455. channel_index = 0;
  456. else if (!strcmp(buf, "G"))
  457. channel_index = 1;
  458. else if (!strcmp(buf, "B"))
  459. channel_index = 2;
  460. else if (!strcmp(buf, "A"))
  461. channel_index = 3;
  462. else
  463. av_log(avctx, AV_LOG_WARNING, "Unsupported channel %.256s\n", buf);
  464. while (bytestream_get_byte(&buf) && buf < channel_list_end)
  465. continue; /* skip */
  466. if (channel_list_end - * &buf < 4) {
  467. av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
  468. return AVERROR_INVALIDDATA;
  469. }
  470. current_pixel_type = bytestream_get_le32(&buf);
  471. if (current_pixel_type > 2) {
  472. av_log(avctx, AV_LOG_ERROR, "Unknown pixel type\n");
  473. return AVERROR_INVALIDDATA;
  474. }
  475. buf += 4;
  476. xsub = bytestream_get_le32(&buf);
  477. ysub = bytestream_get_le32(&buf);
  478. if (xsub != 1 || ysub != 1) {
  479. av_log(avctx, AV_LOG_ERROR, "Unsupported subsampling %dx%d\n", xsub, ysub);
  480. return AVERROR_PATCHWELCOME;
  481. }
  482. if (channel_index >= 0) {
  483. if (s->pixel_type != -1 && s->pixel_type != current_pixel_type) {
  484. av_log(avctx, AV_LOG_ERROR, "RGB channels not of the same depth\n");
  485. return AVERROR_INVALIDDATA;
  486. }
  487. s->pixel_type = current_pixel_type;
  488. s->channel_offsets[channel_index] = current_channel_offset;
  489. }
  490. s->channels = av_realloc_f(s->channels, ++s->nb_channels, sizeof(EXRChannel));
  491. if (!s->channels)
  492. return AVERROR(ENOMEM);
  493. channel = &s->channels[s->nb_channels - 1];
  494. channel->pixel_type = current_pixel_type;
  495. channel->xsub = xsub;
  496. channel->ysub = ysub;
  497. current_channel_offset += 1 << current_pixel_type;
  498. }
  499. /* Check if all channels are set with an offset or if the channels
  500. * are causing an overflow */
  501. if (FFMIN3(s->channel_offsets[0],
  502. s->channel_offsets[1],
  503. s->channel_offsets[2]) < 0) {
  504. if (s->channel_offsets[0] < 0)
  505. av_log(avctx, AV_LOG_ERROR, "Missing red channel\n");
  506. if (s->channel_offsets[1] < 0)
  507. av_log(avctx, AV_LOG_ERROR, "Missing green channel\n");
  508. if (s->channel_offsets[2] < 0)
  509. av_log(avctx, AV_LOG_ERROR, "Missing blue channel\n");
  510. return AVERROR_INVALIDDATA;
  511. }
  512. buf = channel_list_end;
  513. continue;
  514. } else if (check_header_variable(avctx, &buf, buf_end, "dataWindow", "box2i", 31, &variable_buffer_data_size) >= 0) {
  515. if (!variable_buffer_data_size)
  516. return AVERROR_INVALIDDATA;
  517. s->xmin = AV_RL32(buf);
  518. s->ymin = AV_RL32(buf + 4);
  519. s->xmax = AV_RL32(buf + 8);
  520. s->ymax = AV_RL32(buf + 12);
  521. s->xdelta = (s->xmax - s->xmin) + 1;
  522. s->ydelta = (s->ymax - s->ymin) + 1;
  523. buf += variable_buffer_data_size;
  524. continue;
  525. } else if (check_header_variable(avctx, &buf, buf_end, "displayWindow", "box2i", 34, &variable_buffer_data_size) >= 0) {
  526. if (!variable_buffer_data_size)
  527. return AVERROR_INVALIDDATA;
  528. w = AV_RL32(buf + 8) + 1;
  529. h = AV_RL32(buf + 12) + 1;
  530. buf += variable_buffer_data_size;
  531. continue;
  532. } else if (check_header_variable(avctx, &buf, buf_end, "lineOrder", "lineOrder", 25, &variable_buffer_data_size) >= 0) {
  533. if (!variable_buffer_data_size)
  534. return AVERROR_INVALIDDATA;
  535. av_log(avctx, AV_LOG_DEBUG, "line order : %d\n", *buf);
  536. if (*buf > 2) {
  537. av_log(avctx, AV_LOG_ERROR, "Unknown line order\n");
  538. return AVERROR_INVALIDDATA;
  539. }
  540. buf += variable_buffer_data_size;
  541. continue;
  542. } else if (check_header_variable(avctx, &buf, buf_end, "pixelAspectRatio", "float", 31, &variable_buffer_data_size) >= 0) {
  543. if (!variable_buffer_data_size)
  544. return AVERROR_INVALIDDATA;
  545. avctx->sample_aspect_ratio = av_d2q(av_int2float(AV_RL32(buf)), 255);
  546. buf += variable_buffer_data_size;
  547. continue;
  548. } else if (check_header_variable(avctx, &buf, buf_end, "compression", "compression", 29, &variable_buffer_data_size) >= 0) {
  549. if (!variable_buffer_data_size)
  550. return AVERROR_INVALIDDATA;
  551. if (s->compr == -1)
  552. s->compr = *buf;
  553. else
  554. av_log(avctx, AV_LOG_WARNING, "Found more than one compression attribute\n");
  555. buf += variable_buffer_data_size;
  556. continue;
  557. }
  558. // Check if there is enough bytes for a header
  559. if (buf_end - buf <= 9) {
  560. av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
  561. return AVERROR_INVALIDDATA;
  562. }
  563. // Process unknown variables
  564. for (i = 0; i < 2; i++) {
  565. // Skip variable name/type
  566. while (++buf < buf_end)
  567. if (buf[0] == 0x0)
  568. break;
  569. }
  570. buf++;
  571. // Skip variable length
  572. if (buf_end - buf >= 5) {
  573. variable_buffer_data_size = get_header_variable_length(&buf, buf_end);
  574. if (!variable_buffer_data_size) {
  575. av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
  576. return AVERROR_INVALIDDATA;
  577. }
  578. buf += variable_buffer_data_size;
  579. }
  580. }
  581. if (s->compr == -1) {
  582. av_log(avctx, AV_LOG_ERROR, "Missing compression attribute\n");
  583. return AVERROR_INVALIDDATA;
  584. }
  585. if (buf >= buf_end) {
  586. av_log(avctx, AV_LOG_ERROR, "Incomplete frame\n");
  587. return AVERROR_INVALIDDATA;
  588. }
  589. buf++;
  590. switch (s->pixel_type) {
  591. case EXR_FLOAT:
  592. case EXR_HALF:
  593. if (s->channel_offsets[3] >= 0)
  594. avctx->pix_fmt = AV_PIX_FMT_RGBA64;
  595. else
  596. avctx->pix_fmt = AV_PIX_FMT_RGB48;
  597. break;
  598. case EXR_UINT:
  599. av_log_missing_feature(avctx, "32-bit unsigned int", 1);
  600. return AVERROR_PATCHWELCOME;
  601. default:
  602. av_log(avctx, AV_LOG_ERROR, "Missing channel list\n");
  603. return AVERROR_INVALIDDATA;
  604. }
  605. switch (s->compr) {
  606. case EXR_RAW:
  607. case EXR_RLE:
  608. case EXR_ZIP1:
  609. s->scan_lines_per_block = 1;
  610. break;
  611. case EXR_PXR24:
  612. case EXR_ZIP16:
  613. s->scan_lines_per_block = 16;
  614. break;
  615. default:
  616. av_log(avctx, AV_LOG_ERROR, "Compression type %d is not supported\n", s->compr);
  617. return AVERROR_PATCHWELCOME;
  618. }
  619. if (s->picture.data[0])
  620. ff_thread_release_buffer(avctx, &s->picture);
  621. if (av_image_check_size(w, h, 0, avctx))
  622. return AVERROR_INVALIDDATA;
  623. // Verify the xmin, xmax, ymin, ymax and xdelta before setting the actual image size
  624. if (s->xmin > s->xmax ||
  625. s->ymin > s->ymax ||
  626. s->xdelta != s->xmax - s->xmin + 1 ||
  627. s->xmax >= w || s->ymax >= h) {
  628. av_log(avctx, AV_LOG_ERROR, "Wrong sizing or missing size information\n");
  629. return AVERROR_INVALIDDATA;
  630. }
  631. if (w != avctx->width || h != avctx->height) {
  632. avcodec_set_dimensions(avctx, w, h);
  633. }
  634. s->desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  635. out_line_size = avctx->width * 2 * s->desc->nb_components;
  636. s->scan_line_size = s->xdelta * current_channel_offset;
  637. scan_line_blocks = (s->ydelta + s->scan_lines_per_block - 1) / s->scan_lines_per_block;
  638. if (s->compr != EXR_RAW) {
  639. size_t thread_data_size, prev_size;
  640. EXRThreadData *m;
  641. prev_size = s->thread_data_size;
  642. if (av_size_mult(avctx->thread_count, sizeof(EXRThreadData), &thread_data_size))
  643. return AVERROR(EINVAL);
  644. m = av_fast_realloc(s->thread_data, &s->thread_data_size, thread_data_size);
  645. if (!m)
  646. return AVERROR(ENOMEM);
  647. s->thread_data = m;
  648. memset(s->thread_data + prev_size, 0, s->thread_data_size - prev_size);
  649. }
  650. if ((ret = ff_thread_get_buffer(avctx, p)) < 0) {
  651. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  652. return ret;
  653. }
  654. if (buf_end - buf < scan_line_blocks * 8)
  655. return AVERROR_INVALIDDATA;
  656. s->table = buf;
  657. ptr = p->data[0];
  658. // Zero out the start if ymin is not 0
  659. for (y = 0; y < s->ymin; y++) {
  660. memset(ptr, 0, out_line_size);
  661. ptr += p->linesize[0];
  662. }
  663. avctx->execute2(avctx, decode_block, s->thread_data, NULL, scan_line_blocks);
  664. // Zero out the end if ymax+1 is not h
  665. for (y = s->ymax + 1; y < avctx->height; y++) {
  666. memset(ptr, 0, out_line_size);
  667. ptr += p->linesize[0];
  668. }
  669. *picture = s->picture;
  670. *got_frame = 1;
  671. return buf_size;
  672. }
  673. static av_cold int decode_init(AVCodecContext *avctx)
  674. {
  675. EXRContext *s = avctx->priv_data;
  676. avcodec_get_frame_defaults(&s->picture);
  677. avctx->coded_frame = &s->picture;
  678. return 0;
  679. }
  680. static av_cold int decode_end(AVCodecContext *avctx)
  681. {
  682. EXRContext *s = avctx->priv_data;
  683. int i;
  684. if (s->picture.data[0])
  685. avctx->release_buffer(avctx, &s->picture);
  686. for (i = 0; i < s->thread_data_size / sizeof(EXRThreadData); i++) {
  687. EXRThreadData *td = &s->thread_data[i];
  688. av_free(td->uncompressed_data);
  689. av_free(td->tmp);
  690. }
  691. av_freep(&s->thread_data);
  692. s->thread_data_size = 0;
  693. av_freep(&s->channels);
  694. return 0;
  695. }
  696. AVCodec ff_exr_decoder = {
  697. .name = "exr",
  698. .type = AVMEDIA_TYPE_VIDEO,
  699. .id = AV_CODEC_ID_EXR,
  700. .priv_data_size = sizeof(EXRContext),
  701. .init = decode_init,
  702. .close = decode_end,
  703. .decode = decode_frame,
  704. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS | CODEC_CAP_SLICE_THREADS,
  705. .long_name = NULL_IF_CONFIG_SMALL("OpenEXR image"),
  706. };