[FFmpeg-devel] [PATCH] avcodec: add photocd parser

James Almer jamrial at gmail.com
Fri Feb 5 21:38:47 EET 2021


On 2/5/2021 3:51 PM, Paul B Mahol wrote:
> Signed-off-by: Paul B Mahol <onemda at gmail.com>
> ---
>   libavcodec/Makefile         |  1 +
>   libavcodec/parsers.c        |  1 +
>   libavcodec/photocd_parser.c | 87 +++++++++++++++++++++++++++++++++++++
>   3 files changed, 89 insertions(+)
>   create mode 100644 libavcodec/photocd_parser.c
> 
> diff --git a/libavcodec/Makefile b/libavcodec/Makefile
> index 2ddf021f04..b06b028ae4 100644
> --- a/libavcodec/Makefile
> +++ b/libavcodec/Makefile
> @@ -1114,6 +1114,7 @@ OBJS-$(CONFIG_MPEGVIDEO_PARSER)        += mpegvideo_parser.o    \
>                                             mpeg12.o mpeg12data.o
>   OBJS-$(CONFIG_OPUS_PARSER)             += opus_parser.o opus.o opustab.o \
>                                             opus_rc.o vorbis_data.o
> +OBJS-$(CONFIG_PHOTOCD_PARSER)          += photocd_parser.o
>   OBJS-$(CONFIG_PNG_PARSER)              += png_parser.o
>   OBJS-$(CONFIG_PNM_PARSER)              += pnm_parser.o pnm.o
>   OBJS-$(CONFIG_RV30_PARSER)             += rv34_parser.o
> diff --git a/libavcodec/parsers.c b/libavcodec/parsers.c
> index 93af25ac14..d3353a6b39 100644
> --- a/libavcodec/parsers.c
> +++ b/libavcodec/parsers.c
> @@ -59,6 +59,7 @@ extern AVCodecParser ff_mpeg4video_parser;
>   extern AVCodecParser ff_mpegaudio_parser;
>   extern AVCodecParser ff_mpegvideo_parser;
>   extern AVCodecParser ff_opus_parser;
> +extern AVCodecParser ff_photocd_parser;
>   extern AVCodecParser ff_png_parser;
>   extern AVCodecParser ff_pnm_parser;
>   extern AVCodecParser ff_rv30_parser;
> diff --git a/libavcodec/photocd_parser.c b/libavcodec/photocd_parser.c
> new file mode 100644
> index 0000000000..844327f799
> --- /dev/null
> +++ b/libavcodec/photocd_parser.c
> @@ -0,0 +1,87 @@
> +/*
> + * PhotoCD parser
> + * Copyright (c) 2021 Paul B Mahol
> + *
> + * 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
> + */
> +
> +/**
> + * @file
> + * PhotoCD parser
> + */
> +
> +#include "libavutil/common.h"
> +
> +#include "parser.h"
> +
> +typedef struct PhotoCDParser {
> +    ParseContext pc;
> +    int count;
> +} PhotoCDParser;
> +
> +#define KEY (((uint64_t)'P' << 56) | ((uint64_t)'C' << 48) | \
> +             ((uint64_t)'D' << 40) | ((uint64_t)'_' << 32) | \
> +             ((uint64_t)'I' << 24) | ((uint64_t)'P' << 16) | \
> +             ((uint64_t)'I' <<  8) | ((uint64_t)'\0'<<  0))

You could do something like state == MKBETAG('P', 'C', 'D', '_') and 
(state >> 32) == MKBETAG('I', 'P', 'I', '\0') instead.

> +
> +static int photocd_parse(AVCodecParserContext *s, AVCodecContext *avctx,
> +                     const uint8_t **poutbuf, int *poutbuf_size,
> +                     const uint8_t *buf, int buf_size)
> +{
> +    PhotoCDParser *bpc = s->priv_data;
> +    uint64_t state = bpc->pc.state64;
> +    int next = END_NOT_FOUND, i = 0;
> +
> +    s->pict_type = AV_PICTURE_TYPE_I;
> +    s->key_frame = 1;
> +    s->duration  = 1;
> +
> +    *poutbuf_size = 0;
> +    *poutbuf = NULL;
> +
> +    for (; i < buf_size; i++) {

for (int i = 0...

> +        state = (state << 8) | buf[i];
> +
> +        if (state == KEY)
> +            bpc->count++;
> +
> +        if (state == KEY && bpc->count >= 2) {
> +            next = i - (7+2048);
> +            bpc->count = 0;
> +            break;
> +        }
> +    }

https://0x0.st/-HiX.pcd (Crafted file).

I can reproduce an infinite loop by doing "cat INPUT | ./ffmpeg -i -" 
with the above sample after this patch.

> +
> +    bpc->pc.state64 = state;
> +    if (ff_combine_frame(&bpc->pc, next, &buf, &buf_size) < 0) {
> +        *poutbuf = NULL;
> +        *poutbuf_size = 0;
> +        return buf_size;
> +    }
> +
> +    *poutbuf      = buf;
> +    *poutbuf_size = buf_size;
> +
> +    return next;
> +}
> +
> +AVCodecParser ff_photocd_parser = {
> +    .codec_ids      = { AV_CODEC_ID_PHOTOCD },
> +    .priv_data_size = sizeof(PhotoCDParser),
> +    .parser_parse   = photocd_parse,
> +    .parser_close   = ff_parse_close,
> +};
> 



More information about the ffmpeg-devel mailing list