../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
120 / 158
75.9%
Branches:
104 / 168
61.9%
Functions:
3 / 3
100.0%

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