[FFmpeg-devel] [PATCH] libavcodec/pgx: Added pgx decoder
Gautam Ramakrishnan
gautamramk at gmail.com
Thu Jun 25 09:56:56 EEST 2020
On Thu, Jun 25, 2020 at 12:50 AM Carl Eugen Hoyos <ceffmpeg at gmail.com> wrote:
>
> Am Mi., 24. Juni 2020 um 20:55 Uhr schrieb <gautamramk at gmail.com>:
> >
> > From: Gautam Ramakrishnan <gautamramk at gmail.com>
> >
> > This patch support to read and decode
> > pgx files.
> > ---
> > libavcodec/Makefile | 1 +
> > libavcodec/allcodecs.c | 1 +
> > libavcodec/codec_id.h | 1 +
> > libavcodec/pgx.h | 38 +++++++++
> > libavcodec/pgxdec.c | 176 +++++++++++++++++++++++++++++++++++++++
> > libavformat/allformats.c | 1 +
> > libavformat/img2dec.c | 11 +++
> > 7 files changed, 229 insertions(+)
> > create mode 100644 libavcodec/pgx.h
> > create mode 100644 libavcodec/pgxdec.c
> >
> > diff --git a/libavcodec/Makefile b/libavcodec/Makefile
> > index 5a6ea59715..0198c244e0 100644
> > --- a/libavcodec/Makefile
> > +++ b/libavcodec/Makefile
> > @@ -533,6 +533,7 @@ OBJS-$(CONFIG_PCX_ENCODER) += pcxenc.o
> > OBJS-$(CONFIG_PFM_DECODER) += pnmdec.o pnm.o
> > OBJS-$(CONFIG_PGM_DECODER) += pnmdec.o pnm.o
> > OBJS-$(CONFIG_PGM_ENCODER) += pnmenc.o
> > +OBJS-$(CONFIG_PGX_DECODER) += pgxdec.o
> > OBJS-$(CONFIG_PGMYUV_DECODER) += pnmdec.o pnm.o
> > OBJS-$(CONFIG_PGMYUV_ENCODER) += pnmenc.o
> > OBJS-$(CONFIG_PGSSUB_DECODER) += pgssubdec.o
> > diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
> > index fa0c08d42e..b0217e6d6a 100644
> > --- a/libavcodec/allcodecs.c
> > +++ b/libavcodec/allcodecs.c
> > @@ -236,6 +236,7 @@ extern AVCodec ff_pcx_decoder;
> > extern AVCodec ff_pfm_decoder;
> > extern AVCodec ff_pgm_encoder;
> > extern AVCodec ff_pgm_decoder;
> > +extern AVCodec ff_pgx_decoder;
> > extern AVCodec ff_pgmyuv_encoder;
> > extern AVCodec ff_pgmyuv_decoder;
> > extern AVCodec ff_pictor_decoder;
> > diff --git a/libavcodec/codec_id.h b/libavcodec/codec_id.h
> > index d885962c9c..027ef21c62 100644
> > --- a/libavcodec/codec_id.h
> > +++ b/libavcodec/codec_id.h
> > @@ -111,6 +111,7 @@ enum AVCodecID {
> > AV_CODEC_ID_PPM,
> > AV_CODEC_ID_PBM,
> > AV_CODEC_ID_PGM,
>
> > + AV_CODEC_ID_PGX,
>
> You cannot put this in the middle of an enum, it breaks abi.
>
Ok, I shall move it to the bottom
> > AV_CODEC_ID_PGMYUV,
> > AV_CODEC_ID_PAM,
> > AV_CODEC_ID_FFVHUFF,
> > diff --git a/libavcodec/pgx.h b/libavcodec/pgx.h
> > new file mode 100644
> > index 0000000000..bbe93fafe7
> > --- /dev/null
> > +++ b/libavcodec/pgx.h
> > @@ -0,0 +1,38 @@
> > +/*
> > + * PGX image format
> > + * Copyright (c) 2020 Gautam Ramakrishnan
> > + *
> > + * 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
> > + */
> > +
> > +#ifndef AVCODEC_PGX_H
> > +#define AVCODEC_PGX_H
> > +
> > +#include "avcodec.h"
> > +#include "bytestream.h"
> > +
> > +typedef struct PGXContext {
> > + GetByteContext g;
> > + int depth;
> > + int sgnd;
> > + int width;
> > + int height;
> > +} PGXContext;
> > +
> > +int ff_pgx_decode_header(AVCodecContext *avctx, PGXContext * const s);
> > +
> > +#endif /* AVCODEC_PNM_H */
>
> > \ No newline at end of file
>
> Apart from this:
> Why is the header file needed at all?
>
In case we add an encoder. We'll probably make the header file later then?
> > diff --git a/libavcodec/pgxdec.c b/libavcodec/pgxdec.c
> > new file mode 100644
> > index 0000000000..233bf34717
> > --- /dev/null
> > +++ b/libavcodec/pgxdec.c
> > @@ -0,0 +1,176 @@
> > +/*
> > + * PGX image format
> > + * Copyright (c) 2020 Gautam Ramakrishnan
> > + *
> > + * 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
> > + */
> > +
> > +#include "avcodec.h"
> > +#include "internal.h"
> > +#include "pgx.h"
> > +
> > +static int pgx_get_number(AVCodecContext *avctx, PGXContext * const s) {
> > + char number[10];
> > + int i;
> > + int ret;
> > + number[9] = '\0';
> > + for (i = 0; i < 9; i++) {
> > + if (!bytestream2_get_bytes_left(&s->g))
> > + return AVERROR_INVALIDDATA;
> > + number[i] = (char)bytestream2_get_byteu(&s->g);
> > + if (number[i] == ' ' || number[i] == 0xA || number[i] == 0xD) {
> > + number[i] = '\0';
> > + break;
> > + } else if (number[i] < '0' || number[i] > '9') {
> > + return AVERROR_INVALIDDATA;
> > + }
> > + }
> > + if (i > 9)
> > + return AVERROR_INVALIDDATA;
> > + ret = (int)strtol(number, NULL, 10);
> > + if (ret >= 0)
> > + return ret;
> > + return AVERROR_INVALIDDATA;
> > +}
> > +
> > +int ff_pgx_decode_header(AVCodecContext *avctx, PGXContext * s)
> > +{
> > + const char *header_start = "PG ML ";
> > + int i;
> > + int byte;
>
> > + if (bytestream2_get_bytes_left(&s->g) < 13) {
> > + return AVERROR_INVALIDDATA;
> > + }
>
> Is this needed, are other decoders testing this?
>
I shall change it.
> > +
> > + for (i = 0; i < 6; i++) {
> > + if (header_start[i] != (char)bytestream2_get_byteu(&s->g)) {
> > + return AVERROR_INVALIDDATA;
> > + }
>
> Use memcmp() or consider to drop this check:
> If a user forces this decoder, it should not be necessary to depend on this.
>
I did not understand this
> > + }
> > + byte = bytestream2_peek_byte(&s->g);
> > + if (byte == '+') {
> > + s->sgnd = 0;
> > + bytestream2_skip(&s->g, 1);
> > + } else if (byte == '-') {
> > + s->sgnd = 1;
> > + bytestream2_skip(&s->g, 1);
> > + }
>
> > +
> > + if (bytestream2_peek_byte(&s->g) == ' ')
> > + bytestream2_skip(&s->g, 1);
> > + s->depth = pgx_get_number(avctx, s);
> > + s->width = pgx_get_number(avctx, s);
> > + s->height = pgx_get_number(avctx, s);
> > + if (bytestream2_peek_byte(&s->g) == 0xA)
> > + bytestream2_skip(&s->g, 1);
> > + return 0;
> > +}
> > +
> > +static inline int write_frame_16(AVPacket *avpkt, AVFrame *frame, PGXContext * const s)
> > +{
> > + int i, j;
> > + for (i = 0; i < s->height; i++) {
> > + uint16_t* line = (uint16_t*)frame->data[0] + i*frame->linesize[0]/2;
> > + for (j = 0; j < s->width; j++) {
> > + int val;
> > + if (s->sgnd) {
> > + val = (int16_t)bytestream2_get_be16(&s->g) + (1 << (s->depth - 1));
> > + val <<= (16 - s->depth);
> > + *(line + j) = val;
> > + } else {
> > + val = bytestream2_get_be16u(&s->g);
> > + val <<= (16 - s->depth);
> > + *(line + j) = val;
> > + }
> > + }
> > + line += frame->linesize[0]/2;
> > + }
> > + return 0;
> > +}
> > +
> > +static inline int write_frame_8(AVPacket *avpkt, AVFrame *frame, PGXContext * const s)
> > +{
> > + int i, j;
> > + for (i = 0; i < s->height; i++) {
> > + uint8_t* line = frame->data[0] + i*frame->linesize[0];
> > + for (j = 0; j < s->width; j++) {
> > + int val;
> > + if (s->sgnd) {
> > + val = bytestream2_get_byte(&s->g) + (1 << (s->depth - 1));
> > + val <<= (8 - s->depth);
> > + *(line + j) = val;
> > + } else {
> > + val = bytestream2_get_byteu(&s->g);
> > + val <<= (8 - s->depth);
> > + *(line + j) = val;
> > + }
> > + }
> > + line += frame->linesize[0];
> > + }
> > + return 0;
> > +}
> > +
> > +static int pgx_decode_frame(AVCodecContext *avctx, void *data,
> > + int *got_frame, AVPacket *avpkt)
> > +{
> > + PGXContext * s = avctx->priv_data;
> > + AVFrame * const p = data;
>
> PGXContext *s = ...
>
> > + int ret;
> > + int num_vals;
> > + int bpp;
> > + bytestream2_init(&s->g, avpkt->data, avpkt->size);
> > + if ((ret = ff_pgx_decode_header(avctx, s)) < 0)
> > + return ret;
> > +
> > + num_vals = s->width * s->height;
> > +
> > + if (ret = ff_set_dimensions(avctx, s->width, s->height) < 0)
> > + return ret;
> > +
> > + if (s->depth <= 8) {
> > + avctx->pix_fmt = AV_PIX_FMT_GRAY8;
> > + bpp = 8;
> > + } else if (s->depth <= 16) {
> > + avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
> > + bpp = 16;
> > + } else {
> > + av_log(avctx, AV_LOG_ERROR, "Maximum depth of 16 bits supported.\n");
> > + return AVERROR_PATCHWELCOME;
> > + }
> > +
> > + if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
> > + return ret;
> > + *got_frame = 1;
> > + p->pict_type = AV_PICTURE_TYPE_I;
> > + p->key_frame = 1;
> > + avctx->bits_per_raw_sample = s->depth;
> > + if (bpp == 8)
> > + write_frame_8(avpkt, p, s);
> > + else if (bpp == 16)
> > + write_frame_16(avpkt, p, s);
> > + return 0;
> > +}
> > +
> > +AVCodec ff_pgx_decoder = {
> > + .name = "pgx",
> > + .long_name = NULL_IF_CONFIG_SMALL("PGX image"),
> > + .type = AVMEDIA_TYPE_VIDEO,
> > + .id = AV_CODEC_ID_PGX,
> > + .priv_data_size = sizeof(PGXContext),
> > + .decode = pgx_decode_frame,
> > + .capabilities = AV_CODEC_CAP_DR1,
> > +};
>
> > \ No newline at end of file
>
> Please fix this.
>
> > diff --git a/libavformat/allformats.c b/libavformat/allformats.c
>
> I believe other developers will request that you split the demuxer
> and the decoder patch.
>
The only issue is the demuxer refers to the decoder. How do I do this?
> > index 97fd06debb..f8527b1fd4 100644
> > --- a/libavformat/allformats.c
> > +++ b/libavformat/allformats.c
> > @@ -488,6 +488,7 @@ extern AVInputFormat ff_image_pbm_pipe_demuxer;
> > extern AVInputFormat ff_image_pcx_pipe_demuxer;
> > extern AVInputFormat ff_image_pgmyuv_pipe_demuxer;
> > extern AVInputFormat ff_image_pgm_pipe_demuxer;
> > +extern AVInputFormat ff_image_pgx_pipe_demuxer;
> > extern AVInputFormat ff_image_pictor_pipe_demuxer;
> > extern AVInputFormat ff_image_png_pipe_demuxer;
> > extern AVInputFormat ff_image_ppm_pipe_demuxer;
> > diff --git a/libavformat/img2dec.c b/libavformat/img2dec.c
> > index ee7ceed08f..43270901ff 100644
> > --- a/libavformat/img2dec.c
> > +++ b/libavformat/img2dec.c
> > @@ -1000,6 +1000,16 @@ static int pgmyuv_probe(const AVProbeData *p) // custom FFmpeg format recognized
> > return ret && av_match_ext(p->filename, "pgmyuv") ? ret : 0;
> > }
> >
> > +static int pgx_probe(const AVProbeData *p)
> > +{
> > + const uint8_t *b = p->buf;
> > + int ret = (AV_RB32(b) & 0xFFFFFF00) == 0x50472000;
>
> if (AV_RB24(b) != ...)
> return 0;
>
> > + ret = ret && av_match_ext(p->filename, "pgx");
> > + if (ret)
> > + return AVPROBE_SCORE_EXTENSION + 1;
>
> You should instead check if the file internally looks like pgx,
> checking the extension is a final possibility for things that
> are impossible to detect (which I think is not the case here).
>
The previous check does that. It checks if the first 3 bytes are PG and space.
> Carl Eugen
--
-------------
Gautam |
More information about the ffmpeg-devel
mailing list