3 * Copyright (c) 2002, 2003 Fabrice Bellard
5 * This file is part of FFmpeg.
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.
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.
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
25 #include "libavutil/avassert.h"
26 #include "libavutil/imgutils.h"
27 #include "libavutil/avstring.h"
32 static inline int pnm_space(int c)
34 return c == ' ' || c == '\n' || c == '\r' || c == '\t';
37 static void pnm_get(PNMContext *sc, char *str, int buf_size)
41 uint8_t *bs = sc->bytestream;
42 const uint8_t *end = sc->bytestream_end;
44 /* skip spaces and comments */
48 while (c != '\n' && bs < end) {
51 } else if (!pnm_space(c)) {
57 while (bs < end && !pnm_space(c) && (s - str) < buf_size - 1) {
65 int ff_pnm_decode_header(AVCodecContext *avctx, PNMContext * const s)
67 char buf1[32], tuple_type[32];
68 int h, w, depth, maxval;
71 if (s->bytestream_end - s->bytestream < 3 ||
72 s->bytestream[0] != 'P' ||
73 (s->bytestream[1] < '1' ||
74 s->bytestream[1] > '7' &&
75 s->bytestream[1] != 'f' &&
76 s->bytestream[1] != 'F')) {
77 s->bytestream += s->bytestream_end > s->bytestream;
78 s->bytestream += s->bytestream_end > s->bytestream;
79 return AVERROR_INVALIDDATA;
81 pnm_get(s, buf1, sizeof(buf1));
85 avctx->pix_fmt = AV_PIX_FMT_GBRPF32;
86 } else if (buf1[1] == 'f') {
87 avctx->pix_fmt = AV_PIX_FMT_GRAYF32;
88 } else if (s->type==1 || s->type==4) {
89 avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
90 } else if (s->type==2 || s->type==5) {
91 if (avctx->codec_id == AV_CODEC_ID_PGMYUV)
92 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
94 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
95 } else if (s->type==3 || s->type==6) {
96 avctx->pix_fmt = AV_PIX_FMT_RGB24;
97 } else if (s->type==7) {
102 tuple_type[0] = '\0';
104 pnm_get(s, buf1, sizeof(buf1));
105 if (!strcmp(buf1, "WIDTH")) {
106 pnm_get(s, buf1, sizeof(buf1));
107 w = strtol(buf1, NULL, 10);
108 } else if (!strcmp(buf1, "HEIGHT")) {
109 pnm_get(s, buf1, sizeof(buf1));
110 h = strtol(buf1, NULL, 10);
111 } else if (!strcmp(buf1, "DEPTH")) {
112 pnm_get(s, buf1, sizeof(buf1));
113 depth = strtol(buf1, NULL, 10);
114 } else if (!strcmp(buf1, "MAXVAL")) {
115 pnm_get(s, buf1, sizeof(buf1));
116 maxval = strtol(buf1, NULL, 10);
117 } else if (!strcmp(buf1, "TUPLTYPE") ||
118 /* libavcodec used to write invalid files */
119 !strcmp(buf1, "TUPLETYPE")) {
120 pnm_get(s, tuple_type, sizeof(tuple_type));
121 } else if (!strcmp(buf1, "ENDHDR")) {
124 return AVERROR_INVALIDDATA;
127 if (!pnm_space(s->bytestream[-1]))
128 return AVERROR_INVALIDDATA;
130 /* check that all tags are present */
131 if (w <= 0 || h <= 0 || maxval <= 0 || maxval > UINT16_MAX || depth <= 0 || tuple_type[0] == '\0' ||
132 av_image_check_size(w, h, 0, avctx) || s->bytestream >= s->bytestream_end)
133 return AVERROR_INVALIDDATA;
135 ret = ff_set_dimensions(avctx, w, h);
141 avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
142 } else if (maxval < 256) {
143 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
145 avctx->pix_fmt = AV_PIX_FMT_GRAY16;
147 } else if (depth == 2) {
149 avctx->pix_fmt = AV_PIX_FMT_GRAY8A;
151 avctx->pix_fmt = AV_PIX_FMT_YA16;
153 } else if (depth == 3) {
155 avctx->pix_fmt = AV_PIX_FMT_RGB24;
157 avctx->pix_fmt = AV_PIX_FMT_RGB48;
159 } else if (depth == 4) {
161 avctx->pix_fmt = AV_PIX_FMT_RGBA;
163 avctx->pix_fmt = AV_PIX_FMT_RGBA64;
166 return AVERROR_INVALIDDATA;
172 pnm_get(s, buf1, sizeof(buf1));
174 pnm_get(s, buf1, sizeof(buf1));
176 if(w <= 0 || h <= 0 || av_image_check_size(w, h, 0, avctx) || s->bytestream >= s->bytestream_end)
177 return AVERROR_INVALIDDATA;
179 ret = ff_set_dimensions(avctx, w, h);
183 if (avctx->pix_fmt == AV_PIX_FMT_GBRPF32 || avctx->pix_fmt == AV_PIX_FMT_GRAYF32) {
184 pnm_get(s, buf1, sizeof(buf1));
185 if (av_sscanf(buf1, "%f", &s->scale) != 1 || s->scale == 0.0 || !isfinite(s->scale)) {
186 av_log(avctx, AV_LOG_ERROR, "Invalid scale.\n");
187 return AVERROR_INVALIDDATA;
189 s->endian = s->scale < 0.f;
190 s->scale = fabsf(s->scale);
191 s->maxval = (1ULL << 32) - 1;
192 } else if (avctx->pix_fmt != AV_PIX_FMT_MONOWHITE && avctx->pix_fmt != AV_PIX_FMT_MONOBLACK) {
193 pnm_get(s, buf1, sizeof(buf1));
194 s->maxval = atoi(buf1);
195 if (s->maxval <= 0 || s->maxval > UINT16_MAX) {
196 av_log(avctx, AV_LOG_ERROR, "Invalid maxval: %d\n", s->maxval);
199 if (s->maxval >= 256) {
200 if (avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
201 avctx->pix_fmt = AV_PIX_FMT_GRAY16;
202 } else if (avctx->pix_fmt == AV_PIX_FMT_RGB24) {
203 avctx->pix_fmt = AV_PIX_FMT_RGB48;
204 } else if (avctx->pix_fmt == AV_PIX_FMT_YUV420P && s->maxval < 65536) {
206 avctx->pix_fmt = AV_PIX_FMT_YUV420P9;
207 else if (s->maxval < 1024)
208 avctx->pix_fmt = AV_PIX_FMT_YUV420P10;
210 avctx->pix_fmt = AV_PIX_FMT_YUV420P16;
212 av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format\n");
213 avctx->pix_fmt = AV_PIX_FMT_NONE;
214 return AVERROR_INVALIDDATA;
220 if (!pnm_space(s->bytestream[-1]))
221 return AVERROR_INVALIDDATA;
223 /* more check if YUV420 */
224 if ((av_pix_fmt_desc_get(avctx->pix_fmt)->flags & AV_PIX_FMT_FLAG_PLANAR) &&
225 avctx->pix_fmt != AV_PIX_FMT_GBRPF32) {
226 if ((avctx->width & 1) != 0)
227 return AVERROR_INVALIDDATA;
228 h = (avctx->height * 2);
230 return AVERROR_INVALIDDATA;