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"
31 static inline int pnm_space(int c)
33 return c == ' ' || c == '\n' || c == '\r' || c == '\t';
36 static void pnm_get(PNMContext *sc, char *str, int buf_size)
40 uint8_t *bs = sc->bytestream;
41 const uint8_t *end = sc->bytestream_end;
43 /* skip spaces and comments */
47 while (c != '\n' && bs < end) {
50 } else if (!pnm_space(c)) {
56 while (bs < end && !pnm_space(c) && (s - str) < buf_size - 1) {
64 int ff_pnm_decode_header(AVCodecContext *avctx, PNMContext * const s)
66 char buf1[32], tuple_type[32];
67 int h, w, depth, maxval;
70 if (s->bytestream_end - s->bytestream < 3 ||
71 s->bytestream[0] != 'P' ||
72 s->bytestream[1] < '1' ||
73 s->bytestream[1] > '7') {
74 s->bytestream += s->bytestream_end > s->bytestream;
75 s->bytestream += s->bytestream_end > s->bytestream;
76 return AVERROR_INVALIDDATA;
78 pnm_get(s, buf1, sizeof(buf1));
81 if (s->type==1 || s->type==4) {
82 avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
83 } else if (s->type==2 || s->type==5) {
84 if (avctx->codec_id == AV_CODEC_ID_PGMYUV)
85 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
87 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
88 } else if (s->type==3 || s->type==6) {
89 avctx->pix_fmt = AV_PIX_FMT_RGB24;
90 } else if (s->type==7) {
97 pnm_get(s, buf1, sizeof(buf1));
98 if (!strcmp(buf1, "WIDTH")) {
99 pnm_get(s, buf1, sizeof(buf1));
100 w = strtol(buf1, NULL, 10);
101 } else if (!strcmp(buf1, "HEIGHT")) {
102 pnm_get(s, buf1, sizeof(buf1));
103 h = strtol(buf1, NULL, 10);
104 } else if (!strcmp(buf1, "DEPTH")) {
105 pnm_get(s, buf1, sizeof(buf1));
106 depth = strtol(buf1, NULL, 10);
107 } else if (!strcmp(buf1, "MAXVAL")) {
108 pnm_get(s, buf1, sizeof(buf1));
109 maxval = strtol(buf1, NULL, 10);
110 } else if (!strcmp(buf1, "TUPLTYPE") ||
111 /* libavcodec used to write invalid files */
112 !strcmp(buf1, "TUPLETYPE")) {
113 pnm_get(s, tuple_type, sizeof(tuple_type));
114 } else if (!strcmp(buf1, "ENDHDR")) {
117 return AVERROR_INVALIDDATA;
120 if (!pnm_space(s->bytestream[-1]))
121 return AVERROR_INVALIDDATA;
123 /* check that all tags are present */
124 if (w <= 0 || h <= 0 || maxval <= 0 || maxval > UINT16_MAX || depth <= 0 || tuple_type[0] == '\0' ||
125 av_image_check_size(w, h, 0, avctx) || s->bytestream >= s->bytestream_end)
126 return AVERROR_INVALIDDATA;
128 ret = ff_set_dimensions(avctx, w, h);
134 avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
135 } else if (maxval < 256) {
136 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
138 avctx->pix_fmt = AV_PIX_FMT_GRAY16;
140 } else if (depth == 2) {
142 avctx->pix_fmt = AV_PIX_FMT_GRAY8A;
144 avctx->pix_fmt = AV_PIX_FMT_YA16;
146 } else if (depth == 3) {
148 avctx->pix_fmt = AV_PIX_FMT_RGB24;
150 avctx->pix_fmt = AV_PIX_FMT_RGB48;
152 } else if (depth == 4) {
154 avctx->pix_fmt = AV_PIX_FMT_RGBA;
156 avctx->pix_fmt = AV_PIX_FMT_RGBA64;
159 return AVERROR_INVALIDDATA;
165 pnm_get(s, buf1, sizeof(buf1));
167 pnm_get(s, buf1, sizeof(buf1));
169 if(w <= 0 || h <= 0 || av_image_check_size(w, h, 0, avctx) || s->bytestream >= s->bytestream_end)
170 return AVERROR_INVALIDDATA;
172 ret = ff_set_dimensions(avctx, w, h);
176 if (avctx->pix_fmt != AV_PIX_FMT_MONOWHITE && avctx->pix_fmt != AV_PIX_FMT_MONOBLACK) {
177 pnm_get(s, buf1, sizeof(buf1));
178 s->maxval = atoi(buf1);
179 if (s->maxval <= 0 || s->maxval > UINT16_MAX) {
180 av_log(avctx, AV_LOG_ERROR, "Invalid maxval: %d\n", s->maxval);
183 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) {
190 avctx->pix_fmt = AV_PIX_FMT_YUV420P9;
191 else if (s->maxval < 1024)
192 avctx->pix_fmt = AV_PIX_FMT_YUV420P10;
194 avctx->pix_fmt = AV_PIX_FMT_YUV420P16;
196 av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format\n");
197 avctx->pix_fmt = AV_PIX_FMT_NONE;
198 return AVERROR_INVALIDDATA;
204 if (!pnm_space(s->bytestream[-1]))
205 return AVERROR_INVALIDDATA;
207 /* more check if YUV420 */
208 if (av_pix_fmt_desc_get(avctx->pix_fmt)->flags & AV_PIX_FMT_FLAG_PLANAR) {
209 if ((avctx->width & 1) != 0)
210 return AVERROR_INVALIDDATA;
211 h = (avctx->height * 2);
213 return AVERROR_INVALIDDATA;