2 * Photoshop (PSD) image decoder
3 * Copyright (c) 2016 Jokyo Images
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "bytestream.h"
43 typedef struct PSDContext {
46 AVCodecContext *avctx;
51 uint16_t channel_count;
52 uint16_t channel_depth;
54 uint64_t uncompressed_size;
55 unsigned int pixel_size;/* 1 for 8 bits, 2 for 16 bits */
56 uint64_t line_size;/* length of src data (even width) */
61 enum PsdCompr compression;
62 enum PsdColorMode color_mode;
64 uint8_t palette[AVPALETTE_SIZE];
67 static int decode_header(PSDContext * s)
69 int signature, version, color_mode;
73 if (bytestream2_get_bytes_left(&s->gb) < 30) {/* File header section + color map data section length */
74 av_log(s->avctx, AV_LOG_ERROR, "Header too short to parse.\n");
75 return AVERROR_INVALIDDATA;
78 signature = bytestream2_get_le32(&s->gb);
79 if (signature != MKTAG('8','B','P','S')) {
80 av_log(s->avctx, AV_LOG_ERROR, "Wrong signature %d.\n", signature);
81 return AVERROR_INVALIDDATA;
84 version = bytestream2_get_be16(&s->gb);
86 av_log(s->avctx, AV_LOG_ERROR, "Wrong version %d.\n", version);
87 return AVERROR_INVALIDDATA;
90 bytestream2_skip(&s->gb, 6);/* reserved */
92 s->channel_count = bytestream2_get_be16(&s->gb);
93 if ((s->channel_count < 1) || (s->channel_count > 56)) {
94 av_log(s->avctx, AV_LOG_ERROR, "Invalid channel count %d.\n", s->channel_count);
95 return AVERROR_INVALIDDATA;
98 s->height = bytestream2_get_be32(&s->gb);
100 if ((s->height > 30000) && (s->avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL)) {
101 av_log(s->avctx, AV_LOG_ERROR,
102 "Height > 30000 is experimental, add "
103 "'-strict %d' if you want to try to decode the picture.\n",
104 FF_COMPLIANCE_EXPERIMENTAL);
105 return AVERROR_EXPERIMENTAL;
108 s->width = bytestream2_get_be32(&s->gb);
109 if ((s->width > 30000) && (s->avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL)) {
110 av_log(s->avctx, AV_LOG_ERROR,
111 "Width > 30000 is experimental, add "
112 "'-strict %d' if you want to try to decode the picture.\n",
113 FF_COMPLIANCE_EXPERIMENTAL);
114 return AVERROR_EXPERIMENTAL;
117 if ((ret = ff_set_dimensions(s->avctx, s->width, s->height)) < 0)
120 s->channel_depth = bytestream2_get_be16(&s->gb);
122 color_mode = bytestream2_get_be16(&s->gb);
123 switch (color_mode) {
125 s->color_mode = PSD_BITMAP;
128 s->color_mode = PSD_GRAYSCALE;
131 s->color_mode = PSD_INDEXED;
134 s->color_mode = PSD_RGB;
137 s->color_mode = PSD_CMYK;
140 s->color_mode = PSD_MULTICHANNEL;
143 s->color_mode = PSD_DUOTONE;
146 s->color_mode = PSD_LAB;
149 av_log(s->avctx, AV_LOG_ERROR, "Unknown color mode %d.\n", color_mode);
150 return AVERROR_INVALIDDATA;
154 len_section = bytestream2_get_be32(&s->gb);
155 if (len_section < 0) {
156 av_log(s->avctx, AV_LOG_ERROR, "Negative size for color map data section.\n");
157 return AVERROR_INVALIDDATA;
160 if (bytestream2_get_bytes_left(&s->gb) < (len_section + 4)) { /* section and len next section */
161 av_log(s->avctx, AV_LOG_ERROR, "Incomplete file.\n");
162 return AVERROR_INVALIDDATA;
166 memset(s->palette, 0xff, AVPALETTE_SIZE);
167 for (j = HAVE_BIGENDIAN; j < 3 + HAVE_BIGENDIAN; j++)
168 for (i = 0; i < FFMIN(256, len_section / 3); i++)
169 s->palette[i * 4 + (HAVE_BIGENDIAN ? j : 2 - j)] = bytestream2_get_byteu(&s->gb);
170 len_section -= i * 3;
172 bytestream2_skip(&s->gb, len_section);
174 /* image ressources */
175 len_section = bytestream2_get_be32(&s->gb);
176 if (len_section < 0) {
177 av_log(s->avctx, AV_LOG_ERROR, "Negative size for image ressources section.\n");
178 return AVERROR_INVALIDDATA;
181 if (bytestream2_get_bytes_left(&s->gb) < (len_section + 4)) { /* section and len next section */
182 av_log(s->avctx, AV_LOG_ERROR, "Incomplete file.\n");
183 return AVERROR_INVALIDDATA;
185 bytestream2_skip(&s->gb, len_section);
187 /* layers and masks */
188 len_section = bytestream2_get_be32(&s->gb);
189 if (len_section < 0) {
190 av_log(s->avctx, AV_LOG_ERROR, "Negative size for layers and masks data section.\n");
191 return AVERROR_INVALIDDATA;
194 if (bytestream2_get_bytes_left(&s->gb) < len_section) {
195 av_log(s->avctx, AV_LOG_ERROR, "Incomplete file.\n");
196 return AVERROR_INVALIDDATA;
198 bytestream2_skip(&s->gb, len_section);
201 if (bytestream2_get_bytes_left(&s->gb) < 2) {
202 av_log(s->avctx, AV_LOG_ERROR, "File without image data section.\n");
203 return AVERROR_INVALIDDATA;
206 s->compression = bytestream2_get_be16(&s->gb);
207 switch (s->compression) {
212 avpriv_request_sample(s->avctx, "ZIP without predictor compression");
213 return AVERROR_PATCHWELCOME;
215 avpriv_request_sample(s->avctx, "ZIP with predictor compression");
216 return AVERROR_PATCHWELCOME;
218 av_log(s->avctx, AV_LOG_ERROR, "Unknown compression %d.\n", s->compression);
219 return AVERROR_INVALIDDATA;
225 static int decode_rle(PSDContext * s){
226 unsigned int scanline_count;
227 unsigned int sl, count;
228 unsigned long target_index = 0;
231 unsigned int repeat_count;
234 scanline_count = s->height * s->channel_count;
237 if (bytestream2_get_bytes_left(&s->gb) < scanline_count * 2) {
238 av_log(s->avctx, AV_LOG_ERROR, "Not enough data for rle scanline table.\n");
239 return AVERROR_INVALIDDATA;
241 bytestream2_skip(&s->gb, scanline_count * 2);/* size of each scanline */
243 /* decode rle data scanline by scanline */
244 for (sl = 0; sl < scanline_count; sl++) {
247 while (count < s->line_size) {
248 rle_char = bytestream2_get_byte(&s->gb);
250 if (rle_char <= 0) {/* byte repeat */
251 repeat_count = rle_char * -1;
253 if (bytestream2_get_bytes_left(&s->gb) < 1) {
254 av_log(s->avctx, AV_LOG_ERROR, "Not enough data for rle scanline.\n");
255 return AVERROR_INVALIDDATA;
258 if (target_index + repeat_count >= s->uncompressed_size) {
259 av_log(s->avctx, AV_LOG_ERROR, "Invalid rle char.\n");
260 return AVERROR_INVALIDDATA;
263 v = bytestream2_get_byte(&s->gb);
264 for (p = 0; p <= repeat_count; p++) {
265 s->tmp[target_index++] = v;
267 count += repeat_count + 1;
269 if (bytestream2_get_bytes_left(&s->gb) < rle_char) {
270 av_log(s->avctx, AV_LOG_ERROR, "Not enough data for rle scanline.\n");
271 return AVERROR_INVALIDDATA;
274 if (target_index + rle_char >= s->uncompressed_size) {
275 av_log(s->avctx, AV_LOG_ERROR, "Invalid rle char.\n");
276 return AVERROR_INVALIDDATA;
279 for (p = 0; p <= rle_char; p++) {
280 v = bytestream2_get_byte(&s->gb);
281 s->tmp[target_index++] = v;
283 count += rle_char + 1;
291 static int decode_frame(AVCodecContext *avctx, void *data,
292 int *got_frame, AVPacket *avpkt)
296 const uint8_t *ptr_data;
297 int index_out, c, y, x, p;
298 uint8_t eq_channel[4] = {2,0,1,3};/* RGBA -> GBRA channel order */
299 uint8_t plane_number;
301 AVFrame *picture = data;
303 PSDContext *s = avctx->priv_data;
305 s->channel_count = 0;
306 s->channel_depth = 0;
310 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
312 if ((ret = decode_header(s)) < 0)
315 s->pixel_size = s->channel_depth >> 3;/* in byte */
316 s->line_size = s->width * s->pixel_size;
318 switch (s->color_mode) {
320 if (s->channel_depth != 1 || s->channel_count != 1) {
321 av_log(s->avctx, AV_LOG_ERROR,
322 "Invalid bitmap file (channel_depth %d, channel_count %d)\n",
323 s->channel_depth, s->channel_count);
324 return AVERROR_INVALIDDATA;
326 s->line_size = s->width + 7 >> 3;
327 avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
330 if (s->channel_depth != 8 || s->channel_count != 1) {
331 av_log(s->avctx, AV_LOG_ERROR,
332 "Invalid indexed file (channel_depth %d, channel_count %d)\n",
333 s->channel_depth, s->channel_count);
334 return AVERROR_INVALIDDATA;
336 avctx->pix_fmt = AV_PIX_FMT_PAL8;
339 if (s->channel_count == 4) {
340 if (s->channel_depth == 8) {
341 avctx->pix_fmt = AV_PIX_FMT_GBRP;
342 } else if (s->channel_depth == 16) {
343 avctx->pix_fmt = AV_PIX_FMT_GBRP16BE;
345 avpriv_report_missing_feature(avctx, "channel depth %d for cmyk", s->channel_depth);
346 return AVERROR_PATCHWELCOME;
348 } else if (s->channel_count == 5) {
349 if (s->channel_depth == 8) {
350 avctx->pix_fmt = AV_PIX_FMT_GBRAP;
351 } else if (s->channel_depth == 16) {
352 avctx->pix_fmt = AV_PIX_FMT_GBRAP16BE;
354 avpriv_report_missing_feature(avctx, "channel depth %d for cmyk", s->channel_depth);
355 return AVERROR_PATCHWELCOME;
358 avpriv_report_missing_feature(avctx, "channel count %d for cmyk", s->channel_count);
359 return AVERROR_PATCHWELCOME;
363 if (s->channel_count == 3) {
364 if (s->channel_depth == 8) {
365 avctx->pix_fmt = AV_PIX_FMT_GBRP;
366 } else if (s->channel_depth == 16) {
367 avctx->pix_fmt = AV_PIX_FMT_GBRP16BE;
369 avpriv_report_missing_feature(avctx, "channel depth %d for rgb", s->channel_depth);
370 return AVERROR_PATCHWELCOME;
372 } else if (s->channel_count == 4) {
373 if (s->channel_depth == 8) {
374 avctx->pix_fmt = AV_PIX_FMT_GBRAP;
375 } else if (s->channel_depth == 16) {
376 avctx->pix_fmt = AV_PIX_FMT_GBRAP16BE;
378 avpriv_report_missing_feature(avctx, "channel depth %d for rgb", s->channel_depth);
379 return AVERROR_PATCHWELCOME;
382 avpriv_report_missing_feature(avctx, "channel count %d for rgb", s->channel_count);
383 return AVERROR_PATCHWELCOME;
387 av_log(avctx, AV_LOG_WARNING, "ignoring unknown duotone specification.\n");
389 if (s->channel_count == 1) {
390 if (s->channel_depth == 8) {
391 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
392 } else if (s->channel_depth == 16) {
393 avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
394 } else if (s->channel_depth == 32) {
395 avctx->pix_fmt = AV_PIX_FMT_GRAYF32BE;
397 avpriv_report_missing_feature(avctx, "channel depth %d for grayscale", s->channel_depth);
398 return AVERROR_PATCHWELCOME;
400 } else if (s->channel_count == 2) {
401 if (s->channel_depth == 8) {
402 avctx->pix_fmt = AV_PIX_FMT_YA8;
403 } else if (s->channel_depth == 16) {
404 avctx->pix_fmt = AV_PIX_FMT_YA16BE;
406 avpriv_report_missing_feature(avctx, "channel depth %d for grayscale", s->channel_depth);
407 return AVERROR_PATCHWELCOME;
410 avpriv_report_missing_feature(avctx, "channel count %d for grayscale", s->channel_count);
411 return AVERROR_PATCHWELCOME;
415 avpriv_report_missing_feature(avctx, "color mode %d", s->color_mode);
416 return AVERROR_PATCHWELCOME;
419 s->uncompressed_size = s->line_size * s->height * s->channel_count;
421 if ((ret = ff_get_buffer(avctx, picture, 0)) < 0)
424 /* decode picture if need */
425 if (s->compression == PSD_RLE) {
426 s->tmp = av_malloc(s->uncompressed_size);
428 return AVERROR(ENOMEM);
439 if (bytestream2_get_bytes_left(&s->gb) < s->uncompressed_size) {
440 av_log(s->avctx, AV_LOG_ERROR, "Not enough data for raw image data section.\n");
441 return AVERROR_INVALIDDATA;
443 ptr_data = s->gb.buffer;
447 if ((avctx->pix_fmt == AV_PIX_FMT_YA8)||(avctx->pix_fmt == AV_PIX_FMT_YA16BE)){/* Interleaved */
448 ptr = picture->data[0];
449 for (c = 0; c < s->channel_count; c++) {
450 for (y = 0; y < s->height; y++) {
451 for (x = 0; x < s->width; x++) {
452 index_out = y * picture->linesize[0] + x * s->channel_count * s->pixel_size + c * s->pixel_size;
453 for (p = 0; p < s->pixel_size; p++) {
454 ptr[index_out + p] = *ptr_data;
460 } else if (s->color_mode == PSD_CMYK) {
461 uint8_t *dst[4] = { picture->data[0], picture->data[1], picture->data[2], picture->data[3] };
462 const uint8_t *src[5] = { ptr_data };
463 src[1] = src[0] + s->line_size * s->height;
464 src[2] = src[1] + s->line_size * s->height;
465 src[3] = src[2] + s->line_size * s->height;
466 src[4] = src[3] + s->line_size * s->height;
467 if (s->channel_depth == 8) {
468 for (y = 0; y < s->height; y++) {
469 for (x = 0; x < s->width; x++) {
471 int r = src[0][x] * k;
472 int g = src[1][x] * k;
473 int b = src[2][x] * k;
474 dst[0][x] = g * 257 >> 16;
475 dst[1][x] = b * 257 >> 16;
476 dst[2][x] = r * 257 >> 16;
478 dst[0] += picture->linesize[0];
479 dst[1] += picture->linesize[1];
480 dst[2] += picture->linesize[2];
481 src[0] += s->line_size;
482 src[1] += s->line_size;
483 src[2] += s->line_size;
484 src[3] += s->line_size;
486 if (avctx->pix_fmt == AV_PIX_FMT_GBRAP) {
487 for (y = 0; y < s->height; y++) {
488 memcpy(dst[3], src[4], s->line_size);
489 src[4] += s->line_size;
490 dst[3] += picture->linesize[3];
494 for (y = 0; y < s->height; y++) {
495 for (x = 0; x < s->width; x++) {
496 int64_t k = AV_RB16(&src[3][x * 2]);
497 int64_t r = AV_RB16(&src[0][x * 2]) * k;
498 int64_t g = AV_RB16(&src[1][x * 2]) * k;
499 int64_t b = AV_RB16(&src[2][x * 2]) * k;
500 AV_WB16(&dst[0][x * 2], g * 65537 >> 32);
501 AV_WB16(&dst[1][x * 2], b * 65537 >> 32);
502 AV_WB16(&dst[2][x * 2], r * 65537 >> 32);
504 dst[0] += picture->linesize[0];
505 dst[1] += picture->linesize[1];
506 dst[2] += picture->linesize[2];
507 src[0] += s->line_size;
508 src[1] += s->line_size;
509 src[2] += s->line_size;
510 src[3] += s->line_size;
512 if (avctx->pix_fmt == AV_PIX_FMT_GBRAP16BE) {
513 for (y = 0; y < s->height; y++) {
514 memcpy(dst[3], src[4], s->line_size);
515 src[4] += s->line_size;
516 dst[3] += picture->linesize[3];
521 if (s->channel_count == 1)/* gray 8 or gray 16be */
522 eq_channel[0] = 0;/* assign first channel, to first plane */
524 for (c = 0; c < s->channel_count; c++) {
525 plane_number = eq_channel[c];
526 ptr = picture->data[plane_number];/* get the right plane */
527 for (y = 0; y < s->height; y++) {
528 memcpy(ptr, ptr_data, s->line_size);
529 ptr += picture->linesize[plane_number];
530 ptr_data += s->line_size;
535 if (s->color_mode == PSD_INDEXED) {
536 picture->palette_has_changed = 1;
537 memcpy(picture->data[1], s->palette, AVPALETTE_SIZE);
542 picture->pict_type = AV_PICTURE_TYPE_I;
548 AVCodec ff_psd_decoder = {
550 .long_name = NULL_IF_CONFIG_SMALL("Photoshop PSD file"),
551 .type = AVMEDIA_TYPE_VIDEO,
552 .id = AV_CODEC_ID_PSD,
553 .priv_data_size = sizeof(PSDContext),
554 .decode = decode_frame,
555 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,