psd.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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 "libavutil/mem.h"
  22. #include "bytestream.h"
  23. #include "codec_internal.h"
  24. #include "decode.h"
  25. enum PsdCompr {
  26. PSD_RAW,
  27. PSD_RLE,
  28. PSD_ZIP_WITHOUT_P,
  29. PSD_ZIP_WITH_P,
  30. };
  31. enum PsdColorMode {
  32. PSD_BITMAP,
  33. PSD_GRAYSCALE,
  34. PSD_INDEXED,
  35. PSD_RGB,
  36. PSD_CMYK,
  37. PSD_MULTICHANNEL,
  38. PSD_DUOTONE,
  39. PSD_LAB,
  40. };
  41. typedef struct PSDContext {
  42. AVClass *class;
  43. AVFrame *picture;
  44. AVCodecContext *avctx;
  45. GetByteContext gb;
  46. uint8_t * tmp;
  47. uint16_t channel_count;
  48. uint16_t channel_depth;
  49. uint64_t uncompressed_size;
  50. unsigned int pixel_size;/* 1 for 8 bits, 2 for 16 bits */
  51. uint64_t line_size;/* length of src data (even width) */
  52. int width;
  53. int height;
  54. enum PsdCompr compression;
  55. enum PsdColorMode color_mode;
  56. uint8_t palette[AVPALETTE_SIZE];
  57. } PSDContext;
  58. static int decode_header(PSDContext * s)
  59. {
  60. int signature, version, color_mode;
  61. int64_t len_section;
  62. int ret = 0;
  63. if (bytestream2_get_bytes_left(&s->gb) < 30) {/* File header section + color map data section length */
  64. av_log(s->avctx, AV_LOG_ERROR, "Header too short to parse.\n");
  65. return AVERROR_INVALIDDATA;
  66. }
  67. signature = bytestream2_get_le32(&s->gb);
  68. if (signature != MKTAG('8','B','P','S')) {
  69. av_log(s->avctx, AV_LOG_ERROR, "Wrong signature %d.\n", signature);
  70. return AVERROR_INVALIDDATA;
  71. }
  72. version = bytestream2_get_be16(&s->gb);
  73. if (version != 1) {
  74. av_log(s->avctx, AV_LOG_ERROR, "Wrong version %d.\n", version);
  75. return AVERROR_INVALIDDATA;
  76. }
  77. bytestream2_skip(&s->gb, 6);/* reserved */
  78. s->channel_count = bytestream2_get_be16(&s->gb);
  79. if ((s->channel_count < 1) || (s->channel_count > 56)) {
  80. av_log(s->avctx, AV_LOG_ERROR, "Invalid channel count %d.\n", s->channel_count);
  81. return AVERROR_INVALIDDATA;
  82. }
  83. s->height = bytestream2_get_be32(&s->gb);
  84. if ((s->height > 30000) && (s->avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL)) {
  85. av_log(s->avctx, AV_LOG_ERROR,
  86. "Height > 30000 is experimental, add "
  87. "'-strict %d' if you want to try to decode the picture.\n",
  88. FF_COMPLIANCE_EXPERIMENTAL);
  89. return AVERROR_EXPERIMENTAL;
  90. }
  91. s->width = bytestream2_get_be32(&s->gb);
  92. if ((s->width > 30000) && (s->avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL)) {
  93. av_log(s->avctx, AV_LOG_ERROR,
  94. "Width > 30000 is experimental, add "
  95. "'-strict %d' if you want to try to decode the picture.\n",
  96. FF_COMPLIANCE_EXPERIMENTAL);
  97. return AVERROR_EXPERIMENTAL;
  98. }
  99. if ((ret = ff_set_dimensions(s->avctx, s->width, s->height)) < 0)
  100. return ret;
  101. s->channel_depth = bytestream2_get_be16(&s->gb);
  102. color_mode = bytestream2_get_be16(&s->gb);
  103. switch (color_mode) {
  104. case 0:
  105. s->color_mode = PSD_BITMAP;
  106. break;
  107. case 1:
  108. s->color_mode = PSD_GRAYSCALE;
  109. break;
  110. case 2:
  111. s->color_mode = PSD_INDEXED;
  112. break;
  113. case 3:
  114. s->color_mode = PSD_RGB;
  115. break;
  116. case 4:
  117. s->color_mode = PSD_CMYK;
  118. break;
  119. case 7:
  120. s->color_mode = PSD_MULTICHANNEL;
  121. break;
  122. case 8:
  123. s->color_mode = PSD_DUOTONE;
  124. break;
  125. case 9:
  126. s->color_mode = PSD_LAB;
  127. break;
  128. default:
  129. av_log(s->avctx, AV_LOG_ERROR, "Unknown color mode %d.\n", color_mode);
  130. return AVERROR_INVALIDDATA;
  131. }
  132. /* color map data */
  133. len_section = bytestream2_get_be32(&s->gb);
  134. if (len_section < 0) {
  135. av_log(s->avctx, AV_LOG_ERROR, "Negative size for color map data section.\n");
  136. return AVERROR_INVALIDDATA;
  137. }
  138. if (bytestream2_get_bytes_left(&s->gb) < (len_section + 4)) { /* section and len next section */
  139. av_log(s->avctx, AV_LOG_ERROR, "Incomplete file.\n");
  140. return AVERROR_INVALIDDATA;
  141. }
  142. if (len_section) {
  143. int i,j;
  144. memset(s->palette, 0xff, AVPALETTE_SIZE);
  145. for (j = HAVE_BIGENDIAN; j < 3 + HAVE_BIGENDIAN; j++)
  146. for (i = 0; i < FFMIN(256, len_section / 3); i++)
  147. s->palette[i * 4 + (HAVE_BIGENDIAN ? j : 2 - j)] = bytestream2_get_byteu(&s->gb);
  148. len_section -= i * 3;
  149. }
  150. bytestream2_skip(&s->gb, len_section);
  151. /* image resources */
  152. len_section = bytestream2_get_be32(&s->gb);
  153. if (len_section < 0) {
  154. av_log(s->avctx, AV_LOG_ERROR, "Negative size for image resources section.\n");
  155. return AVERROR_INVALIDDATA;
  156. }
  157. if (bytestream2_get_bytes_left(&s->gb) < (len_section + 4)) { /* section and len next section */
  158. av_log(s->avctx, AV_LOG_ERROR, "Incomplete file.\n");
  159. return AVERROR_INVALIDDATA;
  160. }
  161. bytestream2_skip(&s->gb, len_section);
  162. /* layers and masks */
  163. len_section = bytestream2_get_be32(&s->gb);
  164. if (len_section < 0) {
  165. av_log(s->avctx, AV_LOG_ERROR, "Negative size for layers and masks data section.\n");
  166. return AVERROR_INVALIDDATA;
  167. }
  168. if (bytestream2_get_bytes_left(&s->gb) < len_section) {
  169. av_log(s->avctx, AV_LOG_ERROR, "Incomplete file.\n");
  170. return AVERROR_INVALIDDATA;
  171. }
  172. bytestream2_skip(&s->gb, len_section);
  173. /* image section */
  174. if (bytestream2_get_bytes_left(&s->gb) < 2) {
  175. av_log(s->avctx, AV_LOG_ERROR, "File without image data section.\n");
  176. return AVERROR_INVALIDDATA;
  177. }
  178. s->compression = bytestream2_get_be16(&s->gb);
  179. switch (s->compression) {
  180. case 0:
  181. case 1:
  182. break;
  183. case 2:
  184. avpriv_request_sample(s->avctx, "ZIP without predictor compression");
  185. return AVERROR_PATCHWELCOME;
  186. case 3:
  187. avpriv_request_sample(s->avctx, "ZIP with predictor compression");
  188. return AVERROR_PATCHWELCOME;
  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, AVFrame *picture,
  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. PSDContext *s = avctx->priv_data;
  259. s->avctx = avctx;
  260. s->channel_count = 0;
  261. s->channel_depth = 0;
  262. s->tmp = NULL;
  263. s->line_size = 0;
  264. bytestream2_init(&s->gb, avpkt->data, avpkt->size);
  265. if ((ret = decode_header(s)) < 0)
  266. return ret;
  267. s->pixel_size = s->channel_depth >> 3;/* in byte */
  268. s->line_size = s->width * s->pixel_size;
  269. switch (s->color_mode) {
  270. case PSD_BITMAP:
  271. if (s->channel_depth != 1 || s->channel_count != 1) {
  272. av_log(s->avctx, AV_LOG_ERROR,
  273. "Invalid bitmap file (channel_depth %d, channel_count %d)\n",
  274. s->channel_depth, s->channel_count);
  275. return AVERROR_INVALIDDATA;
  276. }
  277. s->line_size = s->width + 7 >> 3;
  278. avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
  279. break;
  280. case PSD_INDEXED:
  281. if (s->channel_depth != 8 || s->channel_count != 1) {
  282. av_log(s->avctx, AV_LOG_ERROR,
  283. "Invalid indexed file (channel_depth %d, channel_count %d)\n",
  284. s->channel_depth, s->channel_count);
  285. return AVERROR_INVALIDDATA;
  286. }
  287. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  288. break;
  289. case PSD_CMYK:
  290. if (s->channel_count == 4) {
  291. if (s->channel_depth == 8) {
  292. avctx->pix_fmt = AV_PIX_FMT_GBRP;
  293. } else if (s->channel_depth == 16) {
  294. avctx->pix_fmt = AV_PIX_FMT_GBRP16BE;
  295. } else {
  296. avpriv_report_missing_feature(avctx, "channel depth %d for cmyk", s->channel_depth);
  297. return AVERROR_PATCHWELCOME;
  298. }
  299. } else if (s->channel_count == 5) {
  300. if (s->channel_depth == 8) {
  301. avctx->pix_fmt = AV_PIX_FMT_GBRAP;
  302. } else if (s->channel_depth == 16) {
  303. avctx->pix_fmt = AV_PIX_FMT_GBRAP16BE;
  304. } else {
  305. avpriv_report_missing_feature(avctx, "channel depth %d for cmyk", s->channel_depth);
  306. return AVERROR_PATCHWELCOME;
  307. }
  308. } else {
  309. avpriv_report_missing_feature(avctx, "channel count %d for cmyk", s->channel_count);
  310. return AVERROR_PATCHWELCOME;
  311. }
  312. break;
  313. case PSD_RGB:
  314. if (s->channel_count == 3) {
  315. if (s->channel_depth == 8) {
  316. avctx->pix_fmt = AV_PIX_FMT_GBRP;
  317. } else if (s->channel_depth == 16) {
  318. avctx->pix_fmt = AV_PIX_FMT_GBRP16BE;
  319. } else {
  320. avpriv_report_missing_feature(avctx, "channel depth %d for rgb", s->channel_depth);
  321. return AVERROR_PATCHWELCOME;
  322. }
  323. } else if (s->channel_count == 4) {
  324. if (s->channel_depth == 8) {
  325. avctx->pix_fmt = AV_PIX_FMT_GBRAP;
  326. } else if (s->channel_depth == 16) {
  327. avctx->pix_fmt = AV_PIX_FMT_GBRAP16BE;
  328. } else {
  329. avpriv_report_missing_feature(avctx, "channel depth %d for rgb", s->channel_depth);
  330. return AVERROR_PATCHWELCOME;
  331. }
  332. } else {
  333. avpriv_report_missing_feature(avctx, "channel count %d for rgb", s->channel_count);
  334. return AVERROR_PATCHWELCOME;
  335. }
  336. break;
  337. case PSD_DUOTONE:
  338. av_log(avctx, AV_LOG_WARNING, "ignoring unknown duotone specification.\n");
  339. case PSD_GRAYSCALE:
  340. if (s->channel_count == 1) {
  341. if (s->channel_depth == 8) {
  342. avctx->pix_fmt = AV_PIX_FMT_GRAY8;
  343. } else if (s->channel_depth == 16) {
  344. avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
  345. } else if (s->channel_depth == 32) {
  346. avctx->pix_fmt = AV_PIX_FMT_GRAYF32BE;
  347. } else {
  348. avpriv_report_missing_feature(avctx, "channel depth %d for grayscale", s->channel_depth);
  349. return AVERROR_PATCHWELCOME;
  350. }
  351. } else if (s->channel_count == 2) {
  352. if (s->channel_depth == 8) {
  353. avctx->pix_fmt = AV_PIX_FMT_YA8;
  354. } else if (s->channel_depth == 16) {
  355. avctx->pix_fmt = AV_PIX_FMT_YA16BE;
  356. } else {
  357. avpriv_report_missing_feature(avctx, "channel depth %d for grayscale", s->channel_depth);
  358. return AVERROR_PATCHWELCOME;
  359. }
  360. } else {
  361. avpriv_report_missing_feature(avctx, "channel count %d for grayscale", s->channel_count);
  362. return AVERROR_PATCHWELCOME;
  363. }
  364. break;
  365. default:
  366. avpriv_report_missing_feature(avctx, "color mode %d", s->color_mode);
  367. return AVERROR_PATCHWELCOME;
  368. }
  369. s->uncompressed_size = s->line_size * s->height * s->channel_count;
  370. /* decode picture if need */
  371. if (s->compression == PSD_RLE) {
  372. s->tmp = av_malloc(s->uncompressed_size);
  373. if (!s->tmp)
  374. return AVERROR(ENOMEM);
  375. ret = decode_rle(s);
  376. if (ret < 0) {
  377. av_freep(&s->tmp);
  378. return ret;
  379. }
  380. ptr_data = s->tmp;
  381. } else {
  382. if (bytestream2_get_bytes_left(&s->gb) < s->uncompressed_size) {
  383. av_log(s->avctx, AV_LOG_ERROR, "Not enough data for raw image data section.\n");
  384. return AVERROR_INVALIDDATA;
  385. }
  386. ptr_data = s->gb.buffer;
  387. }
  388. if ((ret = ff_get_buffer(avctx, picture, 0)) < 0)
  389. return ret;
  390. /* Store data */
  391. if ((avctx->pix_fmt == AV_PIX_FMT_YA8)||(avctx->pix_fmt == AV_PIX_FMT_YA16BE)){/* Interleaved */
  392. ptr = picture->data[0];
  393. for (c = 0; c < s->channel_count; c++) {
  394. for (y = 0; y < s->height; y++) {
  395. for (x = 0; x < s->width; x++) {
  396. index_out = y * picture->linesize[0] + x * s->channel_count * s->pixel_size + c * s->pixel_size;
  397. for (p = 0; p < s->pixel_size; p++) {
  398. ptr[index_out + p] = *ptr_data;
  399. ptr_data ++;
  400. }
  401. }
  402. }
  403. }
  404. } else if (s->color_mode == PSD_CMYK) {
  405. uint8_t *dst[4] = { picture->data[0], picture->data[1], picture->data[2], picture->data[3] };
  406. const uint8_t *src[5] = { ptr_data };
  407. src[1] = src[0] + s->line_size * s->height;
  408. src[2] = src[1] + s->line_size * s->height;
  409. src[3] = src[2] + s->line_size * s->height;
  410. src[4] = src[3] + s->line_size * s->height;
  411. if (s->channel_depth == 8) {
  412. for (y = 0; y < s->height; y++) {
  413. for (x = 0; x < s->width; x++) {
  414. int k = src[3][x];
  415. int r = src[0][x] * k;
  416. int g = src[1][x] * k;
  417. int b = src[2][x] * k;
  418. dst[0][x] = g * 257 >> 16;
  419. dst[1][x] = b * 257 >> 16;
  420. dst[2][x] = r * 257 >> 16;
  421. }
  422. dst[0] += picture->linesize[0];
  423. dst[1] += picture->linesize[1];
  424. dst[2] += picture->linesize[2];
  425. src[0] += s->line_size;
  426. src[1] += s->line_size;
  427. src[2] += s->line_size;
  428. src[3] += s->line_size;
  429. }
  430. if (avctx->pix_fmt == AV_PIX_FMT_GBRAP) {
  431. for (y = 0; y < s->height; y++) {
  432. memcpy(dst[3], src[4], s->line_size);
  433. src[4] += s->line_size;
  434. dst[3] += picture->linesize[3];
  435. }
  436. }
  437. } else {
  438. for (y = 0; y < s->height; y++) {
  439. for (x = 0; x < s->width; x++) {
  440. int64_t k = AV_RB16(&src[3][x * 2]);
  441. int64_t r = AV_RB16(&src[0][x * 2]) * k;
  442. int64_t g = AV_RB16(&src[1][x * 2]) * k;
  443. int64_t b = AV_RB16(&src[2][x * 2]) * k;
  444. AV_WB16(&dst[0][x * 2], g * 65537 >> 32);
  445. AV_WB16(&dst[1][x * 2], b * 65537 >> 32);
  446. AV_WB16(&dst[2][x * 2], r * 65537 >> 32);
  447. }
  448. dst[0] += picture->linesize[0];
  449. dst[1] += picture->linesize[1];
  450. dst[2] += picture->linesize[2];
  451. src[0] += s->line_size;
  452. src[1] += s->line_size;
  453. src[2] += s->line_size;
  454. src[3] += s->line_size;
  455. }
  456. if (avctx->pix_fmt == AV_PIX_FMT_GBRAP16BE) {
  457. for (y = 0; y < s->height; y++) {
  458. memcpy(dst[3], src[4], s->line_size);
  459. src[4] += s->line_size;
  460. dst[3] += picture->linesize[3];
  461. }
  462. }
  463. }
  464. } else {/* Planar */
  465. if (s->channel_count == 1)/* gray 8 or gray 16be */
  466. eq_channel[0] = 0;/* assign first channel, to first plane */
  467. for (c = 0; c < s->channel_count; c++) {
  468. plane_number = eq_channel[c];
  469. ptr = picture->data[plane_number];/* get the right plane */
  470. for (y = 0; y < s->height; y++) {
  471. memcpy(ptr, ptr_data, s->line_size);
  472. ptr += picture->linesize[plane_number];
  473. ptr_data += s->line_size;
  474. }
  475. }
  476. }
  477. if (s->color_mode == PSD_INDEXED) {
  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. const FFCodec ff_psd_decoder = {
  486. .p.name = "psd",
  487. CODEC_LONG_NAME("Photoshop PSD file"),
  488. .p.type = AVMEDIA_TYPE_VIDEO,
  489. .p.id = AV_CODEC_ID_PSD,
  490. .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
  491. .priv_data_size = sizeof(PSDContext),
  492. FF_CODEC_DECODE_CB(decode_frame),
  493. };