5b6d5596 |
/*
* PNM image format |
406792e7 |
* Copyright (c) 2002, 2003 Fabrice Bellard |
5b6d5596 |
* |
b78e7197 |
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or |
5b6d5596 |
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either |
b78e7197 |
* version 2.1 of the License, or (at your option) any later version. |
5b6d5596 |
* |
b78e7197 |
* FFmpeg is distributed in the hope that it will be useful, |
5b6d5596 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public |
b78e7197 |
* License along with FFmpeg; if not, write to the Free Software |
5509bffa |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
5b6d5596 |
*/ |
d3067047 |
|
1d9c2dc8 |
#include <stdlib.h>
#include <string.h>
|
7ffe76e5 |
#include "libavutil/imgutils.h" |
5b6d5596 |
#include "avcodec.h" |
a1c0d1d9 |
#include "internal.h" |
426a189b |
#include "pnm.h" |
5b6d5596 |
|
9ac2e797 |
static inline int pnm_space(int c)
{ |
ccd425e7 |
return c == ' ' || c == '\n' || c == '\r' || c == '\t'; |
5b6d5596 |
}
|
9ac2e797 |
static void pnm_get(PNMContext *sc, char *str, int buf_size) |
99f06236 |
{ |
9ac2e797 |
char *s;
int c;
/* skip spaces and comments */ |
bb7744a4 |
while (sc->bytestream < sc->bytestream_end) { |
9ac2e797 |
c = *sc->bytestream++;
if (c == '#') { |
bb7744a4 |
while (c != '\n' && sc->bytestream < sc->bytestream_end) { |
9ac2e797 |
c = *sc->bytestream++; |
bb7744a4 |
} |
9ac2e797 |
} else if (!pnm_space(c)) {
break;
} |
5b6d5596 |
} |
115329f1 |
|
9ac2e797 |
s = str;
while (sc->bytestream < sc->bytestream_end && !pnm_space(c)) {
if ((s - str) < buf_size - 1)
*s++ = c;
c = *sc->bytestream++;
}
*s = '\0';
} |
5b6d5596 |
|
d3067047 |
int ff_pnm_decode_header(AVCodecContext *avctx, PNMContext * const s)
{ |
9ac2e797 |
char buf1[32], tuple_type[32];
int h, w, depth, maxval; |
a1c0d1d9 |
int ret; |
9ac2e797 |
pnm_get(s, buf1, sizeof(buf1)); |
63538a96 |
if(buf1[0] != 'P') |
380242ca |
return AVERROR_INVALIDDATA; |
b4d525eb |
s->type= buf1[1]-'0'; |
63538a96 |
if (s->type==1 || s->type==4) { |
716d413c |
avctx->pix_fmt = AV_PIX_FMT_MONOWHITE; |
63538a96 |
} else if (s->type==2 || s->type==5) { |
36ef5369 |
if (avctx->codec_id == AV_CODEC_ID_PGMYUV) |
716d413c |
avctx->pix_fmt = AV_PIX_FMT_YUV420P; |
9ac2e797 |
else |
716d413c |
avctx->pix_fmt = AV_PIX_FMT_GRAY8; |
63538a96 |
} else if (s->type==3 || s->type==6) { |
716d413c |
avctx->pix_fmt = AV_PIX_FMT_RGB24; |
63538a96 |
} else if (s->type==7) { |
d3067047 |
w = -1;
h = -1; |
9ac2e797 |
maxval = -1; |
d3067047 |
depth = -1; |
9ac2e797 |
tuple_type[0] = '\0'; |
d3067047 |
for (;;) { |
9ac2e797 |
pnm_get(s, buf1, sizeof(buf1));
if (!strcmp(buf1, "WIDTH")) {
pnm_get(s, buf1, sizeof(buf1));
w = strtol(buf1, NULL, 10);
} else if (!strcmp(buf1, "HEIGHT")) {
pnm_get(s, buf1, sizeof(buf1));
h = strtol(buf1, NULL, 10);
} else if (!strcmp(buf1, "DEPTH")) {
pnm_get(s, buf1, sizeof(buf1));
depth = strtol(buf1, NULL, 10);
} else if (!strcmp(buf1, "MAXVAL")) {
pnm_get(s, buf1, sizeof(buf1));
maxval = strtol(buf1, NULL, 10); |
c1bc1967 |
} else if (!strcmp(buf1, "TUPLTYPE") || |
66b9d706 |
/* libavcodec used to write invalid files */ |
c1bc1967 |
!strcmp(buf1, "TUPLETYPE")) { |
9ac2e797 |
pnm_get(s, tuple_type, sizeof(tuple_type));
} else if (!strcmp(buf1, "ENDHDR")) {
break;
} else { |
380242ca |
return AVERROR_INVALIDDATA; |
5b6d5596 |
}
} |
9ac2e797 |
/* check that all tags are present */ |
484151df |
if (w <= 0 || h <= 0 || maxval <= 0 || maxval > UINT16_MAX || depth <= 0 || tuple_type[0] == '\0' ||
av_image_check_size(w, h, 0, avctx) || s->bytestream >= s->bytestream_end) |
380242ca |
return AVERROR_INVALIDDATA; |
311c7008 |
|
a1c0d1d9 |
ret = ff_set_dimensions(avctx, w, h);
if (ret < 0)
return ret; |
190a0998 |
s->maxval = maxval; |
9ac2e797 |
if (depth == 1) { |
871e2f4f |
if (maxval == 1) { |
ac627b3d |
avctx->pix_fmt = AV_PIX_FMT_MONOBLACK; |
7e8e8ba9 |
} else if (maxval < 256) { |
716d413c |
avctx->pix_fmt = AV_PIX_FMT_GRAY8; |
19b945dc |
} else { |
62738157 |
avctx->pix_fmt = AV_PIX_FMT_GRAY16; |
871e2f4f |
} |
328e7932 |
} else if (depth == 2) { |
6c559a0a |
if (maxval < 256) { |
ac627b3d |
avctx->pix_fmt = AV_PIX_FMT_GRAY8A; |
6c559a0a |
} else {
avctx->pix_fmt = AV_PIX_FMT_YA16;
} |
9ac2e797 |
} else if (depth == 3) { |
0605f5c8 |
if (maxval < 256) { |
cc5bcaa4 |
avctx->pix_fmt = AV_PIX_FMT_RGB24; |
0605f5c8 |
} else { |
62738157 |
avctx->pix_fmt = AV_PIX_FMT_RGB48; |
0605f5c8 |
} |
9ac2e797 |
} else if (depth == 4) { |
febf75f3 |
if (maxval < 256) { |
ac627b3d |
avctx->pix_fmt = AV_PIX_FMT_RGBA; |
febf75f3 |
} else { |
62738157 |
avctx->pix_fmt = AV_PIX_FMT_RGBA64; |
febf75f3 |
} |
9ac2e797 |
} else { |
380242ca |
return AVERROR_INVALIDDATA; |
311c7008 |
} |
9ac2e797 |
return 0;
} else { |
380242ca |
return AVERROR_INVALIDDATA; |
0ecca7a4 |
} |
9ac2e797 |
pnm_get(s, buf1, sizeof(buf1)); |
0049af26 |
w = atoi(buf1); |
9ac2e797 |
pnm_get(s, buf1, sizeof(buf1)); |
0049af26 |
h = atoi(buf1);
if(w <= 0 || h <= 0 || av_image_check_size(w, h, 0, avctx) || s->bytestream >= s->bytestream_end) |
380242ca |
return AVERROR_INVALIDDATA; |
0049af26 |
|
a1c0d1d9 |
ret = ff_set_dimensions(avctx, w, h);
if (ret < 0)
return ret; |
0049af26 |
|
ac627b3d |
if (avctx->pix_fmt != AV_PIX_FMT_MONOWHITE && avctx->pix_fmt != AV_PIX_FMT_MONOBLACK) { |
9ac2e797 |
pnm_get(s, buf1, sizeof(buf1));
s->maxval = atoi(buf1); |
484151df |
if (s->maxval <= 0 || s->maxval > UINT16_MAX) { |
ebc34883 |
av_log(avctx, AV_LOG_ERROR, "Invalid maxval: %d\n", s->maxval); |
8219782a |
s->maxval = 255; |
ebc34883 |
} |
0605f5c8 |
if (s->maxval >= 256) { |
716d413c |
if (avctx->pix_fmt == AV_PIX_FMT_GRAY8) { |
62738157 |
avctx->pix_fmt = AV_PIX_FMT_GRAY16; |
716d413c |
} else if (avctx->pix_fmt == AV_PIX_FMT_RGB24) { |
62738157 |
avctx->pix_fmt = AV_PIX_FMT_RGB48; |
b5f536d2 |
} else if (avctx->pix_fmt == AV_PIX_FMT_YUV420P && s->maxval < 65536) {
if (s->maxval < 512) |
62738157 |
avctx->pix_fmt = AV_PIX_FMT_YUV420P9; |
b5f536d2 |
else if (s->maxval < 1024) |
62738157 |
avctx->pix_fmt = AV_PIX_FMT_YUV420P10; |
b5f536d2 |
else
avctx->pix_fmt = AV_PIX_FMT_YUV420P16; |
0605f5c8 |
} else { |
00182190 |
av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format\n"); |
716d413c |
avctx->pix_fmt = AV_PIX_FMT_NONE; |
380242ca |
return AVERROR_INVALIDDATA; |
0605f5c8 |
} |
5b6d5596 |
} |
63538a96 |
}else
s->maxval=1; |
9ac2e797 |
/* more check if YUV420 */ |
e6c4ac7b |
if (av_pix_fmt_desc_get(avctx->pix_fmt)->flags & AV_PIX_FMT_FLAG_PLANAR) { |
9ac2e797 |
if ((avctx->width & 1) != 0) |
380242ca |
return AVERROR_INVALIDDATA; |
9ac2e797 |
h = (avctx->height * 2);
if ((h % 3) != 0) |
380242ca |
return AVERROR_INVALIDDATA; |
9ac2e797 |
h /= 3;
avctx->height = h; |
5b6d5596 |
} |
9ac2e797 |
return 0; |
5b6d5596 |
} |