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.

508 lines
18KB

  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 "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[4]; // 0 = red, 1 = green, 2 = blue and 3 = alpha
  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->channel_offsets[3] = -1;
  161. s->bits_per_color_id = -1;
  162. if (buf_end - buf < 10) {
  163. av_log(avctx, AV_LOG_ERROR, "Too short header to parse\n");
  164. return -1;
  165. }
  166. magic_number = bytestream_get_le32(&buf);
  167. if (magic_number != 20000630) { // As per documentation of OpenEXR it's supposed to be int 20000630 little-endian
  168. av_log(avctx, AV_LOG_ERROR, "Wrong magic number %d\n", magic_number);
  169. return -1;
  170. }
  171. version_flag = bytestream_get_le32(&buf);
  172. if ((version_flag & 0x200) == 0x200) {
  173. av_log(avctx, AV_LOG_ERROR, "Tile based images are not supported\n");
  174. return -1;
  175. }
  176. // Parse the header
  177. while (buf < buf_end && buf[0]) {
  178. unsigned int variable_buffer_data_size;
  179. // Process the channel list
  180. if (check_header_variable(avctx, &buf, buf_end, "channels", "chlist", 38, &variable_buffer_data_size) >= 0) {
  181. const uint8_t *channel_list_end;
  182. if (!variable_buffer_data_size)
  183. return -1;
  184. channel_list_end = buf + variable_buffer_data_size;
  185. while (channel_list_end - buf >= 19) {
  186. int current_bits_per_color_id = -1;
  187. int channel_index = -1;
  188. if (!strcmp(buf, "R"))
  189. channel_index = 0;
  190. if (!strcmp(buf, "G"))
  191. channel_index = 1;
  192. if (!strcmp(buf, "B"))
  193. channel_index = 2;
  194. if (!strcmp(buf, "A"))
  195. channel_index = 3;
  196. while (bytestream_get_byte(&buf) && buf < channel_list_end)
  197. continue; /* skip */
  198. if (channel_list_end - * &buf < 4) {
  199. av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
  200. return -1;
  201. }
  202. current_bits_per_color_id = bytestream_get_le32(&buf);
  203. if (current_bits_per_color_id > 2) {
  204. av_log(avctx, AV_LOG_ERROR, "Unknown color format\n");
  205. return -1;
  206. }
  207. if (channel_index >= 0) {
  208. if (s->bits_per_color_id != -1 && s->bits_per_color_id != current_bits_per_color_id) {
  209. av_log(avctx, AV_LOG_ERROR, "RGB channels not of the same depth\n");
  210. return -1;
  211. }
  212. s->bits_per_color_id = current_bits_per_color_id;
  213. s->channel_offsets[channel_index] = current_channel_offset;
  214. }
  215. current_channel_offset += 1 << current_bits_per_color_id;
  216. buf += 12;
  217. }
  218. /* Check if all channels are set with an offset or if the channels
  219. * are causing an overflow */
  220. if (FFMIN3(s->channel_offsets[0],
  221. s->channel_offsets[1],
  222. s->channel_offsets[2]) < 0) {
  223. if (s->channel_offsets[0] < 0)
  224. av_log(avctx, AV_LOG_ERROR, "Missing red channel\n");
  225. if (s->channel_offsets[1] < 0)
  226. av_log(avctx, AV_LOG_ERROR, "Missing green channel\n");
  227. if (s->channel_offsets[2] < 0)
  228. av_log(avctx, AV_LOG_ERROR, "Missing blue channel\n");
  229. return -1;
  230. }
  231. buf = channel_list_end;
  232. continue;
  233. }
  234. // Process the dataWindow variable
  235. if (check_header_variable(avctx, &buf, buf_end, "dataWindow", "box2i", 31, &variable_buffer_data_size) >= 0) {
  236. if (!variable_buffer_data_size)
  237. return -1;
  238. xmin = AV_RL32(buf);
  239. ymin = AV_RL32(buf + 4);
  240. xmax = AV_RL32(buf + 8);
  241. ymax = AV_RL32(buf + 12);
  242. xdelta = (xmax-xmin) + 1;
  243. buf += variable_buffer_data_size;
  244. continue;
  245. }
  246. // Process the displayWindow variable
  247. if (check_header_variable(avctx, &buf, buf_end, "displayWindow", "box2i", 34, &variable_buffer_data_size) >= 0) {
  248. if (!variable_buffer_data_size)
  249. return -1;
  250. w = AV_RL32(buf + 8) + 1;
  251. h = AV_RL32(buf + 12) + 1;
  252. buf += variable_buffer_data_size;
  253. continue;
  254. }
  255. // Process the lineOrder variable
  256. if (check_header_variable(avctx, &buf, buf_end, "lineOrder", "lineOrder", 25, &variable_buffer_data_size) >= 0) {
  257. if (!variable_buffer_data_size)
  258. return -1;
  259. if (*buf) {
  260. av_log(avctx, AV_LOG_ERROR, "Doesn't support this line order : %d\n", *buf);
  261. return -1;
  262. }
  263. buf += variable_buffer_data_size;
  264. continue;
  265. }
  266. // Process the pixelAspectRatio variable
  267. if (check_header_variable(avctx, &buf, buf_end, "pixelAspectRatio", "float", 31, &variable_buffer_data_size) >= 0) {
  268. if (!variable_buffer_data_size)
  269. return -1;
  270. avctx->sample_aspect_ratio = av_d2q(av_int2float(AV_RL32(buf)), 255);
  271. buf += variable_buffer_data_size;
  272. continue;
  273. }
  274. // Process the compression variable
  275. if (check_header_variable(avctx, &buf, buf_end, "compression", "compression", 29, &variable_buffer_data_size) >= 0) {
  276. if (!variable_buffer_data_size)
  277. return -1;
  278. s->compr = *buf;
  279. switch (s->compr) {
  280. case EXR_RAW:
  281. break;
  282. case EXR_RLE:
  283. case EXR_ZIP1:
  284. case EXR_ZIP16:
  285. case EXR_PIZ:
  286. case EXR_B44:
  287. default:
  288. av_log(avctx, AV_LOG_ERROR, "Compression type %d is not supported\n", s->compr);
  289. return -1;
  290. }
  291. buf += variable_buffer_data_size;
  292. continue;
  293. }
  294. // Check if there is enough bytes for a header
  295. if (buf_end - buf <= 9) {
  296. av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
  297. return -1;
  298. }
  299. // Process unknown variables
  300. for (i = 0; i < 2; i++) {
  301. // Skip variable name/type
  302. while (++buf < buf_end)
  303. if (buf[0] == 0x0)
  304. break;
  305. }
  306. buf++;
  307. // Skip variable length
  308. if (buf_end - buf >= 5) {
  309. variable_buffer_data_size = get_header_variable_length(&buf, buf_end);
  310. if (!variable_buffer_data_size) {
  311. av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
  312. return -1;
  313. }
  314. buf += variable_buffer_data_size;
  315. }
  316. }
  317. if (buf >= buf_end) {
  318. av_log(avctx, AV_LOG_ERROR, "Incomplete frame\n");
  319. return -1;
  320. }
  321. buf++;
  322. switch (s->bits_per_color_id) {
  323. case 2: // 32-bit
  324. case 1: // 16-bit
  325. if (s->channel_offsets[3] >= 0)
  326. avctx->pix_fmt = PIX_FMT_RGBA64;
  327. else
  328. avctx->pix_fmt = PIX_FMT_RGB48;
  329. break;
  330. // 8-bit
  331. case 0:
  332. av_log_missing_feature(avctx, "8-bit OpenEXR", 1);
  333. return -1;
  334. default:
  335. av_log(avctx, AV_LOG_ERROR, "Unknown color format : %d\n", s->bits_per_color_id);
  336. return -1;
  337. }
  338. if (s->picture.data[0])
  339. avctx->release_buffer(avctx, &s->picture);
  340. if (av_image_check_size(w, h, 0, avctx))
  341. return -1;
  342. // Verify the xmin, xmax, ymin, ymax and xdelta before setting the actual image size
  343. if (xmin > xmax || ymin > ymax || xdelta != xmax - xmin + 1 || xmax >= w || ymax >= h) {
  344. av_log(avctx, AV_LOG_ERROR, "Wrong sizing or missing size information\n");
  345. return -1;
  346. }
  347. if (w != avctx->width || h != avctx->height) {
  348. avcodec_set_dimensions(avctx, w, h);
  349. }
  350. if (avctx->get_buffer(avctx, p) < 0) {
  351. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  352. return -1;
  353. }
  354. ptr = p->data[0];
  355. stride = p->linesize[0];
  356. // Zero out the start if ymin is not 0
  357. for (y = 0; y < ymin; y++) {
  358. memset(ptr, 0, avctx->width * 2 * av_pix_fmt_descriptors[avctx->pix_fmt].nb_components);
  359. ptr += stride;
  360. }
  361. // Process the actual lines
  362. for (y = ymin; y <= ymax; y++) {
  363. uint16_t *ptr_x = (uint16_t *)ptr;
  364. if (buf_end - buf > 8) {
  365. /* Read the lineoffset from the line offset table and add 8 bytes
  366. to skip the coordinates and data size fields */
  367. const uint64_t line_offset = bytestream_get_le64(&buf) + 8;
  368. // Check if the buffer has the required bytes needed from the offset
  369. if (line_offset > avpkt->size - xdelta * current_channel_offset) {
  370. // Line offset is probably wrong and not inside the buffer
  371. av_log(avctx, AV_LOG_WARNING, "Line offset for line %d is out of reach setting it to black\n", y);
  372. memset(ptr_x, 0, avctx->width * 2 * av_pix_fmt_descriptors[avctx->pix_fmt].nb_components);
  373. } else {
  374. const uint8_t *red_channel_buffer = avpkt->data + line_offset + xdelta * s->channel_offsets[0];
  375. const uint8_t *green_channel_buffer = avpkt->data + line_offset + xdelta * s->channel_offsets[1];
  376. const uint8_t *blue_channel_buffer = avpkt->data + line_offset + xdelta * s->channel_offsets[2];
  377. const uint8_t *alpha_channel_buffer = 0;
  378. if (s->channel_offsets[3] >= 0)
  379. alpha_channel_buffer = avpkt->data + line_offset + xdelta * s->channel_offsets[3];
  380. // Zero out the start if xmin is not 0
  381. memset(ptr_x, 0, xmin * 2 * av_pix_fmt_descriptors[avctx->pix_fmt].nb_components);
  382. ptr_x += xmin * av_pix_fmt_descriptors[avctx->pix_fmt].nb_components;
  383. if (s->bits_per_color_id == 2) {
  384. // 32-bit
  385. for (x = 0; x < xdelta; x++) {
  386. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&red_channel_buffer));
  387. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&green_channel_buffer));
  388. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&blue_channel_buffer));
  389. if (alpha_channel_buffer)
  390. *ptr_x++ = exr_flt2uint(bytestream_get_le32(&alpha_channel_buffer));
  391. }
  392. } else {
  393. // 16-bit
  394. for (x = 0; x < xdelta; x++) {
  395. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&red_channel_buffer));
  396. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&green_channel_buffer));
  397. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&blue_channel_buffer));
  398. if (alpha_channel_buffer)
  399. *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&alpha_channel_buffer));
  400. }
  401. }
  402. // Zero out the end if xmax+1 is not w
  403. memset(ptr_x, 0, (avctx->width - (xmax + 1)) * 2 * av_pix_fmt_descriptors[avctx->pix_fmt].nb_components);
  404. ptr_x += (avctx->width - (xmax + 1)) * av_pix_fmt_descriptors[avctx->pix_fmt].nb_components;
  405. }
  406. // Move to next line
  407. ptr += stride;
  408. }
  409. }
  410. // Zero out the end if ymax+1 is not h
  411. for (y = ymax + 1; y < avctx->height; y++) {
  412. memset(ptr, 0, avctx->width * 2 * av_pix_fmt_descriptors[avctx->pix_fmt].nb_components);
  413. ptr += stride;
  414. }
  415. *picture = s->picture;
  416. *data_size = sizeof(AVPicture);
  417. return buf_size;
  418. }
  419. static av_cold int decode_init(AVCodecContext *avctx)
  420. {
  421. EXRContext *s = avctx->priv_data;
  422. avcodec_get_frame_defaults(&s->picture);
  423. avctx->coded_frame = &s->picture;
  424. return 0;
  425. }
  426. static av_cold int decode_end(AVCodecContext *avctx)
  427. {
  428. EXRContext *s = avctx->priv_data;
  429. if (s->picture.data[0])
  430. avctx->release_buffer(avctx, &s->picture);
  431. return 0;
  432. }
  433. AVCodec ff_exr_decoder = {
  434. .name = "exr",
  435. .type = AVMEDIA_TYPE_VIDEO,
  436. .id = CODEC_ID_EXR,
  437. .priv_data_size = sizeof(EXRContext),
  438. .init = decode_init,
  439. .close = decode_end,
  440. .decode = decode_frame,
  441. .capabilities = CODEC_CAP_DR1,
  442. .long_name = NULL_IF_CONFIG_SMALL("OpenEXR image"),
  443. };