FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/pnmdec.c
Date: 2021-09-22 05:58:22
Exec Total Coverage
Lines: 104 230 45.2%
Branches: 41 124 33.1%

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 "avcodec.h"
23 #include "internal.h"
24 #include "put_bits.h"
25 #include "pnm.h"
26
27 6428848 static void samplecpy(uint8_t *dst, const uint8_t *src, int n, int maxval)
28 {
29
2/2
✓ Branch 0 taken 6420784 times.
✓ Branch 1 taken 8064 times.
6428848 if (maxval <= 255) {
30 6420784 memcpy(dst, src, n);
31 } else {
32 int i;
33
2/2
✓ Branch 0 taken 5677056 times.
✓ Branch 1 taken 8064 times.
5685120 for (i=0; i<n/2; i++) {
34 5677056 ((uint16_t *)dst)[i] = AV_RB16(src+2*i);
35 }
36 }
37 6428848 }
38
39 11293 static int pnm_decode_frame(AVCodecContext *avctx, void *data,
40 int *got_frame, AVPacket *avpkt)
41 {
42 11293 const uint8_t *buf = avpkt->data;
43 11293 int buf_size = avpkt->size;
44 11293 PNMContext * const s = avctx->priv_data;
45 11293 AVFrame * const p = data;
46 11293 int i, j, k, n, linesize, h, upgrade = 0, is_mono = 0;
47 unsigned char *ptr;
48 int components, sample_len, ret;
49 float scale;
50
51 11293 s->bytestream_start =
52 11293 s->bytestream = (uint8_t *)buf;
53 11293 s->bytestream_end = (uint8_t *)buf + buf_size;
54
55
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11293 times.
11293 if ((ret = ff_pnm_decode_header(avctx, s)) < 0)
56 return ret;
57
58
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11293 times.
11293 if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
59 return ret;
60 11293 p->pict_type = AV_PICTURE_TYPE_I;
61 11293 p->key_frame = 1;
62 11293 avctx->bits_per_raw_sample = av_log2(s->maxval) + 1;
63
64
7/14
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 14 times.
✓ Branch 4 taken 55 times.
✓ Branch 5 taken 173 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 14 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 39 times.
✓ Branch 10 taken 10984 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
11293 switch (avctx->pix_fmt) {
65 default:
66 return AVERROR(EINVAL);
67 case AV_PIX_FMT_RGBA64:
68 n = avctx->width * 8;
69 components=4;
70 sample_len=16;
71 if (s->maxval < 65535)
72 upgrade = 2;
73 goto do_read;
74 14 case AV_PIX_FMT_RGB48:
75 14 n = avctx->width * 6;
76 14 components=3;
77 14 sample_len=16;
78
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (s->maxval < 65535)
79 upgrade = 2;
80 14 goto do_read;
81 14 case AV_PIX_FMT_RGBA:
82 14 n = avctx->width * 4;
83 14 components=4;
84 14 sample_len=8;
85 14 goto do_read;
86 55 case AV_PIX_FMT_RGB24:
87 55 n = avctx->width * 3;
88 55 components=3;
89 55 sample_len=8;
90
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 55 times.
55 if (s->maxval < 255)
91 upgrade = 1;
92 55 goto do_read;
93 173 case AV_PIX_FMT_GRAY8:
94 173 n = avctx->width;
95 173 components=1;
96 173 sample_len=8;
97
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 173 times.
173 if (s->maxval < 255)
98 upgrade = 1;
99 173 goto do_read;
100 case AV_PIX_FMT_GRAY8A:
101 n = avctx->width * 2;
102 components=2;
103 sample_len=8;
104 goto do_read;
105 14 case AV_PIX_FMT_GRAY16:
106 14 n = avctx->width * 2;
107 14 components=1;
108 14 sample_len=16;
109
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (s->maxval < 65535)
110 upgrade = 2;
111 14 goto do_read;
112 case AV_PIX_FMT_YA16:
113 n = avctx->width * 4;
114 components=2;
115 sample_len=16;
116 if (s->maxval < 65535)
117 upgrade = 2;
118 goto do_read;
119 39 case AV_PIX_FMT_MONOWHITE:
120 case AV_PIX_FMT_MONOBLACK:
121 39 n = (avctx->width + 7) >> 3;
122 39 components=1;
123 39 sample_len=1;
124 39 is_mono = 1;
125 309 do_read:
126 309 ptr = p->data[0];
127 309 linesize = p->linesize[0];
128
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 309 times.
309 if (n * avctx->height > s->bytestream_end - s->bytestream)
129 return AVERROR_INVALIDDATA;
130
5/6
✓ Branch 0 taken 309 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 39 times.
✓ Branch 3 taken 270 times.
✓ Branch 4 taken 14 times.
✓ Branch 5 taken 25 times.
309 if(s->type < 4 || (is_mono && s->type==7)){
131
2/2
✓ Branch 0 taken 4032 times.
✓ Branch 1 taken 14 times.
4046 for (i=0; i<avctx->height; i++) {
132 PutBitContext pb;
133 4032 init_put_bits(&pb, ptr, linesize);
134
2/2
✓ Branch 0 taken 1419264 times.
✓ Branch 1 taken 4032 times.
1423296 for(j=0; j<avctx->width * components; j++){
135 1419264 unsigned int c=0;
136 1419264 unsigned v=0;
137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1419264 times.
1419264 if(s->type < 4)
138 while(s->bytestream < s->bytestream_end && (*s->bytestream < '0' || *s->bytestream > '9' ))
139 s->bytestream++;
140
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1419264 times.
1419264 if(s->bytestream >= s->bytestream_end)
141 return AVERROR_INVALIDDATA;
142
1/2
✓ Branch 0 taken 1419264 times.
✗ Branch 1 not taken.
1419264 if (is_mono) {
143 /* read a single digit */
144 1419264 v = (*s->bytestream++)&1;
145 } else {
146 /* read a sequence of digits */
147 for (k = 0; k < 6 && c <= 9; k += 1) {
148 v = 10*v + c;
149 c = (*s->bytestream++) - '0';
150 }
151 if (v > s->maxval) {
152 av_log(avctx, AV_LOG_ERROR, "value %d larger than maxval %d\n", v, s->maxval);
153 return AVERROR_INVALIDDATA;
154 }
155 }
156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1419264 times.
1419264 if (sample_len == 16) {
157 ((uint16_t*)ptr)[j] = (((1<<sample_len)-1)*v + (s->maxval>>1))/s->maxval;
158 } else
159 1419264 put_bits(&pb, sample_len, (((1<<sample_len)-1)*v + (s->maxval>>1))/s->maxval);
160 }
161
1/2
✓ Branch 0 taken 4032 times.
✗ Branch 1 not taken.
4032 if (sample_len != 16)
162 4032 flush_put_bits(&pb);
163 4032 ptr+= linesize;
164 }
165 }else{
166
2/2
✓ Branch 0 taken 102064 times.
✓ Branch 1 taken 295 times.
102359 for (i = 0; i < avctx->height; i++) {
167
1/2
✓ Branch 0 taken 102064 times.
✗ Branch 1 not taken.
102064 if (!upgrade)
168 102064 samplecpy(ptr, s->bytestream, n, s->maxval);
169 else if (upgrade == 1) {
170 unsigned int j, f = (255 * 128 + s->maxval / 2) / s->maxval;
171 for (j = 0; j < n; j++)
172 ptr[j] = (s->bytestream[j] * f + 64) >> 7;
173 } else if (upgrade == 2) {
174 unsigned int j, v, f = (65535 * 32768 + s->maxval / 2) / s->maxval;
175 for (j = 0; j < n / 2; j++) {
176 v = AV_RB16(s->bytestream + 2*j);
177 ((uint16_t *)ptr)[j] = (v * f + 16384) >> 15;
178 }
179 }
180 102064 s->bytestream += n;
181 102064 ptr += linesize;
182 }
183 }
184 309 break;
185 10984 case AV_PIX_FMT_YUV420P:
186 case AV_PIX_FMT_YUV420P9:
187 case AV_PIX_FMT_YUV420P10:
188 {
189 unsigned char *ptr1, *ptr2;
190
191 10984 n = avctx->width;
192 10984 ptr = p->data[0];
193 10984 linesize = p->linesize[0];
194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10984 times.
10984 if (s->maxval >= 256)
195 n *= 2;
196
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10984 times.
10984 if (n * avctx->height * 3 / 2 > s->bytestream_end - s->bytestream)
197 return AVERROR_INVALIDDATA;
198
2/2
✓ Branch 0 taken 3163392 times.
✓ Branch 1 taken 10984 times.
3174376 for (i = 0; i < avctx->height; i++) {
199 3163392 samplecpy(ptr, s->bytestream, n, s->maxval);
200 3163392 s->bytestream += n;
201 3163392 ptr += linesize;
202 }
203 10984 ptr1 = p->data[1];
204 10984 ptr2 = p->data[2];
205 10984 n >>= 1;
206 10984 h = avctx->height >> 1;
207
2/2
✓ Branch 0 taken 1581696 times.
✓ Branch 1 taken 10984 times.
1592680 for (i = 0; i < h; i++) {
208 1581696 samplecpy(ptr1, s->bytestream, n, s->maxval);
209 1581696 s->bytestream += n;
210 1581696 samplecpy(ptr2, s->bytestream, n, s->maxval);
211 1581696 s->bytestream += n;
212 1581696 ptr1 += p->linesize[1];
213 1581696 ptr2 += p->linesize[2];
214 }
215 }
216 10984 break;
217 case AV_PIX_FMT_YUV420P16:
218 {
219 uint16_t *ptr1, *ptr2;
220 const int f = (65535 * 32768 + s->maxval / 2) / s->maxval;
221 unsigned int j, v;
222
223 n = avctx->width * 2;
224 ptr = p->data[0];
225 linesize = p->linesize[0];
226 if (n * avctx->height * 3 / 2 > s->bytestream_end - s->bytestream)
227 return AVERROR_INVALIDDATA;
228 for (i = 0; i < avctx->height; i++) {
229 for (j = 0; j < n / 2; j++) {
230 v = AV_RB16(s->bytestream + 2*j);
231 ((uint16_t *)ptr)[j] = (v * f + 16384) >> 15;
232 }
233 s->bytestream += n;
234 ptr += linesize;
235 }
236 ptr1 = (uint16_t*)p->data[1];
237 ptr2 = (uint16_t*)p->data[2];
238 n >>= 1;
239 h = avctx->height >> 1;
240 for (i = 0; i < h; i++) {
241 for (j = 0; j < n / 2; j++) {
242 v = AV_RB16(s->bytestream + 2*j);
243 ptr1[j] = (v * f + 16384) >> 15;
244 }
245 s->bytestream += n;
246
247 for (j = 0; j < n / 2; j++) {
248 v = AV_RB16(s->bytestream + 2*j);
249 ptr2[j] = (v * f + 16384) >> 15;
250 }
251 s->bytestream += n;
252
253 ptr1 += p->linesize[1] / 2;
254 ptr2 += p->linesize[2] / 2;
255 }
256 }
257 break;
258 case AV_PIX_FMT_GBRPF32:
259 if (avctx->width * avctx->height * 12 > s->bytestream_end - s->bytestream)
260 return AVERROR_INVALIDDATA;
261 scale = 1.f / s->scale;
262 if (s->endian) {
263 float *r, *g, *b;
264
265 r = (float *)p->data[2];
266 g = (float *)p->data[0];
267 b = (float *)p->data[1];
268 for (int i = 0; i < avctx->height; i++) {
269 for (int j = 0; j < avctx->width; j++) {
270 r[j] = av_int2float(AV_RL32(s->bytestream+0)) * scale;
271 g[j] = av_int2float(AV_RL32(s->bytestream+4)) * scale;
272 b[j] = av_int2float(AV_RL32(s->bytestream+8)) * scale;
273 s->bytestream += 12;
274 }
275
276 r += p->linesize[2] / 4;
277 g += p->linesize[0] / 4;
278 b += p->linesize[1] / 4;
279 }
280 } else {
281 float *r, *g, *b;
282
283 r = (float *)p->data[2];
284 g = (float *)p->data[0];
285 b = (float *)p->data[1];
286 for (int i = 0; i < avctx->height; i++) {
287 for (int j = 0; j < avctx->width; j++) {
288 r[j] = av_int2float(AV_RB32(s->bytestream+0)) * scale;
289 g[j] = av_int2float(AV_RB32(s->bytestream+4)) * scale;
290 b[j] = av_int2float(AV_RB32(s->bytestream+8)) * scale;
291 s->bytestream += 12;
292 }
293
294 r += p->linesize[2] / 4;
295 g += p->linesize[0] / 4;
296 b += p->linesize[1] / 4;
297 }
298 }
299 break;
300 case AV_PIX_FMT_GRAYF32:
301 if (avctx->width * avctx->height * 4 > s->bytestream_end - s->bytestream)
302 return AVERROR_INVALIDDATA;
303 scale = 1.f / s->scale;
304 if (s->endian) {
305 float *g = (float *)p->data[0];
306 for (int i = 0; i < avctx->height; i++) {
307 for (int j = 0; j < avctx->width; j++) {
308 g[j] = av_int2float(AV_RL32(s->bytestream)) * scale;
309 s->bytestream += 4;
310 }
311 g += p->linesize[0] / 4;
312 }
313 } else {
314 float *g = (float *)p->data[0];
315 for (int i = 0; i < avctx->height; i++) {
316 for (int j = 0; j < avctx->width; j++) {
317 g[j] = av_int2float(AV_RB32(s->bytestream)) * scale;
318 s->bytestream += 4;
319 }
320 g += p->linesize[0] / 4;
321 }
322 }
323 break;
324 }
325 11293 *got_frame = 1;
326
327 11293 return s->bytestream - s->bytestream_start;
328 }
329
330
331 #if CONFIG_PGM_DECODER
332 const AVCodec ff_pgm_decoder = {
333 .name = "pgm",
334 .long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
335 .type = AVMEDIA_TYPE_VIDEO,
336 .id = AV_CODEC_ID_PGM,
337 .priv_data_size = sizeof(PNMContext),
338 .decode = pnm_decode_frame,
339 .capabilities = AV_CODEC_CAP_DR1,
340 };
341 #endif
342
343 #if CONFIG_PGMYUV_DECODER
344 const AVCodec ff_pgmyuv_decoder = {
345 .name = "pgmyuv",
346 .long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
347 .type = AVMEDIA_TYPE_VIDEO,
348 .id = AV_CODEC_ID_PGMYUV,
349 .priv_data_size = sizeof(PNMContext),
350 .decode = pnm_decode_frame,
351 .capabilities = AV_CODEC_CAP_DR1,
352 };
353 #endif
354
355 #if CONFIG_PPM_DECODER
356 const AVCodec ff_ppm_decoder = {
357 .name = "ppm",
358 .long_name = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
359 .type = AVMEDIA_TYPE_VIDEO,
360 .id = AV_CODEC_ID_PPM,
361 .priv_data_size = sizeof(PNMContext),
362 .decode = pnm_decode_frame,
363 .capabilities = AV_CODEC_CAP_DR1,
364 };
365 #endif
366
367 #if CONFIG_PBM_DECODER
368 const AVCodec ff_pbm_decoder = {
369 .name = "pbm",
370 .long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
371 .type = AVMEDIA_TYPE_VIDEO,
372 .id = AV_CODEC_ID_PBM,
373 .priv_data_size = sizeof(PNMContext),
374 .decode = pnm_decode_frame,
375 .capabilities = AV_CODEC_CAP_DR1,
376 };
377 #endif
378
379 #if CONFIG_PAM_DECODER
380 const AVCodec ff_pam_decoder = {
381 .name = "pam",
382 .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
383 .type = AVMEDIA_TYPE_VIDEO,
384 .id = AV_CODEC_ID_PAM,
385 .priv_data_size = sizeof(PNMContext),
386 .decode = pnm_decode_frame,
387 .capabilities = AV_CODEC_CAP_DR1,
388 };
389 #endif
390
391 #if CONFIG_PFM_DECODER
392 const AVCodec ff_pfm_decoder = {
393 .name = "pfm",
394 .long_name = NULL_IF_CONFIG_SMALL("PFM (Portable FloatMap) image"),
395 .type = AVMEDIA_TYPE_VIDEO,
396 .id = AV_CODEC_ID_PFM,
397 .priv_data_size = sizeof(PNMContext),
398 .decode = pnm_decode_frame,
399 .capabilities = AV_CODEC_CAP_DR1,
400 };
401 #endif
402