GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/pnm.c Lines: 103 133 77.4 %
Date: 2020-01-28 04:56:05 Branches: 85 140 60.7 %

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 "avcodec.h"
28
#include "internal.h"
29
#include "pnm.h"
30
31
240599
static inline int pnm_space(int c)
32
{
33


240599
    return c == ' ' || c == '\n' || c == '\r' || c == '\t';
34
}
35
36
47767
static void pnm_get(PNMContext *sc, char *str, int buf_size)
37
{
38
    char *s;
39
    int c;
40
47767
    uint8_t *bs  = sc->bytestream;
41
47767
    const uint8_t *end = sc->bytestream_end;
42
43
    /* skip spaces and comments */
44
47769
    while (bs < end) {
45
47769
        c = *bs++;
46
47769
        if (c == '#')  {
47

90
            while (c != '\n' && bs < end) {
48
88
                c = *bs++;
49
            }
50
47767
        } else if (!pnm_space(c)) {
51
47767
            break;
52
        }
53
    }
54
55
47767
    s = str;
56

181002
    while (bs < end && !pnm_space(c) && (s - str) < buf_size - 1) {
57
133235
        *s++ = c;
58
133235
        c = *bs++;
59
    }
60
47767
    *s = '\0';
61
47767
    sc->bytestream = bs;
62
47767
}
63
64
11839
int ff_pnm_decode_header(AVCodecContext *avctx, PNMContext * const s)
65
{
66
    char buf1[32], tuple_type[32];
67
    int h, w, depth, maxval;
68
    int ret;
69
70
11839
    if (s->bytestream_end - s->bytestream < 3 ||
71
11830
        s->bytestream[0] != 'P' ||
72
11830
        s->bytestream[1] < '1'  ||
73
11830
        s->bytestream[1] > '7') {
74
9
        s->bytestream += s->bytestream_end > s->bytestream;
75
9
        s->bytestream += s->bytestream_end > s->bytestream;
76
9
        return AVERROR_INVALIDDATA;
77
    }
78
11830
    pnm_get(s, buf1, sizeof(buf1));
79
11830
    s->type= buf1[1]-'0';
80
81

11830
    if (s->type==1 || s->type==4) {
82
225
        avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
83

11605
    } else if (s->type==2 || s->type==5) {
84
11286
        if (avctx->codec_id == AV_CODEC_ID_PGMYUV)
85
10927
            avctx->pix_fmt = AV_PIX_FMT_YUV420P;
86
        else
87
359
            avctx->pix_fmt = AV_PIX_FMT_GRAY8;
88

319
    } else if (s->type==3 || s->type==6) {
89
235
        avctx->pix_fmt = AV_PIX_FMT_RGB24;
90
84
    } else if (s->type==7) {
91
84
        w      = -1;
92
84
        h      = -1;
93
84
        maxval = -1;
94
84
        depth  = -1;
95
84
        tuple_type[0] = '\0';
96
        for (;;) {
97
504
            pnm_get(s, buf1, sizeof(buf1));
98
504
            if (!strcmp(buf1, "WIDTH")) {
99
84
                pnm_get(s, buf1, sizeof(buf1));
100
84
                w = strtol(buf1, NULL, 10);
101
420
            } else if (!strcmp(buf1, "HEIGHT")) {
102
84
                pnm_get(s, buf1, sizeof(buf1));
103
84
                h = strtol(buf1, NULL, 10);
104
336
            } else if (!strcmp(buf1, "DEPTH")) {
105
84
                pnm_get(s, buf1, sizeof(buf1));
106
84
                depth = strtol(buf1, NULL, 10);
107
252
            } else if (!strcmp(buf1, "MAXVAL")) {
108
84
                pnm_get(s, buf1, sizeof(buf1));
109
84
                maxval = strtol(buf1, NULL, 10);
110
168
            } else if (!strcmp(buf1, "TUPLTYPE") ||
111
                       /* libavcodec used to write invalid files */
112
84
                       !strcmp(buf1, "TUPLETYPE")) {
113
84
                pnm_get(s, tuple_type, sizeof(tuple_type));
114
84
            } else if (!strcmp(buf1, "ENDHDR")) {
115
84
                break;
116
            } else {
117
                return AVERROR_INVALIDDATA;
118
            }
119
        }
120
84
        if (!pnm_space(s->bytestream[-1]))
121
            return AVERROR_INVALIDDATA;
122
123
        /* check that all tags are present */
124



168
        if (w <= 0 || h <= 0 || maxval <= 0 || maxval > UINT16_MAX || depth <= 0 || tuple_type[0] == '\0' ||
125
168
            av_image_check_size(w, h, 0, avctx) || s->bytestream >= s->bytestream_end)
126
            return AVERROR_INVALIDDATA;
127
128
84
        ret = ff_set_dimensions(avctx, w, h);
129
84
        if (ret < 0)
130
            return ret;
131
84
        s->maxval     = maxval;
132
84
        if (depth == 1) {
133
42
            if (maxval == 1) {
134
14
                avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
135
28
            } else if (maxval < 256) {
136
14
                avctx->pix_fmt = AV_PIX_FMT_GRAY8;
137
            } else {
138
14
                avctx->pix_fmt = AV_PIX_FMT_GRAY16;
139
            }
140
42
        } else if (depth == 2) {
141
            if (maxval < 256) {
142
                avctx->pix_fmt = AV_PIX_FMT_GRAY8A;
143
            } else {
144
                avctx->pix_fmt = AV_PIX_FMT_YA16;
145
            }
146
42
        } else if (depth == 3) {
147
28
            if (maxval < 256) {
148
14
                avctx->pix_fmt = AV_PIX_FMT_RGB24;
149
            } else {
150
14
                avctx->pix_fmt = AV_PIX_FMT_RGB48;
151
            }
152
14
        } else if (depth == 4) {
153
14
            if (maxval < 256) {
154
14
                avctx->pix_fmt = AV_PIX_FMT_RGBA;
155
            } else {
156
                avctx->pix_fmt = AV_PIX_FMT_RGBA64;
157
            }
158
        } else {
159
            return AVERROR_INVALIDDATA;
160
        }
161
84
        return 0;
162
    } else {
163
        av_assert0(0);
164
    }
165
11746
    pnm_get(s, buf1, sizeof(buf1));
166
11746
    w = atoi(buf1);
167
11746
    pnm_get(s, buf1, sizeof(buf1));
168
11746
    h = atoi(buf1);
169


11746
    if(w <= 0 || h <= 0 || av_image_check_size(w, h, 0, avctx) || s->bytestream >= s->bytestream_end)
170
        return AVERROR_INVALIDDATA;
171
172
11746
    ret = ff_set_dimensions(avctx, w, h);
173
11746
    if (ret < 0)
174
        return ret;
175
176

11746
    if (avctx->pix_fmt != AV_PIX_FMT_MONOWHITE && avctx->pix_fmt != AV_PIX_FMT_MONOBLACK) {
177
11521
        pnm_get(s, buf1, sizeof(buf1));
178
11521
        s->maxval = atoi(buf1);
179

11521
        if (s->maxval <= 0 || s->maxval > UINT16_MAX) {
180
            av_log(avctx, AV_LOG_ERROR, "Invalid maxval: %d\n", s->maxval);
181
            s->maxval = 255;
182
        }
183
11521
        if (s->maxval >= 256) {
184
            if (avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
185
                avctx->pix_fmt = AV_PIX_FMT_GRAY16;
186
            } else if (avctx->pix_fmt == AV_PIX_FMT_RGB24) {
187
                avctx->pix_fmt = AV_PIX_FMT_RGB48;
188
            } else if (avctx->pix_fmt == AV_PIX_FMT_YUV420P && s->maxval < 65536) {
189
                if (s->maxval < 512)
190
                    avctx->pix_fmt = AV_PIX_FMT_YUV420P9;
191
                else if (s->maxval < 1024)
192
                    avctx->pix_fmt = AV_PIX_FMT_YUV420P10;
193
                else
194
                    avctx->pix_fmt = AV_PIX_FMT_YUV420P16;
195
            } else {
196
                av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format\n");
197
                avctx->pix_fmt = AV_PIX_FMT_NONE;
198
                return AVERROR_INVALIDDATA;
199
            }
200
        }
201
    }else
202
225
        s->maxval=1;
203
204
11746
    if (!pnm_space(s->bytestream[-1]))
205
        return AVERROR_INVALIDDATA;
206
207
    /* more check if YUV420 */
208
11746
    if (av_pix_fmt_desc_get(avctx->pix_fmt)->flags & AV_PIX_FMT_FLAG_PLANAR) {
209
10927
        if ((avctx->width & 1) != 0)
210
            return AVERROR_INVALIDDATA;
211
10927
        h = (avctx->height * 2);
212
10927
        if ((h % 3) != 0)
213
            return AVERROR_INVALIDDATA;
214
10927
        h /= 3;
215
10927
        avctx->height = h;
216
    }
217
11746
    return 0;
218
}