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/imgutils.h"
30 static inline int pnm_space(int c)
32 return c == ' ' || c == '\n' || c == '\r' || c == '\t';
35 static void pnm_get(PNMContext *sc, char *str, int buf_size)
40 /* skip spaces and comments */
41 while (sc->bytestream < sc->bytestream_end) {
42 c = *sc->bytestream++;
44 while (c != '\n' && sc->bytestream < sc->bytestream_end) {
45 c = *sc->bytestream++;
47 } else if (!pnm_space(c)) {
53 while (sc->bytestream < sc->bytestream_end && !pnm_space(c)) {
54 if ((s - str) < buf_size - 1)
56 c = *sc->bytestream++;
61 int ff_pnm_decode_header(AVCodecContext *avctx, PNMContext * const s)
63 char buf1[32], tuple_type[32];
64 int h, w, depth, maxval;
67 pnm_get(s, buf1, sizeof(buf1));
69 return AVERROR_INVALIDDATA;
72 if (s->type==1 || s->type==4) {
73 avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
74 } else if (s->type==2 || s->type==5) {
75 if (avctx->codec_id == AV_CODEC_ID_PGMYUV)
76 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
78 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
79 } else if (s->type==3 || s->type==6) {
80 avctx->pix_fmt = AV_PIX_FMT_RGB24;
81 } else if (s->type==7) {
88 pnm_get(s, buf1, sizeof(buf1));
89 if (!strcmp(buf1, "WIDTH")) {
90 pnm_get(s, buf1, sizeof(buf1));
91 w = strtol(buf1, NULL, 10);
92 } else if (!strcmp(buf1, "HEIGHT")) {
93 pnm_get(s, buf1, sizeof(buf1));
94 h = strtol(buf1, NULL, 10);
95 } else if (!strcmp(buf1, "DEPTH")) {
96 pnm_get(s, buf1, sizeof(buf1));
97 depth = strtol(buf1, NULL, 10);
98 } else if (!strcmp(buf1, "MAXVAL")) {
99 pnm_get(s, buf1, sizeof(buf1));
100 maxval = strtol(buf1, NULL, 10);
101 } else if (!strcmp(buf1, "TUPLTYPE") ||
102 /* libavcodec used to write invalid files */
103 !strcmp(buf1, "TUPLETYPE")) {
104 pnm_get(s, tuple_type, sizeof(tuple_type));
105 } else if (!strcmp(buf1, "ENDHDR")) {
108 return AVERROR_INVALIDDATA;
111 /* check that all tags are present */
112 if (w <= 0 || h <= 0 || maxval <= 0 || maxval > UINT16_MAX || depth <= 0 || tuple_type[0] == '\0' ||
113 av_image_check_size(w, h, 0, avctx) || s->bytestream >= s->bytestream_end)
114 return AVERROR_INVALIDDATA;
116 ret = ff_set_dimensions(avctx, w, h);
122 avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
123 } else if (maxval < 256) {
124 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
126 avctx->pix_fmt = AV_PIX_FMT_GRAY16;
128 } else if (depth == 2) {
130 avctx->pix_fmt = AV_PIX_FMT_GRAY8A;
132 avctx->pix_fmt = AV_PIX_FMT_YA16;
134 } else if (depth == 3) {
136 avctx->pix_fmt = AV_PIX_FMT_RGB24;
138 avctx->pix_fmt = AV_PIX_FMT_RGB48;
140 } else if (depth == 4) {
142 avctx->pix_fmt = AV_PIX_FMT_RGBA;
144 avctx->pix_fmt = AV_PIX_FMT_RGBA64;
147 return AVERROR_INVALIDDATA;
151 return AVERROR_INVALIDDATA;
153 pnm_get(s, buf1, sizeof(buf1));
155 pnm_get(s, buf1, sizeof(buf1));
157 if(w <= 0 || h <= 0 || av_image_check_size(w, h, 0, avctx) || s->bytestream >= s->bytestream_end)
158 return AVERROR_INVALIDDATA;
160 ret = ff_set_dimensions(avctx, w, h);
164 if (avctx->pix_fmt != AV_PIX_FMT_MONOWHITE && avctx->pix_fmt != AV_PIX_FMT_MONOBLACK) {
165 pnm_get(s, buf1, sizeof(buf1));
166 s->maxval = atoi(buf1);
167 if (s->maxval <= 0 || s->maxval > UINT16_MAX) {
168 av_log(avctx, AV_LOG_ERROR, "Invalid maxval: %d\n", s->maxval);
171 if (s->maxval >= 256) {
172 if (avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
173 avctx->pix_fmt = AV_PIX_FMT_GRAY16;
174 } else if (avctx->pix_fmt == AV_PIX_FMT_RGB24) {
175 avctx->pix_fmt = AV_PIX_FMT_RGB48;
176 } else if (avctx->pix_fmt == AV_PIX_FMT_YUV420P && s->maxval < 65536) {
178 avctx->pix_fmt = AV_PIX_FMT_YUV420P9;
179 else if (s->maxval < 1024)
180 avctx->pix_fmt = AV_PIX_FMT_YUV420P10;
182 avctx->pix_fmt = AV_PIX_FMT_YUV420P16;
184 av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format\n");
185 avctx->pix_fmt = AV_PIX_FMT_NONE;
186 return AVERROR_INVALIDDATA;
191 /* more check if YUV420 */
192 if (av_pix_fmt_desc_get(avctx->pix_fmt)->flags & AV_PIX_FMT_FLAG_PLANAR) {
193 if ((avctx->width & 1) != 0)
194 return AVERROR_INVALIDDATA;
195 h = (avctx->height * 2);
197 return AVERROR_INVALIDDATA;