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;
65 static int decode_header(PSDContext * s)
67 int signature, version, color_mode, compression;
71 if (bytestream2_get_bytes_left(&s->gb) < 30) {/* File header section + color map data section length */
72 av_log(s->avctx, AV_LOG_ERROR, "Header too short to parse.\n");
73 return AVERROR_INVALIDDATA;
76 signature = bytestream2_get_le32(&s->gb);
77 if (signature != MKTAG('8','B','P','S')) {
78 av_log(s->avctx, AV_LOG_ERROR, "Wrong signature %d.\n", signature);
79 return AVERROR_INVALIDDATA;
82 version = bytestream2_get_be16(&s->gb);
84 av_log(s->avctx, AV_LOG_ERROR, "Wrong version %d.\n", version);
85 return AVERROR_INVALIDDATA;
88 bytestream2_skip(&s->gb, 6);/* reserved */
90 s->channel_count = bytestream2_get_be16(&s->gb);
91 if ((s->channel_count < 1) || (s->channel_count > 56)) {
92 av_log(s->avctx, AV_LOG_ERROR, "Invalid channel count %d.\n", s->channel_count);
93 return AVERROR_INVALIDDATA;
96 s->height = bytestream2_get_be32(&s->gb);
98 if ((s->height > 30000) && (s->avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL)) {
99 av_log(s->avctx, AV_LOG_ERROR,
100 "Height > 30000 is experimental, add "
101 "'-strict %d' if you want to try to decode the picture.\n",
102 FF_COMPLIANCE_EXPERIMENTAL);
103 return AVERROR_EXPERIMENTAL;
106 s->width = bytestream2_get_be32(&s->gb);
107 if ((s->width > 30000) && (s->avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL)) {
108 av_log(s->avctx, AV_LOG_ERROR,
109 "Width > 30000 is experimental, add "
110 "'-strict %d' if you want to try to decode the picture.\n",
111 FF_COMPLIANCE_EXPERIMENTAL);
112 return AVERROR_EXPERIMENTAL;
115 if ((ret = ff_set_dimensions(s->avctx, s->width, s->height)) < 0)
118 s->channel_depth = bytestream2_get_be16(&s->gb);
120 color_mode = bytestream2_get_be16(&s->gb);
121 switch (color_mode) {
123 s->color_mode = PSD_BITMAP;
126 s->color_mode = PSD_GRAYSCALE;
129 s->color_mode = PSD_INDEXED;
132 s->color_mode = PSD_RGB;
135 s->color_mode = PSD_CMYK;
138 s->color_mode = PSD_MULTICHANNEL;
141 s->color_mode = PSD_DUOTONE;
144 s->color_mode = PSD_LAB;
147 av_log(s->avctx, AV_LOG_ERROR, "Unknown color mode %d.\n", color_mode);
148 return AVERROR_INVALIDDATA;
152 len_section = bytestream2_get_be32(&s->gb);
153 if (len_section < 0) {
154 av_log(s->avctx, AV_LOG_ERROR, "Negative size for color map data section.\n");
155 return AVERROR_INVALIDDATA;
158 if (bytestream2_get_bytes_left(&s->gb) < (len_section + 4)) { /* section and len next section */
159 av_log(s->avctx, AV_LOG_ERROR, "Incomplete file.\n");
160 return AVERROR_INVALIDDATA;
162 bytestream2_skip(&s->gb, len_section);
164 /* image ressources */
165 len_section = bytestream2_get_be32(&s->gb);
166 if (len_section < 0) {
167 av_log(s->avctx, AV_LOG_ERROR, "Negative size for image ressources section.\n");
168 return AVERROR_INVALIDDATA;
171 if (bytestream2_get_bytes_left(&s->gb) < (len_section + 4)) { /* section and len next section */
172 av_log(s->avctx, AV_LOG_ERROR, "Incomplete file.\n");
173 return AVERROR_INVALIDDATA;
175 bytestream2_skip(&s->gb, len_section);
177 /* layers and masks */
178 len_section = bytestream2_get_be32(&s->gb);
179 if (len_section < 0) {
180 av_log(s->avctx, AV_LOG_ERROR, "Negative size for layers and masks data section.\n");
181 return AVERROR_INVALIDDATA;
184 if (bytestream2_get_bytes_left(&s->gb) < len_section) {
185 av_log(s->avctx, AV_LOG_ERROR, "Incomplete file.\n");
186 return AVERROR_INVALIDDATA;
188 bytestream2_skip(&s->gb, len_section);
191 if (bytestream2_get_bytes_left(&s->gb) < 2) {
192 av_log(s->avctx, AV_LOG_ERROR, "File without image data section.\n");
193 return AVERROR_INVALIDDATA;
196 s->compression = bytestream2_get_be16(&s->gb);
197 switch (s->compression) {
202 avpriv_request_sample(s->avctx, "ZIP without predictor compression");
203 return AVERROR_PATCHWELCOME;
206 avpriv_request_sample(s->avctx, "ZIP with predictor compression");
207 return AVERROR_PATCHWELCOME;
210 av_log(s->avctx, AV_LOG_ERROR, "Unknown compression %d.\n", compression);
211 return AVERROR_INVALIDDATA;
217 static int decode_rle(PSDContext * s){
218 unsigned int scanline_count;
219 unsigned int sl, count;
220 unsigned long target_index = 0;
223 unsigned int repeat_count;
226 scanline_count = s->height * s->channel_count;
229 if (bytestream2_get_bytes_left(&s->gb) < scanline_count * 2) {
230 av_log(s->avctx, AV_LOG_ERROR, "Not enough data for rle scanline table.\n");
231 return AVERROR_INVALIDDATA;
233 bytestream2_skip(&s->gb, scanline_count * 2);/* size of each scanline */
235 /* decode rle data scanline by scanline */
236 for (sl = 0; sl < scanline_count; sl++) {
239 while (count < s->line_size) {
240 rle_char = bytestream2_get_byte(&s->gb);
242 if (rle_char <= 0) {/* byte repeat */
243 repeat_count = rle_char * -1;
245 if (bytestream2_get_bytes_left(&s->gb) < 1) {
246 av_log(s->avctx, AV_LOG_ERROR, "Not enough data for rle scanline.\n");
247 return AVERROR_INVALIDDATA;
250 if (target_index + repeat_count >= s->uncompressed_size) {
251 av_log(s->avctx, AV_LOG_ERROR, "Invalid rle char.\n");
252 return AVERROR_INVALIDDATA;
255 v = bytestream2_get_byte(&s->gb);
256 for (p = 0; p <= repeat_count; p++) {
257 s->tmp[target_index++] = v;
259 count += repeat_count + 1;
261 if (bytestream2_get_bytes_left(&s->gb) < rle_char) {
262 av_log(s->avctx, AV_LOG_ERROR, "Not enough data for rle scanline.\n");
263 return AVERROR_INVALIDDATA;
266 if (target_index + rle_char >= s->uncompressed_size) {
267 av_log(s->avctx, AV_LOG_ERROR, "Invalid rle char.\n");
268 return AVERROR_INVALIDDATA;
271 for (p = 0; p <= rle_char; p++) {
272 v = bytestream2_get_byte(&s->gb);
273 s->tmp[target_index++] = v;
275 count += rle_char + 1;
283 static int decode_frame(AVCodecContext *avctx, void *data,
284 int *got_frame, AVPacket *avpkt)
288 const uint8_t *ptr_data;
289 int index_out, c, y, x, p;
290 uint8_t eq_channel[4] = {2,0,1,3};/* RGBA -> GBRA channel order */
291 uint8_t plane_number;
293 AVFrame *picture = data;
295 PSDContext *s = avctx->priv_data;
297 s->channel_count = 0;
298 s->channel_depth = 0;
302 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
304 if ((ret = decode_header(s)) < 0)
307 s->pixel_size = s->channel_depth >> 3;/* in byte */
308 s->line_size = s->width * s->pixel_size;
309 s->uncompressed_size = s->line_size * s->height * s->channel_count;
311 switch (s->color_mode) {
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;
319 avpriv_report_missing_feature(avctx, "channel depth %d for rgb", s->channel_depth);
320 return AVERROR_PATCHWELCOME;
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;
328 avpriv_report_missing_feature(avctx, "channel depth %d for rgb", s->channel_depth);
329 return AVERROR_PATCHWELCOME;
332 avpriv_report_missing_feature(avctx, "channel count %d for rgb", s->channel_count);
333 return AVERROR_PATCHWELCOME;
337 if (s->channel_count == 1) {
338 if (s->channel_depth == 8) {
339 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
340 } else if (s->channel_depth == 16) {
341 avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
343 avpriv_report_missing_feature(avctx, "channel depth %d for grayscale", s->channel_depth);
344 return AVERROR_PATCHWELCOME;
346 } else if (s->channel_count == 2) {
347 if (s->channel_depth == 8) {
348 avctx->pix_fmt = AV_PIX_FMT_YA8;
349 } else if (s->channel_depth == 16) {
350 avctx->pix_fmt = AV_PIX_FMT_YA16BE;
352 avpriv_report_missing_feature(avctx, "channel depth %d for grayscale", s->channel_depth);
353 return AVERROR_PATCHWELCOME;
356 avpriv_report_missing_feature(avctx, "channel count %d for grayscale", s->channel_count);
357 return AVERROR_PATCHWELCOME;
361 avpriv_report_missing_feature(avctx, "color mode %d", s->color_mode);
362 return AVERROR_PATCHWELCOME;
365 if ((ret = ff_get_buffer(avctx, picture, 0)) < 0)
368 /* decode picture if need */
369 if (s->compression == PSD_RLE) {
370 s->tmp = av_malloc(s->uncompressed_size);
372 return AVERROR(ENOMEM);
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;
387 ptr_data = s->gb.buffer;
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;
405 if (s->channel_count == 1)/* gray 8 or gray 16be */
406 eq_channel[0] = 0;/* assign first channel, to first plane */
408 for (c = 0; c < s->channel_count; c++) {
409 plane_number = eq_channel[c];
410 ptr = picture->data[plane_number];/* get the right plane */
411 for (y = 0; y < s->height; y++) {
412 memcpy(ptr, ptr_data, s->width * s->pixel_size);
413 ptr += picture->linesize[plane_number];
414 ptr_data += s->width * s->pixel_size;
421 picture->pict_type = AV_PICTURE_TYPE_I;
427 AVCodec ff_psd_decoder = {
429 .long_name = NULL_IF_CONFIG_SMALL("Photoshop PSD file"),
430 .type = AVMEDIA_TYPE_VIDEO,
431 .id = AV_CODEC_ID_PSD,
432 .priv_data_size = sizeof(PSDContext),
433 .decode = decode_frame,
434 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,