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.

473 lines
15KB

  1. /*
  2. * Photoshop (PSD) image decoder
  3. * Copyright (c) 2016 Jokyo Images
  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. #include "bytestream.h"
  22. #include "internal.h"
  23. enum PsdCompr {
  24. PSD_RAW,
  25. PSD_RLE,
  26. PSD_ZIP_WITHOUT_P,
  27. PSD_ZIP_WITH_P,
  28. };
  29. enum PsdColorMode {
  30. PSD_BITMAP,
  31. PSD_GRAYSCALE,
  32. PSD_INDEXED,
  33. PSD_RGB,
  34. PSD_CMYK,
  35. PSD_MULTICHANNEL,
  36. PSD_DUOTONE,
  37. PSD_LAB,
  38. };
  39. typedef struct PSDContext {
  40. AVClass *class;
  41. AVFrame *picture;
  42. AVCodecContext *avctx;
  43. GetByteContext gb;
  44. uint8_t * tmp;
  45. uint16_t channel_count;
  46. uint16_t channel_depth;
  47. uint64_t uncompressed_size;
  48. unsigned int pixel_size;/* 1 for 8 bits, 2 for 16 bits */
  49. uint64_t line_size;/* length of src data (even width) */
  50. int width;
  51. int height;
  52. enum PsdCompr compression;
  53. enum PsdColorMode color_mode;
  54. uint8_t palette[AVPALETTE_SIZE];
  55. } PSDContext;
  56. static int decode_header(PSDContext * s)
  57. {
  58. int signature, version, color_mode;
  59. int64_t len_section;
  60. int ret = 0;
  61. if (bytestream2_get_bytes_left(&s->gb) < 30) {/* File header section + color map data section length */
  62. av_log(s->avctx, AV_LOG_ERROR, "Header too short to parse.\n");
  63. return AVERROR_INVALIDDATA;
  64. }
  65. signature = bytestream2_get_le32(&s->gb);
  66. if (signature != MKTAG('8','B','P','S')) {
  67. av_log(s->avctx, AV_LOG_ERROR, "Wrong signature %d.\n", signature);
  68. return AVERROR_INVALIDDATA;
  69. }
  70. version = bytestream2_get_be16(&s->gb);
  71. if (version != 1) {
  72. av_log(s->avctx, AV_LOG_ERROR, "Wrong version %d.\n", version);
  73. return AVERROR_INVALIDDATA;
  74. }
  75. bytestream2_skip(&s->gb, 6);/* reserved */
  76. s->channel_count = bytestream2_get_be16(&s->gb);
  77. if ((s->channel_count < 1) || (s->channel_count > 56)) {
  78. av_log(s->avctx, AV_LOG_ERROR, "Invalid channel count %d.\n", s->channel_count);
  79. return AVERROR_INVALIDDATA;
  80. }
  81. s->height = bytestream2_get_be32(&s->gb);
  82. if ((s->height > 30000) && (s->avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL)) {
  83. av_log(s->avctx, AV_LOG_ERROR,
  84. "Height > 30000 is experimental, add "
  85. "'-strict %d' if you want to try to decode the picture.\n",
  86. FF_COMPLIANCE_EXPERIMENTAL);
  87. return AVERROR_EXPERIMENTAL;
  88. }
  89. s->width = bytestream2_get_be32(&s->gb);
  90. if ((s->width > 30000) && (s->avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL)) {
  91. av_log(s->avctx, AV_LOG_ERROR,
  92. "Width > 30000 is experimental, add "
  93. "'-strict %d' if you want to try to decode the picture.\n",
  94. FF_COMPLIANCE_EXPERIMENTAL);
  95. return AVERROR_EXPERIMENTAL;
  96. }
  97. if ((ret = ff_set_dimensions(s->avctx, s->width, s->height)) < 0)
  98. return ret;
  99. s->channel_depth = bytestream2_get_be16(&s->gb);
  100. color_mode = bytestream2_get_be16(&s->gb);
  101. switch (color_mode) {
  102. case 0:
  103. s->color_mode = PSD_BITMAP;
  104. break;
  105. case 1:
  106. s->color_mode = PSD_GRAYSCALE;
  107. break;
  108. case 2:
  109. s->color_mode = PSD_INDEXED;
  110. break;
  111. case 3:
  112. s->color_mode = PSD_RGB;
  113. break;
  114. case 4:
  115. s->color_mode = PSD_CMYK;
  116. break;
  117. case 7:
  118. s->color_mode = PSD_MULTICHANNEL;
  119. break;
  120. case 8:
  121. s->color_mode = PSD_DUOTONE;
  122. break;
  123. case 9:
  124. s->color_mode = PSD_LAB;
  125. break;
  126. default:
  127. av_log(s->avctx, AV_LOG_ERROR, "Unknown color mode %d.\n", color_mode);
  128. return AVERROR_INVALIDDATA;
  129. }
  130. /* color map data */
  131. len_section = bytestream2_get_be32(&s->gb);
  132. if (len_section < 0) {
  133. av_log(s->avctx, AV_LOG_ERROR, "Negative size for color map data section.\n");
  134. return AVERROR_INVALIDDATA;
  135. }
  136. if (bytestream2_get_bytes_left(&s->gb) < (len_section + 4)) { /* section and len next section */
  137. av_log(s->avctx, AV_LOG_ERROR, "Incomplete file.\n");
  138. return AVERROR_INVALIDDATA;
  139. }
  140. if (len_section) {
  141. int i,j;
  142. memset(s->palette, 0xff, AVPALETTE_SIZE);
  143. for (j = HAVE_BIGENDIAN; j < 3 + HAVE_BIGENDIAN; j++)
  144. for (i = 0; i < FFMIN(256, len_section / 3); i++)
  145. s->palette[i * 4 + (HAVE_BIGENDIAN ? j : 2 - j)] = bytestream2_get_byteu(&s->gb);
  146. len_section -= i * 3;
  147. }
  148. bytestream2_skip(&s->gb, len_section);
  149. /* image ressources */
  150. len_section = bytestream2_get_be32(&s->gb);
  151. if (len_section < 0) {
  152. av_log(s->avctx, AV_LOG_ERROR, "Negative size for image ressources section.\n");
  153. return AVERROR_INVALIDDATA;
  154. }
  155. if (bytestream2_get_bytes_left(&s->gb) < (len_section + 4)) { /* section and len next section */
  156. av_log(s->avctx, AV_LOG_ERROR, "Incomplete file.\n");
  157. return AVERROR_INVALIDDATA;
  158. }
  159. bytestream2_skip(&s->gb, len_section);
  160. /* layers and masks */
  161. len_section = bytestream2_get_be32(&s->gb);
  162. if (len_section < 0) {
  163. av_log(s->avctx, AV_LOG_ERROR, "Negative size for layers and masks data section.\n");
  164. return AVERROR_INVALIDDATA;
  165. }
  166. if (bytestream2_get_bytes_left(&s->gb) < len_section) {
  167. av_log(s->avctx, AV_LOG_ERROR, "Incomplete file.\n");
  168. return AVERROR_INVALIDDATA;
  169. }
  170. bytestream2_skip(&s->gb, len_section);
  171. /* image section */
  172. if (bytestream2_get_bytes_left(&s->gb) < 2) {
  173. av_log(s->avctx, AV_LOG_ERROR, "File without image data section.\n");
  174. return AVERROR_INVALIDDATA;
  175. }
  176. s->compression = bytestream2_get_be16(&s->gb);
  177. switch (s->compression) {
  178. case 0:
  179. case 1:
  180. break;
  181. case 2:
  182. avpriv_request_sample(s->avctx, "ZIP without predictor compression");
  183. return AVERROR_PATCHWELCOME;
  184. break;
  185. case 3:
  186. avpriv_request_sample(s->avctx, "ZIP with predictor compression");
  187. return AVERROR_PATCHWELCOME;
  188. break;
  189. default:
  190. av_log(s->avctx, AV_LOG_ERROR, "Unknown compression %d.\n", s->compression);
  191. return AVERROR_INVALIDDATA;
  192. }
  193. return ret;
  194. }
  195. static int decode_rle(PSDContext * s){
  196. unsigned int scanline_count;
  197. unsigned int sl, count;
  198. unsigned long target_index = 0;
  199. unsigned int p;
  200. int8_t rle_char;
  201. unsigned int repeat_count;
  202. uint8_t v;
  203. scanline_count = s->height * s->channel_count;
  204. /* scanline table */
  205. if (bytestream2_get_bytes_left(&s->gb) < scanline_count * 2) {
  206. av_log(s->avctx, AV_LOG_ERROR, "Not enough data for rle scanline table.\n");
  207. return AVERROR_INVALIDDATA;
  208. }
  209. bytestream2_skip(&s->gb, scanline_count * 2);/* size of each scanline */
  210. /* decode rle data scanline by scanline */
  211. for (sl = 0; sl < scanline_count; sl++) {
  212. count = 0;
  213. while (count < s->line_size) {
  214. rle_char = bytestream2_get_byte(&s->gb);
  215. if (rle_char <= 0) {/* byte repeat */
  216. repeat_count = rle_char * -1;
  217. if (bytestream2_get_bytes_left(&s->gb) < 1) {
  218. av_log(s->avctx, AV_LOG_ERROR, "Not enough data for rle scanline.\n");
  219. return AVERROR_INVALIDDATA;
  220. }
  221. if (target_index + repeat_count >= s->uncompressed_size) {
  222. av_log(s->avctx, AV_LOG_ERROR, "Invalid rle char.\n");
  223. return AVERROR_INVALIDDATA;
  224. }
  225. v = bytestream2_get_byte(&s->gb);
  226. for (p = 0; p <= repeat_count; p++) {
  227. s->tmp[target_index++] = v;
  228. }
  229. count += repeat_count + 1;
  230. } else {
  231. if (bytestream2_get_bytes_left(&s->gb) < rle_char) {
  232. av_log(s->avctx, AV_LOG_ERROR, "Not enough data for rle scanline.\n");
  233. return AVERROR_INVALIDDATA;
  234. }
  235. if (target_index + rle_char >= s->uncompressed_size) {
  236. av_log(s->avctx, AV_LOG_ERROR, "Invalid rle char.\n");
  237. return AVERROR_INVALIDDATA;
  238. }
  239. for (p = 0; p <= rle_char; p++) {
  240. v = bytestream2_get_byte(&s->gb);
  241. s->tmp[target_index++] = v;
  242. }
  243. count += rle_char + 1;
  244. }
  245. }
  246. }
  247. return 0;
  248. }
  249. static int decode_frame(AVCodecContext *avctx, void *data,
  250. int *got_frame, AVPacket *avpkt)
  251. {
  252. int ret;
  253. uint8_t *ptr;
  254. const uint8_t *ptr_data;
  255. int index_out, c, y, x, p;
  256. uint8_t eq_channel[4] = {2,0,1,3};/* RGBA -> GBRA channel order */
  257. uint8_t plane_number;
  258. AVFrame *picture = data;
  259. PSDContext *s = avctx->priv_data;
  260. s->avctx = avctx;
  261. s->channel_count = 0;
  262. s->channel_depth = 0;
  263. s->tmp = NULL;
  264. s->line_size = 0;
  265. bytestream2_init(&s->gb, avpkt->data, avpkt->size);
  266. if ((ret = decode_header(s)) < 0)
  267. return ret;
  268. s->pixel_size = s->channel_depth >> 3;/* in byte */
  269. s->line_size = s->width * s->pixel_size;
  270. switch (s->color_mode) {
  271. case PSD_BITMAP:
  272. if (s->channel_depth != 1 || s->channel_count != 1) {
  273. av_log(s->avctx, AV_LOG_ERROR,
  274. "Invalid bitmap file (channel_depth %d, channel_count %d)\n",
  275. s->channel_depth, s->channel_count);
  276. return AVERROR_INVALIDDATA;
  277. }
  278. s->line_size = s->width + 7 >> 3;
  279. avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
  280. break;
  281. case PSD_INDEXED:
  282. if (s->channel_depth != 8 || s->channel_count != 1) {
  283. av_log(s->avctx, AV_LOG_ERROR,
  284. "Invalid indexed file (channel_depth %d, channel_count %d)\n",
  285. s->channel_depth, s->channel_count);
  286. return AVERROR_INVALIDDATA;
  287. }
  288. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  289. break;
  290. case PSD_RGB:
  291. if (s->channel_count == 3) {
  292. if (s->channel_depth == 8) {
  293. avctx->pix_fmt = AV_PIX_FMT_GBRP;
  294. } else if (s->channel_depth == 16) {
  295. avctx->pix_fmt = AV_PIX_FMT_GBRP16BE;
  296. } else {
  297. avpriv_report_missing_feature(avctx, "channel depth %d for rgb", s->channel_depth);
  298. return AVERROR_PATCHWELCOME;
  299. }
  300. } else if (s->channel_count == 4) {
  301. if (s->channel_depth == 8) {
  302. avctx->pix_fmt = AV_PIX_FMT_GBRAP;
  303. } else if (s->channel_depth == 16) {
  304. avctx->pix_fmt = AV_PIX_FMT_GBRAP16BE;
  305. } else {
  306. avpriv_report_missing_feature(avctx, "channel depth %d for rgb", s->channel_depth);
  307. return AVERROR_PATCHWELCOME;
  308. }
  309. } else {
  310. avpriv_report_missing_feature(avctx, "channel count %d for rgb", s->channel_count);
  311. return AVERROR_PATCHWELCOME;
  312. }
  313. break;
  314. case PSD_DUOTONE:
  315. av_log(avctx, AV_LOG_WARNING, "ignoring unknown duotone specification.\n");
  316. case PSD_GRAYSCALE:
  317. if (s->channel_count == 1) {
  318. if (s->channel_depth == 8) {
  319. avctx->pix_fmt = AV_PIX_FMT_GRAY8;
  320. } else if (s->channel_depth == 16) {
  321. avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
  322. } else {
  323. avpriv_report_missing_feature(avctx, "channel depth %d for grayscale", s->channel_depth);
  324. return AVERROR_PATCHWELCOME;
  325. }
  326. } else if (s->channel_count == 2) {
  327. if (s->channel_depth == 8) {
  328. avctx->pix_fmt = AV_PIX_FMT_YA8;
  329. } else if (s->channel_depth == 16) {
  330. avctx->pix_fmt = AV_PIX_FMT_YA16BE;
  331. } else {
  332. avpriv_report_missing_feature(avctx, "channel depth %d for grayscale", s->channel_depth);
  333. return AVERROR_PATCHWELCOME;
  334. }
  335. } else {
  336. avpriv_report_missing_feature(avctx, "channel count %d for grayscale", s->channel_count);
  337. return AVERROR_PATCHWELCOME;
  338. }
  339. break;
  340. default:
  341. avpriv_report_missing_feature(avctx, "color mode %d", s->color_mode);
  342. return AVERROR_PATCHWELCOME;
  343. }
  344. s->uncompressed_size = s->line_size * s->height * s->channel_count;
  345. if ((ret = ff_get_buffer(avctx, picture, 0)) < 0)
  346. return ret;
  347. /* decode picture if need */
  348. if (s->compression == PSD_RLE) {
  349. s->tmp = av_malloc(s->uncompressed_size);
  350. if (!s->tmp)
  351. return AVERROR(ENOMEM);
  352. ret = decode_rle(s);
  353. if (ret < 0) {
  354. av_freep(&s->tmp);
  355. return ret;
  356. }
  357. ptr_data = s->tmp;
  358. } else {
  359. if (bytestream2_get_bytes_left(&s->gb) < s->uncompressed_size) {
  360. av_log(s->avctx, AV_LOG_ERROR, "Not enough data for raw image data section.\n");
  361. return AVERROR_INVALIDDATA;
  362. }
  363. ptr_data = s->gb.buffer;
  364. }
  365. /* Store data */
  366. if ((avctx->pix_fmt == AV_PIX_FMT_YA8)||(avctx->pix_fmt == AV_PIX_FMT_YA16BE)){/* Interleaved */
  367. ptr = picture->data[0];
  368. for (c = 0; c < s->channel_count; c++) {
  369. for (y = 0; y < s->height; y++) {
  370. for (x = 0; x < s->width; x++) {
  371. index_out = y * picture->linesize[0] + x * s->channel_count * s->pixel_size + c * s->pixel_size;
  372. for (p = 0; p < s->pixel_size; p++) {
  373. ptr[index_out + p] = *ptr_data;
  374. ptr_data ++;
  375. }
  376. }
  377. }
  378. }
  379. } else {/* Planar */
  380. if (s->channel_count == 1)/* gray 8 or gray 16be */
  381. eq_channel[0] = 0;/* assign first channel, to first plane */
  382. for (c = 0; c < s->channel_count; c++) {
  383. plane_number = eq_channel[c];
  384. ptr = picture->data[plane_number];/* get the right plane */
  385. for (y = 0; y < s->height; y++) {
  386. memcpy(ptr, ptr_data, s->line_size);
  387. ptr += picture->linesize[plane_number];
  388. ptr_data += s->line_size;
  389. }
  390. }
  391. }
  392. if (s->color_mode == PSD_INDEXED) {
  393. picture->palette_has_changed = 1;
  394. memcpy(picture->data[1], s->palette, AVPALETTE_SIZE);
  395. }
  396. av_freep(&s->tmp);
  397. picture->pict_type = AV_PICTURE_TYPE_I;
  398. *got_frame = 1;
  399. return avpkt->size;
  400. }
  401. AVCodec ff_psd_decoder = {
  402. .name = "psd",
  403. .long_name = NULL_IF_CONFIG_SMALL("Photoshop PSD file"),
  404. .type = AVMEDIA_TYPE_VIDEO,
  405. .id = AV_CODEC_ID_PSD,
  406. .priv_data_size = sizeof(PSDContext),
  407. .decode = decode_frame,
  408. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
  409. };