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