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