From patchwork Sat Dec 16 08:13:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Thilo Borgmann X-Patchwork-Id: 45172 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1225:b0:181:818d:5e7f with SMTP id v37csp6178153pzf; Sat, 16 Dec 2023 00:13:44 -0800 (PST) X-Google-Smtp-Source: AGHT+IG3GzQWPWbXilwPQvG6hoDCMA6eH3TY5N7dDqs9C3xrlf5vGyMIuujc/inkjDGdofkBj2WM X-Received: by 2002:a17:906:1c3:b0:a19:641:66da with SMTP id 3-20020a17090601c300b00a19064166damr7947747ejj.7.1702714424019; Sat, 16 Dec 2023 00:13:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702714424; cv=none; d=google.com; s=arc-20160816; b=H4TdR1wh1ypFlsDVuvQI3EN7WmmWT1FIJfnfX0R3ULK2t/9rHTp1ewrsdpZgcDUy2a xt/mxFesQldW+cm7cgAql+p89isV686Aquze+hIuBpvPi7I5G9OZp+RevwvX2tAc4eI/ V5kcyrqbOAookpmqoTMLCvQHsYSErGrhbrlv3nF8cptEAuuY9/Vs1BvJP9k1OdRrfQMc G1B2oyi91/J1dIBkJu97mG9zYl3iaG7PonqRe6R5N2ODcOHUif3qE3rDFEeOVTyUJWX6 sdAd5SD1qxBHTH0o/R3wFm+XBsiBaj45oOXDlyu/JMa2di/KBJ0XjTJc4/HOTm4Or7n1 20Qw== 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:from :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:references:in-reply-to :message-id:date:to:delivered-to; bh=k/f9bbv2hkjTS7sQsKMhfCLO1/pTGV3JF0IsAO1+sm8=; fh=uZWItAgYCSLFS5Zum7Ip+PilGyWYOeCwun+We0BTHAs=; b=l/VhM5rOk/BtUBKIAa4cfX6RZ9ZZza11p9lGS03+zKlhwgecJb66ekeuRa1rD4++hv hQOkh4gm5vwjBBRtX9O7xl61nQaf8dbzZRWZX1zt0M76XNJiTNp1r2Ysk3rNcR65k5jx PwQHjD92ZwlNxkjDSxc6otDb8szilPx6tPilUleQr7lcRke9LB+/mzTQLRlL4fw7QlVA JB9Wg/bjWonR+hmPVSNhjmkFvFhKpix0gbPPY7Y6Sd7fSAdQR/QsmVp+8PAHB3j1YpUO bR75Sf2TTTyzcqRsgvk7Pxg6TLRlMKrENBVf8HrChaK7h1JjAvRy0/BTNpAUb17p6NdL 5cLg== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id v14-20020a170906b00e00b00a1cf7b31e9esi7734098ejy.289.2023.12.16.00.13.43; Sat, 16 Dec 2023 00:13:43 -0800 (PST) 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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 7A48368C5BA; Sat, 16 Dec 2023 10:13:32 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from shout02.mail.de (shout02.mail.de [62.201.172.25]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 4F53468D048 for ; Sat, 16 Dec 2023 10:13:24 +0200 (EET) Received: from postfix01.mail.de (postfix01.bt.mail.de [10.0.121.125]) by shout02.mail.de (Postfix) with ESMTP id E4A9E240D20 for ; Sat, 16 Dec 2023 09:13:23 +0100 (CET) Received: from smtp01.mail.de (smtp04.bt.mail.de [10.0.121.214]) by postfix01.mail.de (Postfix) with ESMTP id CD29680069 for ; Sat, 16 Dec 2023 09:13:23 +0100 (CET) Received: from [127.0.0.1] (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp01.mail.de (Postfix) with ESMTPSA id 9C7582400D8 for ; Sat, 16 Dec 2023 09:13:23 +0100 (CET) To: ffmpeg-devel@ffmpeg.org Date: Sat, 16 Dec 2023 09:13:21 +0100 Message-Id: <20231216081322.53623-2-thilo.borgmann@mail.de> In-Reply-To: <20231216081322.53623-1-thilo.borgmann@mail.de> References: <20231216081322.53623-1-thilo.borgmann@mail.de> MIME-Version: 1.0 X-purgate: clean X-purgate: This mail is considered clean (visit http://www.eleven.de for further information) X-purgate-type: clean X-purgate-Ad: Categorized by eleven eXpurgate (R) http://www.eleven.de X-purgate: This mail is considered clean (visit http://www.eleven.de for further information) X-purgate: clean X-purgate-size: 14760 X-purgate-ID: 154282::1702714403-94FEA338-9D5F40CF/0/0 Subject: [FFmpeg-devel] [PATCH v3 1/2] avfilter: Add fsync filter 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: , X-Patchwork-Original-From: Thilo Borgmann via ffmpeg-devel From: Thilo Borgmann Reply-To: FFmpeg development discussions and patches Cc: Thilo Borgmann Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: LD0MbXbdrONL --- Changelog | 1 + MAINTAINERS | 1 + configure | 2 + doc/filters.texi | 33 +++++ libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/version.h | 2 +- libavfilter/vf_fsync.c | 286 +++++++++++++++++++++++++++++++++++++++ 8 files changed, 326 insertions(+), 1 deletion(-) create mode 100644 libavfilter/vf_fsync.c diff --git a/Changelog b/Changelog index 67ef92eb02..a25278d227 100644 --- a/Changelog +++ b/Changelog @@ -9,6 +9,7 @@ version : - aap filter - demuxing, decoding, filtering, encoding, and muxing in the ffmpeg CLI now all run in parallel +- fsync filter version 6.1: - libaribcaption decoder diff --git a/MAINTAINERS b/MAINTAINERS index 39b37ee0c5..4257fcad98 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -343,6 +343,7 @@ Filters: vf_delogo.c Jean Delvare (CC ) vf_drawbox.c/drawgrid Andrey Utkin vf_extractplanes.c Paul B Mahol + vf_fsync.c Thilo Borgmann vf_histogram.c Paul B Mahol vf_hqx.c Clément Bœsch vf_idet.c Pascal Massimino diff --git a/configure b/configure index 7d2ee66000..2328c96dde 100755 --- a/configure +++ b/configure @@ -3745,6 +3745,7 @@ frei0r_deps_any="libdl LoadLibrary" frei0r_filter_deps="frei0r" frei0r_src_filter_deps="frei0r" fspp_filter_deps="gpl" +fsync_filter_deps="avformat" gblur_vulkan_filter_deps="vulkan spirv_compiler" hflip_vulkan_filter_deps="vulkan spirv_compiler" histeq_filter_deps="gpl" @@ -7647,6 +7648,7 @@ enabled cover_rect_filter && prepend avfilter_deps "avformat avcodec" enabled ebur128_filter && enabled swresample && prepend avfilter_deps "swresample" enabled elbg_filter && prepend avfilter_deps "avcodec" enabled find_rect_filter && prepend avfilter_deps "avformat avcodec" +enabled fsync_filter && prepend avfilter_deps "avformat" enabled mcdeint_filter && prepend avfilter_deps "avcodec" enabled movie_filter && prepend avfilter_deps "avformat avcodec" enabled pan_filter && prepend avfilter_deps "swresample" diff --git a/doc/filters.texi b/doc/filters.texi index 6d00ba2c3f..9f19cba9df 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -14681,6 +14681,39 @@ option may cause flicker since the B-Frames have often larger QP. Default is @end table +@anchor{fsync} +@section fsync + +Synchronize video frames with an external mapping from a file. + +For each input PTS given in the map file it either drops or creates as many frames as necessary to recreate the sequence of output frames given in the map file. + +This filter is useful to recreate the output frames of a framerate conversion by the @ref{fps} filter, recorded into a map file using the ffmpeg option @code{-stats_mux_pre}, and do further processing to the corresponding frames e.g. quality comparison. + +Each line of the map file must contain three items per input frame, the input PTS (decimal), the output PTS (decimal) and the output TIMEBASE (decimal/decimal), seperated by a space. +This file format corresponds to the output of @code{-stats_mux_pre_fmt="@{ptsi@} @{pts@} @{tb@}"}. + +The filter assumes the map file is sorted by increasing input PTS. + +The filter accepts the following options: +@table @option + +@item file, f +The filename of the map file to be used. +@end table + +Example: +@example +# Convert a video to 25 fps and record a MAP_FILE file with the default format of this filter +ffmpeg -i INPUT -vf fps=fps=25 -stats_mux_pre MAP_FILE -stats_mux_pre_fmt "@{ptsi@} @{pts@} @{tb@}" OUTPUT + +# Sort MAP_FILE by increasing input PTS +sort -n MAP_FILE + +# Use INPUT, OUTPUT and the MAP_FILE from above to compare the corresponding frames in INPUT and OUTPUT via SSIM +ffmpeg -i INPUT -i OUTPUT -filter_complex '[0:v]fsync=file=MAP_FILE[ref];[1:v][ref]ssim' -f null - +@end example + @section gblur Apply Gaussian blur filter. diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 63725f91b4..612616dfb4 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -323,6 +323,7 @@ OBJS-$(CONFIG_FREEZEDETECT_FILTER) += vf_freezedetect.o OBJS-$(CONFIG_FREEZEFRAMES_FILTER) += vf_freezeframes.o OBJS-$(CONFIG_FREI0R_FILTER) += vf_frei0r.o OBJS-$(CONFIG_FSPP_FILTER) += vf_fspp.o qp_table.o +OBJS-$(CONFIG_FSYNC_FILTER) += vf_fsync.o OBJS-$(CONFIG_GBLUR_FILTER) += vf_gblur.o OBJS-$(CONFIG_GBLUR_VULKAN_FILTER) += vf_gblur_vulkan.o vulkan.o vulkan_filter.o OBJS-$(CONFIG_GEQ_FILTER) += vf_geq.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index ed7c32be94..b32ffb2d71 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -299,6 +299,7 @@ extern const AVFilter ff_vf_freezedetect; extern const AVFilter ff_vf_freezeframes; extern const AVFilter ff_vf_frei0r; extern const AVFilter ff_vf_fspp; +extern const AVFilter ff_vf_fsync; extern const AVFilter ff_vf_gblur; extern const AVFilter ff_vf_gblur_vulkan; extern const AVFilter ff_vf_geq; diff --git a/libavfilter/version.h b/libavfilter/version.h index 7642b670d1..59330858bd 100644 --- a/libavfilter/version.h +++ b/libavfilter/version.h @@ -31,7 +31,7 @@ #include "version_major.h" -#define LIBAVFILTER_VERSION_MINOR 14 +#define LIBAVFILTER_VERSION_MINOR 15 #define LIBAVFILTER_VERSION_MICRO 100 diff --git a/libavfilter/vf_fsync.c b/libavfilter/vf_fsync.c new file mode 100644 index 0000000000..f336baf042 --- /dev/null +++ b/libavfilter/vf_fsync.c @@ -0,0 +1,286 @@ +/* + * Copyright (c) 2023 Thilo Borgmann + * + * 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 + */ + +/** + * @file + * Filter for syncing video frames from external source + * + * @author Thilo Borgmann + */ + +#include "libavutil/avstring.h" +#include "libavutil/error.h" +#include "libavutil/opt.h" +#include "libavutil/bprint.h" +#include "libavformat/avio.h" +#include "video.h" +#include "filters.h" + +#define BUF_SIZE 256 + +typedef struct FsyncContext { + const AVClass *class; + AVIOContext *avio_ctx; // reading the map file + AVFrame *last_frame; // buffering the last frame for duplicating eventually + char *filename; // user-specified map file + char *buf; // line buffer for the map file + char *cur; // current position in the line buffer + char *end; // end pointer of the line buffer + int64_t ptsi; // input pts to map to [0-N] output pts + int64_t pts; // output pts + int tb_num; // output timebase num + int tb_den; // output timebase den +} FsyncContext; + +#define OFFSET(x) offsetof(FsyncContext, x) + +static const AVOption fsync_options[] = { + { "file", "set the file name to use for frame sync", OFFSET(filename), AV_OPT_TYPE_STRING, { .str = "" }, .flags= AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM }, + { "f", "set the file name to use for frame sync", OFFSET(filename), AV_OPT_TYPE_STRING, { .str = "" }, .flags= AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM }, + { NULL } +}; + +// fills the buffer from cur to end, add \0 at EOF +static int buf_fill(FsyncContext *ctx) +{ + int ret; + int num = ctx->end - ctx->cur; + + ret = avio_read(ctx->avio_ctx, ctx->cur, num); + if (ret < 0) + return ret; + if (ret < num) { + *(ctx->cur + ret) = '\0'; + } + + return ret; +} + +// copies cur to end to the beginning and fills the rest +static int buf_reload(FsyncContext *ctx) +{ + int i, ret; + int num = ctx->end - ctx->cur; + + for (i = 0; i < num; i++) { + ctx->buf[i] = *ctx->cur++; + } + + ctx->cur = ctx->buf + i; + ret = buf_fill(ctx); + if (ret < 0) + return ret; + ctx->cur = ctx->buf; + + return ret; +} + +// skip from cur over eol +static void buf_skip_eol(FsyncContext *ctx) +{ + char *i; + for (i = ctx->cur; i < ctx->end; i++) { + if (*i != '\n')// && *i != '\r') + break; + } + ctx->cur = i; +} + +// get number of bytes from cur until eol +static int buf_get_line_count(FsyncContext *ctx) +{ + int ret = 0; + char *i; + for (i = ctx->cur; i < ctx->end; i++, ret++) { + if (*i == '\0' || *i == '\n') + return ret; + } + + return -1; +} + +// get number of bytes from cur to '\0' +static int buf_get_zero(FsyncContext *ctx) +{ + return av_strnlen(ctx->cur, ctx->end - ctx->cur); +} + +static int activate(AVFilterContext *ctx) +{ + FsyncContext *s = ctx->priv; + AVFilterLink *inlink = ctx->inputs[0]; + AVFilterLink *outlink = ctx->outputs[0]; + + int ret, line_count; + AVFrame *frame; + + FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink); + + buf_skip_eol(s); + line_count = buf_get_line_count(s); + if (line_count < 0) { + line_count = buf_reload(s); + if (line_count < 0) + return line_count; + line_count = buf_get_line_count(s); + if (line_count < 0) + return line_count; + } + + if (avio_feof(s->avio_ctx) && buf_get_zero(s) < 3) { + av_log(ctx, AV_LOG_DEBUG, "End of file. To zero = %i\n", buf_get_zero(s)); + goto end; + } + + if (s->last_frame) { + ret = av_sscanf(s->cur, "%"PRId64" %"PRId64" %d/%d", &s->ptsi, &s->pts, &s->tb_num, &s->tb_den); + if (ret != 4) { + av_log(ctx, AV_LOG_ERROR, "Unexpected format found (%i / 4).\n", ret); + ff_outlink_set_status(outlink, AVERROR_INVALIDDATA, AV_NOPTS_VALUE); + return AVERROR_INVALIDDATA; + } + + av_log(ctx, AV_LOG_DEBUG, "frame %lli ", s->last_frame->pts); + + if (s->last_frame->pts >= s->ptsi) { + av_log(ctx, AV_LOG_DEBUG, ">= %lli: DUP LAST with pts = %lli\n", s->ptsi, s->pts); + + // clone frame + frame = av_frame_clone(s->last_frame); + if (!frame) { + ff_outlink_set_status(outlink, AVERROR(ENOMEM), AV_NOPTS_VALUE); + return AVERROR(ENOMEM); + } + + // set output pts and timebase + frame->pts = s->pts; + frame->time_base = av_make_q((int)s->tb_num, (int)s->tb_den); + + // advance cur to eol, skip over eol in the next call + s->cur += line_count; + + // call again + if (ff_inoutlink_check_flow(inlink, outlink)) + ff_filter_set_ready(ctx, 100); + + // filter frame + return ff_filter_frame(outlink, frame); + } else if (s->last_frame->pts < s->ptsi) { + av_log(ctx, AV_LOG_DEBUG, "< %lli: DROP\n", s->ptsi); + av_frame_free(&s->last_frame); + + // call again + if (ff_inoutlink_check_flow(inlink, outlink)) + ff_filter_set_ready(ctx, 100); + + return 0; + } + } + +end: + ret = ff_inlink_consume_frame(inlink, &s->last_frame); + if (ret < 0) + return ret; + + FF_FILTER_FORWARD_STATUS(inlink, outlink); + FF_FILTER_FORWARD_WANTED(outlink, inlink); + + return FFERROR_NOT_READY; +} + +static int fsync_config_props(AVFilterLink* outlink) +{ + AVFilterContext *ctx = outlink->src; + FsyncContext *s = ctx->priv; + int ret; + + // read first line to get output timebase + ret = av_sscanf(s->cur, "%"PRId64" %"PRId64" %d/%d", &s->ptsi, &s->pts, &s->tb_num, &s->tb_den); + if (ret != 4) { + av_log(ctx, AV_LOG_ERROR, "Unexpected format found (%i of 4).\n", ret); + ff_outlink_set_status(outlink, AVERROR_INVALIDDATA, AV_NOPTS_VALUE); + return AVERROR_INVALIDDATA; + } + + outlink->frame_rate = av_make_q(1, 0); // unknown or dynamic + outlink->time_base = av_make_q(s->tb_num, s->tb_den); + + return 0; +} + +static av_cold int fsync_init(AVFilterContext *ctx) +{ + FsyncContext *s = ctx->priv; + int ret; + + av_log(ctx, AV_LOG_DEBUG, "filename: %s\n", s->filename); + + s->buf = av_malloc(BUF_SIZE + 1); + if (!s->buf) + return AVERROR(ENOMEM); + + ret = avio_open(&s->avio_ctx, s->filename, AVIO_FLAG_READ); + if (ret < 0) + return ret; + + s->cur = s->buf; + s->end = s->buf + BUF_SIZE; + s->buf[BUF_SIZE] = '\0'; + + ret = buf_fill(s); + if (ret < 0) + return ret; + + + return 0; +} + +static av_cold void fsync_uninit(AVFilterContext *ctx) +{ + FsyncContext *s = ctx->priv; + + avio_closep(&s->avio_ctx); + av_freep(&s->buf); + av_frame_free(&s->last_frame); +} + +AVFILTER_DEFINE_CLASS(fsync); + +static const AVFilterPad fsync_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .config_props = fsync_config_props, + }, +}; + +const AVFilter ff_vf_fsync = { + .name = "fsync", + .description = NULL_IF_CONFIG_SMALL("Synchronize video frames from external source."), + .init = fsync_init, + .uninit = fsync_uninit, + .priv_size = sizeof(FsyncContext), + .priv_class = &fsync_class, + .activate = activate, + .formats_state = FF_FILTER_FORMATS_PASSTHROUGH, + FILTER_INPUTS(ff_video_default_filterpad), + FILTER_OUTPUTS(fsync_outputs), + .flags = AVFILTER_FLAG_METADATA_ONLY, +};