Coverage Report

Created: 2024-09-06 07:53

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