]> git.sesse.net Git - ffmpeg/blob - libavcodec/psd.c
wmavoice: move overflow handling to common code.
[ffmpeg] / libavcodec / psd.c
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
22 #include "bytestream.h"
23 #include "internal.h"
24
25 enum PsdCompr {
26     PSD_RAW,
27     PSD_RLE,
28     PSD_ZIP_WITHOUT_P,
29     PSD_ZIP_WITH_P,
30 };
31
32 enum PsdColorMode {
33     PSD_BITMAP,
34     PSD_GRAYSCALE,
35     PSD_INDEXED,
36     PSD_RGB,
37     PSD_CMYK,
38     PSD_MULTICHANNEL,
39     PSD_DUOTONE,
40     PSD_LAB,
41 };
42
43 typedef struct PSDContext {
44     AVClass *class;
45     AVFrame *picture;
46     AVCodecContext *avctx;
47     GetByteContext gb;
48
49     uint8_t * tmp;
50
51     uint16_t channel_count;
52     uint16_t channel_depth;
53
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) */
57
58     int width;
59     int height;
60
61     enum PsdCompr compression;
62     enum PsdColorMode color_mode;
63 } PSDContext;
64
65 static int decode_header(PSDContext * s)
66 {
67     int signature, version, color_mode, compression;
68     int64_t len_section;
69     int ret = 0;
70
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;
74     }
75
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;
80     }
81
82     version = bytestream2_get_be16(&s->gb);
83     if (version != 1) {
84         av_log(s->avctx, AV_LOG_ERROR, "Wrong version %d.\n", version);
85         return AVERROR_INVALIDDATA;
86     }
87
88     bytestream2_skip(&s->gb, 6);/* reserved */
89
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;
94     }
95
96     s->height = bytestream2_get_be32(&s->gb);
97
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;
104     }
105
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;
113     }
114
115     if ((ret = ff_set_dimensions(s->avctx, s->width, s->height)) < 0)
116         return ret;
117
118     s->channel_depth = bytestream2_get_be16(&s->gb);
119
120     color_mode = bytestream2_get_be16(&s->gb);
121     switch (color_mode) {
122     case 0:
123         s->color_mode = PSD_BITMAP;
124         break;
125     case 1:
126         s->color_mode = PSD_GRAYSCALE;
127         break;
128     case 2:
129         s->color_mode = PSD_INDEXED;
130         break;
131     case 3:
132         s->color_mode = PSD_RGB;
133         break;
134     case 4:
135         s->color_mode = PSD_CMYK;
136         break;
137     case 7:
138         s->color_mode = PSD_MULTICHANNEL;
139         break;
140     case 8:
141         s->color_mode = PSD_DUOTONE;
142         break;
143     case 9:
144         s->color_mode = PSD_LAB;
145         break;
146     default:
147         av_log(s->avctx, AV_LOG_ERROR, "Unknown color mode %d.\n", color_mode);
148         return AVERROR_INVALIDDATA;
149     }
150
151     /* color map data */
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;
156     }
157
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;
161     }
162     bytestream2_skip(&s->gb, len_section);
163
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;
169     }
170
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;
174     }
175     bytestream2_skip(&s->gb, len_section);
176
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;
182     }
183
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;
187     }
188     bytestream2_skip(&s->gb, len_section);
189
190     /* image 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;
194     }
195
196     s->compression = bytestream2_get_be16(&s->gb);
197     switch (s->compression) {
198     case 0:
199     case 1:
200         break;
201     case 2:
202         avpriv_request_sample(s->avctx, "ZIP without predictor compression");
203         return AVERROR_PATCHWELCOME;
204         break;
205     case 3:
206         avpriv_request_sample(s->avctx, "ZIP with predictor compression");
207         return AVERROR_PATCHWELCOME;
208         break;
209     default:
210         av_log(s->avctx, AV_LOG_ERROR, "Unknown compression %d.\n", compression);
211         return AVERROR_INVALIDDATA;
212     }
213
214     return ret;
215 }
216
217 static int decode_rle(PSDContext * s){
218     unsigned int scanline_count;
219     unsigned int sl, count;
220     unsigned long target_index = 0;
221     unsigned int p;
222     int8_t rle_char;
223     unsigned int repeat_count;
224     uint8_t v;
225
226     scanline_count = s->height * s->channel_count;
227
228     /* scanline table */
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;
232     }
233     bytestream2_skip(&s->gb, scanline_count * 2);/* size of each scanline */
234
235     /* decode rle data scanline by scanline */
236     for (sl = 0; sl < scanline_count; sl++) {
237         count = 0;
238
239         while (count < s->line_size) {
240             rle_char = bytestream2_get_byte(&s->gb);
241
242             if (rle_char <= 0) {/* byte repeat */
243                 repeat_count = rle_char * -1;
244
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;
248                 }
249
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;
253                 }
254
255                 v = bytestream2_get_byte(&s->gb);
256                 for (p = 0; p <= repeat_count; p++) {
257                     s->tmp[target_index++] = v;
258                 }
259                 count += repeat_count + 1;
260             } else {
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;
264                 }
265
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;
269                 }
270
271                 for (p = 0; p <= rle_char; p++) {
272                     v = bytestream2_get_byte(&s->gb);
273                     s->tmp[target_index++] = v;
274                 }
275                 count += rle_char + 1;
276             }
277         }
278     }
279
280     return 0;
281 }
282
283 static int decode_frame(AVCodecContext *avctx, void *data,
284                         int *got_frame, AVPacket *avpkt)
285 {
286     int ret;
287     uint8_t *ptr;
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;
292
293     AVFrame *picture = data;
294
295     PSDContext *s = avctx->priv_data;
296     s->avctx     = avctx;
297     s->channel_count = 0;
298     s->channel_depth = 0;
299     s->tmp           = NULL;
300     s->line_size     = 0;
301
302     bytestream2_init(&s->gb, avpkt->data, avpkt->size);
303
304     if ((ret = decode_header(s)) < 0)
305         return ret;
306
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;
310
311     switch (s->color_mode) {
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_GRAYSCALE:
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;
342             } else {
343                 avpriv_report_missing_feature(avctx, "channel depth %d for grayscale", s->channel_depth);
344                 return AVERROR_PATCHWELCOME;
345             }
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;
351             } else {
352                 avpriv_report_missing_feature(avctx, "channel depth %d for grayscale", s->channel_depth);
353                 return AVERROR_PATCHWELCOME;
354             }
355         } else {
356             avpriv_report_missing_feature(avctx, "channel count %d for grayscale", s->channel_count);
357             return AVERROR_PATCHWELCOME;
358         }
359         break;
360     default:
361         avpriv_report_missing_feature(avctx, "color mode %d", s->color_mode);
362         return AVERROR_PATCHWELCOME;
363     }
364
365     if ((ret = ff_get_buffer(avctx, picture, 0)) < 0)
366         return ret;
367
368     /* decode picture if need */
369     if (s->compression == PSD_RLE) {
370         s->tmp = av_malloc(s->uncompressed_size);
371         if (!s->tmp)
372             return AVERROR(ENOMEM);
373
374         ret = decode_rle(s);
375
376         if (ret < 0) {
377             av_freep(&s->tmp);
378             return ret;
379         }
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
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 {/* Planar */
405         if (s->channel_count == 1)/* gray 8 or gray 16be */
406             eq_channel[0] = 0;/* assign first channel, to first plane */
407
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;
415             }
416         }
417     }
418
419     av_freep(&s->tmp);
420
421     picture->pict_type = AV_PICTURE_TYPE_I;
422     *got_frame = 1;
423
424     return avpkt->size;
425 }
426
427 AVCodec ff_psd_decoder = {
428     .name             = "psd",
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,
435 };