From: Paul B Mahol <onemda@gmail.com> To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org> Subject: [FFmpeg-devel] [PATCH] PDV format support Date: Mon, 17 Apr 2023 20:46:15 +0200 Message-ID: <CAPYw7P71Zj_BazLTW5Pqe4g9=bTqtN2wNRRU_WW7KaVbgASfEg@mail.gmail.com> (raw) [-- Attachment #1: Type: text/plain, Size: 18 bytes --] Patches attached. [-- Attachment #2: 0002-avformat-add-PDV-demuxer.patch --] [-- Type: text/x-patch, Size: 6868 bytes --] From acf25fc6ca24838196316b81b25b753d01adbfab Mon Sep 17 00:00:00 2001 From: Paul B Mahol <onemda@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@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 [-- Attachment #3: 0001-avcodec-add-PDV-decoder.patch --] [-- Type: text/x-patch, Size: 6583 bytes --] From 5a26b13f589c96075fa1dc269cc768163afe1651 Mon Sep 17 00:00:00 2001 From: Paul B Mahol <onemda@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@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(+) create mode 100644 libavcodec/pdvdec.c diff --git a/libavcodec/Makefile b/libavcodec/Makefile index aa10fbfcf8..a1a8b8b88e 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -583,6 +583,7 @@ OBJS-$(CONFIG_PBM_DECODER) += pnmdec.o pnm.o OBJS-$(CONFIG_PBM_ENCODER) += pnmenc.o OBJS-$(CONFIG_PCX_DECODER) += pcx.o OBJS-$(CONFIG_PCX_ENCODER) += pcxenc.o +OBJS-$(CONFIG_PDV_DECODER) += pdvdec.o OBJS-$(CONFIG_PFM_DECODER) += pnmdec.o pnm.o OBJS-$(CONFIG_PFM_ENCODER) += pnmenc.o OBJS-$(CONFIG_PGM_DECODER) += pnmdec.o pnm.o diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index 797fe39486..8eeed34e57 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -251,6 +251,7 @@ extern const FFCodec ff_pbm_encoder; extern const FFCodec ff_pbm_decoder; extern const FFCodec ff_pcx_encoder; extern const FFCodec ff_pcx_decoder; +extern const FFCodec ff_pdv_decoder; extern const FFCodec ff_pfm_encoder; extern const FFCodec ff_pfm_decoder; extern const FFCodec ff_pgm_encoder; diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c index efdcb59bc9..d40977d6b3 100644 --- a/libavcodec/codec_desc.c +++ b/libavcodec/codec_desc.c @@ -1923,6 +1923,13 @@ static const AVCodecDescriptor codec_descriptors[] = { .long_name = NULL_IF_CONFIG_SMALL("ViewQuest VQC"), .props = AV_CODEC_PROP_LOSSY, }, + { + .id = AV_CODEC_ID_PDV, + .type = AVMEDIA_TYPE_VIDEO, + .name = "pdv", + .long_name = NULL_IF_CONFIG_SMALL("PDV (PlayDate Video)"), + .props = AV_CODEC_PROP_LOSSY, + }, /* various PCM "codecs" */ { diff --git a/libavcodec/codec_id.h b/libavcodec/codec_id.h index 64df9699f4..70800ec20b 100644 --- a/libavcodec/codec_id.h +++ b/libavcodec/codec_id.h @@ -320,6 +320,7 @@ enum AVCodecID { AV_CODEC_ID_WBMP, AV_CODEC_ID_MEDIA100, AV_CODEC_ID_VQC, + AV_CODEC_ID_PDV, /* various PCM "codecs" */ AV_CODEC_ID_FIRST_AUDIO = 0x10000, ///< A dummy id pointing at the start of audio codecs 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> + +typedef struct PDVContext { + AVFrame *previous_frame; + FFZStream zstream; +} PDVContext; + +static av_cold int decode_init(AVCodecContext *avctx) +{ + PDVContext *s = avctx->priv_data; + + avctx->pix_fmt = AV_PIX_FMT_MONOWHITE; + + s->previous_frame = av_frame_alloc(); + if (!s->previous_frame) + return AVERROR(ENOMEM); + + return ff_inflate_init(&s->zstream, avctx); +} + +static av_cold int decode_end(AVCodecContext *avctx) +{ + PDVContext *s = avctx->priv_data; + + av_frame_free(&s->previous_frame); + ff_inflate_end(&s->zstream); + + return 0; +} + +static int decode_frame(AVCodecContext *avctx, AVFrame *frame, + int *got_frame, AVPacket *avpkt) +{ + PDVContext *s = avctx->priv_data; + AVFrame *prev_frame = s->previous_frame; + z_stream *const zstream = &s->zstream.zstream; + uint8_t *dst, *prev = prev_frame->data[0]; + int ret, zret; + + zret = inflateReset(zstream); + if (zret != Z_OK) { + av_log(avctx, AV_LOG_ERROR, "Could not reset inflate: %d.\n", zret); + return AVERROR_INVALIDDATA; + } + + if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0) + return ret; + + zstream->next_in = avpkt->data; + zstream->avail_in = avpkt->size; + + dst = frame->data[0]; + for (int i = 0; i < avctx->height; i++) { + zstream->next_out = dst; + zstream->avail_out = (avctx->width + 7) >> 3; + + zret = inflate(zstream, Z_SYNC_FLUSH); + if (zret != Z_OK && zret != Z_STREAM_END) { + av_log(avctx, AV_LOG_ERROR, + "Inflate failed with return code: %d.\n", zret); + return AVERROR_INVALIDDATA; + } + + if (!(avpkt->flags & AV_PKT_FLAG_KEY)) { + for (int j = 0; j < (avctx->width + 7) >> 3; j++) + dst[j] ^= prev[j]; + prev += prev_frame->linesize[0]; + } + + dst += frame->linesize[0]; + } + + av_frame_unref(s->previous_frame); + if ((ret = av_frame_ref(s->previous_frame, frame)) < 0) + return ret; + + if (avpkt->flags & AV_PKT_FLAG_KEY) { + frame->key_frame = 1; + frame->pict_type = AV_PICTURE_TYPE_I; + } else { + frame->pict_type = AV_PICTURE_TYPE_P; + } + + *got_frame = 1; + + return avpkt->size; +} + +const FFCodec ff_pdv_decoder = { + .p.name = "pdv", + CODEC_LONG_NAME("PDV (PlayDate Video)"), + .priv_data_size = sizeof(PDVContext), + .p.type = AVMEDIA_TYPE_VIDEO, + .p.id = AV_CODEC_ID_PDV, + .p.capabilities = AV_CODEC_CAP_DR1, + .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM, + .init = decode_init, + .close = decode_end, + FF_CODEC_DECODE_CB(decode_frame), +}; -- 2.39.1 [-- Attachment #4: Type: text/plain, Size: 251 bytes --] _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
next reply other threads:[~2023-04-17 18:46 UTC|newest] Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top 2023-04-17 18:46 Paul B Mahol [this message] 2023-04-18 21:07 ` Michael Niedermayer 2023-04-20 7:06 ` Paul B Mahol
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to='CAPYw7P71Zj_BazLTW5Pqe4g9=bTqtN2wNRRU_WW7KaVbgASfEg@mail.gmail.com' \ --to=onemda@gmail.com \ --cc=ffmpeg-devel@ffmpeg.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel This inbox may be cloned and mirrored by anyone: git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git # If you have public-inbox 1.1+ installed, you may # initialize and index your mirror using the following commands: public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \ ffmpegdev@gitmailbox.com public-inbox-index ffmpegdev Example config snippet for mirrors. AGPL code for this site: git clone https://public-inbox.org/public-inbox.git