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