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.

557 lines
19KB

  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. case 3:
  185. avpriv_request_sample(s->avctx, "ZIP with predictor compression");
  186. return AVERROR_PATCHWELCOME;
  187. default:
  188. av_log(s->avctx, AV_LOG_ERROR, "Unknown compression %d.\n", s->compression);
  189. return AVERROR_INVALIDDATA;
  190. }
  191. return ret;
  192. }
  193. static int decode_rle(PSDContext * s){
  194. unsigned int scanline_count;
  195. unsigned int sl, count;
  196. unsigned long target_index = 0;
  197. unsigned int p;
  198. int8_t rle_char;
  199. unsigned int repeat_count;
  200. uint8_t v;
  201. scanline_count = s->height * s->channel_count;
  202. /* scanline table */
  203. if (bytestream2_get_bytes_left(&s->gb) < scanline_count * 2) {
  204. av_log(s->avctx, AV_LOG_ERROR, "Not enough data for rle scanline table.\n");
  205. return AVERROR_INVALIDDATA;
  206. }
  207. bytestream2_skip(&s->gb, scanline_count * 2);/* size of each scanline */
  208. /* decode rle data scanline by scanline */
  209. for (sl = 0; sl < scanline_count; sl++) {
  210. count = 0;
  211. while (count < s->line_size) {
  212. rle_char = bytestream2_get_byte(&s->gb);
  213. if (rle_char <= 0) {/* byte repeat */
  214. repeat_count = rle_char * -1;
  215. if (bytestream2_get_bytes_left(&s->gb) < 1) {
  216. av_log(s->avctx, AV_LOG_ERROR, "Not enough data for rle scanline.\n");
  217. return AVERROR_INVALIDDATA;
  218. }
  219. if (target_index + repeat_count >= s->uncompressed_size) {
  220. av_log(s->avctx, AV_LOG_ERROR, "Invalid rle char.\n");
  221. return AVERROR_INVALIDDATA;
  222. }
  223. v = bytestream2_get_byte(&s->gb);
  224. for (p = 0; p <= repeat_count; p++) {
  225. s->tmp[target_index++] = v;
  226. }
  227. count += repeat_count + 1;
  228. } else {
  229. if (bytestream2_get_bytes_left(&s->gb) < rle_char) {
  230. av_log(s->avctx, AV_LOG_ERROR, "Not enough data for rle scanline.\n");
  231. return AVERROR_INVALIDDATA;
  232. }
  233. if (target_index + rle_char >= s->uncompressed_size) {
  234. av_log(s->avctx, AV_LOG_ERROR, "Invalid rle char.\n");
  235. return AVERROR_INVALIDDATA;
  236. }
  237. for (p = 0; p <= rle_char; p++) {
  238. v = bytestream2_get_byte(&s->gb);
  239. s->tmp[target_index++] = v;
  240. }
  241. count += rle_char + 1;
  242. }
  243. }
  244. }
  245. return 0;
  246. }
  247. static int decode_frame(AVCodecContext *avctx, void *data,
  248. int *got_frame, AVPacket *avpkt)
  249. {
  250. int ret;
  251. uint8_t *ptr;
  252. const uint8_t *ptr_data;
  253. int index_out, c, y, x, p;
  254. uint8_t eq_channel[4] = {2,0,1,3};/* RGBA -> GBRA channel order */
  255. uint8_t plane_number;
  256. AVFrame *picture = data;
  257. PSDContext *s = avctx->priv_data;
  258. s->avctx = avctx;
  259. s->channel_count = 0;
  260. s->channel_depth = 0;
  261. s->tmp = NULL;
  262. s->line_size = 0;
  263. bytestream2_init(&s->gb, avpkt->data, avpkt->size);
  264. if ((ret = decode_header(s)) < 0)
  265. return ret;
  266. s->pixel_size = s->channel_depth >> 3;/* in byte */
  267. s->line_size = s->width * s->pixel_size;
  268. switch (s->color_mode) {
  269. case PSD_BITMAP:
  270. if (s->channel_depth != 1 || s->channel_count != 1) {
  271. av_log(s->avctx, AV_LOG_ERROR,
  272. "Invalid bitmap file (channel_depth %d, channel_count %d)\n",
  273. s->channel_depth, s->channel_count);
  274. return AVERROR_INVALIDDATA;
  275. }
  276. s->line_size = s->width + 7 >> 3;
  277. avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
  278. break;
  279. case PSD_INDEXED:
  280. if (s->channel_depth != 8 || s->channel_count != 1) {
  281. av_log(s->avctx, AV_LOG_ERROR,
  282. "Invalid indexed file (channel_depth %d, channel_count %d)\n",
  283. s->channel_depth, s->channel_count);
  284. return AVERROR_INVALIDDATA;
  285. }
  286. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  287. break;
  288. case PSD_CMYK:
  289. if (s->channel_count == 4) {
  290. if (s->channel_depth == 8) {
  291. avctx->pix_fmt = AV_PIX_FMT_GBRP;
  292. } else if (s->channel_depth == 16) {
  293. avctx->pix_fmt = AV_PIX_FMT_GBRP16BE;
  294. } else {
  295. avpriv_report_missing_feature(avctx, "channel depth %d for cmyk", s->channel_depth);
  296. return AVERROR_PATCHWELCOME;
  297. }
  298. } else if (s->channel_count == 5) {
  299. if (s->channel_depth == 8) {
  300. avctx->pix_fmt = AV_PIX_FMT_GBRAP;
  301. } else if (s->channel_depth == 16) {
  302. avctx->pix_fmt = AV_PIX_FMT_GBRAP16BE;
  303. } else {
  304. avpriv_report_missing_feature(avctx, "channel depth %d for cmyk", s->channel_depth);
  305. return AVERROR_PATCHWELCOME;
  306. }
  307. } else {
  308. avpriv_report_missing_feature(avctx, "channel count %d for cmyk", s->channel_count);
  309. return AVERROR_PATCHWELCOME;
  310. }
  311. break;
  312. case PSD_RGB:
  313. if (s->channel_count == 3) {
  314. if (s->channel_depth == 8) {
  315. avctx->pix_fmt = AV_PIX_FMT_GBRP;
  316. } else if (s->channel_depth == 16) {
  317. avctx->pix_fmt = AV_PIX_FMT_GBRP16BE;
  318. } else {
  319. avpriv_report_missing_feature(avctx, "channel depth %d for rgb", s->channel_depth);
  320. return AVERROR_PATCHWELCOME;
  321. }
  322. } else if (s->channel_count == 4) {
  323. if (s->channel_depth == 8) {
  324. avctx->pix_fmt = AV_PIX_FMT_GBRAP;
  325. } else if (s->channel_depth == 16) {
  326. avctx->pix_fmt = AV_PIX_FMT_GBRAP16BE;
  327. } else {
  328. avpriv_report_missing_feature(avctx, "channel depth %d for rgb", s->channel_depth);
  329. return AVERROR_PATCHWELCOME;
  330. }
  331. } else {
  332. avpriv_report_missing_feature(avctx, "channel count %d for rgb", s->channel_count);
  333. return AVERROR_PATCHWELCOME;
  334. }
  335. break;
  336. case PSD_DUOTONE:
  337. av_log(avctx, AV_LOG_WARNING, "ignoring unknown duotone specification.\n");
  338. case PSD_GRAYSCALE:
  339. if (s->channel_count == 1) {
  340. if (s->channel_depth == 8) {
  341. avctx->pix_fmt = AV_PIX_FMT_GRAY8;
  342. } else if (s->channel_depth == 16) {
  343. avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
  344. } else if (s->channel_depth == 32) {
  345. avctx->pix_fmt = AV_PIX_FMT_GRAYF32BE;
  346. } else {
  347. avpriv_report_missing_feature(avctx, "channel depth %d for grayscale", s->channel_depth);
  348. return AVERROR_PATCHWELCOME;
  349. }
  350. } else if (s->channel_count == 2) {
  351. if (s->channel_depth == 8) {
  352. avctx->pix_fmt = AV_PIX_FMT_YA8;
  353. } else if (s->channel_depth == 16) {
  354. avctx->pix_fmt = AV_PIX_FMT_YA16BE;
  355. } else {
  356. avpriv_report_missing_feature(avctx, "channel depth %d for grayscale", s->channel_depth);
  357. return AVERROR_PATCHWELCOME;
  358. }
  359. } else {
  360. avpriv_report_missing_feature(avctx, "channel count %d for grayscale", s->channel_count);
  361. return AVERROR_PATCHWELCOME;
  362. }
  363. break;
  364. default:
  365. avpriv_report_missing_feature(avctx, "color mode %d", s->color_mode);
  366. return AVERROR_PATCHWELCOME;
  367. }
  368. s->uncompressed_size = s->line_size * s->height * s->channel_count;
  369. if ((ret = ff_get_buffer(avctx, picture, 0)) < 0)
  370. return ret;
  371. /* decode picture if need */
  372. if (s->compression == PSD_RLE) {
  373. s->tmp = av_malloc(s->uncompressed_size);
  374. if (!s->tmp)
  375. return AVERROR(ENOMEM);
  376. ret = decode_rle(s);
  377. if (ret < 0) {
  378. av_freep(&s->tmp);
  379. return ret;
  380. }
  381. ptr_data = s->tmp;
  382. } else {
  383. if (bytestream2_get_bytes_left(&s->gb) < s->uncompressed_size) {
  384. av_log(s->avctx, AV_LOG_ERROR, "Not enough data for raw image data section.\n");
  385. return AVERROR_INVALIDDATA;
  386. }
  387. ptr_data = s->gb.buffer;
  388. }
  389. /* Store data */
  390. if ((avctx->pix_fmt == AV_PIX_FMT_YA8)||(avctx->pix_fmt == AV_PIX_FMT_YA16BE)){/* Interleaved */
  391. ptr = picture->data[0];
  392. for (c = 0; c < s->channel_count; c++) {
  393. for (y = 0; y < s->height; y++) {
  394. for (x = 0; x < s->width; x++) {
  395. index_out = y * picture->linesize[0] + x * s->channel_count * s->pixel_size + c * s->pixel_size;
  396. for (p = 0; p < s->pixel_size; p++) {
  397. ptr[index_out + p] = *ptr_data;
  398. ptr_data ++;
  399. }
  400. }
  401. }
  402. }
  403. } else if (s->color_mode == PSD_CMYK) {
  404. uint8_t *dst[4] = { picture->data[0], picture->data[1], picture->data[2], picture->data[3] };
  405. const uint8_t *src[5] = { ptr_data };
  406. src[1] = src[0] + s->line_size * s->height;
  407. src[2] = src[1] + s->line_size * s->height;
  408. src[3] = src[2] + s->line_size * s->height;
  409. src[4] = src[3] + s->line_size * s->height;
  410. if (s->channel_depth == 8) {
  411. for (y = 0; y < s->height; y++) {
  412. for (x = 0; x < s->width; x++) {
  413. int k = src[3][x];
  414. int r = src[0][x] * k;
  415. int g = src[1][x] * k;
  416. int b = src[2][x] * k;
  417. dst[0][x] = g * 257 >> 16;
  418. dst[1][x] = b * 257 >> 16;
  419. dst[2][x] = r * 257 >> 16;
  420. }
  421. dst[0] += picture->linesize[0];
  422. dst[1] += picture->linesize[1];
  423. dst[2] += picture->linesize[2];
  424. src[0] += s->line_size;
  425. src[1] += s->line_size;
  426. src[2] += s->line_size;
  427. src[3] += s->line_size;
  428. }
  429. if (avctx->pix_fmt == AV_PIX_FMT_GBRAP) {
  430. for (y = 0; y < s->height; y++) {
  431. memcpy(dst[3], src[4], s->line_size);
  432. src[4] += s->line_size;
  433. dst[3] += picture->linesize[3];
  434. }
  435. }
  436. } else {
  437. for (y = 0; y < s->height; y++) {
  438. for (x = 0; x < s->width; x++) {
  439. int64_t k = AV_RB16(&src[3][x * 2]);
  440. int64_t r = AV_RB16(&src[0][x * 2]) * k;
  441. int64_t g = AV_RB16(&src[1][x * 2]) * k;
  442. int64_t b = AV_RB16(&src[2][x * 2]) * k;
  443. AV_WB16(&dst[0][x * 2], g * 65537 >> 32);
  444. AV_WB16(&dst[1][x * 2], b * 65537 >> 32);
  445. AV_WB16(&dst[2][x * 2], r * 65537 >> 32);
  446. }
  447. dst[0] += picture->linesize[0];
  448. dst[1] += picture->linesize[1];
  449. dst[2] += picture->linesize[2];
  450. src[0] += s->line_size;
  451. src[1] += s->line_size;
  452. src[2] += s->line_size;
  453. src[3] += s->line_size;
  454. }
  455. if (avctx->pix_fmt == AV_PIX_FMT_GBRAP16BE) {
  456. for (y = 0; y < s->height; y++) {
  457. memcpy(dst[3], src[4], s->line_size);
  458. src[4] += s->line_size;
  459. dst[3] += picture->linesize[3];
  460. }
  461. }
  462. }
  463. } else {/* Planar */
  464. if (s->channel_count == 1)/* gray 8 or gray 16be */
  465. eq_channel[0] = 0;/* assign first channel, to first plane */
  466. for (c = 0; c < s->channel_count; c++) {
  467. plane_number = eq_channel[c];
  468. ptr = picture->data[plane_number];/* get the right plane */
  469. for (y = 0; y < s->height; y++) {
  470. memcpy(ptr, ptr_data, s->line_size);
  471. ptr += picture->linesize[plane_number];
  472. ptr_data += s->line_size;
  473. }
  474. }
  475. }
  476. if (s->color_mode == PSD_INDEXED) {
  477. picture->palette_has_changed = 1;
  478. memcpy(picture->data[1], s->palette, AVPALETTE_SIZE);
  479. }
  480. av_freep(&s->tmp);
  481. picture->pict_type = AV_PICTURE_TYPE_I;
  482. *got_frame = 1;
  483. return avpkt->size;
  484. }
  485. AVCodec ff_psd_decoder = {
  486. .name = "psd",
  487. .long_name = NULL_IF_CONFIG_SMALL("Photoshop PSD file"),
  488. .type = AVMEDIA_TYPE_VIDEO,
  489. .id = AV_CODEC_ID_PSD,
  490. .priv_data_size = sizeof(PSDContext),
  491. .decode = decode_frame,
  492. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
  493. };