FFmpeg
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/avassert.h"
26 #include "libavutil/imgutils.h"
27 #include "avcodec.h"
28 #include "internal.h"
29 #include "pnm.h"
30 
31 static inline int pnm_space(int c)
32 {
33  return c == ' ' || c == '\n' || c == '\r' || c == '\t';
34 }
35 
36 static void pnm_get(PNMContext *sc, char *str, int buf_size)
37 {
38  char *s;
39  int c;
40  uint8_t *bs = sc->bytestream;
41  const uint8_t *end = sc->bytestream_end;
42 
43  /* skip spaces and comments */
44  while (bs < end) {
45  c = *bs++;
46  if (c == '#') {
47  while (c != '\n' && bs < end) {
48  c = *bs++;
49  }
50  } else if (!pnm_space(c)) {
51  break;
52  }
53  }
54 
55  s = str;
56  while (bs < end && !pnm_space(c) && (s - str) < buf_size - 1) {
57  *s++ = c;
58  c = *bs++;
59  }
60  *s = '\0';
61  sc->bytestream = bs;
62 }
63 
65 {
66  char buf1[32], tuple_type[32];
67  int h, w, depth, maxval;
68  int ret;
69 
70  if (s->bytestream_end - s->bytestream < 3 ||
71  s->bytestream[0] != 'P' ||
72  s->bytestream[1] < '1' ||
73  s->bytestream[1] > '7') {
74  s->bytestream += s->bytestream_end > s->bytestream;
75  s->bytestream += s->bytestream_end > s->bytestream;
76  return AVERROR_INVALIDDATA;
77  }
78  pnm_get(s, buf1, sizeof(buf1));
79  s->type= buf1[1]-'0';
80 
81  if (s->type==1 || s->type==4) {
83  } else if (s->type==2 || s->type==5) {
84  if (avctx->codec_id == AV_CODEC_ID_PGMYUV)
85  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
86  else
87  avctx->pix_fmt = AV_PIX_FMT_GRAY8;
88  } else if (s->type==3 || s->type==6) {
89  avctx->pix_fmt = AV_PIX_FMT_RGB24;
90  } else if (s->type==7) {
91  w = -1;
92  h = -1;
93  maxval = -1;
94  depth = -1;
95  tuple_type[0] = '\0';
96  for (;;) {
97  pnm_get(s, buf1, sizeof(buf1));
98  if (!strcmp(buf1, "WIDTH")) {
99  pnm_get(s, buf1, sizeof(buf1));
100  w = strtol(buf1, NULL, 10);
101  } else if (!strcmp(buf1, "HEIGHT")) {
102  pnm_get(s, buf1, sizeof(buf1));
103  h = strtol(buf1, NULL, 10);
104  } else if (!strcmp(buf1, "DEPTH")) {
105  pnm_get(s, buf1, sizeof(buf1));
106  depth = strtol(buf1, NULL, 10);
107  } else if (!strcmp(buf1, "MAXVAL")) {
108  pnm_get(s, buf1, sizeof(buf1));
109  maxval = strtol(buf1, NULL, 10);
110  } else if (!strcmp(buf1, "TUPLTYPE") ||
111  /* libavcodec used to write invalid files */
112  !strcmp(buf1, "TUPLETYPE")) {
113  pnm_get(s, tuple_type, sizeof(tuple_type));
114  } else if (!strcmp(buf1, "ENDHDR")) {
115  break;
116  } else {
117  return AVERROR_INVALIDDATA;
118  }
119  }
120  /* check that all tags are present */
121  if (w <= 0 || h <= 0 || maxval <= 0 || maxval > UINT16_MAX || depth <= 0 || tuple_type[0] == '\0' ||
122  av_image_check_size(w, h, 0, avctx) || s->bytestream >= s->bytestream_end)
123  return AVERROR_INVALIDDATA;
124 
125  ret = ff_set_dimensions(avctx, w, h);
126  if (ret < 0)
127  return ret;
128  s->maxval = maxval;
129  if (depth == 1) {
130  if (maxval == 1) {
131  avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
132  } else if (maxval < 256) {
133  avctx->pix_fmt = AV_PIX_FMT_GRAY8;
134  } else {
135  avctx->pix_fmt = AV_PIX_FMT_GRAY16;
136  }
137  } else if (depth == 2) {
138  if (maxval < 256) {
139  avctx->pix_fmt = AV_PIX_FMT_GRAY8A;
140  } else {
141  avctx->pix_fmt = AV_PIX_FMT_YA16;
142  }
143  } else if (depth == 3) {
144  if (maxval < 256) {
145  avctx->pix_fmt = AV_PIX_FMT_RGB24;
146  } else {
147  avctx->pix_fmt = AV_PIX_FMT_RGB48;
148  }
149  } else if (depth == 4) {
150  if (maxval < 256) {
151  avctx->pix_fmt = AV_PIX_FMT_RGBA;
152  } else {
153  avctx->pix_fmt = AV_PIX_FMT_RGBA64;
154  }
155  } else {
156  return AVERROR_INVALIDDATA;
157  }
158  return 0;
159  } else {
160  av_assert0(0);
161  }
162  pnm_get(s, buf1, sizeof(buf1));
163  w = atoi(buf1);
164  pnm_get(s, buf1, sizeof(buf1));
165  h = atoi(buf1);
166  if(w <= 0 || h <= 0 || av_image_check_size(w, h, 0, avctx) || s->bytestream >= s->bytestream_end)
167  return AVERROR_INVALIDDATA;
168 
169  ret = ff_set_dimensions(avctx, w, h);
170  if (ret < 0)
171  return ret;
172 
173  if (avctx->pix_fmt != AV_PIX_FMT_MONOWHITE && avctx->pix_fmt != AV_PIX_FMT_MONOBLACK) {
174  pnm_get(s, buf1, sizeof(buf1));
175  s->maxval = atoi(buf1);
176  if (s->maxval <= 0 || s->maxval > UINT16_MAX) {
177  av_log(avctx, AV_LOG_ERROR, "Invalid maxval: %d\n", s->maxval);
178  s->maxval = 255;
179  }
180  if (s->maxval >= 256) {
181  if (avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
182  avctx->pix_fmt = AV_PIX_FMT_GRAY16;
183  } else if (avctx->pix_fmt == AV_PIX_FMT_RGB24) {
184  avctx->pix_fmt = AV_PIX_FMT_RGB48;
185  } else if (avctx->pix_fmt == AV_PIX_FMT_YUV420P && s->maxval < 65536) {
186  if (s->maxval < 512)
187  avctx->pix_fmt = AV_PIX_FMT_YUV420P9;
188  else if (s->maxval < 1024)
189  avctx->pix_fmt = AV_PIX_FMT_YUV420P10;
190  else
191  avctx->pix_fmt = AV_PIX_FMT_YUV420P16;
192  } else {
193  av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format\n");
194  avctx->pix_fmt = AV_PIX_FMT_NONE;
195  return AVERROR_INVALIDDATA;
196  }
197  }
198  }else
199  s->maxval=1;
200  /* more check if YUV420 */
202  if ((avctx->width & 1) != 0)
203  return AVERROR_INVALIDDATA;
204  h = (avctx->height * 2);
205  if ((h % 3) != 0)
206  return AVERROR_INVALIDDATA;
207  h /= 3;
208  avctx->height = h;
209  }
210  return 0;
211 }
#define NULL
Definition: coverity.c:32
#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:2522
static int pnm_space(int c)
Definition: pnm.c:31
misc image utilities
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:68
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:104
#define AV_PIX_FMT_RGBA64
Definition: pixfmt.h:377
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:1778
uint8_t * bytestream
Definition: pnm.h:28
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
uint8_t
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
#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:372
#define AV_PIX_FMT_RGB48
Definition: pixfmt.h:373
simple assert() macros that are a bit more flexible than ISO C assert().
static void pnm_get(PNMContext *sc, char *str, int buf_size)
Definition: pnm.c:36
uint8_t * bytestream_end
Definition: pnm.h:30
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:93
uint64_t flags
Combination of AV_PIX_FMT_FLAG_...
Definition: pixdesc.h:106
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:282
alias for AV_PIX_FMT_YA8
Definition: pixfmt.h:146
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:371
int width
picture width / height.
Definition: avcodec.h:1741
uint8_t w
Definition: llviddspenc.c:38
#define s(width, name)
Definition: cbs_vp9.c:257
#define AV_PIX_FMT_YUV420P16
Definition: pixfmt.h:398
Definition: pnm.h:27
Libavcodec external API header.
enum AVCodecID codec_id
Definition: avcodec.h:1578
int ff_pnm_decode_header(AVCodecContext *avctx, PNMContext *const s)
Definition: pnm.c:64
main external API structure.
Definition: avcodec.h:1568
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:387
#define AV_PIX_FMT_YUV420P9
Definition: pixfmt.h:384
Y , 1bpp, 0 is black, 1 is white, in each byte pixels are ordered from the msb to the lsb...
Definition: pixfmt.h:76
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
Y , 8bpp.
Definition: pixfmt.h:74
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:75
#define AV_PIX_FMT_FLAG_PLANAR
At least one pixel component is not in the first data plane.
Definition: pixdesc.h:144
int type
Definition: pnm.h:32