Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Lynne via ffmpeg-devel <ffmpeg-devel@ffmpeg.org>
To: ffmpeg-devel@ffmpeg.org
Cc: Lynne <dev@lynne.ee>
Subject: Re: [FFmpeg-devel] [PATCH v13] avformat: add farbfeld muxer and demuxer
Date: Tue, 2 Jul 2024 02:20:06 +0200
Message-ID: <07879641-52e8-4ec1-8e3d-f884f8b77ccc@lynne.ee> (raw)
In-Reply-To: <20240701232818.3883-2-marcus@marcusspencer.xyz>


[-- Attachment #1.1.1.1: Type: text/plain, Size: 12726 bytes --]

On 02/07/2024 01:28, Marcus B Spencer wrote:
> farbfeld is an uncompressed image format that is a part of suckless
> tools (https://tools.suckless.org).
> 
> Its documentation is available at https://tools.suckless.org/farbfeld.
> 
> Add support for this image format in avformat.
> 
> NOTE: The demuxer is experimental.

Is there a reason the demuxer is experimental? We generally don't do 
experimental decoders or demuxers.

> 
> Signed-off-by: Marcus B Spencer <marcus@marcusspencer.xyz>
> ---
>   Changelog                 |   1 +
>   doc/general_contents.texi |   1 +
>   libavformat/Makefile      |   2 +
>   libavformat/allformats.c  |   2 +
>   libavformat/farbfelddec.c |  66 +++++++++++++++
>   libavformat/farbfeldenc.c | 164 ++++++++++++++++++++++++++++++++++++++
>   libavformat/version.h     |   2 +-
>   7 files changed, 237 insertions(+), 1 deletion(-)
>   create mode 100644 libavformat/farbfelddec.c
>   create mode 100644 libavformat/farbfeldenc.c
> 
> diff --git a/Changelog b/Changelog
> index e6a40b0252..823773ce2c 100644
> --- a/Changelog
> +++ b/Changelog
> @@ -15,6 +15,7 @@ version <next>:
>   - removed DEC Alpha DSP and support code
>   - VVC encoding support via libvvenc
>   - perlin video source
> +- farbfeld muxer and demuxer
>   
>   
>   version 7.0:
> diff --git a/doc/general_contents.texi b/doc/general_contents.texi
> index e7cf4f8239..9929c23654 100644
> --- a/doc/general_contents.texi
> +++ b/doc/general_contents.texi
> @@ -786,6 +786,7 @@ following image formats are supported:
>       @tab Digital Picture Exchange
>   @item EXR          @tab   @tab X
>       @tab OpenEXR
> +@item FF           @tab X @tab X
>   @item FITS         @tab X @tab X
>       @tab Flexible Image Transport System
>   @item HDR          @tab X @tab X
> diff --git a/libavformat/Makefile b/libavformat/Makefile
> index 7ca68a7036..ab8f8216ff 100644
> --- a/libavformat/Makefile
> +++ b/libavformat/Makefile
> @@ -204,6 +204,8 @@ OBJS-$(CONFIG_EA_DEMUXER)                += electronicarts.o
>   OBJS-$(CONFIG_EAC3_DEMUXER)              += ac3dec.o rawdec.o
>   OBJS-$(CONFIG_EAC3_MUXER)                += rawenc.o
>   OBJS-$(CONFIG_EPAF_DEMUXER)              += epafdec.o pcm.o
> +OBJS-$(CONFIG_FARBFELD_DEMUXER)          += farbfelddec.o
> +OBJS-$(CONFIG_FARBFELD_MUXER)            += farbfeldenc.o
>   OBJS-$(CONFIG_FFMETADATA_DEMUXER)        += ffmetadec.o
>   OBJS-$(CONFIG_FFMETADATA_MUXER)          += ffmetaenc.o
>   OBJS-$(CONFIG_FIFO_MUXER)                += fifo.o
> diff --git a/libavformat/allformats.c b/libavformat/allformats.c
> index 305fa46532..0cda72c195 100644
> --- a/libavformat/allformats.c
> +++ b/libavformat/allformats.c
> @@ -161,6 +161,8 @@ extern const FFOutputFormat ff_eac3_muxer;
>   extern const FFInputFormat  ff_epaf_demuxer;
>   extern const FFInputFormat  ff_evc_demuxer;
>   extern const FFOutputFormat ff_evc_muxer;
> +extern const FFInputFormat  ff_farbfeld_demuxer;
> +extern const FFOutputFormat ff_farbfeld_muxer;
>   extern const FFOutputFormat ff_f4v_muxer;
>   extern const FFInputFormat  ff_ffmetadata_demuxer;
>   extern const FFOutputFormat ff_ffmetadata_muxer;
> diff --git a/libavformat/farbfelddec.c b/libavformat/farbfelddec.c
> new file mode 100644
> index 0000000000..a87cdb4674
> --- /dev/null
> +++ b/libavformat/farbfelddec.c
> @@ -0,0 +1,66 @@
> +/*
> + * Copyright (c) 2024 Marcus B Spencer
> + *
> + * 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 "avformat.h"
> +#include "demux.h"
> +#include "internal.h"
> +
> +static int farbfeld_read_header(AVFormatContext *ctx)
> +{
> +    AVStream *st = avformat_new_stream(ctx, NULL);
> +    char magic[8];
> +    int ret;
> +
> +    if (avio_size(ctx->pb) < 16)
> +        return AVERROR_INVALIDDATA;
> +
> +    if ((ret = avio_read(ctx->pb, magic, 8)) < 0)
> +        return ret;
> +
> +    if (memcmp(magic, "farbfeld", 8))
> +        return AVERROR_INVALIDDATA;
> +
> +    st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
> +    st->codecpar->format     = AV_PIX_FMT_RGBA64BE;
> +    st->codecpar->codec_id   = AV_CODEC_ID_RAWVIDEO;
> +
> +    st->codecpar->width         = avio_rb32(ctx->pb);
> +    st->codecpar->height        = avio_rb32(ctx->pb);
> +    st->codecpar->framerate.num = 25;
> +    st->codecpar->framerate.den = 1;
> +    st->avg_frame_rate          =
> +    st->r_frame_rate            = st->codecpar->framerate;
> +
> +    return 0;
> +}
> +
> +static int farbfeld_read_packet(AVFormatContext *ctx, AVPacket *pkt)
> +{
> +    return av_get_packet(ctx->pb, pkt, avio_size(ctx->pb) - 16);
> +}
> +
> +const FFInputFormat ff_farbfeld_demuxer = {
> +    .p.name       = "farbfeld",
> +    .p.long_name  = NULL_IF_CONFIG_SMALL("farbfeld uncompressed image"),
> +    .p.flags      = AVFMT_NOTIMESTAMPS | AVFMT_EXPERIMENTAL,
> +    .p.extensions = "ff",
> +    .read_header  = farbfeld_read_header,
> +    .read_packet  = farbfeld_read_packet,
> +};
> diff --git a/libavformat/farbfeldenc.c b/libavformat/farbfeldenc.c
> new file mode 100644
> index 0000000000..a1507d02c8
> --- /dev/null
> +++ b/libavformat/farbfeldenc.c
> @@ -0,0 +1,164 @@
> +/*
> + * YUV4MPEG muxer
> + * Image format
> + *
> + * Modified by Marcus B Spencer to suit the farbfeld muxer on 30 June 2024
> + *
> + * Copyright (c) 2001, 2002, 2003 Fabrice Bellard
> + * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
> + * Copyright (c) 2004 Michael Niedermayer
> + * Copyright (c) 2024 Marcus B Spencer
> + *
> + * 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 "avformat.h"
> +#include "internal.h"
> +#include "libavutil/avstring.h"
> +#include "libavutil/frame.h"
> +#include "libavutil/imgutils.h"
> +#include "libavutil/opt.h"
> +#include "libavutil/mem.h"
> +#include "mux.h"
> +
> +#define PLANE_COUNT 4 // required by av_image_copy_to_buffer
> +
> +typedef struct FarbfeldMuxContext {
> +    const AVClass *class;
> +    int start_number, img_number;
> +} FarbfeldMuxContext;
> +
> +static int farbfeld_init(AVFormatContext *ctx)
> +{
> +    FarbfeldMuxContext *priv = ctx->priv_data;
> +
> +    if (ctx->streams[0]->codecpar->codec_id != AV_CODEC_ID_WRAPPED_AVFRAME &&
> +        ctx->streams[0]->codecpar->codec_id != AV_CODEC_ID_RAWVIDEO) {
> +        av_log(ctx, AV_LOG_ERROR, "Codec not supported.\n");
> +        return AVERROR_INVALIDDATA;
> +    }
> +
> +    if (ctx->streams[0]->codecpar->format != AV_PIX_FMT_RGBA64BE) {
> +        av_log(ctx, AV_LOG_ERROR,
> +            "farbfeld only supports the rgba64be pixel format. "
> +            "Add \"-pix_fmt rgba64be\" to your output options to resolve "
> +            "this error.\n"
> +        );
> +        return AVERROR_INVALIDDATA;
> +    }
> +
> +    priv->img_number = priv->start_number;
> +
> +    return 0;
> +}
> +
> +static int farbfeld_write_packet(AVFormatContext *ctx, AVPacket *pkt)
> +{
> +    AVStream *st = ctx->streams[pkt->stream_index];
> +    int width = st->codecpar->width, height = st->codecpar->height;
> +    FarbfeldMuxContext *priv = ctx->priv_data;
> +    const uint8_t *planes[PLANE_COUNT];
> +    enum AVPixelFormat pix_fmt;
> +    char filename[1024];
> +    const AVFrame *p;
> +    int raw_img_size;
> +    AVIOContext *pb;
> +    uint8_t *dst;
> +    int ret;
> +
> +    if ((ret = av_get_frame_filename2(filename, sizeof filename, ctx->url,
> +        priv->img_number, AV_FRAME_FILENAME_FLAGS_MULTIPLE)) < 0) {
> +        if (priv->img_number == priv->start_number) {
> +            av_log(ctx, AV_LOG_WARNING, "The specified filename '%s' does not contain an image sequence pattern or a pattern is invalid.\n", ctx->url);
> +            av_log(ctx, AV_LOG_WARNING, "Use a pattern such as %%03d for an image sequence.\n");
> +            av_strlcpy(filename, ctx->url, sizeof filename);
> +        } else {
> +            av_log(ctx, AV_LOG_ERROR, "Cannot write more than one file with the same name.\n");
> +            return AVERROR(EINVAL);
> +        }
> +    }
> +
> +    if ((ret = ctx->io_open(ctx, &pb, filename, AVIO_FLAG_WRITE, NULL)) < 0) {
> +        av_log(ctx, AV_LOG_ERROR, "Could not open file : %s\n", filename);
> +        return ret;
> +    }
> +
> +    avio_write(pb, "farbfeld", 8);
> +
> +    avio_wb32(pb, width);
> +    avio_wb32(pb, height);
> +
> +    if (st->codecpar->codec_id == AV_CODEC_ID_RAWVIDEO) {
> +        avio_write(pb, pkt->data, pkt->size);
> +        return 0;
> +    }
> +
> +    p       = (const AVFrame *)pkt->data;
> +    pix_fmt = st->codecpar->format;
> +
> +    if ((raw_img_size = av_image_get_buffer_size(pix_fmt, width, height, 1)) < 0)
> +        return raw_img_size;
> +
> +    if (!(dst = av_malloc(raw_img_size)))
> +        return AVERROR(ENOMEM);
> +
> +    for (int i = 0; i < PLANE_COUNT; i++)
> +        planes[i] = p->data[i];
> +
> +    if ((ret = av_image_copy_to_buffer(dst, raw_img_size, planes, p->linesize,
> +        pix_fmt, width, height, 1)) < 0)
> +        return ret;
> +
> +    avio_write(pb, dst, raw_img_size);

This looks like its dealing with stride != width. If you process this 
line-by-line, you can eliminate using a temporary buffer.

> +
> +    av_freep(&dst);
> +
> +    priv->img_number++;
> +
> +    avio_flush(pb);
> +
> +    return ff_format_io_close(ctx, &pb);
> +}
> +
> +#define OFFSET(x) offsetof(FarbfeldMuxContext, x)
> +#define ENC AV_OPT_FLAG_ENCODING_PARAM
> +static const AVOption muxoptions[] = {
> +    { "start_number", "set first number in the sequence", OFFSET(start_number), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, INT_MAX, ENC },
> +    { NULL },
> +};
> +
> +static const AVClass farbfeld_mux_class = {
> +    .class_name = "farbfeld muxer",
> +    .item_name  = av_default_item_name,
> +    .option     = muxoptions,
> +    .version    = LIBAVUTIL_VERSION_INT,
> +};
> +
> +const FFOutputFormat ff_farbfeld_muxer = {
> +    .p.name           = "farbfeld",
> +    .p.long_name      = NULL_IF_CONFIG_SMALL("farbfeld uncompressed image"),
> +    .p.extensions     = "ff",
> +    .p.audio_codec    = AV_CODEC_ID_NONE,
> +    .p.video_codec    = AV_CODEC_ID_WRAPPED_AVFRAME,
> +    .p.subtitle_codec = AV_CODEC_ID_NONE,
> +    .p.flags          = AVFMT_NOTIMESTAMPS | AVFMT_NOFILE,
> +    .p.priv_class     = &farbfeld_mux_class,
> +    .priv_data_size   = sizeof(FarbfeldMuxContext),
> +    .init             = farbfeld_init,
> +    .write_packet     = farbfeld_write_packet,
> +    .flags_internal   = FF_OFMT_FLAG_MAX_ONE_OF_EACH,
> +};
> diff --git a/libavformat/version.h b/libavformat/version.h
> index af7d0a1024..e2634b85ae 100644
> --- a/libavformat/version.h
> +++ b/libavformat/version.h
> @@ -31,7 +31,7 @@
>   
>   #include "version_major.h"
>   
> -#define LIBAVFORMAT_VERSION_MINOR   4
> +#define LIBAVFORMAT_VERSION_MINOR   5
>   #define LIBAVFORMAT_VERSION_MICRO 100
>   
>   #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \

Apart from that, looks fine, much better than using a codec.

[-- Attachment #1.1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 637 bytes --]

[-- Attachment #1.2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 236 bytes --]

[-- Attachment #2: 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".

      reply	other threads:[~2024-07-02  0:20 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-01 23:28 Marcus B Spencer
2024-07-02  0:20 ` Lynne via ffmpeg-devel [this message]

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=07879641-52e8-4ec1-8e3d-f884f8b77ccc@lynne.ee \
    --to=ffmpeg-devel@ffmpeg.org \
    --cc=dev@lynne.ee \
    /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