2004-11-11 20:09:28 +02:00
|
|
|
/*
|
|
|
|
* PNM image format
|
2009-01-19 17:46:40 +02:00
|
|
|
* Copyright (c) 2002, 2003 Fabrice Bellard
|
2004-11-11 20:09:28 +02:00
|
|
|
*
|
2006-10-07 18:30:46 +03:00
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2004-11-11 20:09:28 +02:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2006-10-07 18:30:46 +03:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2004-11-11 20:09:28 +02:00
|
|
|
*
|
2006-10-07 18:30:46 +03:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2004-11-11 20:09:28 +02:00
|
|
|
* 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
|
2006-10-07 18:30:46 +03:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2006-01-13 00:43:26 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2004-11-11 20:09:28 +02:00
|
|
|
*/
|
2009-10-27 18:57:35 +02:00
|
|
|
|
2012-08-06 16:49:32 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2019-08-01 20:04:15 +02:00
|
|
|
#include "libavutil/avassert.h"
|
2011-02-07 15:37:08 +02:00
|
|
|
#include "libavutil/imgutils.h"
|
2020-05-27 16:42:24 +02:00
|
|
|
#include "libavutil/avstring.h"
|
2004-11-11 20:09:28 +02:00
|
|
|
#include "avcodec.h"
|
2017-05-31 13:39:45 +02:00
|
|
|
#include "internal.h"
|
2007-05-11 02:03:14 +03:00
|
|
|
#include "pnm.h"
|
2004-11-11 20:09:28 +02:00
|
|
|
|
2007-05-11 12:28:07 +03:00
|
|
|
static inline int pnm_space(int c)
|
|
|
|
{
|
2008-05-06 12:16:36 +03:00
|
|
|
return c == ' ' || c == '\n' || c == '\r' || c == '\t';
|
2004-11-11 20:09:28 +02:00
|
|
|
}
|
|
|
|
|
2007-05-11 12:28:07 +03:00
|
|
|
static void pnm_get(PNMContext *sc, char *str, int buf_size)
|
2004-11-13 00:55:29 +02:00
|
|
|
{
|
2007-05-11 12:28:07 +03:00
|
|
|
char *s;
|
|
|
|
int c;
|
2022-07-26 00:32:28 +02:00
|
|
|
const uint8_t *bs = sc->bytestream;
|
2019-02-21 19:38:40 +02:00
|
|
|
const uint8_t *end = sc->bytestream_end;
|
2007-05-11 12:28:07 +03:00
|
|
|
|
|
|
|
/* skip spaces and comments */
|
2019-02-21 19:38:40 +02:00
|
|
|
while (bs < end) {
|
|
|
|
c = *bs++;
|
2007-05-11 12:28:07 +03:00
|
|
|
if (c == '#') {
|
2019-02-21 19:38:40 +02:00
|
|
|
while (c != '\n' && bs < end) {
|
|
|
|
c = *bs++;
|
2012-08-07 21:57:40 +03:00
|
|
|
}
|
2007-05-11 12:28:07 +03:00
|
|
|
} else if (!pnm_space(c)) {
|
|
|
|
break;
|
|
|
|
}
|
2004-11-11 20:09:28 +02:00
|
|
|
}
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2007-05-11 12:28:07 +03:00
|
|
|
s = str;
|
2019-03-10 23:39:53 +02:00
|
|
|
while (bs < end && !pnm_space(c) && (s - str) < buf_size - 1) {
|
|
|
|
*s++ = c;
|
2019-02-21 19:38:40 +02:00
|
|
|
c = *bs++;
|
2007-05-11 12:28:07 +03:00
|
|
|
}
|
|
|
|
*s = '\0';
|
2019-02-21 19:38:40 +02:00
|
|
|
sc->bytestream = bs;
|
2007-05-11 12:28:07 +03:00
|
|
|
}
|
2004-11-11 20:09:28 +02:00
|
|
|
|
2009-10-27 18:57:35 +02:00
|
|
|
int ff_pnm_decode_header(AVCodecContext *avctx, PNMContext * const s)
|
|
|
|
{
|
2007-05-11 12:28:07 +03:00
|
|
|
char buf1[32], tuple_type[32];
|
|
|
|
int h, w, depth, maxval;
|
2017-05-31 13:39:45 +02:00
|
|
|
int ret;
|
2007-05-11 12:28:07 +03:00
|
|
|
|
2019-08-01 20:04:15 +02:00
|
|
|
if (s->bytestream_end - s->bytestream < 3 ||
|
|
|
|
s->bytestream[0] != 'P' ||
|
2020-05-27 16:42:24 +02:00
|
|
|
(s->bytestream[1] < '1' ||
|
|
|
|
s->bytestream[1] > '7' &&
|
2021-02-12 00:37:06 +02:00
|
|
|
s->bytestream[1] != 'f' &&
|
2022-07-01 10:06:15 +02:00
|
|
|
s->bytestream[1] != 'F' &&
|
|
|
|
s->bytestream[1] != 'H' &&
|
|
|
|
s->bytestream[1] != 'h')) {
|
2019-08-01 20:04:15 +02:00
|
|
|
s->bytestream += s->bytestream_end > s->bytestream;
|
|
|
|
s->bytestream += s->bytestream_end > s->bytestream;
|
2012-11-16 13:45:34 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2019-08-01 20:04:15 +02:00
|
|
|
}
|
|
|
|
pnm_get(s, buf1, sizeof(buf1));
|
2014-08-03 15:01:03 +03:00
|
|
|
s->type= buf1[1]-'0';
|
2022-07-01 10:06:15 +02:00
|
|
|
s->half = 0;
|
2009-12-01 18:56:13 +02:00
|
|
|
|
2020-05-27 16:42:24 +02:00
|
|
|
if (buf1[1] == 'F') {
|
|
|
|
avctx->pix_fmt = AV_PIX_FMT_GBRPF32;
|
2021-02-12 00:37:06 +02:00
|
|
|
} else if (buf1[1] == 'f') {
|
|
|
|
avctx->pix_fmt = AV_PIX_FMT_GRAYF32;
|
2022-07-01 10:06:15 +02:00
|
|
|
} else if (buf1[1] == 'H') {
|
|
|
|
avctx->pix_fmt = AV_PIX_FMT_GBRPF32;
|
|
|
|
s->half = 1;
|
|
|
|
} else if (buf1[1] == 'h') {
|
|
|
|
avctx->pix_fmt = AV_PIX_FMT_GRAYF32;
|
|
|
|
s->half = 1;
|
2020-05-27 16:42:24 +02:00
|
|
|
} else if (s->type==1 || s->type==4) {
|
2012-10-06 13:10:34 +03:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
|
2009-12-01 18:56:13 +02:00
|
|
|
} else if (s->type==2 || s->type==5) {
|
2012-08-05 12:11:04 +03:00
|
|
|
if (avctx->codec_id == AV_CODEC_ID_PGMYUV)
|
2012-10-06 13:10:34 +03:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_YUV420P;
|
2007-05-11 12:28:07 +03:00
|
|
|
else
|
2012-10-06 13:10:34 +03:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_GRAY8;
|
2009-12-01 18:56:13 +02:00
|
|
|
} else if (s->type==3 || s->type==6) {
|
2012-10-06 13:10:34 +03:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_RGB24;
|
2009-12-01 18:56:13 +02:00
|
|
|
} else if (s->type==7) {
|
2009-10-27 18:57:35 +02:00
|
|
|
w = -1;
|
|
|
|
h = -1;
|
2007-05-11 12:28:07 +03:00
|
|
|
maxval = -1;
|
2009-10-27 18:57:35 +02:00
|
|
|
depth = -1;
|
2007-05-11 12:28:07 +03:00
|
|
|
tuple_type[0] = '\0';
|
2009-10-27 18:57:35 +02:00
|
|
|
for (;;) {
|
2007-05-11 12:28:07 +03:00
|
|
|
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);
|
2011-12-28 07:54:59 +03:00
|
|
|
} else if (!strcmp(buf1, "TUPLTYPE") ||
|
2011-12-28 07:54:59 +03:00
|
|
|
/* libavcodec used to write invalid files */
|
2011-12-28 07:54:59 +03:00
|
|
|
!strcmp(buf1, "TUPLETYPE")) {
|
2007-05-11 12:28:07 +03:00
|
|
|
pnm_get(s, tuple_type, sizeof(tuple_type));
|
|
|
|
} else if (!strcmp(buf1, "ENDHDR")) {
|
|
|
|
break;
|
|
|
|
} else {
|
2012-11-16 13:45:34 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2004-11-11 20:09:28 +02:00
|
|
|
}
|
|
|
|
}
|
2019-12-14 20:19:57 +02:00
|
|
|
if (!pnm_space(s->bytestream[-1]))
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2007-05-11 12:28:07 +03:00
|
|
|
/* check that all tags are present */
|
2016-11-10 21:31:02 +02:00
|
|
|
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)
|
2012-11-16 13:45:34 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2004-11-13 01:38:43 +02:00
|
|
|
|
2017-05-31 13:39:45 +02:00
|
|
|
ret = ff_set_dimensions(avctx, w, h);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2011-12-28 07:59:39 +03:00
|
|
|
s->maxval = maxval;
|
2007-05-11 12:28:07 +03:00
|
|
|
if (depth == 1) {
|
2012-01-05 22:57:49 +03:00
|
|
|
if (maxval == 1) {
|
2012-10-08 21:54:00 +03:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
|
2013-07-28 19:12:23 +03:00
|
|
|
} else if (maxval < 256) {
|
2012-10-06 13:10:34 +03:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_GRAY8;
|
2012-01-07 21:23:21 +03:00
|
|
|
} else {
|
2013-08-03 15:14:55 +03:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_GRAY16;
|
2012-01-05 22:57:49 +03:00
|
|
|
}
|
2012-01-17 02:37:20 +03:00
|
|
|
} else if (depth == 2) {
|
2015-01-22 02:49:04 +02:00
|
|
|
if (maxval < 256) {
|
2012-10-08 21:54:00 +03:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_GRAY8A;
|
2015-01-22 02:49:04 +02:00
|
|
|
} else {
|
|
|
|
avctx->pix_fmt = AV_PIX_FMT_YA16;
|
|
|
|
}
|
2007-05-11 12:28:07 +03:00
|
|
|
} else if (depth == 3) {
|
2008-09-14 18:50:59 +03:00
|
|
|
if (maxval < 256) {
|
2012-11-05 02:35:09 +03:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_RGB24;
|
2008-09-14 18:50:59 +03:00
|
|
|
} else {
|
2013-08-03 15:14:55 +03:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_RGB48;
|
2008-09-14 18:50:59 +03:00
|
|
|
}
|
2007-05-11 12:28:07 +03:00
|
|
|
} else if (depth == 4) {
|
2012-01-06 03:25:54 +03:00
|
|
|
if (maxval < 256) {
|
2012-10-08 21:54:00 +03:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_RGBA;
|
2012-01-06 03:25:54 +03:00
|
|
|
} else {
|
2013-08-03 15:14:55 +03:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_RGBA64;
|
2012-01-06 03:25:54 +03:00
|
|
|
}
|
2007-05-11 12:28:07 +03:00
|
|
|
} else {
|
2012-11-16 13:45:34 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2004-11-13 01:38:43 +02:00
|
|
|
}
|
2007-05-11 12:28:07 +03:00
|
|
|
return 0;
|
|
|
|
} else {
|
2019-08-01 20:04:15 +02:00
|
|
|
av_assert0(0);
|
2005-01-12 02:16:25 +02:00
|
|
|
}
|
2007-05-11 12:28:07 +03:00
|
|
|
pnm_get(s, buf1, sizeof(buf1));
|
2012-08-07 21:58:25 +03:00
|
|
|
w = atoi(buf1);
|
2007-05-11 12:28:07 +03:00
|
|
|
pnm_get(s, buf1, sizeof(buf1));
|
2012-08-07 21:58:25 +03:00
|
|
|
h = atoi(buf1);
|
|
|
|
if(w <= 0 || h <= 0 || av_image_check_size(w, h, 0, avctx) || s->bytestream >= s->bytestream_end)
|
2012-11-16 13:45:34 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2012-08-07 21:58:25 +03:00
|
|
|
|
2017-05-31 13:39:45 +02:00
|
|
|
ret = ff_set_dimensions(avctx, w, h);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2012-08-07 21:58:25 +03:00
|
|
|
|
2021-02-12 00:37:06 +02:00
|
|
|
if (avctx->pix_fmt == AV_PIX_FMT_GBRPF32 || avctx->pix_fmt == AV_PIX_FMT_GRAYF32) {
|
2020-05-27 16:42:24 +02:00
|
|
|
pnm_get(s, buf1, sizeof(buf1));
|
2020-06-06 17:50:21 +02:00
|
|
|
if (av_sscanf(buf1, "%f", &s->scale) != 1 || s->scale == 0.0 || !isfinite(s->scale)) {
|
2020-05-27 16:42:24 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Invalid scale.\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
s->endian = s->scale < 0.f;
|
|
|
|
s->scale = fabsf(s->scale);
|
|
|
|
s->maxval = (1ULL << 32) - 1;
|
|
|
|
} else if (avctx->pix_fmt != AV_PIX_FMT_MONOWHITE && avctx->pix_fmt != AV_PIX_FMT_MONOBLACK) {
|
2007-05-11 12:28:07 +03:00
|
|
|
pnm_get(s, buf1, sizeof(buf1));
|
|
|
|
s->maxval = atoi(buf1);
|
2016-11-10 21:31:02 +02:00
|
|
|
if (s->maxval <= 0 || s->maxval > UINT16_MAX) {
|
2011-01-10 02:42:57 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Invalid maxval: %d\n", s->maxval);
|
2011-01-11 00:09:52 +02:00
|
|
|
s->maxval = 255;
|
2011-01-10 02:42:57 +02:00
|
|
|
}
|
2008-09-14 18:50:59 +03:00
|
|
|
if (s->maxval >= 256) {
|
2012-10-06 13:10:34 +03:00
|
|
|
if (avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
|
2013-08-03 15:14:55 +03:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_GRAY16;
|
2012-10-06 13:10:34 +03:00
|
|
|
} else if (avctx->pix_fmt == AV_PIX_FMT_RGB24) {
|
2013-08-03 15:14:55 +03:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_RGB48;
|
2013-02-22 13:57:59 +03:00
|
|
|
} else if (avctx->pix_fmt == AV_PIX_FMT_YUV420P && s->maxval < 65536) {
|
|
|
|
if (s->maxval < 512)
|
2013-08-03 15:14:55 +03:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_YUV420P9;
|
2013-02-22 13:57:59 +03:00
|
|
|
else if (s->maxval < 1024)
|
2013-08-03 15:14:55 +03:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_YUV420P10;
|
2013-02-22 13:57:59 +03:00
|
|
|
else
|
|
|
|
avctx->pix_fmt = AV_PIX_FMT_YUV420P16;
|
2008-09-14 18:50:59 +03:00
|
|
|
} else {
|
2009-02-22 02:56:55 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format\n");
|
2012-10-06 13:10:34 +03:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_NONE;
|
2012-11-16 13:45:34 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2008-09-14 18:50:59 +03:00
|
|
|
}
|
2004-11-11 20:09:28 +02:00
|
|
|
}
|
2009-12-01 18:56:13 +02:00
|
|
|
}else
|
|
|
|
s->maxval=1;
|
2019-12-14 20:19:57 +02:00
|
|
|
|
|
|
|
if (!pnm_space(s->bytestream[-1]))
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2007-05-11 12:28:07 +03:00
|
|
|
/* more check if YUV420 */
|
2021-01-31 15:04:51 +02:00
|
|
|
if ((av_pix_fmt_desc_get(avctx->pix_fmt)->flags & AV_PIX_FMT_FLAG_PLANAR) &&
|
|
|
|
avctx->pix_fmt != AV_PIX_FMT_GBRPF32) {
|
2007-05-11 12:28:07 +03:00
|
|
|
if ((avctx->width & 1) != 0)
|
2012-11-16 13:45:34 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2007-05-11 12:28:07 +03:00
|
|
|
h = (avctx->height * 2);
|
|
|
|
if ((h % 3) != 0)
|
2012-11-16 13:45:34 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2007-05-11 12:28:07 +03:00
|
|
|
h /= 3;
|
|
|
|
avctx->height = h;
|
2004-11-11 20:09:28 +02:00
|
|
|
}
|
2007-05-11 12:28:07 +03:00
|
|
|
return 0;
|
2004-11-11 20:09:28 +02:00
|
|
|
}
|