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