]> git.sesse.net Git - ffmpeg/blob - libavcodec/pnm.c
avcodec/pnm: Check magic bytes directly without pnm_get()
[ffmpeg] / libavcodec / pnm.c
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 static inline int pnm_space(int c)
32 {
33     return c == ' ' || c == '\n' || c == '\r' || c == '\t';
34 }
35
36 static void pnm_get(PNMContext *sc, char *str, int buf_size)
37 {
38     char *s;
39     int c;
40     uint8_t *bs  = sc->bytestream;
41     const uint8_t *end = sc->bytestream_end;
42
43     /* skip spaces and comments */
44     while (bs < end) {
45         c = *bs++;
46         if (c == '#')  {
47             while (c != '\n' && bs < end) {
48                 c = *bs++;
49             }
50         } else if (!pnm_space(c)) {
51             break;
52         }
53     }
54
55     s = str;
56     while (bs < end && !pnm_space(c) && (s - str) < buf_size - 1) {
57         *s++ = c;
58         c = *bs++;
59     }
60     *s = '\0';
61     while (bs < end && !pnm_space(c))
62         c = *bs++;
63     sc->bytestream = bs;
64 }
65
66 int ff_pnm_decode_header(AVCodecContext *avctx, PNMContext * const s)
67 {
68     char buf1[32], tuple_type[32];
69     int h, w, depth, maxval;
70     int ret;
71
72     if (s->bytestream_end - s->bytestream < 3 ||
73         s->bytestream[0] != 'P' ||
74         s->bytestream[1] < '1'  ||
75         s->bytestream[1] > '7') {
76         s->bytestream += s->bytestream_end > s->bytestream;
77         s->bytestream += s->bytestream_end > s->bytestream;
78         return AVERROR_INVALIDDATA;
79     }
80     pnm_get(s, buf1, sizeof(buf1));
81     s->type= buf1[1]-'0';
82
83     if (s->type==1 || s->type==4) {
84         avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
85     } else if (s->type==2 || s->type==5) {
86         if (avctx->codec_id == AV_CODEC_ID_PGMYUV)
87             avctx->pix_fmt = AV_PIX_FMT_YUV420P;
88         else
89             avctx->pix_fmt = AV_PIX_FMT_GRAY8;
90     } else if (s->type==3 || s->type==6) {
91         avctx->pix_fmt = AV_PIX_FMT_RGB24;
92     } else if (s->type==7) {
93         w      = -1;
94         h      = -1;
95         maxval = -1;
96         depth  = -1;
97         tuple_type[0] = '\0';
98         for (;;) {
99             pnm_get(s, buf1, sizeof(buf1));
100             if (!strcmp(buf1, "WIDTH")) {
101                 pnm_get(s, buf1, sizeof(buf1));
102                 w = strtol(buf1, NULL, 10);
103             } else if (!strcmp(buf1, "HEIGHT")) {
104                 pnm_get(s, buf1, sizeof(buf1));
105                 h = strtol(buf1, NULL, 10);
106             } else if (!strcmp(buf1, "DEPTH")) {
107                 pnm_get(s, buf1, sizeof(buf1));
108                 depth = strtol(buf1, NULL, 10);
109             } else if (!strcmp(buf1, "MAXVAL")) {
110                 pnm_get(s, buf1, sizeof(buf1));
111                 maxval = strtol(buf1, NULL, 10);
112             } else if (!strcmp(buf1, "TUPLTYPE") ||
113                        /* libavcodec used to write invalid files */
114                        !strcmp(buf1, "TUPLETYPE")) {
115                 pnm_get(s, tuple_type, sizeof(tuple_type));
116             } else if (!strcmp(buf1, "ENDHDR")) {
117                 break;
118             } else {
119                 return AVERROR_INVALIDDATA;
120             }
121         }
122         /* check that all tags are present */
123         if (w <= 0 || h <= 0 || maxval <= 0 || maxval > UINT16_MAX || depth <= 0 || tuple_type[0] == '\0' ||
124             av_image_check_size(w, h, 0, avctx) || s->bytestream >= s->bytestream_end)
125             return AVERROR_INVALIDDATA;
126
127         ret = ff_set_dimensions(avctx, w, h);
128         if (ret < 0)
129             return ret;
130         s->maxval     = maxval;
131         if (depth == 1) {
132             if (maxval == 1) {
133                 avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
134             } else if (maxval < 256) {
135                 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
136             } else {
137                 avctx->pix_fmt = AV_PIX_FMT_GRAY16;
138             }
139         } else if (depth == 2) {
140             if (maxval < 256) {
141                 avctx->pix_fmt = AV_PIX_FMT_GRAY8A;
142             } else {
143                 avctx->pix_fmt = AV_PIX_FMT_YA16;
144             }
145         } else if (depth == 3) {
146             if (maxval < 256) {
147                 avctx->pix_fmt = AV_PIX_FMT_RGB24;
148             } else {
149                 avctx->pix_fmt = AV_PIX_FMT_RGB48;
150             }
151         } else if (depth == 4) {
152             if (maxval < 256) {
153                 avctx->pix_fmt = AV_PIX_FMT_RGBA;
154             } else {
155                 avctx->pix_fmt = AV_PIX_FMT_RGBA64;
156             }
157         } else {
158             return AVERROR_INVALIDDATA;
159         }
160         return 0;
161     } else {
162         av_assert0(0);
163     }
164     pnm_get(s, buf1, sizeof(buf1));
165     w = atoi(buf1);
166     pnm_get(s, buf1, sizeof(buf1));
167     h = atoi(buf1);
168     if(w <= 0 || h <= 0 || av_image_check_size(w, h, 0, avctx) || s->bytestream >= s->bytestream_end)
169         return AVERROR_INVALIDDATA;
170
171     ret = ff_set_dimensions(avctx, w, h);
172     if (ret < 0)
173         return ret;
174
175     if (avctx->pix_fmt != AV_PIX_FMT_MONOWHITE && avctx->pix_fmt != AV_PIX_FMT_MONOBLACK) {
176         pnm_get(s, buf1, sizeof(buf1));
177         s->maxval = atoi(buf1);
178         if (s->maxval <= 0 || s->maxval > UINT16_MAX) {
179             av_log(avctx, AV_LOG_ERROR, "Invalid maxval: %d\n", s->maxval);
180             s->maxval = 255;
181         }
182         if (s->maxval >= 256) {
183             if (avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
184                 avctx->pix_fmt = AV_PIX_FMT_GRAY16;
185             } else if (avctx->pix_fmt == AV_PIX_FMT_RGB24) {
186                 avctx->pix_fmt = AV_PIX_FMT_RGB48;
187             } else if (avctx->pix_fmt == AV_PIX_FMT_YUV420P && s->maxval < 65536) {
188                 if (s->maxval < 512)
189                     avctx->pix_fmt = AV_PIX_FMT_YUV420P9;
190                 else if (s->maxval < 1024)
191                     avctx->pix_fmt = AV_PIX_FMT_YUV420P10;
192                 else
193                     avctx->pix_fmt = AV_PIX_FMT_YUV420P16;
194             } else {
195                 av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format\n");
196                 avctx->pix_fmt = AV_PIX_FMT_NONE;
197                 return AVERROR_INVALIDDATA;
198             }
199         }
200     }else
201         s->maxval=1;
202     /* more check if YUV420 */
203     if (av_pix_fmt_desc_get(avctx->pix_fmt)->flags & AV_PIX_FMT_FLAG_PLANAR) {
204         if ((avctx->width & 1) != 0)
205             return AVERROR_INVALIDDATA;
206         h = (avctx->height * 2);
207         if ((h % 3) != 0)
208             return AVERROR_INVALIDDATA;
209         h /= 3;
210         avctx->height = h;
211     }
212     return 0;
213 }