FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/pcx.c
Date: 2023-03-31 03:41:15
Exec Total Coverage
Lines: 62 135 45.9%
Functions: 2 3 66.7%
Branches: 33 99 33.3%

Line Branch Exec Source
1 /*
2 * PC Paintbrush PCX (.pcx) image decoder
3 * Copyright (c) 2007, 2008 Ivo van Poorten
4 *
5 * This decoder does not support CGA palettes. I am unable to find samples
6 * and Netpbm cannot generate them.
7 *
8 * This file is part of FFmpeg.
9 *
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25 #include "avcodec.h"
26 #include "bytestream.h"
27 #include "codec_internal.h"
28 #include "decode.h"
29 #include "get_bits.h"
30
31 #define PCX_HEADER_SIZE 128
32
33 4320 static int pcx_rle_decode(GetByteContext *gb,
34 uint8_t *dst,
35 unsigned int bytes_per_scanline,
36 int compressed)
37 {
38 4320 unsigned int i = 0;
39 unsigned char run, value;
40
41
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4320 times.
4320 if (bytestream2_get_bytes_left(gb) < 1)
42 return AVERROR_INVALIDDATA;
43
44
1/2
✓ Branch 0 taken 4320 times.
✗ Branch 1 not taken.
4320 if (compressed) {
45
3/4
✓ Branch 0 taken 4325705 times.
✓ Branch 1 taken 4320 times.
✓ Branch 3 taken 4325705 times.
✗ Branch 4 not taken.
4330025 while (i < bytes_per_scanline && bytestream2_get_bytes_left(gb)>0) {
46 4325705 run = 1;
47 4325705 value = bytestream2_get_byte(gb);
48
3/4
✓ Branch 0 taken 1136991 times.
✓ Branch 1 taken 3188714 times.
✓ Branch 3 taken 1136991 times.
✗ Branch 4 not taken.
4325705 if (value >= 0xc0 && bytestream2_get_bytes_left(gb)>0) {
49 1136991 run = value & 0x3f;
50 1136991 value = bytestream2_get_byte(gb);
51 }
52
4/4
✓ Branch 0 taken 8883305 times.
✓ Branch 1 taken 4320 times.
✓ Branch 2 taken 4561920 times.
✓ Branch 3 taken 4321385 times.
8887625 while (i < bytes_per_scanline && run--)
53 4561920 dst[i++] = value;
54 }
55 } else {
56 bytestream2_get_buffer(gb, dst, bytes_per_scanline);
57 }
58 4320 return 0;
59 }
60
61 static void pcx_palette(GetByteContext *gb, uint32_t *dst, int pallen)
62 {
63 int i;
64
65 pallen = FFMIN(pallen, bytestream2_get_bytes_left(gb) / 3);
66 for (i = 0; i < pallen; i++)
67 *dst++ = 0xFF000000 | bytestream2_get_be24u(gb);
68 if (pallen < 256)
69 memset(dst, 0, (256 - pallen) * sizeof(*dst));
70 }
71
72 15 static int pcx_decode_frame(AVCodecContext *avctx, AVFrame *p,
73 int *got_frame, AVPacket *avpkt)
74 {
75 GetByteContext gb;
76 int compressed, xmin, ymin, xmax, ymax;
77 int ret;
78 unsigned int w, h, bits_per_pixel, bytes_per_line, nplanes, stride, y, x,
79 bytes_per_scanline;
80 uint8_t *ptr, *scanline;
81
82
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (avpkt->size < PCX_HEADER_SIZE) {
83 av_log(avctx, AV_LOG_ERROR, "Packet too small\n");
84 return AVERROR_INVALIDDATA;
85 }
86
87 15 bytestream2_init(&gb, avpkt->data, avpkt->size);
88
89
2/4
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 15 times.
15 if (bytestream2_get_byteu(&gb) != 0x0a || bytestream2_get_byteu(&gb) > 5) {
90 av_log(avctx, AV_LOG_ERROR, "this is not PCX encoded data\n");
91 return AVERROR_INVALIDDATA;
92 }
93
94 15 compressed = bytestream2_get_byteu(&gb);
95 15 bits_per_pixel = bytestream2_get_byteu(&gb);
96 15 xmin = bytestream2_get_le16u(&gb);
97 15 ymin = bytestream2_get_le16u(&gb);
98 15 xmax = bytestream2_get_le16u(&gb);
99 15 ymax = bytestream2_get_le16u(&gb);
100 15 avctx->sample_aspect_ratio.num = bytestream2_get_le16u(&gb);
101 15 avctx->sample_aspect_ratio.den = bytestream2_get_le16u(&gb);
102
103
2/4
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 15 times.
15 if (xmax < xmin || ymax < ymin) {
104 av_log(avctx, AV_LOG_ERROR, "invalid image dimensions\n");
105 return AVERROR_INVALIDDATA;
106 }
107
108 15 w = xmax - xmin + 1;
109 15 h = ymax - ymin + 1;
110
111 15 bytestream2_skipu(&gb, 49);
112 15 nplanes = bytestream2_get_byteu(&gb);
113 15 bytes_per_line = bytestream2_get_le16u(&gb);
114 15 bytes_per_scanline = nplanes * bytes_per_line;
115
116
2/4
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 15 times.
15 if (bytes_per_scanline < (w * bits_per_pixel * nplanes + 7) / 8 ||
117 (!compressed && bytes_per_scanline > bytestream2_get_bytes_left(&gb) / h)) {
118 av_log(avctx, AV_LOG_ERROR, "PCX data is corrupted\n");
119 return AVERROR_INVALIDDATA;
120 }
121
122
1/3
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
15 switch ((nplanes << 8) + bits_per_pixel) {
123 15 case 0x0308:
124 15 avctx->pix_fmt = AV_PIX_FMT_RGB24;
125 15 break;
126 case 0x0108:
127 case 0x0104:
128 case 0x0102:
129 case 0x0101:
130 case 0x0401:
131 case 0x0301:
132 case 0x0201:
133 avctx->pix_fmt = AV_PIX_FMT_PAL8;
134 break;
135 default:
136 av_log(avctx, AV_LOG_ERROR, "invalid PCX file\n");
137 return AVERROR_INVALIDDATA;
138 }
139
140 15 bytestream2_skipu(&gb, 60);
141
142
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 if ((ret = ff_set_dimensions(avctx, w, h)) < 0)
143 return ret;
144
145
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
146 return ret;
147
148 15 p->pict_type = AV_PICTURE_TYPE_I;
149
150 15 ptr = p->data[0];
151 15 stride = p->linesize[0];
152
153 15 scanline = av_malloc(bytes_per_scanline + AV_INPUT_BUFFER_PADDING_SIZE);
154
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (!scanline)
155 return AVERROR(ENOMEM);
156
157
2/4
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
15 if (nplanes == 3 && bits_per_pixel == 8) {
158
2/2
✓ Branch 0 taken 4320 times.
✓ Branch 1 taken 15 times.
4335 for (y = 0; y < h; y++) {
159 4320 ret = pcx_rle_decode(&gb, scanline, bytes_per_scanline, compressed);
160
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4320 times.
4320 if (ret < 0)
161 goto end;
162
163
2/2
✓ Branch 0 taken 1520640 times.
✓ Branch 1 taken 4320 times.
1524960 for (x = 0; x < w; x++) {
164 1520640 ptr[3 * x] = scanline[x];
165 1520640 ptr[3 * x + 1] = scanline[x + bytes_per_line];
166 1520640 ptr[3 * x + 2] = scanline[x + (bytes_per_line << 1)];
167 }
168
169 4320 ptr += stride;
170 }
171 } else if (nplanes == 1 && bits_per_pixel == 8) {
172 int palstart = avpkt->size - 769;
173
174 if (avpkt->size < 769) {
175 av_log(avctx, AV_LOG_ERROR, "File is too short\n");
176 ret = avctx->err_recognition & AV_EF_EXPLODE ?
177 AVERROR_INVALIDDATA : avpkt->size;
178 goto end;
179 }
180
181 for (y = 0; y < h; y++, ptr += stride) {
182 ret = pcx_rle_decode(&gb, scanline, bytes_per_scanline, compressed);
183 if (ret < 0)
184 goto end;
185 memcpy(ptr, scanline, w);
186 }
187
188 if (bytestream2_tell(&gb) != palstart) {
189 av_log(avctx, AV_LOG_WARNING, "image data possibly corrupted\n");
190 bytestream2_seek(&gb, palstart, SEEK_SET);
191 }
192 if (bytestream2_get_byte(&gb) != 12) {
193 av_log(avctx, AV_LOG_ERROR, "expected palette after image data\n");
194 ret = avctx->err_recognition & AV_EF_EXPLODE ?
195 AVERROR_INVALIDDATA : avpkt->size;
196 goto end;
197 }
198 } else if (nplanes == 1) { /* all packed formats, max. 16 colors */
199 GetBitContext s;
200
201 for (y = 0; y < h; y++) {
202 init_get_bits8(&s, scanline, bytes_per_scanline);
203
204 ret = pcx_rle_decode(&gb, scanline, bytes_per_scanline, compressed);
205 if (ret < 0)
206 goto end;
207
208 for (x = 0; x < w; x++)
209 ptr[x] = get_bits(&s, bits_per_pixel);
210 ptr += stride;
211 }
212 } else { /* planar, 4, 8 or 16 colors */
213 int i;
214
215 for (y = 0; y < h; y++) {
216 ret = pcx_rle_decode(&gb, scanline, bytes_per_scanline, compressed);
217 if (ret < 0)
218 goto end;
219
220 for (x = 0; x < w; x++) {
221 int m = 0x80 >> (x & 7), v = 0;
222 for (i = nplanes - 1; i >= 0; i--) {
223 v <<= 1;
224 v += !!(scanline[i * bytes_per_line + (x >> 3)] & m);
225 }
226 ptr[x] = v;
227 }
228 ptr += stride;
229 }
230 }
231
232 15 ret = bytestream2_tell(&gb);
233
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
15 if (nplanes == 1 && bits_per_pixel == 8) {
234 pcx_palette(&gb, (uint32_t *)p->data[1], 256);
235 ret += 256 * 3;
236
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 } else if (bits_per_pixel * nplanes == 1) {
237 AV_WN32A(p->data[1] , 0xFF000000);
238 AV_WN32A(p->data[1]+4, 0xFFFFFFFF);
239
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 } else if (bits_per_pixel < 8) {
240 bytestream2_seek(&gb, 16, SEEK_SET);
241 pcx_palette(&gb, (uint32_t *)p->data[1], 16);
242 }
243
244 15 *got_frame = 1;
245
246 15 end:
247 15 av_free(scanline);
248 15 return ret;
249 }
250
251 const FFCodec ff_pcx_decoder = {
252 .p.name = "pcx",
253 CODEC_LONG_NAME("PC Paintbrush PCX image"),
254 .p.type = AVMEDIA_TYPE_VIDEO,
255 .p.id = AV_CODEC_ID_PCX,
256 FF_CODEC_DECODE_CB(pcx_decode_frame),
257 .p.capabilities = AV_CODEC_CAP_DR1,
258 };
259