[FFmpeg-devel] [PATCH] PDV format support

Michael Niedermayer michael at niedermayer.cc
Wed Apr 19 00:07:05 EEST 2023


On Mon, Apr 17, 2023 at 08:46:15PM +0200, Paul B Mahol wrote:
> Patches attached.

>  Makefile     |    1 
>  allformats.c |    1 
>  pdvdec.c     |  172 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 174 insertions(+)
> c1a534f179e76dfe3731528666d37032b57fb5b0  0002-avformat-add-PDV-demuxer.patch
> From acf25fc6ca24838196316b81b25b753d01adbfab Mon Sep 17 00:00:00 2001
> From: Paul B Mahol <onemda at gmail.com>
> Date: Mon, 17 Apr 2023 17:45:23 +0200
> Subject: [PATCH 2/2] avformat: add PDV demuxer
> 
> Signed-off-by: Paul B Mahol <onemda at gmail.com>
> ---
>  libavformat/Makefile     |   1 +
>  libavformat/allformats.c |   1 +
>  libavformat/pdvdec.c     | 172 +++++++++++++++++++++++++++++++++++++++
>  3 files changed, 174 insertions(+)
>  create mode 100644 libavformat/pdvdec.c
> 
> diff --git a/libavformat/Makefile b/libavformat/Makefile
> index 048649689b..f8ad7c6a11 100644
> --- a/libavformat/Makefile
> +++ b/libavformat/Makefile
> @@ -471,6 +471,7 @@ OBJS-$(CONFIG_PCM_U8_DEMUXER)            += pcmdec.o pcm.o
>  OBJS-$(CONFIG_PCM_U8_MUXER)              += pcmenc.o rawenc.o
>  OBJS-$(CONFIG_PCM_VIDC_DEMUXER)          += pcmdec.o pcm.o
>  OBJS-$(CONFIG_PCM_VIDC_MUXER)            += pcmenc.o rawenc.o
> +OBJS-$(CONFIG_PDV_DEMUXER)               += pdvdec.o
>  OBJS-$(CONFIG_PJS_DEMUXER)               += pjsdec.o subtitles.o
>  OBJS-$(CONFIG_PMP_DEMUXER)               += pmpdec.o
>  OBJS-$(CONFIG_PP_BNK_DEMUXER)            += pp_bnk.o
> diff --git a/libavformat/allformats.c b/libavformat/allformats.c
> index cb5b69e9cd..efdb34e29d 100644
> --- a/libavformat/allformats.c
> +++ b/libavformat/allformats.c
> @@ -369,6 +369,7 @@ extern const AVInputFormat  ff_pcm_u16le_demuxer;
>  extern const FFOutputFormat ff_pcm_u16le_muxer;
>  extern const AVInputFormat  ff_pcm_u8_demuxer;
>  extern const FFOutputFormat ff_pcm_u8_muxer;
> +extern const AVInputFormat  ff_pdv_demuxer;
>  extern const AVInputFormat  ff_pjs_demuxer;
>  extern const AVInputFormat  ff_pmp_demuxer;
>  extern const AVInputFormat  ff_pp_bnk_demuxer;
> diff --git a/libavformat/pdvdec.c b/libavformat/pdvdec.c
> new file mode 100644
> index 0000000000..3dfef819a9
> --- /dev/null
> +++ b/libavformat/pdvdec.c
> @@ -0,0 +1,172 @@
> +/*
> + * PDV demuxer
> + * Copyright (c) 2023 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
> + */
> +
> +#include "libavutil/avstring.h"
> +#include "libavutil/imgutils.h"
> +
> +#include "avformat.h"
> +#include "internal.h"
> +
> +#define PDV_MAGIC "Playdate VID"
> +
> +typedef struct PDVDemuxContext {
> +    int current_frame;
> +    uint8_t *frame_flags;
> +    uint32_t *frame_offsets;
> +} PDVDemuxContext;
> +
> +static int pdv_probe(const AVProbeData *pd)
> +{
> +    if (strncmp(pd->buf, PDV_MAGIC, sizeof(PDV_MAGIC) - 1) == 0)
> +        return AVPROBE_SCORE_MAX;
> +    return 0;
> +}
> +
> +static int pdv_read_header(AVFormatContext *s)
> +{
> +    PDVDemuxContext *p = s->priv_data;
> +    AVIOContext *pb = s->pb;
> +    AVCodecParameters *par;
> +    AVStream *st;
> +    uint64_t start;
> +    uint32_t fps;
> +
> +    avio_skip(pb, 16);
> +
> +    st = avformat_new_stream(s, NULL);
> +    if (!st)
> +        return AVERROR(ENOMEM);
> +
> +    par              = st->codecpar;
> +    par->codec_type  = AVMEDIA_TYPE_VIDEO;
> +    par->codec_id    = AV_CODEC_ID_PDV;
> +    st->start_time   = 0;
> +    st->duration     =
> +    st->nb_frames    = avio_rl16(pb);
> +    avio_skip(pb, 2);
> +    fps = avio_rl32(pb);
> +    st->avg_frame_rate = av_d2q(av_int2float(fps), INT_MAX);
> +    par->width       = avio_rl16(pb);
> +    par->height      = avio_rl16(pb);
> +
> +    avpriv_set_pts_info(st, 64, st->avg_frame_rate.den, st->avg_frame_rate.num);
> +
> +    p->current_frame = 0;
> +    p->frame_flags = av_calloc(st->nb_frames + 1, sizeof(*p->frame_flags));
> +    p->frame_offsets = av_calloc(st->nb_frames + 1, sizeof(*p->frame_offsets));
> +
> +    if (!p->frame_flags || !p->frame_offsets)
> +        return AVERROR(ENOMEM);
> +
> +    for (int n = 0; n <= st->nb_frames; n++) {
> +        const uint32_t entry = avio_rl32(pb);
> +
> +        p->frame_flags[n] = entry & 3;
> +        p->frame_offsets[n] = entry >> 2;
> +    }
> +
> +    start = avio_tell(pb);
> +
> +    for (int n = 0; n < st->nb_frames; n++) {
> +        const uint64_t pos = start + p->frame_offsets[n];
> +        const int32_t size = p->frame_offsets[n+1] - p->frame_offsets[n];
> +        const int flags = p->frame_flags[n] & 1 ? AVINDEX_KEYFRAME : 0;
> +
> +        if (p->frame_flags[n] == 0 || size <= 0 || pos + size > avio_size(pb))
> +            break;
> +        av_add_index_entry(st, pos, n, size, 0, flags);
> +    }
> +
> +    return 0;
> +}
> +
> +static int pdv_read_packet(AVFormatContext *s, AVPacket *pkt)
> +{
> +    PDVDemuxContext *p = s->priv_data;
> +    AVStream *st = s->streams[0];
> +    FFStream *const sti = ffstream(st);
> +    AVIOContext *pb = s->pb;
> +    int32_t size, flags, ret;
> +    int64_t pos;
> +
> +    if (p->current_frame >= st->nb_frames)
> +        return AVERROR_EOF;
> +
> +    pos   = sti->index_entries[p->current_frame].pos;
> +    flags = sti->index_entries[p->current_frame].flags;
> +    size  = sti->index_entries[p->current_frame].size;
> +
> +    avio_seek(pb, pos, SEEK_SET);
> +    if (avio_feof(pb) || pos + size > avio_size(pb) || size == 0)
> +        return AVERROR_EOF;
> +
> +    ret = av_get_packet(pb, pkt, size);
> +    if (ret < 0)
> +        return ret;
> +
> +    if (flags & AVINDEX_KEYFRAME)
> +        pkt->flags |= AV_PKT_FLAG_KEY;
> +    pkt->stream_index = 0;
> +    pkt->pts = p->current_frame++;
> +    pkt->duration = 1;
> +
> +    return 0;
> +}
> +
> +static int pdv_read_close(AVFormatContext *s)
> +{
> +    PDVDemuxContext *p = s->priv_data;
> +
> +    av_freep(&p->frame_flags);
> +    av_freep(&p->frame_offsets);
> +
> +    return 0;
> +}
> +
> +static int pdv_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
> +{
> +    PDVDemuxContext *p = s->priv_data;
> +    AVStream *st = s->streams[stream_index];
> +    int index = av_index_search_timestamp(st, timestamp, flags);
> +
> +    if (index < 0)
> +        return -1;
> +
> +    if (avio_seek(s->pb, ffstream(st)->index_entries[index].pos, SEEK_SET) < 0)
> +        return -1;
> +
> +    p->current_frame = index;
> +
> +    return 0;
> +}
> +
> +const AVInputFormat ff_pdv_demuxer = {
> +    .name           = "pdv",
> +    .long_name      = NULL_IF_CONFIG_SMALL("PlayDate Video"),
> +    .priv_data_size = sizeof(PDVDemuxContext),
> +    .flags_internal = FF_FMT_INIT_CLEANUP,
> +    .read_probe     = pdv_probe,
> +    .read_header    = pdv_read_header,
> +    .read_packet    = pdv_read_packet,
> +    .read_close     = pdv_read_close,
> +    .read_seek      = pdv_read_seek,
> +    .extensions     = "pdv",
> +};
> -- 
> 2.39.1
> 

>  Makefile     |    1 
>  allcodecs.c  |    1 
>  codec_desc.c |    7 +++
>  codec_id.h   |    1 
>  pdvdec.c     |  127 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  5 files changed, 137 insertions(+)
> 8b37d78c146e291e2a693a16ae0b9241699a4a69  0001-avcodec-add-PDV-decoder.patch
> From 5a26b13f589c96075fa1dc269cc768163afe1651 Mon Sep 17 00:00:00 2001
> From: Paul B Mahol <onemda at gmail.com>
> Date: Mon, 17 Apr 2023 19:19:42 +0200
> Subject: [PATCH 1/2] avcodec: add PDV decoder
> 
> Signed-off-by: Paul B Mahol <onemda at gmail.com>
> ---
>  libavcodec/Makefile     |   1 +
>  libavcodec/allcodecs.c  |   1 +
>  libavcodec/codec_desc.c |   7 +++
>  libavcodec/codec_id.h   |   1 +
>  libavcodec/pdvdec.c     | 127 ++++++++++++++++++++++++++++++++++++++++
>  5 files changed, 137 insertions(+)

[...]

> diff --git a/libavcodec/pdvdec.c b/libavcodec/pdvdec.c
> new file mode 100644
> index 0000000000..8359feac0d
> --- /dev/null
> +++ b/libavcodec/pdvdec.c
> @@ -0,0 +1,127 @@
> +/*
> + * PDV video format
> + *
> + * Copyright (c) 2023 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
> + */
> +
> +#include "avcodec.h"
> +#include "codec_internal.h"
> +#include "decode.h"
> +#include "zlib_wrapper.h"
> +
> +#include <zlib.h>

some zlib depenacy in configure is needed

[...]

-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

Those who are too smart to engage in politics are punished by being
governed by those who are dumber. -- Plato 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 195 bytes
Desc: not available
URL: <https://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20230418/e19c05a4/attachment.sig>


More information about the ffmpeg-devel mailing list