FFmpeg  2.8.17
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
pnm.c
Go to the documentation of this file.
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 
40  /* skip spaces and comments */
41  while (sc->bytestream < sc->bytestream_end) {
42  c = *sc->bytestream++;
43  if (c == '#') {
44  while (c != '\n' && sc->bytestream < sc->bytestream_end) {
45  c = *sc->bytestream++;
46  }
47  } else if (!pnm_space(c)) {
48  break;
49  }
50  }
51 
52  s = str;
53  while (sc->bytestream < sc->bytestream_end && !pnm_space(c)) {
54  if ((s - str) < buf_size - 1)
55  *s++ = c;
56  c = *sc->bytestream++;
57  }
58  *s = '\0';
59 }
60 
62 {
63  char buf1[32], tuple_type[32];
64  int h, w, depth, maxval;
65  int ret;
66 
67  pnm_get(s, buf1, sizeof(buf1));
68  if(buf1[0] != 'P')
69  return AVERROR_INVALIDDATA;
70  s->type= buf1[1]-'0';
71 
72  if (s->type==1 || s->type==4) {
74  } else if (s->type==2 || s->type==5) {
75  if (avctx->codec_id == AV_CODEC_ID_PGMYUV)
76  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
77  else
78  avctx->pix_fmt = AV_PIX_FMT_GRAY8;
79  } else if (s->type==3 || s->type==6) {
80  avctx->pix_fmt = AV_PIX_FMT_RGB24;
81  } else if (s->type==7) {
82  w = -1;
83  h = -1;
84  maxval = -1;
85  depth = -1;
86  tuple_type[0] = '\0';
87  for (;;) {
88  pnm_get(s, buf1, sizeof(buf1));
89  if (!strcmp(buf1, "WIDTH")) {
90  pnm_get(s, buf1, sizeof(buf1));
91  w = strtol(buf1, NULL, 10);
92  } else if (!strcmp(buf1, "HEIGHT")) {
93  pnm_get(s, buf1, sizeof(buf1));
94  h = strtol(buf1, NULL, 10);
95  } else if (!strcmp(buf1, "DEPTH")) {
96  pnm_get(s, buf1, sizeof(buf1));
97  depth = strtol(buf1, NULL, 10);
98  } else if (!strcmp(buf1, "MAXVAL")) {
99  pnm_get(s, buf1, sizeof(buf1));
100  maxval = strtol(buf1, NULL, 10);
101  } else if (!strcmp(buf1, "TUPLTYPE") ||
102  /* libavcodec used to write invalid files */
103  !strcmp(buf1, "TUPLETYPE")) {
104  pnm_get(s, tuple_type, sizeof(tuple_type));
105  } else if (!strcmp(buf1, "ENDHDR")) {
106  break;
107  } else {
108  return AVERROR_INVALIDDATA;
109  }
110  }
111  if (!pnm_space(s->bytestream[-1]))
112  return AVERROR_INVALIDDATA;
113 
114  /* check that all tags are present */
115  if (w <= 0 || h <= 0 || maxval <= 0 || depth <= 0 || tuple_type[0] == '\0' || av_image_check_size(w, h, 0, avctx) || s->bytestream >= s->bytestream_end)
116  return AVERROR_INVALIDDATA;
117 
118  ret = ff_set_dimensions(avctx, w, h);
119  if (ret < 0)
120  return ret;
121  s->maxval = maxval;
122  if (depth == 1) {
123  if (maxval == 1) {
124  avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
125  } else if (maxval < 256) {
126  avctx->pix_fmt = AV_PIX_FMT_GRAY8;
127  } else {
128  avctx->pix_fmt = AV_PIX_FMT_GRAY16;
129  }
130  } else if (depth == 2) {
131  if (maxval < 256) {
132  avctx->pix_fmt = AV_PIX_FMT_GRAY8A;
133  } else {
134  avctx->pix_fmt = AV_PIX_FMT_YA16;
135  }
136  } else if (depth == 3) {
137  if (maxval < 256) {
138  avctx->pix_fmt = AV_PIX_FMT_RGB24;
139  } else {
140  avctx->pix_fmt = AV_PIX_FMT_RGB48;
141  }
142  } else if (depth == 4) {
143  if (maxval < 256) {
144  avctx->pix_fmt = AV_PIX_FMT_RGBA;
145  } else {
146  avctx->pix_fmt = AV_PIX_FMT_RGBA64;
147  }
148  } else {
149  return AVERROR_INVALIDDATA;
150  }
151  return 0;
152  } else {
153  return AVERROR_INVALIDDATA;
154  }
155  pnm_get(s, buf1, sizeof(buf1));
156  w = atoi(buf1);
157  pnm_get(s, buf1, sizeof(buf1));
158  h = atoi(buf1);
159  if(w <= 0 || h <= 0 || av_image_check_size(w, h, 0, avctx) || s->bytestream >= s->bytestream_end)
160  return AVERROR_INVALIDDATA;
161 
162  ret = ff_set_dimensions(avctx, w, h);
163  if (ret < 0)
164  return ret;
165 
166  if (avctx->pix_fmt != AV_PIX_FMT_MONOWHITE && avctx->pix_fmt != AV_PIX_FMT_MONOBLACK) {
167  pnm_get(s, buf1, sizeof(buf1));
168  s->maxval = atoi(buf1);
169  if (s->maxval <= 0) {
170  av_log(avctx, AV_LOG_ERROR, "Invalid maxval: %d\n", s->maxval);
171  s->maxval = 255;
172  }
173  if (s->maxval >= 256) {
174  if (avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
175  avctx->pix_fmt = AV_PIX_FMT_GRAY16;
176  } else if (avctx->pix_fmt == AV_PIX_FMT_RGB24) {
177  avctx->pix_fmt = AV_PIX_FMT_RGB48;
178  } else if (avctx->pix_fmt == AV_PIX_FMT_YUV420P && s->maxval < 65536) {
179  if (s->maxval < 512)
180  avctx->pix_fmt = AV_PIX_FMT_YUV420P9;
181  else if (s->maxval < 1024)
182  avctx->pix_fmt = AV_PIX_FMT_YUV420P10;
183  else
184  avctx->pix_fmt = AV_PIX_FMT_YUV420P16;
185  } else {
186  av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format\n");
187  avctx->pix_fmt = AV_PIX_FMT_NONE;
188  return AVERROR_INVALIDDATA;
189  }
190  }
191  }else
192  s->maxval=1;
193 
194  if (!pnm_space(s->bytestream[-1]))
195  return AVERROR_INVALIDDATA;
196 
197  /* more check if YUV420 */
199  if ((avctx->width & 1) != 0)
200  return AVERROR_INVALIDDATA;
201  h = (avctx->height * 2);
202  if ((h % 3) != 0)
203  return AVERROR_INVALIDDATA;
204  h /= 3;
205  avctx->height = h;
206  }
207  return 0;
208 }
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2129
static int pnm_space(int c)
Definition: pnm.c:30
misc image utilities
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:69
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:216
#define AV_PIX_FMT_RGBA64
Definition: pixfmt.h:374
int maxval
maximum value of a pixel
Definition: pnm.h:31
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1732
uint8_t * bytestream
Definition: pnm.h:28
#define av_log(a,...)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AV_PIX_FMT_YA16
Definition: pixfmt.h:369
#define AV_PIX_FMT_RGB48
Definition: pixfmt.h:370
static void pnm_get(PNMContext *sc, char *str, int buf_size)
Definition: pnm.c:35
uint8_t * bytestream_end
Definition: pnm.h:30
Libavcodec external API header.
int depth
Definition: v4l.c:62
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:99
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition: imgutils.c:266
alias for AV_PIX_FMT_YA8
Definition: pixfmt.h:162
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:368
int width
picture width / height.
Definition: avcodec.h:1691
#define AV_PIX_FMT_YUV420P16
Definition: pixfmt.h:395
Definition: pnm.h:27
enum AVCodecID codec_id
Definition: avcodec.h:1529
int ff_pnm_decode_header(AVCodecContext *avctx, PNMContext *const s)
Definition: pnm.c:61
uint8_t flags
Definition: pixdesc.h:90
main external API structure.
Definition: avcodec.h:1512
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:384
#define AV_PIX_FMT_YUV420P9
Definition: pixfmt.h:381
Y , 1bpp, 0 is black, 1 is white, in each byte pixels are ordered from the msb to the lsb...
Definition: pixfmt.h:77
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:67
Y , 8bpp.
Definition: pixfmt.h:75
common internal api header.
Y , 1bpp, 0 is white, 1 is black, in each byte pixels are ordered from the msb to the lsb...
Definition: pixfmt.h:76
static double c[64]
#define AV_PIX_FMT_FLAG_PLANAR
At least one pixel component is not in the first data plane.
Definition: pixdesc.h:127
int type
Definition: pnm.h:32