FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/pnm.c
Date: 2023-09-24 13:02:57
Exec Total Coverage
Lines: 119 157 75.8%
Functions: 3 3 100.0%
Branches: 104 168 61.9%

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