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.

462 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. s->uncompressed_size = s->line_size * s->height * s->channel_count;
  271. switch (s->color_mode) {
  272. case PSD_INDEXED:
  273. if (s->channel_depth != 8 || s->channel_count != 1) {
  274. av_log(s->avctx, AV_LOG_ERROR,
  275. "Invalid indexed file (channel_depth %d, channel_count %d)\n",
  276. s->channel_depth, s->channel_count);
  277. return AVERROR_INVALIDDATA;
  278. }
  279. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  280. break;
  281. case PSD_RGB:
  282. if (s->channel_count == 3) {
  283. if (s->channel_depth == 8) {
  284. avctx->pix_fmt = AV_PIX_FMT_GBRP;
  285. } else if (s->channel_depth == 16) {
  286. avctx->pix_fmt = AV_PIX_FMT_GBRP16BE;
  287. } else {
  288. avpriv_report_missing_feature(avctx, "channel depth %d for rgb", s->channel_depth);
  289. return AVERROR_PATCHWELCOME;
  290. }
  291. } else if (s->channel_count == 4) {
  292. if (s->channel_depth == 8) {
  293. avctx->pix_fmt = AV_PIX_FMT_GBRAP;
  294. } else if (s->channel_depth == 16) {
  295. avctx->pix_fmt = AV_PIX_FMT_GBRAP16BE;
  296. } else {
  297. avpriv_report_missing_feature(avctx, "channel depth %d for rgb", s->channel_depth);
  298. return AVERROR_PATCHWELCOME;
  299. }
  300. } else {
  301. avpriv_report_missing_feature(avctx, "channel count %d for rgb", s->channel_count);
  302. return AVERROR_PATCHWELCOME;
  303. }
  304. break;
  305. case PSD_DUOTONE:
  306. av_log(avctx, AV_LOG_WARNING, "ignoring unknwon duotone specification.\n");
  307. case PSD_GRAYSCALE:
  308. if (s->channel_count == 1) {
  309. if (s->channel_depth == 8) {
  310. avctx->pix_fmt = AV_PIX_FMT_GRAY8;
  311. } else if (s->channel_depth == 16) {
  312. avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
  313. } else {
  314. avpriv_report_missing_feature(avctx, "channel depth %d for grayscale", s->channel_depth);
  315. return AVERROR_PATCHWELCOME;
  316. }
  317. } else if (s->channel_count == 2) {
  318. if (s->channel_depth == 8) {
  319. avctx->pix_fmt = AV_PIX_FMT_YA8;
  320. } else if (s->channel_depth == 16) {
  321. avctx->pix_fmt = AV_PIX_FMT_YA16BE;
  322. } else {
  323. avpriv_report_missing_feature(avctx, "channel depth %d for grayscale", s->channel_depth);
  324. return AVERROR_PATCHWELCOME;
  325. }
  326. } else {
  327. avpriv_report_missing_feature(avctx, "channel count %d for grayscale", s->channel_count);
  328. return AVERROR_PATCHWELCOME;
  329. }
  330. break;
  331. default:
  332. avpriv_report_missing_feature(avctx, "color mode %d", s->color_mode);
  333. return AVERROR_PATCHWELCOME;
  334. }
  335. if ((ret = ff_get_buffer(avctx, picture, 0)) < 0)
  336. return ret;
  337. /* decode picture if need */
  338. if (s->compression == PSD_RLE) {
  339. s->tmp = av_malloc(s->uncompressed_size);
  340. if (!s->tmp)
  341. return AVERROR(ENOMEM);
  342. ret = decode_rle(s);
  343. if (ret < 0) {
  344. av_freep(&s->tmp);
  345. return ret;
  346. }
  347. ptr_data = s->tmp;
  348. } else {
  349. if (bytestream2_get_bytes_left(&s->gb) < s->uncompressed_size) {
  350. av_log(s->avctx, AV_LOG_ERROR, "Not enough data for raw image data section.\n");
  351. return AVERROR_INVALIDDATA;
  352. }
  353. ptr_data = s->gb.buffer;
  354. }
  355. /* Store data */
  356. if ((avctx->pix_fmt == AV_PIX_FMT_YA8)||(avctx->pix_fmt == AV_PIX_FMT_YA16BE)){/* Interleaved */
  357. ptr = picture->data[0];
  358. for (c = 0; c < s->channel_count; c++) {
  359. for (y = 0; y < s->height; y++) {
  360. for (x = 0; x < s->width; x++) {
  361. index_out = y * picture->linesize[0] + x * s->channel_count * s->pixel_size + c * s->pixel_size;
  362. for (p = 0; p < s->pixel_size; p++) {
  363. ptr[index_out + p] = *ptr_data;
  364. ptr_data ++;
  365. }
  366. }
  367. }
  368. }
  369. } else {/* Planar */
  370. if (s->channel_count == 1)/* gray 8 or gray 16be */
  371. eq_channel[0] = 0;/* assign first channel, to first plane */
  372. for (c = 0; c < s->channel_count; c++) {
  373. plane_number = eq_channel[c];
  374. ptr = picture->data[plane_number];/* get the right plane */
  375. for (y = 0; y < s->height; y++) {
  376. memcpy(ptr, ptr_data, s->width * s->pixel_size);
  377. ptr += picture->linesize[plane_number];
  378. ptr_data += s->width * s->pixel_size;
  379. }
  380. }
  381. }
  382. if (s->color_mode == PSD_INDEXED) {
  383. picture->palette_has_changed = 1;
  384. memcpy(picture->data[1], s->palette, AVPALETTE_SIZE);
  385. }
  386. av_freep(&s->tmp);
  387. picture->pict_type = AV_PICTURE_TYPE_I;
  388. *got_frame = 1;
  389. return avpkt->size;
  390. }
  391. AVCodec ff_psd_decoder = {
  392. .name = "psd",
  393. .long_name = NULL_IF_CONFIG_SMALL("Photoshop PSD file"),
  394. .type = AVMEDIA_TYPE_VIDEO,
  395. .id = AV_CODEC_ID_PSD,
  396. .priv_data_size = sizeof(PSDContext),
  397. .decode = decode_frame,
  398. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
  399. };