2007-05-10 23:03:14 +00:00
|
|
|
/*
|
|
|
|
* PNM image parser
|
2009-01-19 15:46:40 +00:00
|
|
|
* Copyright (c) 2002, 2003 Fabrice Bellard
|
2007-05-10 23:03:14 +00:00
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* 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
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2015-10-14 09:33:24 +00:00
|
|
|
#include "libavutil/imgutils.h"
|
|
|
|
|
2007-05-10 23:03:14 +00:00
|
|
|
#include "parser.h" //for ParseContext
|
|
|
|
#include "pnm.h"
|
|
|
|
|
|
|
|
|
2009-10-27 16:57:35 +00:00
|
|
|
static int pnm_parse(AVCodecParserContext *s, AVCodecContext *avctx,
|
|
|
|
const uint8_t **poutbuf, int *poutbuf_size,
|
|
|
|
const uint8_t *buf, int buf_size)
|
2007-05-10 23:03:14 +00:00
|
|
|
{
|
|
|
|
ParseContext *pc = s->priv_data;
|
|
|
|
PNMContext pnmctx;
|
|
|
|
int next;
|
|
|
|
|
2009-10-27 16:57:35 +00:00
|
|
|
for (; pc->overread > 0; pc->overread--) {
|
2007-05-10 23:03:14 +00:00
|
|
|
pc->buffer[pc->index++]= pc->buffer[pc->overread_index++];
|
|
|
|
}
|
|
|
|
retry:
|
2009-10-27 16:57:35 +00:00
|
|
|
if (pc->index) {
|
|
|
|
pnmctx.bytestream_start =
|
|
|
|
pnmctx.bytestream = pc->buffer;
|
|
|
|
pnmctx.bytestream_end = pc->buffer + pc->index;
|
|
|
|
} else {
|
|
|
|
pnmctx.bytestream_start =
|
|
|
|
pnmctx.bytestream = (uint8_t *) buf; /* casts avoid warnings */
|
|
|
|
pnmctx.bytestream_end = (uint8_t *) buf + buf_size;
|
2007-05-10 23:03:14 +00:00
|
|
|
}
|
2009-10-27 16:57:35 +00:00
|
|
|
if (ff_pnm_decode_header(avctx, &pnmctx) < 0) {
|
|
|
|
if (pnmctx.bytestream < pnmctx.bytestream_end) {
|
|
|
|
if (pc->index) {
|
|
|
|
pc->index = 0;
|
|
|
|
} else {
|
2007-05-10 23:03:14 +00:00
|
|
|
buf++;
|
|
|
|
buf_size--;
|
|
|
|
}
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
#if 0
|
2015-06-29 21:48:34 +00:00
|
|
|
if (pc->index && pc->index * 2 + AV_INPUT_BUFFER_PADDING_SIZE < pc->buffer_size && buf_size > pc->index) {
|
2007-05-10 23:03:14 +00:00
|
|
|
memcpy(pc->buffer + pc->index, buf, pc->index);
|
|
|
|
pc->index += pc->index;
|
2009-10-27 16:57:35 +00:00
|
|
|
buf += pc->index;
|
|
|
|
buf_size -= pc->index;
|
2007-05-10 23:03:14 +00:00
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
#endif
|
2009-10-27 16:57:35 +00:00
|
|
|
next = END_NOT_FOUND;
|
|
|
|
} else {
|
|
|
|
next = pnmctx.bytestream - pnmctx.bytestream_start
|
2015-10-14 09:33:24 +00:00
|
|
|
+ av_image_get_buffer_size(avctx->pix_fmt, avctx->width, avctx->height, 1);
|
2009-10-27 16:57:35 +00:00
|
|
|
if (pnmctx.bytestream_start != buf)
|
|
|
|
next -= pc->index;
|
|
|
|
if (next > buf_size)
|
|
|
|
next = END_NOT_FOUND;
|
2007-05-10 23:03:14 +00:00
|
|
|
}
|
|
|
|
|
2009-10-27 16:57:35 +00:00
|
|
|
if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
|
|
|
|
*poutbuf = NULL;
|
2007-05-10 23:03:14 +00:00
|
|
|
*poutbuf_size = 0;
|
|
|
|
return buf_size;
|
|
|
|
}
|
2009-10-27 16:57:35 +00:00
|
|
|
*poutbuf = buf;
|
2007-05-10 23:03:14 +00:00
|
|
|
*poutbuf_size = buf_size;
|
|
|
|
return next;
|
|
|
|
}
|
|
|
|
|
2011-01-25 21:40:11 +00:00
|
|
|
AVCodecParser ff_pnm_parser = {
|
2012-08-05 09:11:04 +00:00
|
|
|
.codec_ids = { AV_CODEC_ID_PGM, AV_CODEC_ID_PGMYUV, AV_CODEC_ID_PPM,
|
|
|
|
AV_CODEC_ID_PBM, AV_CODEC_ID_PAM },
|
2011-11-02 08:34:41 +00:00
|
|
|
.priv_data_size = sizeof(ParseContext),
|
|
|
|
.parser_parse = pnm_parse,
|
|
|
|
.parser_close = ff_parse_close,
|
2007-05-10 23:03:14 +00:00
|
|
|
};
|