p*menc: use the AVFrame API properly.
[libav.git] / libavcodec / pnm.c
CommitLineData
5b6d5596
MN
1/*
2 * PNM image format
406792e7 3 * Copyright (c) 2002, 2003 Fabrice Bellard
5b6d5596 4 *
2912e87a 5 * This file is part of Libav.
b78e7197 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
5b6d5596
MN
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
5b6d5596 11 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
5b6d5596
MN
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
2912e87a 18 * License along with Libav; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
5b6d5596 20 */
d3067047 21
1d9c2dc8
MS
22#include <stdlib.h>
23#include <string.h>
24
737eb597 25#include "libavutil/imgutils.h"
5b6d5596 26#include "avcodec.h"
426a189b 27#include "pnm.h"
5b6d5596 28
9ac2e797
AJ
29static inline int pnm_space(int c)
30{
ccd425e7 31 return c == ' ' || c == '\n' || c == '\r' || c == '\t';
5b6d5596
MN
32}
33
9ac2e797 34static void pnm_get(PNMContext *sc, char *str, int buf_size)
99f06236 35{
9ac2e797
AJ
36 char *s;
37 int c;
38
39 /* skip spaces and comments */
d3067047 40 for (;;) {
9ac2e797
AJ
41 c = *sc->bytestream++;
42 if (c == '#') {
d3067047 43 do {
9ac2e797
AJ
44 c = *sc->bytestream++;
45 } while (c != '\n' && sc->bytestream < sc->bytestream_end);
46 } else if (!pnm_space(c)) {
47 break;
48 }
5b6d5596 49 }
115329f1 50
9ac2e797
AJ
51 s = str;
52 while (sc->bytestream < sc->bytestream_end && !pnm_space(c)) {
53 if ((s - str) < buf_size - 1)
54 *s++ = c;
55 c = *sc->bytestream++;
56 }
57 *s = '\0';
58}
5b6d5596 59
d3067047
DB
60int ff_pnm_decode_header(AVCodecContext *avctx, PNMContext * const s)
61{
9ac2e797
AJ
62 char buf1[32], tuple_type[32];
63 int h, w, depth, maxval;
64
65 pnm_get(s, buf1, sizeof(buf1));
63538a96
MN
66 s->type= buf1[1]-'0';
67 if(buf1[0] != 'P')
380242ca 68 return AVERROR_INVALIDDATA;
63538a96
MN
69
70 if (s->type==1 || s->type==4) {
716d413c 71 avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
63538a96 72 } else if (s->type==2 || s->type==5) {
36ef5369 73 if (avctx->codec_id == AV_CODEC_ID_PGMYUV)
716d413c 74 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
9ac2e797 75 else
716d413c 76 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
63538a96 77 } else if (s->type==3 || s->type==6) {
716d413c 78 avctx->pix_fmt = AV_PIX_FMT_RGB24;
63538a96 79 } else if (s->type==7) {
d3067047
DB
80 w = -1;
81 h = -1;
9ac2e797 82 maxval = -1;
d3067047 83 depth = -1;
9ac2e797 84 tuple_type[0] = '\0';
d3067047 85 for (;;) {
9ac2e797
AJ
86 pnm_get(s, buf1, sizeof(buf1));
87 if (!strcmp(buf1, "WIDTH")) {
88 pnm_get(s, buf1, sizeof(buf1));
89 w = strtol(buf1, NULL, 10);
90 } else if (!strcmp(buf1, "HEIGHT")) {
91 pnm_get(s, buf1, sizeof(buf1));
92 h = strtol(buf1, NULL, 10);
93 } else if (!strcmp(buf1, "DEPTH")) {
94 pnm_get(s, buf1, sizeof(buf1));
95 depth = strtol(buf1, NULL, 10);
96 } else if (!strcmp(buf1, "MAXVAL")) {
97 pnm_get(s, buf1, sizeof(buf1));
98 maxval = strtol(buf1, NULL, 10);
66b9d706
CEH
99 } else if (!strcmp(buf1, "TUPLTYPE") ||
100 /* libavcodec used to write invalid files */
101 !strcmp(buf1, "TUPLETYPE")) {
9ac2e797
AJ
102 pnm_get(s, tuple_type, sizeof(tuple_type));
103 } else if (!strcmp(buf1, "ENDHDR")) {
104 break;
105 } else {
380242ca 106 return AVERROR_INVALIDDATA;
5b6d5596
MN
107 }
108 }
9ac2e797 109 /* check that all tags are present */
e16f217c 110 if (w <= 0 || h <= 0 || maxval <= 0 || depth <= 0 || tuple_type[0] == '\0' || av_image_check_size(w, h, 0, avctx))
380242ca 111 return AVERROR_INVALIDDATA;
311c7008 112
d3067047 113 avctx->width = w;
9ac2e797
AJ
114 avctx->height = h;
115 if (depth == 1) {
116 if (maxval == 1)
716d413c 117 avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
9ac2e797 118 else
716d413c 119 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
9ac2e797 120 } else if (depth == 3) {
0605f5c8 121 if (maxval < 256) {
716d413c 122 avctx->pix_fmt = AV_PIX_FMT_RGB24;
0605f5c8
JR
123 } else {
124 av_log(avctx, AV_LOG_ERROR, "16-bit components are only supported for grayscale\n");
716d413c 125 avctx->pix_fmt = AV_PIX_FMT_NONE;
380242ca 126 return AVERROR_INVALIDDATA;
0605f5c8 127 }
9ac2e797 128 } else if (depth == 4) {
716d413c 129 avctx->pix_fmt = AV_PIX_FMT_RGB32;
9ac2e797 130 } else {
380242ca 131 return AVERROR_INVALIDDATA;
311c7008 132 }
9ac2e797
AJ
133 return 0;
134 } else {
380242ca 135 return AVERROR_INVALIDDATA;
0ecca7a4 136 }
9ac2e797
AJ
137 pnm_get(s, buf1, sizeof(buf1));
138 avctx->width = atoi(buf1);
139 if (avctx->width <= 0)
380242ca 140 return AVERROR_INVALIDDATA;
9ac2e797
AJ
141 pnm_get(s, buf1, sizeof(buf1));
142 avctx->height = atoi(buf1);
e16f217c 143 if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
380242ca 144 return AVERROR_INVALIDDATA;
716d413c 145 if (avctx->pix_fmt != AV_PIX_FMT_MONOWHITE) {
9ac2e797
AJ
146 pnm_get(s, buf1, sizeof(buf1));
147 s->maxval = atoi(buf1);
ebc34883
DK
148 if (s->maxval <= 0) {
149 av_log(avctx, AV_LOG_ERROR, "Invalid maxval: %d\n", s->maxval);
8219782a 150 s->maxval = 255;
ebc34883 151 }
0605f5c8 152 if (s->maxval >= 256) {
716d413c
AK
153 if (avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
154 avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
8fd64adf 155 if (s->maxval != 65535)
716d413c
AK
156 avctx->pix_fmt = AV_PIX_FMT_GRAY16;
157 } else if (avctx->pix_fmt == AV_PIX_FMT_RGB24) {
00182190 158 if (s->maxval > 255)
716d413c 159 avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
b5f536d2
KS
160 } else if (avctx->pix_fmt == AV_PIX_FMT_YUV420P && s->maxval < 65536) {
161 if (s->maxval < 512)
162 avctx->pix_fmt = AV_PIX_FMT_YUV420P9BE;
163 else if (s->maxval < 1024)
164 avctx->pix_fmt = AV_PIX_FMT_YUV420P10BE;
165 else
166 avctx->pix_fmt = AV_PIX_FMT_YUV420P16;
0605f5c8 167 } else {
00182190 168 av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format\n");
716d413c 169 avctx->pix_fmt = AV_PIX_FMT_NONE;
380242ca 170 return AVERROR_INVALIDDATA;
0605f5c8 171 }
5b6d5596 172 }
63538a96
MN
173 }else
174 s->maxval=1;
9ac2e797 175 /* more check if YUV420 */
e6c4ac7b 176 if (av_pix_fmt_desc_get(avctx->pix_fmt)->flags & AV_PIX_FMT_FLAG_PLANAR) {
9ac2e797 177 if ((avctx->width & 1) != 0)
380242ca 178 return AVERROR_INVALIDDATA;
9ac2e797
AJ
179 h = (avctx->height * 2);
180 if ((h % 3) != 0)
380242ca 181 return AVERROR_INVALIDDATA;
9ac2e797
AJ
182 h /= 3;
183 avctx->height = h;
5b6d5596 184 }
9ac2e797 185 return 0;
5b6d5596 186}