../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
173 / 339
51.0%
Branches:
98 / 190
51.6%
Functions:
3 / 3
100.0%

libavcodec/psd.c
Line Branch Exec Source
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 "libavutil/mem.h"
23 #include "bytestream.h"
24 #include "codec_internal.h"
25 #include "decode.h"
26
27 enum PsdCompr {
28 PSD_RAW,
29 PSD_RLE,
30 PSD_ZIP_WITHOUT_P,
31 PSD_ZIP_WITH_P,
32 };
33
34 enum PsdColorMode {
35 PSD_BITMAP,
36 PSD_GRAYSCALE,
37 PSD_INDEXED,
38 PSD_RGB,
39 PSD_CMYK,
40 PSD_MULTICHANNEL,
41 PSD_DUOTONE,
42 PSD_LAB,
43 };
44
45 typedef struct PSDContext {
46 AVClass *class;
47 AVFrame *picture;
48 AVCodecContext *avctx;
49 GetByteContext gb;
50
51 uint8_t * tmp;
52
53 uint16_t channel_count;
54 uint16_t channel_depth;
55 uint16_t primary_channels;
56
57 uint64_t uncompressed_size;
58 unsigned int pixel_size;/* 1 for 8 bits, 2 for 16 bits */
59 uint64_t line_size;/* length of src data (even width) */
60
61 int width;
62 int height;
63
64 int16_t layer_count;
65
66 enum PsdCompr compression;
67 enum PsdColorMode color_mode;
68
69 uint8_t palette[AVPALETTE_SIZE];
70 } PSDContext;
71
72 32 static int decode_header(PSDContext * s)
73 {
74 int signature, version, color_mode;
75 int64_t len_section;
76 32 int ret = 0;
77
78
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 6 taken 32 times.
32 if (bytestream2_get_bytes_left(&s->gb) < 30) {/* File header section + color map data section length */
79 ✗ av_log(s->avctx, AV_LOG_ERROR, "Header too short to parse.\n");
80 ✗ return AVERROR_INVALIDDATA;
81 }
82
83 32 signature = bytestream2_get_le32(&s->gb);
84
1/2
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 10 taken 32 times.
32 if (signature != MKTAG('8','B','P','S')) {
85 ✗ av_log(s->avctx, AV_LOG_ERROR, "Wrong signature %d.\n", signature);
86 ✗ return AVERROR_INVALIDDATA;
87 }
88
89 32 version = bytestream2_get_be16(&s->gb);
90
1/2
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 14 taken 32 times.
32 if (version != 1) {
91 ✗ av_log(s->avctx, AV_LOG_ERROR, "Wrong version %d.\n", version);
92 ✗ return AVERROR_INVALIDDATA;
93 }
94
95 32 bytestream2_skip(&s->gb, 6);/* reserved */
96
97 32 s->channel_count = bytestream2_get_be16(&s->gb);
98
2/4
✓ Branch 16 → 17 taken 32 times.
✗ Branch 16 → 18 not taken.
✗ Branch 17 → 18 not taken.
✓ Branch 17 → 20 taken 32 times.
32 if ((s->channel_count < 1) || (s->channel_count > 56)) {
99 ✗ av_log(s->avctx, AV_LOG_ERROR, "Invalid channel count %d.\n", s->channel_count);
100 ✗ return AVERROR_INVALIDDATA;
101 }
102
103 32 s->height = bytestream2_get_be32(&s->gb);
104
105
1/4
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 25 taken 32 times.
✗ Branch 22 → 23 not taken.
✗ Branch 22 → 25 not taken.
32 if ((s->height > 30000) && (s->avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL)) {
106 ✗ av_log(s->avctx, AV_LOG_ERROR,
107 "Height > 30000 is experimental, add "
108 "'-strict %d' if you want to try to decode the picture.\n",
109 FF_COMPLIANCE_EXPERIMENTAL);
110 ✗ return AVERROR_EXPERIMENTAL;
111 }
112
113 32 s->width = bytestream2_get_be32(&s->gb);
114
1/4
✗ Branch 26 → 27 not taken.
✓ Branch 26 → 30 taken 32 times.
✗ Branch 27 → 28 not taken.
✗ Branch 27 → 30 not taken.
32 if ((s->width > 30000) && (s->avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL)) {
115 ✗ av_log(s->avctx, AV_LOG_ERROR,
116 "Width > 30000 is experimental, add "
117 "'-strict %d' if you want to try to decode the picture.\n",
118 FF_COMPLIANCE_EXPERIMENTAL);
119 ✗ return AVERROR_EXPERIMENTAL;
120 }
121
122
1/2
✗ Branch 31 → 32 not taken.
✓ Branch 31 → 33 taken 32 times.
32 if ((ret = ff_set_dimensions(s->avctx, s->width, s->height)) < 0)
123 ✗ return ret;
124
125 32 s->channel_depth = bytestream2_get_be16(&s->gb);
126
127 32 color_mode = bytestream2_get_be16(&s->gb);
128
5/9
✓ Branch 35 → 36 taken 2 times.
✓ Branch 35 → 37 taken 8 times.
✓ Branch 35 → 38 taken 2 times.
✓ Branch 35 → 39 taken 18 times.
✗ Branch 35 → 40 not taken.
✗ Branch 35 → 41 not taken.
✓ Branch 35 → 42 taken 2 times.
✗ Branch 35 → 43 not taken.
✗ Branch 35 → 44 not taken.
32 switch (color_mode) {
129 2 case 0:
130 2 s->color_mode = PSD_BITMAP;
131 2 break;
132 8 case 1:
133 8 s->color_mode = PSD_GRAYSCALE;
134 8 break;
135 2 case 2:
136 2 s->color_mode = PSD_INDEXED;
137 2 break;
138 18 case 3:
139 18 s->color_mode = PSD_RGB;
140 18 break;
141 ✗ case 4:
142 ✗ s->color_mode = PSD_CMYK;
143 ✗ break;
144 ✗ case 7:
145 ✗ s->color_mode = PSD_MULTICHANNEL;
146 ✗ break;
147 2 case 8:
148 2 s->color_mode = PSD_DUOTONE;
149 2 break;
150 ✗ case 9:
151 ✗ s->color_mode = PSD_LAB;
152 ✗ break;
153 ✗ default:
154 ✗ av_log(s->avctx, AV_LOG_ERROR, "Unknown color mode %d.\n", color_mode);
155 ✗ return AVERROR_INVALIDDATA;
156 }
157
158 /* color map data */
159 32 len_section = bytestream2_get_be32(&s->gb);
160
1/2
✗ Branch 47 → 48 not taken.
✓ Branch 47 → 50 taken 32 times.
32 if (len_section < 0) {
161 ✗ av_log(s->avctx, AV_LOG_ERROR, "Negative size for color map data section.\n");
162 ✗ return AVERROR_INVALIDDATA;
163 }
164
165
1/2
✗ Branch 51 → 52 not taken.
✓ Branch 51 → 54 taken 32 times.
32 if (bytestream2_get_bytes_left(&s->gb) < (len_section + 4)) { /* section and len next section */
166 ✗ av_log(s->avctx, AV_LOG_ERROR, "Incomplete file.\n");
167 ✗ return AVERROR_INVALIDDATA;
168 }
169
2/2
✓ Branch 54 → 55 taken 4 times.
✓ Branch 54 → 66 taken 28 times.
32 if (len_section) {
170 int i,j;
171 4 memset(s->palette, 0xff, AVPALETTE_SIZE);
172
2/2
✓ Branch 64 → 56 taken 12 times.
✓ Branch 64 → 65 taken 4 times.
16 for (j = HAVE_BIGENDIAN; j < 3 + HAVE_BIGENDIAN; j++)
173
4/4
✓ Branch 59 → 60 taken 1050 times.
✓ Branch 59 → 61 taken 1542 times.
✓ Branch 62 → 57 taken 2580 times.
✓ Branch 62 → 63 taken 12 times.
2592 for (i = 0; i < FFMIN(256, len_section / 3); i++)
174 2580 s->palette[i * 4 + (HAVE_BIGENDIAN ? j : 2 - j)] = bytestream2_get_byteu(&s->gb);
175 4 len_section -= i * 3;
176 }
177 32 bytestream2_skip(&s->gb, len_section);
178
179 /* image resources */
180 32 len_section = bytestream2_get_be32(&s->gb);
181
1/2
✗ Branch 68 → 69 not taken.
✓ Branch 68 → 71 taken 32 times.
32 if (len_section < 0) {
182 ✗ av_log(s->avctx, AV_LOG_ERROR, "Negative size for image resources section.\n");
183 ✗ return AVERROR_INVALIDDATA;
184 }
185
186
1/2
✗ Branch 72 → 73 not taken.
✓ Branch 72 → 75 taken 32 times.
32 if (bytestream2_get_bytes_left(&s->gb) < (len_section + 4)) { /* section and len next section */
187 ✗ av_log(s->avctx, AV_LOG_ERROR, "Incomplete file.\n");
188 ✗ return AVERROR_INVALIDDATA;
189 }
190 32 bytestream2_skip(&s->gb, len_section);
191
192 /* layers and masks */
193 32 len_section = bytestream2_get_be32(&s->gb);
194
1/2
✗ Branch 77 → 78 not taken.
✓ Branch 77 → 80 taken 32 times.
32 if (len_section < 0) {
195 ✗ av_log(s->avctx, AV_LOG_ERROR, "Negative size for layers and masks data section.\n");
196 ✗ return AVERROR_INVALIDDATA;
197 }
198
199
1/2
✓ Branch 80 → 81 taken 32 times.
✗ Branch 80 → 84 not taken.
32 if (len_section >= 6) {
200 /* layer count (in layers and masks section) */
201 32 bytestream2_skip(&s->gb, 4);
202 32 s->layer_count = bytestream2_get_be16(&s->gb);
203 32 len_section -= 6;
204 }
205
206
1/2
✗ Branch 85 → 86 not taken.
✓ Branch 85 → 88 taken 32 times.
32 if (bytestream2_get_bytes_left(&s->gb) < len_section) {
207 ✗ av_log(s->avctx, AV_LOG_ERROR, "Incomplete file.\n");
208 ✗ return AVERROR_INVALIDDATA;
209 }
210 32 bytestream2_skip(&s->gb, len_section);
211
212 /* image section */
213
1/2
✗ Branch 90 → 91 not taken.
✓ Branch 90 → 93 taken 32 times.
32 if (bytestream2_get_bytes_left(&s->gb) < 2) {
214 ✗ av_log(s->avctx, AV_LOG_ERROR, "File without image data section.\n");
215 ✗ return AVERROR_INVALIDDATA;
216 }
217
218 32 s->compression = bytestream2_get_be16(&s->gb);
219
1/4
✓ Branch 94 → 95 taken 32 times.
✗ Branch 94 → 96 not taken.
✗ Branch 94 → 98 not taken.
✗ Branch 94 → 100 not taken.
32 switch (s->compression) {
220 32 case 0:
221 case 1:
222 32 break;
223 ✗ case 2:
224 ✗ avpriv_request_sample(s->avctx, "ZIP without predictor compression");
225 ✗ return AVERROR_PATCHWELCOME;
226 ✗ case 3:
227 ✗ avpriv_request_sample(s->avctx, "ZIP with predictor compression");
228 ✗ return AVERROR_PATCHWELCOME;
229 ✗ default:
230 ✗ av_log(s->avctx, AV_LOG_ERROR, "Unknown compression %d.\n", s->compression);
231 ✗ return AVERROR_INVALIDDATA;
232 }
233
234 32 return ret;
235 }
236
237 8 static int decode_rle(PSDContext * s){
238 unsigned int scanline_count;
239 unsigned int sl, count;
240 8 unsigned long target_index = 0;
241 unsigned int p;
242 int8_t rle_char;
243 unsigned int repeat_count;
244 uint8_t v;
245
246 8 scanline_count = s->height * s->channel_count;
247
248 /* scanline table */
249
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 6 taken 8 times.
8 if (bytestream2_get_bytes_left(&s->gb) < scanline_count * 2) {
250 ✗ av_log(s->avctx, AV_LOG_ERROR, "Not enough data for rle scanline table.\n");
251 ✗ return AVERROR_INVALIDDATA;
252 }
253 8 bytestream2_skip(&s->gb, scanline_count * 2);/* size of each scanline */
254
255 /* decode rle data scanline by scanline */
256
2/2
✓ Branch 38 → 8 taken 2804 times.
✓ Branch 38 → 39 taken 8 times.
2812 for (sl = 0; sl < scanline_count; sl++) {
257 2804 count = 0;
258
259
2/2
✓ Branch 36 → 9 taken 18540 times.
✓ Branch 36 → 37 taken 2804 times.
21344 while (count < s->line_size) {
260 18540 rle_char = bytestream2_get_byte(&s->gb);
261
262
2/2
✓ Branch 10 → 11 taken 12340 times.
✓ Branch 10 → 23 taken 6200 times.
18540 if (rle_char <= 0) {/* byte repeat */
263 12340 repeat_count = rle_char * -1;
264
265
1/2
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 15 taken 12340 times.
12340 if (bytestream2_get_bytes_left(&s->gb) < 1) {
266 ✗ av_log(s->avctx, AV_LOG_ERROR, "Not enough data for rle scanline.\n");
267 ✗ return AVERROR_INVALIDDATA;
268 }
269
270
1/2
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 18 taken 12340 times.
12340 if (target_index + repeat_count >= s->uncompressed_size) {
271 ✗ av_log(s->avctx, AV_LOG_ERROR, "Invalid rle char.\n");
272 ✗ return AVERROR_INVALIDDATA;
273 }
274
275 12340 v = bytestream2_get_byte(&s->gb);
276
2/2
✓ Branch 21 → 20 taken 210352 times.
✓ Branch 21 → 22 taken 12340 times.
222692 for (p = 0; p <= repeat_count; p++) {
277 210352 s->tmp[target_index++] = v;
278 }
279 12340 count += repeat_count + 1;
280 } else {
281
1/2
✗ Branch 24 → 25 not taken.
✓ Branch 24 → 27 taken 6200 times.
6200 if (bytestream2_get_bytes_left(&s->gb) < rle_char) {
282 ✗ av_log(s->avctx, AV_LOG_ERROR, "Not enough data for rle scanline.\n");
283 ✗ return AVERROR_INVALIDDATA;
284 }
285
286
1/2
✗ Branch 27 → 28 not taken.
✓ Branch 27 → 30 taken 6200 times.
6200 if (target_index + rle_char >= s->uncompressed_size) {
287 ✗ av_log(s->avctx, AV_LOG_ERROR, "Invalid rle char.\n");
288 ✗ return AVERROR_INVALIDDATA;
289 }
290
291
2/2
✓ Branch 33 → 31 taken 243810 times.
✓ Branch 33 → 34 taken 6200 times.
250010 for (p = 0; p <= rle_char; p++) {
292 243810 v = bytestream2_get_byte(&s->gb);
293 243810 s->tmp[target_index++] = v;
294 }
295 6200 count += rle_char + 1;
296 }
297 }
298 }
299
300 8 return 0;
301 }
302
303 32 static int decode_frame(AVCodecContext *avctx, AVFrame *picture,
304 int *got_frame, AVPacket *avpkt)
305 {
306 int ret;
307 uint8_t *ptr;
308 const uint8_t *ptr_data;
309 int index_out, c, y, x, p;
310 32 uint8_t eq_channel[4] = {2,0,1,3};/* RGBA -> GBRA channel order */
311 uint8_t plane_number;
312
313 32 PSDContext *s = avctx->priv_data;
314 32 s->avctx = avctx;
315 32 s->channel_count = 0;
316 32 s->channel_depth = 0;
317 32 s->primary_channels = 0;
318 32 s->tmp = NULL;
319 32 s->line_size = 0;
320 32 s->layer_count = 0;
321
322 32 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
323
324
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 32 times.
32 if ((ret = decode_header(s)) < 0)
325 ✗ return ret;
326
327 32 s->pixel_size = s->channel_depth >> 3;/* in byte */
328 32 s->line_size = s->width * s->pixel_size;
329
330
5/7
✓ Branch 6 → 7 taken 2 times.
✓ Branch 6 → 12 taken 2 times.
✗ Branch 6 → 17 not taken.
✓ Branch 6 → 37 taken 18 times.
✓ Branch 6 → 57 taken 2 times.
✓ Branch 6 → 58 taken 8 times.
✗ Branch 6 → 80 not taken.
32 switch (s->color_mode) {
331 2 case PSD_BITMAP:
332
2/4
✓ Branch 7 → 8 taken 2 times.
✗ Branch 7 → 9 not taken.
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 11 taken 2 times.
2 if (s->channel_depth != 1 || s->channel_count < 1) {
333 ✗ av_log(s->avctx, AV_LOG_ERROR,
334 "Invalid bitmap file (channel_depth %d, channel_count %d)\n",
335 ✗ s->channel_depth, s->channel_count);
336 ✗ return AVERROR_INVALIDDATA;
337 }
338 2 s->line_size = s->width + 7 >> 3;
339 2 s->primary_channels = 1;
340 2 avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
341 2 break;
342 2 case PSD_INDEXED:
343
2/4
✓ Branch 12 → 13 taken 2 times.
✗ Branch 12 → 14 not taken.
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 16 taken 2 times.
2 if (s->channel_depth != 8 || s->channel_count < 1) {
344 ✗ av_log(s->avctx, AV_LOG_ERROR,
345 "Invalid indexed file (channel_depth %d, channel_count %d)\n",
346 ✗ s->channel_depth, s->channel_count);
347 ✗ return AVERROR_INVALIDDATA;
348 }
349 2 s->primary_channels = 1;
350 2 avctx->pix_fmt = AV_PIX_FMT_PAL8;
351 2 break;
352 ✗ case PSD_CMYK:
353
0/4
✗ Branch 17 → 18 not taken.
✗ Branch 17 → 26 not taken.
✗ Branch 18 → 19 not taken.
✗ Branch 18 → 26 not taken.
✗ if (s->layer_count < 0 && s->channel_count >= 5) {
354
0/2
✗ Branch 19 → 20 not taken.
✗ Branch 19 → 21 not taken.
✗ if (s->channel_depth == 8) {
355 ✗ avctx->pix_fmt = AV_PIX_FMT_GBRAP;
356
0/2
✗ Branch 21 → 22 not taken.
✗ Branch 21 → 23 not taken.
✗ } else if (s->channel_depth == 16) {
357 ✗ avctx->pix_fmt = AV_PIX_FMT_GBRAP16BE;
358 } else {
359 ✗ avpriv_report_missing_feature(avctx, "channel depth %d for cmyk", s->channel_depth);
360 ✗ return AVERROR_PATCHWELCOME;
361 }
362 ✗ s->primary_channels = 5;
363
0/2
✗ Branch 26 → 27 not taken.
✗ Branch 26 → 34 not taken.
✗ } else if (s->channel_count >= 4) {
364
0/2
✗ Branch 27 → 28 not taken.
✗ Branch 27 → 29 not taken.
✗ if (s->channel_depth == 8) {
365 ✗ avctx->pix_fmt = AV_PIX_FMT_GBRP;
366
0/2
✗ Branch 29 → 30 not taken.
✗ Branch 29 → 31 not taken.
✗ } else if (s->channel_depth == 16) {
367 ✗ avctx->pix_fmt = AV_PIX_FMT_GBRP16BE;
368 } else {
369 ✗ avpriv_report_missing_feature(avctx, "channel depth %d for cmyk", s->channel_depth);
370 ✗ return AVERROR_PATCHWELCOME;
371 }
372 ✗ s->primary_channels = 4;
373 } else {
374 ✗ av_log(s->avctx, AV_LOG_ERROR,
375 "Invalid cmyk file (channel_count %d)\n",
376 ✗ s->channel_count);
377 ✗ return AVERROR_INVALIDDATA;
378 }
379 ✗ break;
380 18 case PSD_RGB:
381
3/4
✓ Branch 37 → 38 taken 4 times.
✓ Branch 37 → 46 taken 14 times.
✓ Branch 38 → 39 taken 4 times.
✗ Branch 38 → 46 not taken.
18 if (s->layer_count < 0 && s->channel_count >= 4) {
382
1/2
✓ Branch 39 → 40 taken 4 times.
✗ Branch 39 → 41 not taken.
4 if (s->channel_depth == 8) {
383 4 avctx->pix_fmt = AV_PIX_FMT_GBRAP;
384
0/2
✗ Branch 41 → 42 not taken.
✗ Branch 41 → 43 not taken.
✗ } else if (s->channel_depth == 16) {
385 ✗ avctx->pix_fmt = AV_PIX_FMT_GBRAP16BE;
386 } else {
387 ✗ avpriv_report_missing_feature(avctx, "channel depth %d for rgb", s->channel_depth);
388 ✗ return AVERROR_PATCHWELCOME;
389 }
390 4 s->primary_channels = 4;
391
1/2
✓ Branch 46 → 47 taken 14 times.
✗ Branch 46 → 54 not taken.
14 } else if (s->channel_count >= 3) {
392
2/2
✓ Branch 47 → 48 taken 8 times.
✓ Branch 47 → 49 taken 6 times.
14 if (s->channel_depth == 8) {
393 8 avctx->pix_fmt = AV_PIX_FMT_GBRP;
394
1/2
✓ Branch 49 → 50 taken 6 times.
✗ Branch 49 → 51 not taken.
6 } else if (s->channel_depth == 16) {
395 6 avctx->pix_fmt = AV_PIX_FMT_GBRP16BE;
396 } else {
397 ✗ avpriv_report_missing_feature(avctx, "channel depth %d for rgb", s->channel_depth);
398 ✗ return AVERROR_PATCHWELCOME;
399 }
400 14 s->primary_channels = 3;
401 } else {
402 ✗ av_log(s->avctx, AV_LOG_ERROR,
403 "Invalid rgb file (channel_count %d)\n",
404 ✗ s->channel_count);
405 ✗ return AVERROR_INVALIDDATA;
406 }
407 18 break;
408 2 case PSD_DUOTONE:
409 2 av_log(avctx, AV_LOG_WARNING, "ignoring unknown duotone specification.\n");
410 av_fallthrough;
411 10 case PSD_GRAYSCALE:
412
3/4
✓ Branch 58 → 59 taken 2 times.
✓ Branch 58 → 67 taken 8 times.
✓ Branch 59 → 60 taken 2 times.
✗ Branch 59 → 67 not taken.
10 if (s->layer_count < 0 && s->channel_count >= 2) {
413
1/2
✓ Branch 60 → 61 taken 2 times.
✗ Branch 60 → 62 not taken.
2 if (s->channel_depth == 8) {
414 2 avctx->pix_fmt = AV_PIX_FMT_YA8;
415
0/2
✗ Branch 62 → 63 not taken.
✗ Branch 62 → 64 not taken.
✗ } else if (s->channel_depth == 16) {
416 ✗ avctx->pix_fmt = AV_PIX_FMT_YA16BE;
417 } else {
418 ✗ avpriv_report_missing_feature(avctx, "channel depth %d for grayscale", s->channel_depth);
419 ✗ return AVERROR_PATCHWELCOME;
420 }
421 2 s->primary_channels = 2;
422
1/2
✓ Branch 67 → 68 taken 8 times.
✗ Branch 67 → 77 not taken.
8 } else if (s->channel_count >= 1) {
423
2/2
✓ Branch 68 → 69 taken 4 times.
✓ Branch 68 → 70 taken 4 times.
8 if (s->channel_depth == 8) {
424 4 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
425
1/2
✓ Branch 70 → 71 taken 4 times.
✗ Branch 70 → 72 not taken.
4 } else if (s->channel_depth == 16) {
426 4 avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
427
0/2
✗ Branch 72 → 73 not taken.
✗ Branch 72 → 74 not taken.
✗ } else if (s->channel_depth == 32) {
428 ✗ avctx->pix_fmt = AV_PIX_FMT_GRAYF32BE;
429 } else {
430 ✗ avpriv_report_missing_feature(avctx, "channel depth %d for grayscale", s->channel_depth);
431 ✗ return AVERROR_PATCHWELCOME;
432 }
433 8 s->primary_channels = 1;
434 } else {
435 ✗ av_log(s->avctx, AV_LOG_ERROR,
436 "Invalid grayscale file (channel_count %d)\n",
437 ✗ s->channel_count);
438 ✗ return AVERROR_INVALIDDATA;
439 }
440 10 break;
441 ✗ default:
442 ✗ avpriv_report_missing_feature(avctx, "color mode %d", s->color_mode);
443 ✗ return AVERROR_PATCHWELCOME;
444 }
445
446 32 s->uncompressed_size = s->line_size * s->height * s->channel_count;
447
1/2
✗ Branch 82 → 83 not taken.
✓ Branch 82 → 84 taken 32 times.
32 if (s->uncompressed_size > INT_MAX)
448 ✗ return AVERROR_INVALIDDATA;
449
450 /* decode picture if need */
451
2/2
✓ Branch 84 → 85 taken 8 times.
✓ Branch 84 → 93 taken 24 times.
32 if (s->compression == PSD_RLE) {
452 8 s->tmp = av_malloc(s->uncompressed_size);
453
1/2
✗ Branch 86 → 87 not taken.
✓ Branch 86 → 88 taken 8 times.
8 if (!s->tmp)
454 ✗ return AVERROR(ENOMEM);
455
456 8 ret = decode_rle(s);
457
458
1/2
✗ Branch 89 → 90 not taken.
✓ Branch 89 → 92 taken 8 times.
8 if (ret < 0) {
459 ✗ av_freep(&s->tmp);
460 ✗ return ret;
461 }
462
463 8 ptr_data = s->tmp;
464 } else {
465
1/2
✗ Branch 94 → 95 not taken.
✓ Branch 94 → 97 taken 24 times.
24 if (bytestream2_get_bytes_left(&s->gb) < s->uncompressed_size) {
466 ✗ av_log(s->avctx, AV_LOG_ERROR, "Not enough data for raw image data section.\n");
467 ✗ return AVERROR_INVALIDDATA;
468 }
469 24 ptr_data = s->gb.buffer;
470 }
471
472
1/2
✗ Branch 99 → 100 not taken.
✓ Branch 99 → 102 taken 32 times.
32 if ((ret = ff_get_buffer(avctx, picture, 0)) < 0) {
473 ✗ av_freep(&s->tmp);
474 ✗ return ret;
475 }
476
477 /* Store data */
478
3/4
✓ Branch 102 → 103 taken 30 times.
✓ Branch 102 → 104 taken 2 times.
✗ Branch 103 → 104 not taken.
✓ Branch 103 → 117 taken 30 times.
32 if ((avctx->pix_fmt == AV_PIX_FMT_YA8)||(avctx->pix_fmt == AV_PIX_FMT_YA16BE)){/* Interleaved */
479 2 ptr = picture->data[0];
480
2/2
✓ Branch 115 → 105 taken 4 times.
✓ Branch 115 → 116 taken 2 times.
6 for (c = 0; c < 2; c++) {
481
2/2
✓ Branch 113 → 106 taken 512 times.
✓ Branch 113 → 114 taken 4 times.
516 for (y = 0; y < s->height; y++) {
482
2/2
✓ Branch 111 → 107 taken 65536 times.
✓ Branch 111 → 112 taken 512 times.
66048 for (x = 0; x < s->width; x++) {
483 65536 index_out = y * picture->linesize[0] + x * 2 * s->pixel_size + c * s->pixel_size;
484
2/2
✓ Branch 109 → 108 taken 65536 times.
✓ Branch 109 → 110 taken 65536 times.
131072 for (p = 0; p < s->pixel_size; p++) {
485 65536 ptr[index_out + p] = *ptr_data;
486 65536 ptr_data ++;
487 }
488 }
489 }
490 }
491
1/2
✗ Branch 117 → 118 not taken.
✓ Branch 117 → 140 taken 30 times.
30 } else if (s->color_mode == PSD_CMYK) {
492 ✗ uint8_t *dst[4] = { picture->data[0], picture->data[1], picture->data[2], picture->data[3] };
493 ✗ const uint8_t *src[5] = { ptr_data };
494 ✗ src[1] = src[0] + s->line_size * s->height;
495 ✗ src[2] = src[1] + s->line_size * s->height;
496 ✗ src[3] = src[2] + s->line_size * s->height;
497 ✗ src[4] = src[3] + s->line_size * s->height;
498
0/2
✗ Branch 118 → 119 not taken.
✗ Branch 118 → 129 not taken.
✗ if (s->channel_depth == 8) {
499
0/2
✗ Branch 124 → 120 not taken.
✗ Branch 124 → 125 not taken.
✗ for (y = 0; y < s->height; y++) {
500
0/2
✗ Branch 122 → 121 not taken.
✗ Branch 122 → 123 not taken.
✗ for (x = 0; x < s->width; x++) {
501 ✗ int k = src[3][x];
502 ✗ int r = src[0][x] * k;
503 ✗ int g = src[1][x] * k;
504 ✗ int b = src[2][x] * k;
505 ✗ dst[0][x] = g * 257 >> 16;
506 ✗ dst[1][x] = b * 257 >> 16;
507 ✗ dst[2][x] = r * 257 >> 16;
508 }
509 ✗ dst[0] += picture->linesize[0];
510 ✗ dst[1] += picture->linesize[1];
511 ✗ dst[2] += picture->linesize[2];
512 ✗ src[0] += s->line_size;
513 ✗ src[1] += s->line_size;
514 ✗ src[2] += s->line_size;
515 ✗ src[3] += s->line_size;
516 }
517
0/2
✗ Branch 125 → 126 not taken.
✗ Branch 125 → 139 not taken.
✗ if (avctx->pix_fmt == AV_PIX_FMT_GBRAP) {
518
0/2
✗ Branch 128 → 127 not taken.
✗ Branch 128 → 139 not taken.
✗ for (y = 0; y < s->height; y++) {
519 ✗ memcpy(dst[3], src[4], s->line_size);
520 ✗ src[4] += s->line_size;
521 ✗ dst[3] += picture->linesize[3];
522 }
523 }
524 } else {
525
0/2
✗ Branch 134 → 130 not taken.
✗ Branch 134 → 135 not taken.
✗ for (y = 0; y < s->height; y++) {
526
0/2
✗ Branch 132 → 131 not taken.
✗ Branch 132 → 133 not taken.
✗ for (x = 0; x < s->width; x++) {
527 ✗ int64_t k = AV_RB16(&src[3][x * 2]);
528 ✗ int64_t r = AV_RB16(&src[0][x * 2]) * k;
529 ✗ int64_t g = AV_RB16(&src[1][x * 2]) * k;
530 ✗ int64_t b = AV_RB16(&src[2][x * 2]) * k;
531 ✗ AV_WB16(&dst[0][x * 2], g * 65537 >> 32);
532 ✗ AV_WB16(&dst[1][x * 2], b * 65537 >> 32);
533 ✗ AV_WB16(&dst[2][x * 2], r * 65537 >> 32);
534 }
535 ✗ dst[0] += picture->linesize[0];
536 ✗ dst[1] += picture->linesize[1];
537 ✗ dst[2] += picture->linesize[2];
538 ✗ src[0] += s->line_size;
539 ✗ src[1] += s->line_size;
540 ✗ src[2] += s->line_size;
541 ✗ src[3] += s->line_size;
542 }
543
0/2
✗ Branch 135 → 136 not taken.
✗ Branch 135 → 139 not taken.
✗ if (avctx->pix_fmt == AV_PIX_FMT_GBRAP16BE) {
544
0/2
✗ Branch 138 → 137 not taken.
✗ Branch 138 → 139 not taken.
✗ for (y = 0; y < s->height; y++) {
545 ✗ memcpy(dst[3], src[4], s->line_size);
546 ✗ src[4] += s->line_size;
547 ✗ dst[3] += picture->linesize[3];
548 }
549 }
550 }
551 } else {/* Planar */
552
2/2
✓ Branch 140 → 141 taken 12 times.
✓ Branch 140 → 142 taken 18 times.
30 if (s->primary_channels == 1)/* bitmap, indexed, grayscale */
553 12 eq_channel[0] = 0;/* assign first channel, to first plane */
554
555
2/2
✓ Branch 147 → 143 taken 70 times.
✓ Branch 147 → 148 taken 30 times.
100 for (c = 0; c < s->primary_channels; c++) {
556 70 plane_number = eq_channel[c];
557 70 ptr = picture->data[plane_number];/* get the right plane */
558
2/2
✓ Branch 145 → 144 taken 8942 times.
✓ Branch 145 → 146 taken 70 times.
9012 for (y = 0; y < s->height; y++) {
559 8942 memcpy(ptr, ptr_data, s->line_size);
560 8942 ptr += picture->linesize[plane_number];
561 8942 ptr_data += s->line_size;
562 }
563 }
564 }
565
566
2/2
✓ Branch 148 → 149 taken 2 times.
✓ Branch 148 → 150 taken 30 times.
32 if (s->color_mode == PSD_INDEXED) {
567 2 memcpy(picture->data[1], s->palette, AVPALETTE_SIZE);
568 }
569
570 32 av_freep(&s->tmp);
571
572 32 picture->pict_type = AV_PICTURE_TYPE_I;
573 32 *got_frame = 1;
574
575 32 return avpkt->size;
576 }
577
578 const FFCodec ff_psd_decoder = {
579 .p.name = "psd",
580 CODEC_LONG_NAME("Photoshop PSD file"),
581 .p.type = AVMEDIA_TYPE_VIDEO,
582 .p.id = AV_CODEC_ID_PSD,
583 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
584 .priv_data_size = sizeof(PSDContext),
585 FF_CODEC_DECODE_CB(decode_frame),
586 };
587