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.

621 lines
21KB

  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 "libavutil/imgutils.h"
  35. enum ExrCompr {
  36. EXR_RAW = 0,
  37. EXR_RLE = 1,
  38. EXR_ZIP1 = 2,
  39. EXR_ZIP16 = 3,
  40. EXR_PIZ = 4,
  41. EXR_B44 = 6,
  42. EXR_B44A = 7,
  43. };
  44. typedef struct EXRContext {
  45. AVFrame picture;
  46. int compr;
  47. int bits_per_color_id;
  48. int8_t channel_offsets[4]; // 0 = red, 1 = green, 2 = blue and 3 = alpha
  49. uint8_t *uncompressed_data;
  50. int uncompressed_size;
  51. uint8_t *tmp;
  52. int tmp_size;;
  53. } EXRContext;
  54. /**
  55. * Converts from 32-bit float as uint32_t to uint16_t
  56. *
  57. * @param v 32-bit float
  58. * @return normalized 16-bit unsigned int
  59. */
  60. static inline uint16_t exr_flt2uint(uint32_t v)
  61. {
  62. unsigned int exp = v >> 23;
  63. // "HACK": negative values result in exp< 0, so clipping them to 0
  64. // is also handled by this condition, avoids explicit check for sign bit.
  65. if (exp<= 127 + 7 - 24) // we would shift out all bits anyway
  66. return 0;
  67. if (exp >= 127)
  68. return 0xffff;
  69. v &= 0x007fffff;
  70. return (v + (1 << 23)) >> (127 + 7 - exp);
  71. }
  72. /**
  73. * Converts from 16-bit float as uint16_t to uint16_t
  74. *
  75. * @param v 16-bit float
  76. * @return normalized 16-bit unsigned int
  77. */
  78. static inline uint16_t exr_halflt2uint(uint16_t v)
  79. {
  80. unsigned exp = 14 - (v >> 10);
  81. if (exp >= 14) {
  82. if (exp == 14) return (v >> 9) & 1;
  83. else return (v & 0x8000) ? 0 : 0xffff;
  84. }
  85. v <<= 6;
  86. return (v + (1 << 16)) >> (exp + 1);
  87. }
  88. /**
  89. * Gets the size of the header variable
  90. *
  91. * @param **buf the current pointer location in the header where
  92. * the variable data starts
  93. * @param *buf_end pointer location of the end of the buffer
  94. * @return size of variable data
  95. */
  96. static unsigned int get_header_variable_length(const uint8_t **buf,
  97. const uint8_t *buf_end)
  98. {
  99. unsigned int variable_buffer_data_size = bytestream_get_le32(buf);
  100. if (variable_buffer_data_size >= buf_end - *buf)
  101. return 0;
  102. return variable_buffer_data_size;
  103. }
  104. /**
  105. * Checks if the variable name corresponds with it's data type
  106. *
  107. * @param *avctx the AVCodecContext
  108. * @param **buf the current pointer location in the header where
  109. * the variable name starts
  110. * @param *buf_end pointer location of the end of the buffer
  111. * @param *value_name name of the varible to check
  112. * @param *value_type type of the varible to check
  113. * @param minimum_length minimum length of the variable data
  114. * @param variable_buffer_data_size variable length read from the header
  115. * after it's checked
  116. * @return negative if variable is invalid
  117. */
  118. static int check_header_variable(AVCodecContext *avctx,
  119. const uint8_t **buf,
  120. const uint8_t *buf_end,
  121. const char *value_name,
  122. const char *value_type,
  123. unsigned int minimum_length,
  124. unsigned int *variable_buffer_data_size)
  125. {
  126. if (buf_end - *buf >= minimum_length && !strcmp(*buf, value_name)) {
  127. *buf += strlen(value_name)+1;
  128. if (!strcmp(*buf, value_type)) {
  129. *buf += strlen(value_type)+1;
  130. *variable_buffer_data_size = get_header_variable_length(buf, buf_end);
  131. if (!*variable_buffer_data_size)
  132. av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
  133. if (*variable_buffer_data_size > buf_end - *buf)
  134. return -1;
  135. return 1;
  136. }
  137. *buf -= strlen(value_name)+1;
  138. av_log(avctx, AV_LOG_WARNING, "Unknown data type for header variable %s\n", value_name);
  139. }
  140. return -1;
  141. }
  142. static void predictor(uint8_t *src, int size)
  143. {
  144. uint8_t *t = src + 1;
  145. uint8_t *stop = src + size;
  146. while (t < stop) {
  147. int d = (int)t[-1] + (int)t[0] - 128;
  148. t[0] = d;
  149. ++t;
  150. }
  151. }
  152. static void reorder_pixels(uint8_t *src, uint8_t *dst, int size)
  153. {
  154. const int8_t *t1 = src;
  155. const int8_t *t2 = src + (size + 1) / 2;
  156. int8_t *s = dst;
  157. int8_t *stop = s + size;
  158. while (1) {
  159. if (s < stop)
  160. *(s++) = *(t1++);
  161. else
  162. break;
  163. if (s < stop)
  164. *(s++) = *(t2++);
  165. else
  166. break;
  167. }
  168. }
  169. static int decode_frame(AVCodecContext *avctx,
  170. void *data,
  171. int *data_size,
  172. AVPacket *avpkt)
  173. {
  174. const uint8_t *buf = avpkt->data;
  175. unsigned int buf_size = avpkt->size;
  176. const uint8_t *buf_end = buf + buf_size;
  177. EXRContext *const s = avctx->priv_data;
  178. AVFrame *picture = data;
  179. AVFrame *const p = &s->picture;
  180. uint8_t *ptr;
  181. int i, x, y, stride, magic_number, version_flag, ret;
  182. int w = 0;
  183. int h = 0;
  184. unsigned int xmin = ~0;
  185. unsigned int xmax = ~0;
  186. unsigned int ymin = ~0;
  187. unsigned int ymax = ~0;
  188. unsigned int xdelta = ~0;
  189. int out_line_size;
  190. int bxmin, axmax;
  191. int scan_lines_per_block;
  192. unsigned long scan_line_size;
  193. unsigned long uncompressed_size;
  194. unsigned int current_channel_offset = 0;
  195. s->channel_offsets[0] = -1;
  196. s->channel_offsets[1] = -1;
  197. s->channel_offsets[2] = -1;
  198. s->channel_offsets[3] = -1;
  199. s->bits_per_color_id = -1;
  200. if (buf_size < 10) {
  201. av_log(avctx, AV_LOG_ERROR, "Too short header to parse\n");
  202. return AVERROR_INVALIDDATA;
  203. }
  204. magic_number = bytestream_get_le32(&buf);
  205. if (magic_number != 20000630) { // As per documentation of OpenEXR it's supposed to be int 20000630 little-endian
  206. av_log(avctx, AV_LOG_ERROR, "Wrong magic number %d\n", magic_number);
  207. return AVERROR_INVALIDDATA;
  208. }
  209. version_flag = bytestream_get_le32(&buf);
  210. if ((version_flag & 0x200) == 0x200) {
  211. av_log(avctx, AV_LOG_ERROR, "Tile based images are not supported\n");
  212. return AVERROR_PATCHWELCOME;
  213. }
  214. // Parse the header
  215. while (buf < buf_end && buf[0]) {
  216. unsigned int variable_buffer_data_size;
  217. // Process the channel list
  218. if (check_header_variable(avctx, &buf, buf_end, "channels", "chlist", 38, &variable_buffer_data_size) >= 0) {
  219. const uint8_t *channel_list_end;
  220. if (!variable_buffer_data_size)
  221. return AVERROR_INVALIDDATA;
  222. channel_list_end = buf + variable_buffer_data_size;
  223. while (channel_list_end - buf >= 19) {
  224. int current_bits_per_color_id = -1;
  225. int channel_index = -1;
  226. if (!strcmp(buf, "R"))
  227. channel_index = 0;
  228. if (!strcmp(buf, "G"))
  229. channel_index = 1;
  230. if (!strcmp(buf, "B"))
  231. channel_index = 2;
  232. if (!strcmp(buf, "A"))
  233. channel_index = 3;
  234. while (bytestream_get_byte(&buf) && buf < channel_list_end)
  235. continue; /* skip */
  236. if (channel_list_end - * &buf < 4) {
  237. av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
  238. return AVERROR_INVALIDDATA;
  239. }
  240. current_bits_per_color_id = bytestream_get_le32(&buf);
  241. if (current_bits_per_color_id > 2) {
  242. av_log(avctx, AV_LOG_ERROR, "Unknown color format\n");
  243. return AVERROR_INVALIDDATA;
  244. }
  245. if (channel_index >= 0) {
  246. if (s->bits_per_color_id != -1 && s->bits_per_color_id != current_bits_per_color_id) {
  247. av_log(avctx, AV_LOG_ERROR, "RGB channels not of the same depth\n");
  248. return AVERROR_INVALIDDATA;
  249. }
  250. s->bits_per_color_id = current_bits_per_color_id;
  251. s->channel_offsets[channel_index] = current_channel_offset;
  252. }
  253. current_channel_offset += 1 << current_bits_per_color_id;
  254. buf += 12;
  255. }
  256. /* Check if all channels are set with an offset or if the channels
  257. * are causing an overflow */
  258. if (FFMIN3(s->channel_offsets[0],
  259. s->channel_offsets[1],
  260. s->channel_offsets[2]) < 0) {
  261. if (s->channel_offsets[0] < 0)
  262. av_log(avctx, AV_LOG_ERROR, "Missing red channel\n");
  263. if (s->channel_offsets[1] < 0)
  264. av_log(avctx, AV_LOG_ERROR, "Missing green channel\n");
  265. if (s->channel_offsets[2] < 0)
  266. av_log(avctx, AV_LOG_ERROR, "Missing blue channel\n");
  267. return AVERROR_INVALIDDATA;
  268. }
  269. buf = channel_list_end;
  270. continue;
  271. }
  272. // Process the dataWindow variable
  273. if (check_header_variable(avctx, &buf, buf_end, "dataWindow", "box2i", 31, &variable_buffer_data_size) >= 0) {
  274. if (!variable_buffer_data_size)
  275. return AVERROR_INVALIDDATA;
  276. xmin = AV_RL32(buf);
  277. ymin = AV_RL32(buf + 4);
  278. xmax = AV_RL32(buf + 8);
  279. ymax = AV_RL32(buf + 12);
  280. xdelta = (xmax-xmin) + 1;
  281. buf += variable_buffer_data_size;
  282. continue;
  283. }
  284. // Process the displayWindow variable
  285. if (check_header_variable(avctx, &buf, buf_end, "displayWindow", "box2i", 34, &variable_buffer_data_size) >= 0) {
  286. if (!variable_buffer_data_size)
  287. return AVERROR_INVALIDDATA;
  288. w = AV_RL32(buf + 8) + 1;
  289. h = AV_RL32(buf + 12) + 1;
  290. buf += variable_buffer_data_size;
  291. continue;
  292. }
  293. // Process the lineOrder variable
  294. if (check_header_variable(avctx, &buf, buf_end, "lineOrder", "lineOrder", 25, &variable_buffer_data_size) >= 0) {
  295. if (!variable_buffer_data_size)
  296. return AVERROR_INVALIDDATA;
  297. if (*buf) {
  298. av_log(avctx, AV_LOG_ERROR, "Doesn't support this line order : %d\n", *buf);
  299. return AVERROR_PATCHWELCOME;
  300. }
  301. buf += variable_buffer_data_size;
  302. continue;
  303. }
  304. // Process the pixelAspectRatio variable
  305. if (check_header_variable(avctx, &buf, buf_end, "pixelAspectRatio", "float", 31, &variable_buffer_data_size) >= 0) {
  306. if (!variable_buffer_data_size)
  307. return AVERROR_INVALIDDATA;
  308. avctx->sample_aspect_ratio = av_d2q(av_int2float(AV_RL32(buf)), 255);
  309. buf += variable_buffer_data_size;
  310. continue;
  311. }
  312. // Process the compression variable
  313. if (check_header_variable(avctx, &buf, buf_end, "compression", "compression", 29, &variable_buffer_data_size) >= 0) {
  314. if (!variable_buffer_data_size)
  315. return AVERROR_INVALIDDATA;
  316. s->compr = *buf;
  317. switch (s->compr) {
  318. case EXR_RAW:
  319. case EXR_ZIP1:
  320. case EXR_ZIP16:
  321. break;
  322. case EXR_RLE:
  323. case EXR_PIZ:
  324. case EXR_B44:
  325. default:
  326. av_log(avctx, AV_LOG_ERROR, "Compression type %d is not supported\n", s->compr);
  327. return AVERROR_PATCHWELCOME;
  328. }
  329. buf += variable_buffer_data_size;
  330. continue;
  331. }
  332. // Check if there is enough bytes for a header
  333. if (buf_end - buf <= 9) {
  334. av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
  335. return AVERROR_INVALIDDATA;
  336. }
  337. // Process unknown variables
  338. for (i = 0; i < 2; i++) {
  339. // Skip variable name/type
  340. while (++buf < buf_end)
  341. if (buf[0] == 0x0)
  342. break;
  343. }
  344. buf++;
  345. // Skip variable length
  346. if (buf_end - buf >= 5) {
  347. variable_buffer_data_size = get_header_variable_length(&buf, buf_end);
  348. if (!variable_buffer_data_size) {
  349. av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
  350. return AVERROR_INVALIDDATA;
  351. }
  352. buf += variable_buffer_data_size;
  353. }
  354. }
  355. if (buf >= buf_end) {
  356. av_log(avctx, AV_LOG_ERROR, "Incomplete frame\n");
  357. return AVERROR_INVALIDDATA;
  358. }
  359. buf++;
  360. switch (s->bits_per_color_id) {
  361. case 2: // 32-bit
  362. case 1: // 16-bit
  363. if (s->channel_offsets[3] >= 0)
  364. avctx->pix_fmt = PIX_FMT_RGBA64;
  365. else
  366. avctx->pix_fmt = PIX_FMT_RGB48;
  367. break;
  368. // 8-bit
  369. case 0:
  370. av_log_missing_feature(avctx, "8-bit OpenEXR", 1);
  371. return AVERROR_PATCHWELCOME;
  372. default:
  373. av_log(avctx, AV_LOG_ERROR, "Unknown color format : %d\n", s->bits_per_color_id);
  374. return AVERROR_INVALIDDATA;
  375. }
  376. switch (s->compr) {
  377. case EXR_RAW:
  378. case EXR_ZIP1:
  379. scan_lines_per_block = 1;
  380. break;
  381. case EXR_ZIP16:
  382. scan_lines_per_block = 16;
  383. break;
  384. }
  385. if (s->picture.data[0])
  386. avctx->release_buffer(avctx, &s->picture);
  387. if (av_image_check_size(w, h, 0, avctx))
  388. return AVERROR_INVALIDDATA;
  389. // Verify the xmin, xmax, ymin, ymax and xdelta before setting the actual image size
  390. if (xmin > xmax || ymin > ymax || xdelta != xmax - xmin + 1 || xmax >= w || ymax >= h) {
  391. av_log(avctx, AV_LOG_ERROR, "Wrong sizing or missing size information\n");
  392. return AVERROR_INVALIDDATA;
  393. }
  394. if (w != avctx->width || h != avctx->height) {
  395. avcodec_set_dimensions(avctx, w, h);
  396. }
  397. bxmin = xmin * 2 * av_pix_fmt_descriptors[avctx->pix_fmt].nb_components;
  398. axmax = (avctx->width - (xmax + 1)) * 2 * av_pix_fmt_descriptors[avctx->pix_fmt].nb_components;
  399. out_line_size = avctx->width * 2 * av_pix_fmt_descriptors[avctx->pix_fmt].nb_components;
  400. scan_line_size = xdelta * av_pix_fmt_descriptors[avctx->pix_fmt].nb_components * FFMAX(2 * s->bits_per_color_id, 1);
  401. uncompressed_size = scan_line_size * scan_lines_per_block;
  402. if (s->compr != EXR_RAW) {
  403. av_fast_padded_malloc(&s->uncompressed_data, &s->uncompressed_size, uncompressed_size);
  404. av_fast_padded_malloc(&s->tmp, &s->tmp_size, uncompressed_size);
  405. if (!s->uncompressed_data || !s->tmp)
  406. return AVERROR(ENOMEM);
  407. }
  408. if ((ret = avctx->get_buffer(avctx, p)) < 0) {
  409. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  410. return ret;
  411. }
  412. ptr = p->data[0];
  413. stride = p->linesize[0];
  414. // Zero out the start if ymin is not 0
  415. for (y = 0; y < ymin; y++) {
  416. memset(ptr, 0, out_line_size);
  417. ptr += stride;
  418. }
  419. // Process the actual scan line blocks
  420. for (y = ymin; y <= ymax; y += scan_lines_per_block) {
  421. uint16_t *ptr_x = (uint16_t *)ptr;
  422. if (buf_end - buf > 8) {
  423. /* Read the lineoffset from the line offset table and add 8 bytes
  424. to skip the coordinates and data size fields */
  425. const uint64_t line_offset = bytestream_get_le64(&buf) + 8;
  426. int32_t data_size;
  427. // Check if the buffer has the required bytes needed from the offset
  428. if ((line_offset > buf_size) ||
  429. (s->compr == EXR_RAW && line_offset > avpkt->size - xdelta * current_channel_offset) ||
  430. (s->compr != EXR_RAW && line_offset > buf_size - (data_size = AV_RL32(avpkt->data + line_offset - 4)))) {
  431. // Line offset is probably wrong and not inside the buffer
  432. av_log(avctx, AV_LOG_WARNING, "Line offset for line %d is out of reach setting it to black\n", y);
  433. for (i = 0; i < scan_lines_per_block && y + i <= ymax; i++, ptr += stride) {
  434. ptr_x = (uint16_t *)ptr;
  435. memset(ptr_x, 0, out_line_size);
  436. }
  437. } else {
  438. const uint8_t *red_channel_buffer, *green_channel_buffer, *blue_channel_buffer, *alpha_channel_buffer = 0;
  439. if ((s->compr == EXR_ZIP1 || s->compr == EXR_ZIP16) && data_size < uncompressed_size) {
  440. if (uncompress(s->tmp, &uncompressed_size, avpkt->data + line_offset, data_size) != Z_OK) {
  441. av_log(avctx, AV_LOG_ERROR, "error during zlib decompression\n");
  442. return AVERROR(EINVAL);
  443. }
  444. predictor(s->tmp, uncompressed_size);
  445. reorder_pixels(s->tmp, s->uncompressed_data, uncompressed_size);
  446. red_channel_buffer = s->uncompressed_data + xdelta * s->channel_offsets[0];
  447. green_channel_buffer = s->uncompressed_data + xdelta * s->channel_offsets[1];
  448. blue_channel_buffer = s->uncompressed_data + xdelta * s->channel_offsets[2];
  449. if (s->channel_offsets[3] >= 0)
  450. alpha_channel_buffer = s->uncompressed_data + xdelta * s->channel_offsets[3];
  451. } else {
  452. red_channel_buffer = avpkt->data + line_offset + xdelta * s->channel_offsets[0];
  453. green_channel_buffer = avpkt->data + line_offset + xdelta * s->channel_offsets[1];
  454. blue_channel_buffer = avpkt->data + line_offset + xdelta * s->channel_offsets[2];
  455. if (s->channel_offsets[3] >= 0)
  456. alpha_channel_buffer = avpkt->data + line_offset + xdelta * s->channel_offsets[3];
  457. }
  458. for (i = 0; i < scan_lines_per_block && y + i <= ymax; i++, ptr += stride) {
  459. const uint8_t *r, *g, *b, *a;
  460. r = red_channel_buffer;
  461. g = green_channel_buffer;
  462. b = blue_channel_buffer;
  463. if (alpha_channel_buffer)
  464. a = alpha_channel_buffer;
  465. ptr_x = (uint16_t *)ptr;
  466. // Zero out the start if xmin is not 0
  467. memset(ptr_x, 0, bxmin);
  468. ptr_x += xmin * av_pix_fmt_descriptors[avctx->pix_fmt].nb_components;
  469. if (s->bits_per_color_id == 2) {
  470. // 32-bit
  471. for (x = 0; x < xdelta; x++) {
  472. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&r));
  473. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&g));
  474. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&b));
  475. if (alpha_channel_buffer)
  476. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a));
  477. }
  478. } else {
  479. // 16-bit
  480. for (x = 0; x < xdelta; x++) {
  481. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&r));
  482. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&g));
  483. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&b));
  484. if (alpha_channel_buffer)
  485. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&a));
  486. }
  487. }
  488. // Zero out the end if xmax+1 is not w
  489. memset(ptr_x, 0, axmax);
  490. red_channel_buffer += scan_line_size;
  491. green_channel_buffer += scan_line_size;
  492. blue_channel_buffer += scan_line_size;
  493. if (alpha_channel_buffer)
  494. alpha_channel_buffer += scan_line_size;
  495. }
  496. }
  497. }
  498. }
  499. // Zero out the end if ymax+1 is not h
  500. for (y = ymax + 1; y < avctx->height; y++) {
  501. memset(ptr, 0, out_line_size);
  502. ptr += stride;
  503. }
  504. *picture = s->picture;
  505. *data_size = sizeof(AVPicture);
  506. return buf_size;
  507. }
  508. static av_cold int decode_init(AVCodecContext *avctx)
  509. {
  510. EXRContext *s = avctx->priv_data;
  511. avcodec_get_frame_defaults(&s->picture);
  512. avctx->coded_frame = &s->picture;
  513. return 0;
  514. }
  515. static av_cold int decode_end(AVCodecContext *avctx)
  516. {
  517. EXRContext *s = avctx->priv_data;
  518. if (s->picture.data[0])
  519. avctx->release_buffer(avctx, &s->picture);
  520. av_freep(&s->uncompressed_data);
  521. av_freep(&s->tmp);
  522. return 0;
  523. }
  524. AVCodec ff_exr_decoder = {
  525. .name = "exr",
  526. .type = AVMEDIA_TYPE_VIDEO,
  527. .id = CODEC_ID_EXR,
  528. .priv_data_size = sizeof(EXRContext),
  529. .init = decode_init,
  530. .close = decode_end,
  531. .decode = decode_frame,
  532. .capabilities = CODEC_CAP_DR1,
  533. .long_name = NULL_IF_CONFIG_SMALL("OpenEXR image"),
  534. };