From patchwork Sun Jun 30 20:24:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcus B Spencer X-Patchwork-Id: 50224 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a59:cc64:0:b0:482:c625:d099 with SMTP id k4csp1414810vqv; Sun, 30 Jun 2024 13:25:02 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCWc5CUzf2U2RmSCMqcYyO72iY9k1aUhWgena9fXREBExt5Qke9SCH77UwvSukUZ1xghL7A9n06U1M8xwWg52qFVUIL1BPfpsPOeMA== X-Google-Smtp-Source: AGHT+IEH3wf8VNHEZFyEijCAO1tNu+xsf1qCiPoJ4mmH7PTziQQ4WZmhl1CVgX5ckmjgLXCF/7ht X-Received: by 2002:a2e:b52f:0:b0:2ec:5a85:66ec with SMTP id 38308e7fff4ca-2ee5e6f53e0mr23070141fa.48.1719779102641; Sun, 30 Jun 2024 13:25:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1719779102; cv=none; d=google.com; s=arc-20160816; b=DwXyIQ+EC5CiEJuX78NYrJwa9gaRLyAjeyrTcwH2H0eu95YdcEoo2FWbCxkK+YLYhV Ur/xI9EmBbwVLgbhqdF3fkgDiABX1B/CB5A/OblaSjIWtqEcXIkPB6jukDFI2zZ3Qndu 1qV7tJkvU28DF+KoNxNraAI9L7zjDS952EHE/JpT6hZOxnR2A6mZnXAy6bWpoW5nPi27 nPI1F9UOJpv5HxdkP47Pr/g1c/QMcxyYMoEbLUCjly3QofqiBPPJ3bLU7bgB/soMMdTv +ZM79l8thsap/h8WCicSQdpYSaZQHRw7su6zS35VfQZi/GSbVZj5h/QD14psgMRMNtLG qSVg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:message-id:date:to:from :dkim-signature:delivered-to; bh=mYK4Zi4JOhMvS47irttN0bLdosY96y8BMxMxUf6xOS8=; fh=JuegC/vje050XLj6U2KtH3S0d9hYf9AYxo0tX+p7f28=; b=Zz1LxZ/ie7PlEEx4Iou3ETNxOg1pRUIveh2FK3P6Ieaa0KOlvSYo9q45t+Du0R2Zfq rWX56QjHvK7NuRaRWSvG6jp24QXKuA0x5oaxbzP1GTw5Topt7y/znNNzH7lxsqEJJDw+ Lflz3liV4s4yPFC/ys4bBPHVk+tvQvXk+Vvoz15YZ7Z+Ca/WeYVzUo4Vsclix6f79ARF fGXA67l2iqSkxqG7boJH5YrEUZ220C89bJWD06gnPq7nUmyZElJgNuxgnFbk3Mz1dHMG LXfjay29G0GspHWHk8i2bGFAdw6fsKIV2mJi5NhB8PtV+JirE7JmYCqpcCY9xWBAZ/qN nqvw==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@marcusspencer.xyz header.s=protonmail header.b=mTweNDfd; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=marcusspencer.xyz Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id 38308e7fff4ca-2ee5155c27esi15653801fa.481.2024.06.30.13.25.02; Sun, 30 Jun 2024 13:25:02 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@marcusspencer.xyz header.s=protonmail header.b=mTweNDfd; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=marcusspencer.xyz Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id BE73D68D69E; Sun, 30 Jun 2024 23:24:57 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-40111.protonmail.ch (mail-40111.protonmail.ch [185.70.40.111]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id AD79D68D631 for ; Sun, 30 Jun 2024 23:24:50 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=marcusspencer.xyz; s=protonmail; t=1719779088; x=1720038288; bh=+iH/pGHRO8GhDFOjhyEQ6WLiDp4LOjCN8ldxNGrQxVU=; h=From:To:Cc:Subject:Date:Message-ID:From:To:Cc:Date:Subject: Reply-To:Feedback-ID:Message-ID:BIMI-Selector; b=mTweNDfdhNmdojpYp8nZ46duxkLetbvBc2k2a3WQ++z4GqOOj0Ec1Xb0MaKeM3s1C PQQYmdLWVZ7lyVmSz36kU5bfP+iddPQissX6nJpsBNxIGABr7gmitMGlXPFIl2wpgo DN+9M3tyOFlBQq9DzHp3rSmxT9lFSFyRspSx8MxVss7gHCKLEU0IYnpAkRh3LZK3+Z WwRWZR0IpaLdfsSsDGjG1Y6wQjIsSnyu2xKzJochVTbRz0jCb9hjqTayms5l7TPl7R oapzXvB7On4NETQPzOQVgC9yzdaDEhis7jKBgbvN+YMMexHU/bQst0jbrYYNU+u6RU R8C18DIHySwWg== X-Pm-Submission-Id: 4WC0yk0Qc9z3R From: Marcus B Spencer To: ffmpeg-devel@ffmpeg.org Date: Sun, 30 Jun 2024 15:24:42 -0500 Message-ID: <20240630202442.248551-1-marcus@marcusspencer.xyz> X-Mailer: git-send-email 2.45.2 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v12] avformat: add farbfeld muxer and demuxer X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Marcus B Spencer Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: D7eyI1k81Xsa 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. Signed-off-by: Marcus B Spencer --- 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 06c00e981a..3024e8d7fb 100644 --- a/Changelog +++ b/Changelog @@ -14,6 +14,7 @@ version : - xHE-AAC decoder - removed DEC Alpha DSP and support code - VVC encoding support via libvvenc +- 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); + + 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, \