FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/pnm.c
Date: 2021-09-16 08:47:15
Exec Total Coverage
Lines: 107 148 72.3%
Branches: 90 160 56.2%

Line Branch Exec Source
1 /*
2 * PNM image format
3 * Copyright (c) 2002, 2003 Fabrice Bellard
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 <stdlib.h>
23 #include <string.h>
24
25 #include "libavutil/avassert.h"
26 #include "libavutil/imgutils.h"
27 #include "libavutil/avstring.h"
28 #include "avcodec.h"
29 #include "internal.h"
30 #include "pnm.h"
31
32 241739 static inline int pnm_space(int c)
33 {
34
6/8
✓ Branch 0 taken 229516 times.
✓ Branch 1 taken 12223 times.
✓ Branch 2 taken 181857 times.
✓ Branch 3 taken 47659 times.
✓ Branch 4 taken 181857 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 181857 times.
241739 return c == ' ' || c == '\n' || c == '\r' || c == '\t';
35 }
36
37 47995 static void pnm_get(PNMContext *sc, char *str, int buf_size)
38 {
39 char *s;
40 int c;
41 47995 uint8_t *bs = sc->bytestream;
42 47995 const uint8_t *end = sc->bytestream_end;
43
44 /* skip spaces and comments */
45
1/2
✓ Branch 0 taken 47997 times.
✗ Branch 1 not taken.
47997 while (bs < end) {
46 47997 c = *bs++;
47
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 47995 times.
47997 if (c == '#') {
48
3/4
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 88 times.
✗ Branch 3 not taken.
90 while (c != '\n' && bs < end) {
49 88 c = *bs++;
50 }
51
1/2
✓ Branch 1 taken 47995 times.
✗ Branch 2 not taken.
47995 } else if (!pnm_space(c)) {
52 47995 break;
53 }
54 }
55
56 47995 s = str;
57
4/6
✓ Branch 0 taken 181857 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 133862 times.
✓ Branch 4 taken 47995 times.
✓ Branch 5 taken 133862 times.
✗ Branch 6 not taken.
181857 while (bs < end && !pnm_space(c) && (s - str) < buf_size - 1) {
58 133862 *s++ = c;
59 133862 c = *bs++;
60 }
61 47995 *s = '\0';
62 47995 sc->bytestream = bs;
63 47995 }
64
65 11896 int ff_pnm_decode_header(AVCodecContext *avctx, PNMContext * const s)
66 {
67 char buf1[32], tuple_type[32];
68 int h, w, depth, maxval;
69 int ret;
70
71
2/2
✓ Branch 0 taken 11887 times.
✓ Branch 1 taken 9 times.
11896 if (s->bytestream_end - s->bytestream < 3 ||
72
1/2
✓ Branch 0 taken 11887 times.
✗ Branch 1 not taken.
11887 s->bytestream[0] != 'P' ||
73
1/2
✓ Branch 0 taken 11887 times.
✗ Branch 1 not taken.
11887 (s->bytestream[1] < '1' ||
74
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11887 times.
11887 s->bytestream[1] > '7' &&
75 s->bytestream[1] != 'f' &&
76 s->bytestream[1] != 'F')) {
77 9 s->bytestream += s->bytestream_end > s->bytestream;
78 9 s->bytestream += s->bytestream_end > s->bytestream;
79 9 return AVERROR_INVALIDDATA;
80 }
81 11887 pnm_get(s, buf1, sizeof(buf1));
82 11887 s->type= buf1[1]-'0';
83
84
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11887 times.
11887 if (buf1[1] == 'F') {
85 avctx->pix_fmt = AV_PIX_FMT_GBRPF32;
86
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11887 times.
11887 } else if (buf1[1] == 'f') {
87 avctx->pix_fmt = AV_PIX_FMT_GRAYF32;
88
3/4
✓ Branch 0 taken 11887 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 225 times.
✓ Branch 3 taken 11662 times.
11887 } else if (s->type==1 || s->type==4) {
89 225 avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
90
3/4
✓ Branch 0 taken 11662 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11343 times.
✓ Branch 3 taken 319 times.
11662 } else if (s->type==2 || s->type==5) {
91
2/2
✓ Branch 0 taken 10984 times.
✓ Branch 1 taken 359 times.
11343 if (avctx->codec_id == AV_CODEC_ID_PGMYUV)
92 10984 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
93 else
94 359 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
95
3/4
✓ Branch 0 taken 319 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 235 times.
✓ Branch 3 taken 84 times.
319 } else if (s->type==3 || s->type==6) {
96 235 avctx->pix_fmt = AV_PIX_FMT_RGB24;
97
1/2
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
84 } else if (s->type==7) {
98 84 w = -1;
99 84 h = -1;
100 84 maxval = -1;
101 84 depth = -1;
102 84 tuple_type[0] = '\0';
103 for (;;) {
104 504 pnm_get(s, buf1, sizeof(buf1));
105
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 420 times.
504 if (!strcmp(buf1, "WIDTH")) {
106 84 pnm_get(s, buf1, sizeof(buf1));
107 84 w = strtol(buf1, NULL, 10);
108
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 336 times.
420 } else if (!strcmp(buf1, "HEIGHT")) {
109 84 pnm_get(s, buf1, sizeof(buf1));
110 84 h = strtol(buf1, NULL, 10);
111
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 252 times.
336 } else if (!strcmp(buf1, "DEPTH")) {
112 84 pnm_get(s, buf1, sizeof(buf1));
113 84 depth = strtol(buf1, NULL, 10);
114
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 168 times.
252 } else if (!strcmp(buf1, "MAXVAL")) {
115 84 pnm_get(s, buf1, sizeof(buf1));
116 84 maxval = strtol(buf1, NULL, 10);
117
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 84 times.
168 } else if (!strcmp(buf1, "TUPLTYPE") ||
118 /* libavcodec used to write invalid files */
119
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 84 times.
84 !strcmp(buf1, "TUPLETYPE")) {
120 84 pnm_get(s, tuple_type, sizeof(tuple_type));
121
1/2
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
84 } else if (!strcmp(buf1, "ENDHDR")) {
122 84 break;
123 } else {
124 return AVERROR_INVALIDDATA;
125 }
126 }
127
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 84 times.
84 if (!pnm_space(s->bytestream[-1]))
128 return AVERROR_INVALIDDATA;
129
130 /* check that all tags are present */
131
7/14
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 84 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 84 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 84 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 84 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 84 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 84 times.
✗ Branch 13 not taken.
168 if (w <= 0 || h <= 0 || maxval <= 0 || maxval > UINT16_MAX || depth <= 0 || tuple_type[0] == '\0' ||
132
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 84 times.
168 av_image_check_size(w, h, 0, avctx) || s->bytestream >= s->bytestream_end)
133 return AVERROR_INVALIDDATA;
134
135 84 ret = ff_set_dimensions(avctx, w, h);
136
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 84 times.
84 if (ret < 0)
137 return ret;
138 84 s->maxval = maxval;
139
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 42 times.
84 if (depth == 1) {
140
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 28 times.
42 if (maxval == 1) {
141 14 avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
142
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 14 times.
28 } else if (maxval < 256) {
143 14 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
144 } else {
145 14 avctx->pix_fmt = AV_PIX_FMT_GRAY16;
146 }
147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 } else if (depth == 2) {
148 if (maxval < 256) {
149 avctx->pix_fmt = AV_PIX_FMT_GRAY8A;
150 } else {
151 avctx->pix_fmt = AV_PIX_FMT_YA16;
152 }
153
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 14 times.
42 } else if (depth == 3) {
154
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 14 times.
28 if (maxval < 256) {
155 14 avctx->pix_fmt = AV_PIX_FMT_RGB24;
156 } else {
157 14 avctx->pix_fmt = AV_PIX_FMT_RGB48;
158 }
159
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 } else if (depth == 4) {
160
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 if (maxval < 256) {
161 14 avctx->pix_fmt = AV_PIX_FMT_RGBA;
162 } else {
163 avctx->pix_fmt = AV_PIX_FMT_RGBA64;
164 }
165 } else {
166 return AVERROR_INVALIDDATA;
167 }
168 84 return 0;
169 } else {
170 av_assert0(0);
171 }
172 11803 pnm_get(s, buf1, sizeof(buf1));
173 11803 w = atoi(buf1);
174 11803 pnm_get(s, buf1, sizeof(buf1));
175 11803 h = atoi(buf1);
176
4/8
✓ Branch 0 taken 11803 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11803 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 11803 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 11803 times.
11803 if(w <= 0 || h <= 0 || av_image_check_size(w, h, 0, avctx) || s->bytestream >= s->bytestream_end)
177 return AVERROR_INVALIDDATA;
178
179 11803 ret = ff_set_dimensions(avctx, w, h);
180
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11803 times.
11803 if (ret < 0)
181 return ret;
182
183
2/4
✓ Branch 0 taken 11803 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 11803 times.
11803 if (avctx->pix_fmt == AV_PIX_FMT_GBRPF32 || avctx->pix_fmt == AV_PIX_FMT_GRAYF32) {
184 pnm_get(s, buf1, sizeof(buf1));
185 if (av_sscanf(buf1, "%f", &s->scale) != 1 || s->scale == 0.0 || !isfinite(s->scale)) {
186 av_log(avctx, AV_LOG_ERROR, "Invalid scale.\n");
187 return AVERROR_INVALIDDATA;
188 }
189 s->endian = s->scale < 0.f;
190 s->scale = fabsf(s->scale);
191 s->maxval = (1ULL << 32) - 1;
192
3/4
✓ Branch 0 taken 11578 times.
✓ Branch 1 taken 225 times.
✓ Branch 2 taken 11578 times.
✗ Branch 3 not taken.
11803 } else if (avctx->pix_fmt != AV_PIX_FMT_MONOWHITE && avctx->pix_fmt != AV_PIX_FMT_MONOBLACK) {
193 11578 pnm_get(s, buf1, sizeof(buf1));
194 11578 s->maxval = atoi(buf1);
195
2/4
✓ Branch 0 taken 11578 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 11578 times.
11578 if (s->maxval <= 0 || s->maxval > UINT16_MAX) {
196 av_log(avctx, AV_LOG_ERROR, "Invalid maxval: %d\n", s->maxval);
197 s->maxval = 255;
198 }
199
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11578 times.
11578 if (s->maxval >= 256) {
200 if (avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
201 avctx->pix_fmt = AV_PIX_FMT_GRAY16;
202 } else if (avctx->pix_fmt == AV_PIX_FMT_RGB24) {
203 avctx->pix_fmt = AV_PIX_FMT_RGB48;
204 } else if (avctx->pix_fmt == AV_PIX_FMT_YUV420P && s->maxval < 65536) {
205 if (s->maxval < 512)
206 avctx->pix_fmt = AV_PIX_FMT_YUV420P9;
207 else if (s->maxval < 1024)
208 avctx->pix_fmt = AV_PIX_FMT_YUV420P10;
209 else
210 avctx->pix_fmt = AV_PIX_FMT_YUV420P16;
211 } else {
212 av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format\n");
213 avctx->pix_fmt = AV_PIX_FMT_NONE;
214 return AVERROR_INVALIDDATA;
215 }
216 }
217 }else
218 225 s->maxval=1;
219
220
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11803 times.
11803 if (!pnm_space(s->bytestream[-1]))
221 return AVERROR_INVALIDDATA;
222
223 /* more check if YUV420 */
224
2/2
✓ Branch 1 taken 10984 times.
✓ Branch 2 taken 819 times.
11803 if ((av_pix_fmt_desc_get(avctx->pix_fmt)->flags & AV_PIX_FMT_FLAG_PLANAR) &&
225
1/2
✓ Branch 0 taken 10984 times.
✗ Branch 1 not taken.
10984 avctx->pix_fmt != AV_PIX_FMT_GBRPF32) {
226
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10984 times.
10984 if ((avctx->width & 1) != 0)
227 return AVERROR_INVALIDDATA;
228 10984 h = (avctx->height * 2);
229
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10984 times.
10984 if ((h % 3) != 0)
230 return AVERROR_INVALIDDATA;
231 10984 h /= 3;
232 10984 avctx->height = h;
233 }
234 11803 return 0;
235 }
236