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.

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