| 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 1 not taken.
✓ Branch 2 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 0 not taken.
✓ Branch 1 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 0 not taken.
✓ Branch 1 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 0 taken 32 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 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 0 not taken.
✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
✗ Branch 3 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 0 not taken.
✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
✗ Branch 3 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 1 not taken.
✓ Branch 2 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 0 taken 2 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 18 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✗ Branch 8 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 0 not taken.
✓ Branch 1 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 1 not taken.
✓ Branch 2 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 0 taken 4 times.
✓ Branch 1 taken 28 times.
|
32 | if (len_section) { |
| 170 | int i,j; | ||
| 171 | 4 | memset(s->palette, 0xff, AVPALETTE_SIZE); | |
| 172 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 4 times.
|
16 | for (j = HAVE_BIGENDIAN; j < 3 + HAVE_BIGENDIAN; j++) |
| 173 |
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++) |
| 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 0 not taken.
✓ Branch 1 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 1 not taken.
✓ Branch 2 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 0 not taken.
✓ Branch 1 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 0 taken 32 times.
✗ Branch 1 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 1 not taken.
✓ Branch 2 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 1 not taken.
✓ Branch 2 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 0 taken 32 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 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 1 not taken.
✓ Branch 2 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 0 taken 2804 times.
✓ Branch 1 taken 8 times.
|
2812 | for (sl = 0; sl < scanline_count; sl++) { |
| 257 | 2804 | count = 0; | |
| 258 | |||
| 259 |
2/2✓ Branch 0 taken 18540 times.
✓ Branch 1 taken 2804 times.
|
21344 | while (count < s->line_size) { |
| 260 | 18540 | rle_char = bytestream2_get_byte(&s->gb); | |
| 261 | |||
| 262 |
2/2✓ Branch 0 taken 12340 times.
✓ Branch 1 taken 6200 times.
|
18540 | if (rle_char <= 0) {/* byte repeat */ |
| 263 | 12340 | repeat_count = rle_char * -1; | |
| 264 | |||
| 265 |
1/2✗ Branch 1 not taken.
✓ Branch 2 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 0 not taken.
✓ Branch 1 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 0 taken 210352 times.
✓ Branch 1 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 1 not taken.
✓ Branch 2 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 0 not taken.
✓ Branch 1 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 0 taken 243810 times.
✓ Branch 1 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 1 not taken.
✓ Branch 2 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 0 taken 2 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 8 times.
✗ Branch 6 not taken.
|
32 | switch (s->color_mode) { |
| 331 | 2 | case PSD_BITMAP: | |
| 332 |
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) { |
| 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 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) { |
| 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 | ✗ | if (s->layer_count < 0 && s->channel_count >= 5) { | |
| 354 | ✗ | if (s->channel_depth == 8) { | |
| 355 | ✗ | avctx->pix_fmt = AV_PIX_FMT_GBRAP; | |
| 356 | ✗ | } 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 | ✗ | } else if (s->channel_count >= 4) { | |
| 364 | ✗ | if (s->channel_depth == 8) { | |
| 365 | ✗ | avctx->pix_fmt = AV_PIX_FMT_GBRP; | |
| 366 | ✗ | } 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 0 taken 4 times.
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
|
18 | if (s->layer_count < 0 && s->channel_count >= 4) { |
| 382 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | if (s->channel_depth == 8) { |
| 383 | 4 | avctx->pix_fmt = AV_PIX_FMT_GBRAP; | |
| 384 | ✗ | } 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 0 taken 14 times.
✗ Branch 1 not taken.
|
14 | } else if (s->channel_count >= 3) { |
| 392 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 6 times.
|
14 | if (s->channel_depth == 8) { |
| 393 | 8 | avctx->pix_fmt = AV_PIX_FMT_GBRP; | |
| 394 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 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 | 10 | case PSD_GRAYSCALE: | |
| 411 |
3/4✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
10 | if (s->layer_count < 0 && s->channel_count >= 2) { |
| 412 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (s->channel_depth == 8) { |
| 413 | 2 | avctx->pix_fmt = AV_PIX_FMT_YA8; | |
| 414 | ✗ | } else if (s->channel_depth == 16) { | |
| 415 | ✗ | avctx->pix_fmt = AV_PIX_FMT_YA16BE; | |
| 416 | } else { | ||
| 417 | ✗ | avpriv_report_missing_feature(avctx, "channel depth %d for grayscale", s->channel_depth); | |
| 418 | ✗ | return AVERROR_PATCHWELCOME; | |
| 419 | } | ||
| 420 | 2 | s->primary_channels = 2; | |
| 421 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | } else if (s->channel_count >= 1) { |
| 422 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
|
8 | if (s->channel_depth == 8) { |
| 423 | 4 | avctx->pix_fmt = AV_PIX_FMT_GRAY8; | |
| 424 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | } else if (s->channel_depth == 16) { |
| 425 | 4 | avctx->pix_fmt = AV_PIX_FMT_GRAY16BE; | |
| 426 | ✗ | } else if (s->channel_depth == 32) { | |
| 427 | ✗ | avctx->pix_fmt = AV_PIX_FMT_GRAYF32BE; | |
| 428 | } else { | ||
| 429 | ✗ | avpriv_report_missing_feature(avctx, "channel depth %d for grayscale", s->channel_depth); | |
| 430 | ✗ | return AVERROR_PATCHWELCOME; | |
| 431 | } | ||
| 432 | 8 | s->primary_channels = 1; | |
| 433 | } else { | ||
| 434 | ✗ | av_log(s->avctx, AV_LOG_ERROR, | |
| 435 | "Invalid grayscale file (channel_count %d)\n", | ||
| 436 | ✗ | s->channel_count); | |
| 437 | ✗ | return AVERROR_INVALIDDATA; | |
| 438 | } | ||
| 439 | 10 | break; | |
| 440 | ✗ | default: | |
| 441 | ✗ | avpriv_report_missing_feature(avctx, "color mode %d", s->color_mode); | |
| 442 | ✗ | return AVERROR_PATCHWELCOME; | |
| 443 | } | ||
| 444 | |||
| 445 | 32 | s->uncompressed_size = s->line_size * s->height * s->channel_count; | |
| 446 | |||
| 447 | /* decode picture if need */ | ||
| 448 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 24 times.
|
32 | if (s->compression == PSD_RLE) { |
| 449 | 8 | s->tmp = av_malloc(s->uncompressed_size); | |
| 450 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | if (!s->tmp) |
| 451 | ✗ | return AVERROR(ENOMEM); | |
| 452 | |||
| 453 | 8 | ret = decode_rle(s); | |
| 454 | |||
| 455 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | if (ret < 0) { |
| 456 | ✗ | av_freep(&s->tmp); | |
| 457 | ✗ | return ret; | |
| 458 | } | ||
| 459 | |||
| 460 | 8 | ptr_data = s->tmp; | |
| 461 | } else { | ||
| 462 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
|
24 | if (bytestream2_get_bytes_left(&s->gb) < s->uncompressed_size) { |
| 463 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Not enough data for raw image data section.\n"); | |
| 464 | ✗ | return AVERROR_INVALIDDATA; | |
| 465 | } | ||
| 466 | 24 | ptr_data = s->gb.buffer; | |
| 467 | } | ||
| 468 | |||
| 469 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
|
32 | if ((ret = ff_get_buffer(avctx, picture, 0)) < 0) |
| 470 | ✗ | return ret; | |
| 471 | |||
| 472 | /* Store data */ | ||
| 473 |
3/4✓ Branch 0 taken 30 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 30 times.
|
32 | if ((avctx->pix_fmt == AV_PIX_FMT_YA8)||(avctx->pix_fmt == AV_PIX_FMT_YA16BE)){/* Interleaved */ |
| 474 | 2 | ptr = picture->data[0]; | |
| 475 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
|
6 | for (c = 0; c < 2; c++) { |
| 476 |
2/2✓ Branch 0 taken 512 times.
✓ Branch 1 taken 4 times.
|
516 | for (y = 0; y < s->height; y++) { |
| 477 |
2/2✓ Branch 0 taken 65536 times.
✓ Branch 1 taken 512 times.
|
66048 | for (x = 0; x < s->width; x++) { |
| 478 | 65536 | index_out = y * picture->linesize[0] + x * 2 * s->pixel_size + c * s->pixel_size; | |
| 479 |
2/2✓ Branch 0 taken 65536 times.
✓ Branch 1 taken 65536 times.
|
131072 | for (p = 0; p < s->pixel_size; p++) { |
| 480 | 65536 | ptr[index_out + p] = *ptr_data; | |
| 481 | 65536 | ptr_data ++; | |
| 482 | } | ||
| 483 | } | ||
| 484 | } | ||
| 485 | } | ||
| 486 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
|
30 | } else if (s->color_mode == PSD_CMYK) { |
| 487 | ✗ | uint8_t *dst[4] = { picture->data[0], picture->data[1], picture->data[2], picture->data[3] }; | |
| 488 | ✗ | const uint8_t *src[5] = { ptr_data }; | |
| 489 | ✗ | src[1] = src[0] + s->line_size * s->height; | |
| 490 | ✗ | src[2] = src[1] + s->line_size * s->height; | |
| 491 | ✗ | src[3] = src[2] + s->line_size * s->height; | |
| 492 | ✗ | src[4] = src[3] + s->line_size * s->height; | |
| 493 | ✗ | if (s->channel_depth == 8) { | |
| 494 | ✗ | for (y = 0; y < s->height; y++) { | |
| 495 | ✗ | for (x = 0; x < s->width; x++) { | |
| 496 | ✗ | int k = src[3][x]; | |
| 497 | ✗ | int r = src[0][x] * k; | |
| 498 | ✗ | int g = src[1][x] * k; | |
| 499 | ✗ | int b = src[2][x] * k; | |
| 500 | ✗ | dst[0][x] = g * 257 >> 16; | |
| 501 | ✗ | dst[1][x] = b * 257 >> 16; | |
| 502 | ✗ | dst[2][x] = r * 257 >> 16; | |
| 503 | } | ||
| 504 | ✗ | dst[0] += picture->linesize[0]; | |
| 505 | ✗ | dst[1] += picture->linesize[1]; | |
| 506 | ✗ | dst[2] += picture->linesize[2]; | |
| 507 | ✗ | src[0] += s->line_size; | |
| 508 | ✗ | src[1] += s->line_size; | |
| 509 | ✗ | src[2] += s->line_size; | |
| 510 | ✗ | src[3] += s->line_size; | |
| 511 | } | ||
| 512 | ✗ | if (avctx->pix_fmt == AV_PIX_FMT_GBRAP) { | |
| 513 | ✗ | for (y = 0; y < s->height; y++) { | |
| 514 | ✗ | memcpy(dst[3], src[4], s->line_size); | |
| 515 | ✗ | src[4] += s->line_size; | |
| 516 | ✗ | dst[3] += picture->linesize[3]; | |
| 517 | } | ||
| 518 | } | ||
| 519 | } else { | ||
| 520 | ✗ | for (y = 0; y < s->height; y++) { | |
| 521 | ✗ | for (x = 0; x < s->width; x++) { | |
| 522 | ✗ | int64_t k = AV_RB16(&src[3][x * 2]); | |
| 523 | ✗ | int64_t r = AV_RB16(&src[0][x * 2]) * k; | |
| 524 | ✗ | int64_t g = AV_RB16(&src[1][x * 2]) * k; | |
| 525 | ✗ | int64_t b = AV_RB16(&src[2][x * 2]) * k; | |
| 526 | ✗ | AV_WB16(&dst[0][x * 2], g * 65537 >> 32); | |
| 527 | ✗ | AV_WB16(&dst[1][x * 2], b * 65537 >> 32); | |
| 528 | ✗ | AV_WB16(&dst[2][x * 2], r * 65537 >> 32); | |
| 529 | } | ||
| 530 | ✗ | dst[0] += picture->linesize[0]; | |
| 531 | ✗ | dst[1] += picture->linesize[1]; | |
| 532 | ✗ | dst[2] += picture->linesize[2]; | |
| 533 | ✗ | src[0] += s->line_size; | |
| 534 | ✗ | src[1] += s->line_size; | |
| 535 | ✗ | src[2] += s->line_size; | |
| 536 | ✗ | src[3] += s->line_size; | |
| 537 | } | ||
| 538 | ✗ | if (avctx->pix_fmt == AV_PIX_FMT_GBRAP16BE) { | |
| 539 | ✗ | for (y = 0; y < s->height; y++) { | |
| 540 | ✗ | memcpy(dst[3], src[4], s->line_size); | |
| 541 | ✗ | src[4] += s->line_size; | |
| 542 | ✗ | dst[3] += picture->linesize[3]; | |
| 543 | } | ||
| 544 | } | ||
| 545 | } | ||
| 546 | } else {/* Planar */ | ||
| 547 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 18 times.
|
30 | if (s->primary_channels == 1)/* bitmap, indexed, grayscale */ |
| 548 | 12 | eq_channel[0] = 0;/* assign first channel, to first plane */ | |
| 549 | |||
| 550 |
2/2✓ Branch 0 taken 70 times.
✓ Branch 1 taken 30 times.
|
100 | for (c = 0; c < s->primary_channels; c++) { |
| 551 | 70 | plane_number = eq_channel[c]; | |
| 552 | 70 | ptr = picture->data[plane_number];/* get the right plane */ | |
| 553 |
2/2✓ Branch 0 taken 8942 times.
✓ Branch 1 taken 70 times.
|
9012 | for (y = 0; y < s->height; y++) { |
| 554 | 8942 | memcpy(ptr, ptr_data, s->line_size); | |
| 555 | 8942 | ptr += picture->linesize[plane_number]; | |
| 556 | 8942 | ptr_data += s->line_size; | |
| 557 | } | ||
| 558 | } | ||
| 559 | } | ||
| 560 | |||
| 561 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 30 times.
|
32 | if (s->color_mode == PSD_INDEXED) { |
| 562 | 2 | memcpy(picture->data[1], s->palette, AVPALETTE_SIZE); | |
| 563 | } | ||
| 564 | |||
| 565 | 32 | av_freep(&s->tmp); | |
| 566 | |||
| 567 | 32 | picture->pict_type = AV_PICTURE_TYPE_I; | |
| 568 | 32 | *got_frame = 1; | |
| 569 | |||
| 570 | 32 | return avpkt->size; | |
| 571 | } | ||
| 572 | |||
| 573 | const FFCodec ff_psd_decoder = { | ||
| 574 | .p.name = "psd", | ||
| 575 | CODEC_LONG_NAME("Photoshop PSD file"), | ||
| 576 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
| 577 | .p.id = AV_CODEC_ID_PSD, | ||
| 578 | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS, | ||
| 579 | .priv_data_size = sizeof(PSDContext), | ||
| 580 | FF_CODEC_DECODE_CB(decode_frame), | ||
| 581 | }; | ||
| 582 |