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