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.

484 lines
16KB

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