Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [WIP] [PATCH 1/2] lavfi/framesync: support filters with multiple outputs
@ 2023-06-26 20:09 Nicolas George
  2023-06-26 20:09 ` [FFmpeg-devel] [WIP] [PATCH 2/2] lavfi/vf_framematch: add filter to output matching synchronized frames Nicolas George
  0 siblings, 1 reply; 2+ messages in thread
From: Nicolas George @ 2023-06-26 20:09 UTC (permalink / raw)
  To: ffmpeg-devel

The filters will have to provide the logic to set the status
and check for a wanted frame.

Signed-off-by: Nicolas George <george@nsup.org>
---
 libavfilter/framesync.c | 46 ++++++++++++++++++++++++++++++-----------
 libavfilter/framesync.h | 28 +++++++++++++++++++++++++
 2 files changed, 62 insertions(+), 12 deletions(-)


Untested yet.


diff --git a/libavfilter/framesync.c b/libavfilter/framesync.c
index c748262ba6..0923e8c22b 100644
--- a/libavfilter/framesync.c
+++ b/libavfilter/framesync.c
@@ -75,6 +75,10 @@ enum {
 
 static int consume_from_fifos(FFFrameSync *fs);
 
+static void default_on_eof(FFFrameSync *fs);
+
+static int default_on_miss(FFFrameSync *fs);
+
 void ff_framesync_preinit(FFFrameSync *fs)
 {
     if (fs->class)
@@ -83,28 +87,36 @@ void ff_framesync_preinit(FFFrameSync *fs)
     av_opt_set_defaults(fs);
 }
 
-int ff_framesync_init(FFFrameSync *fs, AVFilterContext *parent, unsigned nb_in)
+int ff_framesync_postinit(FFFrameSync *fs)
 {
-    /* For filters with several outputs, we will not be able to assume which
-       output is relevant for ff_outlink_frame_wanted() and
-       ff_outlink_set_status(). To be designed when needed. */
-    av_assert0(parent->nb_outputs == 1);
-
-    ff_framesync_preinit(fs);
-    fs->parent = parent;
-    fs->nb_in  = nb_in;
+    if (fs->parent->nb_outputs == 1) {
+        if (!fs->on_eof)
+            fs->on_eof = default_on_eof;
+        if (!fs->on_miss)
+            fs->on_miss = default_on_miss;
+    }
+    av_assert0(fs->on_eof);
+    av_assert0(fs->on_miss);
 
-    fs->in = av_calloc(nb_in, sizeof(*fs->in));
+    fs->in = av_calloc(fs->nb_in, sizeof(*fs->in));
     if (!fs->in)
         return AVERROR(ENOMEM);
     return 0;
 }
 
+int ff_framesync_init(FFFrameSync *fs, AVFilterContext *parent, unsigned nb_in)
+{
+    ff_framesync_preinit(fs);
+    fs->parent = parent;
+    fs->nb_in  = nb_in;
+    return ff_framesync_postinit(fs);
+}
+
 static void framesync_eof(FFFrameSync *fs)
 {
     fs->eof = 1;
     fs->frame_ready = 0;
-    ff_outlink_set_status(fs->parent->outputs[0], AVERROR_EOF, AV_NOPTS_VALUE);
+    fs->on_eof(fs);
 }
 
 static void framesync_sync_level_update(FFFrameSync *fs)
@@ -342,7 +354,7 @@ static int consume_from_fifos(FFFrameSync *fs)
         }
     }
     if (nb_miss) {
-        if (nb_miss == nb_active && !ff_outlink_frame_wanted(ctx->outputs[0]))
+        if (nb_miss == nb_active && !fs->on_miss(fs))
             return FFERROR_NOT_READY;
         for (i = 0; i < fs->nb_in; i++)
             if (!fs->in[i].have_next && fs->in[i].state != STATE_EOF)
@@ -422,3 +434,13 @@ int ff_framesync_dualinput_get_writable(FFFrameSync *fs, AVFrame **f0, AVFrame *
     }
     return 0;
 }
+
+static void default_on_eof(FFFrameSync *fs)
+{
+    ff_outlink_set_status(fs->parent->outputs[0], AVERROR_EOF, AV_NOPTS_VALUE);
+}
+
+static int default_on_miss(FFFrameSync *fs)
+{
+    return ff_outlink_frame_wanted(fs->parent->outputs[0]);
+}
diff --git a/libavfilter/framesync.h b/libavfilter/framesync.h
index 233f50a0eb..1957396c7f 100644
--- a/libavfilter/framesync.h
+++ b/libavfilter/framesync.h
@@ -193,6 +193,19 @@ typedef struct FFFrameSync {
      */
     int (*on_event)(struct FFFrameSync *fs);
 
+    /**
+     * Callback called when EOF is reached; can be NULL
+     * The default is to ff_outlink_set_status() on the single output.
+     */
+    void (*on_eof)(struct FFFrameSync *fs);
+
+    /**
+     * Callback called when no input can be produced to decide if input must
+     * be requested
+     * The default is ff_outlink_frame_wanted() on the singe output.
+     */
+    int (*on_miss)(struct FFFrameSync *fs);
+
     /**
      * Opaque pointer, not used by the API
      */
@@ -240,6 +253,21 @@ typedef struct FFFrameSync {
  */
 void ff_framesync_preinit(FFFrameSync *fs);
 
+/**
+ * Finish the initialization of a frame sync structure
+ *
+ * Use it in combination with ff_framesync_preinit() and set fields and
+ * options in between.
+ *
+ *   ff_framesync_preinit(fs);
+ *   fs->parent = parent;
+ *   fs->nb_in  = nb_in;
+ *   fs->field  = value;
+ *   ret = ff_framesync_postinit(fs);
+ *   if (ret < 0) ...
+ */
+int ff_framesync_postinit(FFFrameSync *fs);
+
 /**
  * Initialize a frame sync structure.
  *
-- 
2.39.2

_______________________________________________
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".

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [FFmpeg-devel] [WIP] [PATCH 2/2] lavfi/vf_framematch: add filter to output matching synchronized frames
  2023-06-26 20:09 [FFmpeg-devel] [WIP] [PATCH 1/2] lavfi/framesync: support filters with multiple outputs Nicolas George
@ 2023-06-26 20:09 ` Nicolas George
  0 siblings, 0 replies; 2+ messages in thread
From: Nicolas George @ 2023-06-26 20:09 UTC (permalink / raw)
  To: ffmpeg-devel

Signed-off-by: Nicolas George <george@nsup.org>
---
 libavfilter/Makefile        |  1 +
 libavfilter/allfilters.c    |  1 +
 libavfilter/vf_framematch.c | 82 +++++++++++++++++++++++++++++++++++++
 3 files changed, 84 insertions(+)
 create mode 100644 libavfilter/vf_framematch.c


Unfinished yet, but should be rather straightforward. I think I can
squeeze some work on this the day after tomorrow.


diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 9b7813575a..290a473b63 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -313,6 +313,7 @@ OBJS-$(CONFIG_FIND_RECT_FILTER)              += vf_find_rect.o lavfutils.o
 OBJS-$(CONFIG_FLOODFILL_FILTER)              += vf_floodfill.o
 OBJS-$(CONFIG_FORMAT_FILTER)                 += vf_format.o
 OBJS-$(CONFIG_FPS_FILTER)                    += vf_fps.o
+OBJS-$(CONFIG_FRAMEMATCH_FILTER)             += vf_framematch.o
 OBJS-$(CONFIG_FRAMEPACK_FILTER)              += vf_framepack.o
 OBJS-$(CONFIG_FRAMERATE_FILTER)              += vf_framerate.o
 OBJS-$(CONFIG_FRAMESTEP_FILTER)              += vf_framestep.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 9a7fadc58d..aa42b353c1 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -289,6 +289,7 @@ extern const AVFilter ff_vf_flip_vulkan;
 extern const AVFilter ff_vf_floodfill;
 extern const AVFilter ff_vf_format;
 extern const AVFilter ff_vf_fps;
+extern const AVFilter ff_vf_framematch;
 extern const AVFilter ff_vf_framepack;
 extern const AVFilter ff_vf_framerate;
 extern const AVFilter ff_vf_framestep;
diff --git a/libavfilter/vf_framematch.c b/libavfilter/vf_framematch.c
new file mode 100644
index 0000000000..703b5ad93b
--- /dev/null
+++ b/libavfilter/vf_framematch.c
@@ -0,0 +1,82 @@
+
+#include "libavutil/avstring.h"
+#include "libavutil/opt.h"
+
+#include "avfilter.h"
+#include "framesync.h"
+#include "internal.h"
+
+typedef struct MatchContext {
+    const AVClass *class;
+    int nb;
+    FFFrameSync fs;
+} MatchContext;
+
+static av_cold int init(AVFilterContext *ctx)
+{
+    int i, ret;
+    MatchContext *s = ctx->priv;
+
+    for (i = 0; i < s->nb; i++) {
+        AVFilterPad pad = { 0 };
+        pad.type = AVMEDIA_TYPE_VIDEO;
+        pad.name = av_asprintf("input%d", i);
+        if (!pad.name)
+            return AVERROR(ENOMEM);
+        if ((ret = ff_append_inpad_free_name(ctx, &pad)) < 0)
+            return ret;
+        pad.name = av_asprintf("output%d", i);
+        if (!pad.name)
+            return AVERROR(ENOMEM);
+        if ((ret = ff_append_outpad_free_name(ctx, &pad)) < 0)
+            return ret;
+    }
+    return 0;
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+    MatchContext *s = ctx->priv;
+    int i, ret;
+
+    for (i = 0; i < s->nb; i++) {
+        AVFilterFormats *f = ff_all_formats(AVMEDIA_TYPE_AUDIO);
+        if ((ret = ff_formats_ref(f, &ctx->inputs [i]-> incfg.formats)) < 0 ||
+            (ret = ff_formats_ref(f, &ctx->outputs[i]->outcfg.formats)) < 0)
+            return ret;
+    }
+    return 0;
+}
+
+static av_cold void uninit(AVFilterContext *ctx)
+{
+    MatchContext *s = ctx->priv;
+    //ff_framesync_uninit(&s->fs);
+}
+
+static int activate(AVFilterContext *ctx)
+{
+    MatchContext *s = ctx->priv;
+    return ff_framesync_activate(&s->fs);
+}
+
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
+#define OFFSET(x) offsetof(MatchContext, x)
+static const AVOption framematch_options[] = {
+    { "s", "set number of streams", OFFSET(nb), AV_OPT_TYPE_INT, {.i64=2}, 2, INT_MAX, .flags = FLAGS },
+    { NULL },
+};
+
+AVFILTER_DEFINE_CLASS(framematch);
+
+const AVFilter ff_vf_framematch = {
+    .name          = "framematch",
+    .description   = NULL_IF_CONFIG_SMALL("Match frames from multiple inputs"),
+    .priv_size     = sizeof(MatchContext),
+    .priv_class    = &framematch_class,
+    FILTER_QUERY_FUNC(query_formats),
+    .init          = init,
+    .uninit        = uninit,
+    .activate      = activate,
+    .flags         = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_DYNAMIC_OUTPUTS,
+};
-- 
2.39.2

_______________________________________________
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".

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-06-26 20:09 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-26 20:09 [FFmpeg-devel] [WIP] [PATCH 1/2] lavfi/framesync: support filters with multiple outputs Nicolas George
2023-06-26 20:09 ` [FFmpeg-devel] [WIP] [PATCH 2/2] lavfi/vf_framematch: add filter to output matching synchronized frames Nicolas George

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