2007-05-11 02:03:14 +03:00
|
|
|
/*
|
|
|
|
* PNM image parser
|
2009-01-19 17:46:40 +02:00
|
|
|
* Copyright (c) 2002, 2003 Fabrice Bellard
|
2007-05-11 02:03:14 +03:00
|
|
|
*
|
2011-03-18 19:35:10 +02:00
|
|
|
* This file is part of Libav.
|
2007-05-11 02:03:14 +03:00
|
|
|
*
|
2011-03-18 19:35:10 +02:00
|
|
|
* Libav is free software; you can redistribute it and/or
|
2007-05-11 02:03:14 +03:00
|
|
|
* 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.
|
|
|
|
*
|
2011-03-18 19:35:10 +02:00
|
|
|
* Libav is distributed in the hope that it will be useful,
|
2007-05-11 02:03:14 +03: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
|
2011-03-18 19:35:10 +02:00
|
|
|
* License along with Libav; if not, write to the Free Software
|
2007-05-11 02:03:14 +03:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "parser.h" //for ParseContext
|
|
|
|
#include "pnm.h"
|
|
|
|
|
|
|
|
|
2009-10-27 18:57:35 +02: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-11 02:03:14 +03:00
|
|
|
{
|
|
|
|
ParseContext *pc = s->priv_data;
|
|
|
|
PNMContext pnmctx;
|
|
|
|
int next;
|
|
|
|
|
2009-10-27 18:57:35 +02:00
|
|
|
for (; pc->overread > 0; pc->overread--) {
|
2007-05-11 02:03:14 +03:00
|
|
|
pc->buffer[pc->index++]= pc->buffer[pc->overread_index++];
|
|
|
|
}
|
|
|
|
retry:
|
2009-10-27 18:57:35 +02: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-11 02:03:14 +03:00
|
|
|
}
|
2009-10-27 18:57:35 +02:00
|
|
|
if (ff_pnm_decode_header(avctx, &pnmctx) < 0) {
|
|
|
|
if (pnmctx.bytestream < pnmctx.bytestream_end) {
|
|
|
|
if (pc->index) {
|
|
|
|
pc->index = 0;
|
|
|
|
} else {
|
2007-05-11 02:03:14 +03:00
|
|
|
buf++;
|
|
|
|
buf_size--;
|
|
|
|
}
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
#if 0
|
2009-10-27 18:57:35 +02:00
|
|
|
if (pc->index && pc->index * 2 + FF_INPUT_BUFFER_PADDING_SIZE < pc->buffer_size && buf_size > pc->index) {
|
2007-05-11 02:03:14 +03:00
|
|
|
memcpy(pc->buffer + pc->index, buf, pc->index);
|
|
|
|
pc->index += pc->index;
|
2009-10-27 18:57:35 +02:00
|
|
|
buf += pc->index;
|
|
|
|
buf_size -= pc->index;
|
2007-05-11 02:03:14 +03:00
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
#endif
|
2009-10-27 18:57:35 +02:00
|
|
|
next = END_NOT_FOUND;
|
|
|
|
} else {
|
|
|
|
next = pnmctx.bytestream - pnmctx.bytestream_start
|
|
|
|
+ avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
|
|
|
|
if (pnmctx.bytestream_start != buf)
|
|
|
|
next -= pc->index;
|
|
|
|
if (next > buf_size)
|
|
|
|
next = END_NOT_FOUND;
|
2007-05-11 02:03:14 +03:00
|
|
|
}
|
|
|
|
|
2009-10-27 18:57:35 +02:00
|
|
|
if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
|
|
|
|
*poutbuf = NULL;
|
2007-05-11 02:03:14 +03:00
|
|
|
*poutbuf_size = 0;
|
|
|
|
return buf_size;
|
|
|
|
}
|
2009-10-27 18:57:35 +02:00
|
|
|
*poutbuf = buf;
|
2007-05-11 02:03:14 +03:00
|
|
|
*poutbuf_size = buf_size;
|
|
|
|
return next;
|
|
|
|
}
|
|
|
|
|
2011-01-25 23:40:11 +02:00
|
|
|
AVCodecParser ff_pnm_parser = {
|
2007-05-11 02:03:14 +03:00
|
|
|
{ CODEC_ID_PGM, CODEC_ID_PGMYUV, CODEC_ID_PPM, CODEC_ID_PBM, CODEC_ID_PAM},
|
|
|
|
sizeof(ParseContext),
|
|
|
|
NULL,
|
|
|
|
pnm_parse,
|
|
|
|
ff_parse_close,
|
|
|
|
};
|